ESP8266_swiss_army_board/sdCard_content/WWW/rsrc/script.js
2019-04-26 17:39:39 +02:00

276 lines
6.4 KiB
JavaScript

/**
* JavaScript code for the dashboard logic, refresh and other things
*
* Anatole SCHRAMM-HENRY - Th3maz1ng
**/
//Variable needed for the app
var sigStrenthInter = null;
var rtcInter = null;
var sysinfoInter = null;
//This function set the app up
function init()
{
console.log("Body loaded - starting setup");
refreshRtc();
rtcClockInter = setInterval(refreshRtc,1000);
refreshSysInfo();
sysinfoInter = setInterval(refreshSysInfo,10000);
refreshSigStrength();
sigStrenthInter = setInterval(refreshSigStrength,1000);
sdCardSize();
console.log("Ending setup");
}
function onTimeOut(e)
{
console.error("Request timed out");
document.getElementById('imgConn').style.display = 'none';
document.getElementById('imgDisco').style.display = 'block';
}
function onConnected()
{
document.getElementById('imgDisco').style.display = 'none';
document.getElementById('imgConn').style.display = 'block';
}
function refreshRtc()
{
console.log("Refreshing RTC");
var ajaxReq = new XMLHttpRequest();
ajaxReq.timeout = 5000;
ajaxReq.onreadystatechange = function()
{
switch(this.readyState)
{
case 0:
break;
case 1:
console.log("Connection established");
break;
case 2:
console.log("Request received");
break;
case 3:
console.log("Processing request");
break;
case 4:
console.log("Response is ready");
if(this.status != 0){onConnected();}
if(this.status == 200)
{
console.log("Response : " + this.responseText);
var rtcObj = JSON.parse(this.responseText);
console.log(rtcObj);
document.getElementById('rtcValue').innerHTML = rtcObj.time + ' ' + rtcObj.date;
}
else
{
console.log("Error, status is : " + this.status);
document.getElementById('rtcValue').innerHTML = 'ERROR';
}
break;
default:
console.log("Unknown state");
}
}
ajaxReq.ontimeout = onTimeOut;
ajaxReq.open('GET',"/sab/rtc/get/datetime");
ajaxReq.send();
}
function refreshSysInfo()
{
console.log("Refreshing Sys Infos");
var ajaxReq = new XMLHttpRequest();
ajaxReq.timeout = 5000;
ajaxReq.onreadystatechange = function()
{
switch(this.readyState)
{
case 0:
break;
case 1:
console.log("Connection established");
break;
case 2:
console.log("Request received");
break;
case 3:
console.log("Processing request");
break;
case 4:
console.log("Response is ready");
if(this.status == 200)
{
console.log("Response : " + this.responseText);
var systemInfoObj = JSON.parse(this.responseText);
console.log(systemInfoObj);
document.getElementById('ramInfo').innerHTML = systemInfoObj['free RAM'];
document.getElementById('cpuInfo').innerHTML = systemInfoObj['CPU freq'];
document.getElementById('fragInfo').innerHTML = systemInfoObj['heap frag'];
document.getElementById('blockInfo').innerHTML = systemInfoObj['max block'];
}
else
{
console.log("Error, status is : " + this.status);
document.getElementById('ramInfo').innerHTML = 'NaN';
document.getElementById('cpuInfo').innerHTML = 'NaN';
document.getElementById('fragInfo').innerHTML = 'NaN';
document.getElementById('blockInfo').innerHTML = 'NaN';
}
break;
default:
console.log("Unknown state");
}
}
ajaxReq.ontimeout = onTimeOut;
ajaxReq.open('GET',"/sab/systeminfo");
ajaxReq.send();
}
function refreshSigStrength()
{
console.log("Refreshing Sig Strength");
var ajaxReq = new XMLHttpRequest();
ajaxReq.timeout = 5000;
ajaxReq.onreadystatechange = function()
{
switch(this.readyState)
{
case 0:
break;
case 1:
console.log("Connection established");
break;
case 2:
console.log("Request received");
break;
case 3:
console.log("Processing request");
break;
case 4:
console.log("Response is ready");
if(this.status == 200)
{
console.log("Response : " + this.responseText);
var wifiStaObj = JSON.parse(this.responseText);
console.log(wifiStaObj);
document.getElementById('sigStrength').innerHTML = wifiStaObj.RSSI;
document.getElementById('localIP').innerHTML = wifiStaObj['local IP'];
}
else
{
console.log("Error, status is : " + this.status);
document.getElementById('sigStrength').innerHTML = 'NaN';
document.getElementById('localIP').innerHTML = 'ERROR';
}
break;
default:
console.log("Unknown state");
}
}
ajaxReq.ontimeout = onTimeOut;
ajaxReq.open('GET',"/sab/wifi/stainfo");
ajaxReq.send();
}
function sdCardMountUnmnout(action)
{
console.log("Sd card action : "+action);
var ajaxReq = new XMLHttpRequest();
ajaxReq.timeout = 5000;
ajaxReq.onreadystatechange = function()
{
switch(this.readyState)
{
case 0:
break;
case 1:
console.log("Connection established");
break;
case 2:
console.log("Request received");
break;
case 3:
console.log("Processing request");
break;
case 4:
console.log("Response is ready");
if(this.status == 200)
{
console.log("Response : " + this.responseText);
var sdCardObj = JSON.parse(this.responseText);
console.log(sdCardObj);
sdCardSize();
}
else
{
console.log("Error, status is : " + this.status);
}
break;
default:
console.log("Unknown state");
}
}
ajaxReq.ontimeout = onTimeOut;
ajaxReq.open('GET',"/sab/sdcard/"+action);
ajaxReq.send();
}
function sdCardSize()
{
console.log("Refreshing SD card size : ");
var ajaxReq = new XMLHttpRequest();
ajaxReq.timeout = 5000;
ajaxReq.onreadystatechange = function()
{
switch(this.readyState)
{
case 0:
break;
case 1:
console.log("Connection established");
break;
case 2:
console.log("Request received");
break;
case 3:
console.log("Processing request");
break;
case 4:
console.log("Response is ready");
if(this.status == 200)
{
console.log("Response : " + this.responseText);
var sdCardObj = JSON.parse(this.responseText);
console.log(sdCardObj);
document.getElementById('cardSize').innerHTML = sdCardObj.size == 0 ? 'NO CARD' : sdCardObj.size + ' GByte(s)';
}
else
{
console.log("Error, status is : " + this.status);
document.getElementById('cardSize').innerHTML = 'NaN GByte(s)';
}
break;
default:
console.log("Unknown state");
}
}
ajaxReq.ontimeout = onTimeOut;
ajaxReq.open('GET',"/sab/sdcard/size");
ajaxReq.send();
}