[Service] Upgrade device home as v1.0.4
[platform/framework/web/wrtjs.git] / device_home / pincode / js / pincode.js
index fd1de1b..2520121 100755 (executable)
@@ -1,21 +1,16 @@
+const TAG = '[DeviceHome][pincode]';
 const serverPort = 9000;
 const serverURL = window.location.protocol + '//' + window.location.hostname;
 
 var publicKey;
 var input = '';
 var dots = document.querySelectorAll('.dot'), numbers = document.querySelectorAll('.number');
-var tryCount = 0;
 
-function preloadKey() {
+function displayPincode() {
     var xhr = new XMLHttpRequest();
-    xhr.onload = function () {
+    xhr.onload = function() {
         if (xhr.status === 200 || xhr.status === 201) {
             publicKey = xhr.responseText;
-            if (publicKey) {
-                init();
-            } else {
-                console.error('[WT] Failed to get public key.');
-            }
         } else {
             console.error(xhr.responseText);
         }
@@ -27,8 +22,8 @@ function preloadKey() {
 function init() {
     dots = Array.prototype.slice.call(dots);
     numbers = Array.prototype.slice.call(numbers);
-    numbers.forEach(function (number, index) {
-        number.addEventListener('click', function () {
+    numbers.forEach(function(number, index) {
+        number.addEventListener('click', function() {
             number.className += ' grow';
             if (number.innerHTML === '0') {
                 input += 0;
@@ -37,79 +32,74 @@ function init() {
             }
             dots[input.length - 1].className += ' active';
             if (input.length >= 4) {
+                console.log(`${TAG} input: ${input}`);
                 sendPinCode(input);
 
-                setTimeout(function () {
-                    dots.forEach(function (dot, index) {
+                setTimeout(function() {
+                    dots.forEach(function(dot, index) {
                         dot.className = 'dot';
                     });
                     input = '';
                 }, 900);
-                setTimeout(function () {
+                setTimeout(function() {
                     document.body.className = '';
                 }, 1000);
             }
-            setTimeout(function () {
+            setTimeout(function() {
                 number.className = 'number';
             }, 1000);
         });
     });
 }
 
-function sendPinCode(data) {
+async function sendPinCode(data) {
     var encrypt = new JSEncrypt();
     encrypt.setPublicKey(publicKey);
     var xhr = new XMLHttpRequest();
-    xhr.onload = function () {
+    xhr.onload = function() {
         if (xhr.status === 200 || xhr.status === 201) {
             console.log(`result : ${xhr.responseText}`);
-            chkPinCode(xhr.responseText === 'true' ? true : false);
+            if (xhr.responseText === 'retry') {
+                retryPinCode();
+            } else {
+                chkPinCode(xhr.responseText === 'true' ? true : false);
+            }
         } else {
             console.error(xhr.responseText);
         }
     };
     xhr.open('POST', serverURL + ':' + serverPort + '/pincode/pinCodeToServer');
-    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
-    xhr.send(encrypt.encrypt(data));
+    xhr.setRequestHeader("Content-Type", "application/json");
+    var data = encrypt.encrypt(data);
+    console.log(`${TAG} data: ${data}`);
+    xhr.send(JSON.stringify({ pincode: data.toString("utf8") }));
 }
 
-function generatePinCodeNumber() {
-    var xhr = new XMLHttpRequest();
-    xhr.onload = function () {
-        if (xhr.status === 200 || xhr.status === 201) {
-            console.log(xhr.responseText);
-        } else {
-            console.error(xhr.responseText);
-        }
-    };
-    xhr.open('GET', serverURL + ':' + serverPort + '/retryPinCode');
-    xhr.send();
+function retryPinCode() {
+    dots.forEach(function(dot, index) {
+        dot.className += ' wrong';
+    });
+    document.body.className += ' wrong';
+    displayPincode();
+    setTimeout(function() {
+        alert('Failed to input 5 times. A new Pincode has been generated, so check the TV notification.');
+    }, 1000);
 }
 
 function chkPinCode(returnVal) {
     if (returnVal) {
-        dots.forEach(function (dot, index) {
+        dots.forEach(function(dot, index) {
             dot.className += ' correct';
         });
         document.body.className += ' correct';
-        tryCount = 0;
-        setTimeout(function () {
+        setTimeout(function() {
             loginForm.submit();
-            //pinCode();
         }, 1000);
     } else {
-        dots.forEach(function (dot, index) {
+        dots.forEach(function(dot, index) {
             dot.className += ' wrong';
         });
         document.body.className += ' wrong';
-        tryCount++;
-        if (tryCount === 5) {
-            tryCount = 0;
-            generatePinCodeNumber();
-            setTimeout(function () {
-                alert('Failed to input 5 times. A new Pincode has been generated, so check the TV notification.');
-            }, 1000);
-        }
     }
 }
 
@@ -117,6 +107,7 @@ function pinCode() {
     window.location.href = serverURL + ':' + serverPort + '/client/client.html';
 }
 
-window.onload = function () {
-    preloadKey();
+window.onload = function() {
+    displayPincode();
+    init();
 };