2017-12-04 18:22:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Time in seconds between every request.
|
|
|
|
*/
|
2017-12-05 12:59:22 +00:00
|
|
|
var INTERVAL = 10;
|
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;
|
2017-12-11 08:38:25 +00:00
|
|
|
var lastText = null;
|
|
|
|
var lastError = null;
|
2017-12-04 18:22:58 +00:00
|
|
|
|
2017-12-05 11:49:05 +00:00
|
|
|
function onBodyLoad ()
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
updateText ();
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateText ()
|
|
|
|
{
|
|
|
|
var deviceId = localStorage.getItem ('hederaDeviceId');
|
|
|
|
|
|
|
|
if (!deviceId)
|
|
|
|
{
|
|
|
|
display ('Device id not set', true);
|
|
|
|
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:
|
|
|
|
var json = JSON.parse (request.responseText);
|
|
|
|
display (json.data);
|
|
|
|
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);
|
|
|
|
default:
|
|
|
|
throw new Error ('HTTP '+ request.status +': '+ request.statusText);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (e)
|
|
|
|
{
|
|
|
|
display (e.message, true);
|
|
|
|
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
|
|
|
{
|
|
|
|
requestInterval += parseInt (Math.random() * 10) + 1;
|
|
|
|
console.warn ('Request interval increased to %d seconds.', requestInterval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
timeoutId = setTimeout (updateText, requestInterval * 1000);
|
|
|
|
request = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function display (text, error)
|
|
|
|
{
|
2017-12-11 08:38:25 +00:00
|
|
|
var bgColor;
|
|
|
|
|
2017-12-04 18:22:58 +00:00
|
|
|
if (!error)
|
|
|
|
{
|
|
|
|
$('text').textContent = text;
|
|
|
|
$('error').textContent = '';
|
|
|
|
$('error').style.display = 'none';
|
2017-12-11 08:38:25 +00:00
|
|
|
bgColor = 'green';
|
2017-12-04 18:22:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$('text').textContent = 'Er';
|
|
|
|
$('error').textContent = text;
|
|
|
|
$('error').style.display = 'block';
|
2017-12-11 08:38:25 +00:00
|
|
|
bgColor = 'red';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (text != lastText || lastError != error)
|
|
|
|
{
|
|
|
|
var body = document.body;
|
|
|
|
body.addEventListener ('transitionend', onTransitionEnd);
|
|
|
|
body.style.backgroundColor = bgColor;
|
2017-12-04 18:22:58 +00:00
|
|
|
}
|
2017-12-11 08:38:25 +00:00
|
|
|
|
|
|
|
lastText = text;
|
|
|
|
lastError = error;
|
|
|
|
}
|
|
|
|
|
|
|
|
function onTransitionEnd ()
|
|
|
|
{
|
|
|
|
var body = document.body;
|
|
|
|
body.removeEventListener ('transitionend', onTransitionEnd);
|
|
|
|
body.style.backgroundColor = '';
|
2017-12-04 18:22:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function $ (elementId)
|
|
|
|
{
|
|
|
|
return document.getElementById (elementId);
|
|
|
|
}
|