2017-12-04 18:22:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Time in seconds between every request.
|
|
|
|
*/
|
2018-01-17 11:49:09 +00:00
|
|
|
var INTERVAL = 15;
|
2017-12-04 18:22:58 +00:00
|
|
|
|
|
|
|
var requestInterval = INTERVAL;
|
2017-12-05 11:49:05 +00:00
|
|
|
var failedRequests = 0;
|
2017-12-04 18:22:58 +00:00
|
|
|
var timeoutId = null;
|
|
|
|
var request = null;
|
2018-01-17 11:49:09 +00:00
|
|
|
var lastText;
|
|
|
|
var lastStatus;
|
|
|
|
var lastError;
|
2017-12-04 18:22:58 +00:00
|
|
|
|
2017-12-05 11:49:05 +00:00
|
|
|
function onBodyLoad ()
|
|
|
|
{
|
2018-01-17 11:49:09 +00:00
|
|
|
setText ();
|
|
|
|
}
|
|
|
|
|
|
|
|
function setText ()
|
|
|
|
{
|
|
|
|
lastText = null;
|
|
|
|
lastStatus = null;
|
|
|
|
lastError = null;
|
2017-12-05 11:49:05 +00:00
|
|
|
updateText ();
|
|
|
|
}
|
|
|
|
|
2017-12-04 18:22:58 +00:00
|
|
|
function onSettingsClick ()
|
|
|
|
{
|
|
|
|
var deviceId = localStorage.getItem ('hederaDeviceId');
|
|
|
|
|
|
|
|
if (deviceId == null)
|
|
|
|
deviceId = '';
|
|
|
|
|
|
|
|
deviceId = prompt ('Enter the device identifier', deviceId);
|
|
|
|
|
2018-01-17 11:49:09 +00:00
|
|
|
if (deviceId == null)
|
|
|
|
return;
|
|
|
|
|
2017-12-04 18:22:58 +00:00
|
|
|
if (deviceId)
|
|
|
|
localStorage.setItem ('hederaDeviceId', deviceId);
|
|
|
|
else if (deviceId === '')
|
|
|
|
localStorage.removeItem ('hederaDeviceId');
|
|
|
|
|
|
|
|
if (timeoutId != null)
|
|
|
|
{
|
|
|
|
clearTimeout (timeoutId);
|
|
|
|
timeoutId = null;
|
|
|
|
}
|
|
|
|
if (request)
|
|
|
|
{
|
|
|
|
request.abort ();
|
|
|
|
request = null;
|
|
|
|
}
|
|
|
|
|
2018-01-17 11:49:09 +00:00
|
|
|
setText ();
|
2017-12-04 18:22:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function updateText ()
|
|
|
|
{
|
|
|
|
var deviceId = localStorage.getItem ('hederaDeviceId');
|
|
|
|
|
|
|
|
if (!deviceId)
|
|
|
|
{
|
2018-01-17 11:49:09 +00:00
|
|
|
display (null, null, 'Device id not set');
|
2017-12-04 18:22:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var formData = new FormData ();
|
|
|
|
formData.append ('srv', 'json:misc/production');
|
|
|
|
formData.append ('deviceId', deviceId);
|
|
|
|
|
|
|
|
request = new XMLHttpRequest();
|
2017-12-05 11:49:05 +00:00
|
|
|
request.open ('POST', '', true);
|
2017-12-04 18:22:58 +00:00
|
|
|
request.onreadystatechange =
|
|
|
|
onRequestChange.bind (null, request);
|
|
|
|
request.send (formData);
|
|
|
|
}
|
|
|
|
|
|
|
|
function onRequestChange (request)
|
|
|
|
{
|
|
|
|
if (request.readyState !== 4)
|
|
|
|
return;
|
|
|
|
|
|
|
|
try {
|
|
|
|
switch (request.status)
|
|
|
|
{
|
|
|
|
case 200:
|
2018-01-17 11:49:09 +00:00
|
|
|
var data = JSON.parse (request.responseText).data;
|
|
|
|
display (data.displayText, data.status, null);
|
2017-12-04 18:22:58 +00:00
|
|
|
requestInterval = INTERVAL;
|
2017-12-05 11:49:05 +00:00
|
|
|
failedRequests = 0;
|
2017-12-04 18:22:58 +00:00
|
|
|
break;
|
|
|
|
case 400:
|
|
|
|
var json = JSON.parse (request.responseText);
|
|
|
|
throw new Error (json.data.message);
|
2018-01-17 11:49:09 +00:00
|
|
|
case 0:
|
|
|
|
throw new Error ('Connection lost');
|
2017-12-04 18:22:58 +00:00
|
|
|
default:
|
|
|
|
throw new Error ('HTTP '+ request.status +': '+ request.statusText);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (e)
|
|
|
|
{
|
2018-01-17 11:49:09 +00:00
|
|
|
display (null, null, e.message);
|
2017-12-04 18:22:58 +00:00
|
|
|
console.error (e.message);
|
2017-12-05 11:49:05 +00:00
|
|
|
failedRequests++;
|
2017-12-04 18:22:58 +00:00
|
|
|
|
2017-12-05 11:49:05 +00:00
|
|
|
if (failedRequests > 10 && requestInterval < 60)
|
2017-12-04 18:22:58 +00:00
|
|
|
{
|
2018-01-17 11:49:09 +00:00
|
|
|
requestInterval += parseInt (Math.random() * INTERVAL) + 1;
|
2017-12-04 18:22:58 +00:00
|
|
|
console.warn ('Request interval increased to %d seconds.', requestInterval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
timeoutId = setTimeout (updateText, requestInterval * 1000);
|
|
|
|
request = null;
|
|
|
|
}
|
|
|
|
|
2018-01-17 11:49:09 +00:00
|
|
|
function display (text, status, error)
|
2017-12-04 18:22:58 +00:00
|
|
|
{
|
2018-01-17 11:49:09 +00:00
|
|
|
if (error)
|
2017-12-04 18:22:58 +00:00
|
|
|
{
|
2018-01-17 11:49:09 +00:00
|
|
|
text = lastText;
|
|
|
|
status = lastStatus;
|
2017-12-04 18:22:58 +00:00
|
|
|
}
|
2018-01-17 11:49:09 +00:00
|
|
|
|
|
|
|
if (text === lastText && status === lastStatus && error === lastError)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var bgColor;
|
|
|
|
$('text').textContent = text;
|
|
|
|
|
|
|
|
if (error)
|
2017-12-04 18:22:58 +00:00
|
|
|
{
|
2018-01-17 11:49:09 +00:00
|
|
|
$('error').textContent = error;
|
2017-12-04 18:22:58 +00:00
|
|
|
$('error').style.display = 'block';
|
2017-12-11 08:38:25 +00:00
|
|
|
bgColor = 'red';
|
|
|
|
}
|
2018-01-17 11:49:09 +00:00
|
|
|
else
|
2017-12-11 08:38:25 +00:00
|
|
|
{
|
2018-01-17 11:49:09 +00:00
|
|
|
$('error').textContent = '';
|
|
|
|
$('error').style.display = 'none';
|
|
|
|
bgColor = 'green';
|
2017-12-04 18:22:58 +00:00
|
|
|
}
|
2017-12-11 08:38:25 +00:00
|
|
|
|
2018-01-17 11:49:09 +00:00
|
|
|
var body = document.body;
|
|
|
|
body.addEventListener ('transitionend', onTransitionEnd);
|
|
|
|
body.style.backgroundColor = bgColor;
|
|
|
|
|
2017-12-11 08:38:25 +00:00
|
|
|
lastText = text;
|
2018-01-17 11:49:09 +00:00
|
|
|
lastStatus = status;
|
2017-12-11 08:38:25 +00:00
|
|
|
lastError = error;
|
|
|
|
}
|
|
|
|
|
|
|
|
function onTransitionEnd ()
|
|
|
|
{
|
|
|
|
var body = document.body;
|
|
|
|
body.removeEventListener ('transitionend', onTransitionEnd);
|
|
|
|
body.style.backgroundColor = '';
|
2018-01-17 11:49:09 +00:00
|
|
|
body.className = lastStatus ? lastStatus : '';
|
2017-12-04 18:22:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function $ (elementId)
|
|
|
|
{
|
|
|
|
return document.getElementById (elementId);
|
|
|
|
}
|