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