[Service] Upgrade device home as v1.0.1
[platform/framework/web/wrtjs.git] / device_home / pincode / js / pincode.js
1 const serverPort = 9000;
2 const serverURL = window.location.protocol + '//' + window.location.hostname;
3
4 var publicKey;
5 var input = '';
6 var dots = document.querySelectorAll('.dot'), numbers = document.querySelectorAll('.number');
7 var tryCount = 0;
8
9 function preloadKey() {
10     var xhr = new XMLHttpRequest();
11     xhr.onload = function () {
12         if (xhr.status === 200 || xhr.status === 201) {
13             publicKey = xhr.responseText;
14             if (publicKey) {
15                 init();
16             } else {
17                 console.error('[WT] Failed to get public key.');
18             }
19         } else {
20             console.error(xhr.responseText);
21         }
22     };
23     xhr.open('GET', serverURL + ':' + serverPort + '/pincode/publicKey');
24     xhr.send();
25 }
26
27 function init() {
28     dots = Array.prototype.slice.call(dots);
29     numbers = Array.prototype.slice.call(numbers);
30     numbers.forEach(function (number, index) {
31         number.addEventListener('click', function () {
32             number.className += ' grow';
33             if (number.innerHTML === '0') {
34                 input += 0;
35             } else {
36                 input += index + 1;
37             }
38             dots[input.length - 1].className += ' active';
39             if (input.length >= 4) {
40                 sendPinCode(input);
41
42                 setTimeout(function () {
43                     dots.forEach(function (dot, index) {
44                         dot.className = 'dot';
45                     });
46                     input = '';
47                 }, 900);
48                 setTimeout(function () {
49                     document.body.className = '';
50                 }, 1000);
51             }
52             setTimeout(function () {
53                 number.className = 'number';
54             }, 1000);
55         });
56     });
57 }
58
59 function sendPinCode(data) {
60     var encrypt = new JSEncrypt();
61     encrypt.setPublicKey(publicKey);
62     var xhr = new XMLHttpRequest();
63     xhr.onload = function () {
64         if (xhr.status === 200 || xhr.status === 201) {
65             console.log(`result : ${xhr.responseText}`);
66             chkPinCode(xhr.responseText === 'true' ? true : false);
67         } else {
68             console.error(xhr.responseText);
69         }
70     };
71     xhr.open('POST', serverURL + ':' + serverPort + '/pincode/pinCodeToServer');
72     xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
73     xhr.send(encrypt.encrypt(data));
74 }
75
76 function generatePinCodeNumber() {
77     var xhr = new XMLHttpRequest();
78     xhr.onload = function () {
79         if (xhr.status === 200 || xhr.status === 201) {
80             console.log(xhr.responseText);
81         } else {
82             console.error(xhr.responseText);
83         }
84     };
85     xhr.open('GET', serverURL + ':' + serverPort + '/retryPinCode');
86     xhr.send();
87 }
88
89 function chkPinCode(returnVal) {
90     if (returnVal) {
91         dots.forEach(function (dot, index) {
92             dot.className += ' correct';
93         });
94         document.body.className += ' correct';
95         tryCount = 0;
96         setTimeout(function () {
97             loginForm.submit();
98             //pinCode();
99         }, 1000);
100     } else {
101         dots.forEach(function (dot, index) {
102             dot.className += ' wrong';
103         });
104         document.body.className += ' wrong';
105         tryCount++;
106         if (tryCount === 5) {
107             tryCount = 0;
108             generatePinCodeNumber();
109             setTimeout(function () {
110                 alert('Failed to input 5 times. A new Pincode has been generated, so check the TV notification.');
111             }, 1000);
112         }
113     }
114 }
115
116 function pinCode() {
117     window.location.href = serverURL + ':' + serverPort + '/client/client.html';
118 }
119
120 window.onload = function () {
121     preloadKey();
122 };