347 lines
9 KiB
JavaScript
347 lines
9 KiB
JavaScript
var LN = (function(){
|
|
var appInterface;
|
|
|
|
var defaultOptions = {
|
|
url: null,
|
|
|
|
};
|
|
|
|
class LNInterface {
|
|
constructor(opt){
|
|
var self = this;
|
|
|
|
this.options = {}
|
|
Object.assign(this.options,opt);
|
|
|
|
if (this.options.url == null)
|
|
this.options.url = this.constructURL();
|
|
|
|
|
|
this.rpcCallbacks = [];
|
|
this.rpcNextID = 1;
|
|
|
|
this.websocket = new WebSocket(this.options.url);
|
|
this.websocket.onerror = function(e){
|
|
alert("WebSocket caught error: " + e.date);
|
|
}
|
|
this.websocket.onmessage = function(e){
|
|
try{
|
|
var j = JSON.parse(e.data);
|
|
if (j.state){
|
|
updateState(j.state);
|
|
} else if (j.id)
|
|
{
|
|
for (var n=0;n<self.rpcCallbacks.length;n++)
|
|
{
|
|
if (self.rpcCallbacks[n].id == j.id)
|
|
{
|
|
if (j.error)
|
|
{
|
|
console.log("RPCResult with error received: " + JSON.stringify(j.error));
|
|
}
|
|
self.rpcCallbacks[n].cbfn(j.result,j.error);
|
|
self.rpcCallbacks.splice(n,1);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
} catch(exc){
|
|
console.log(exc);
|
|
console.log("websocket malformed message: " + (e.data));
|
|
|
|
$("<textarea></textarea>").text(e.data).appendTo($(body));
|
|
}
|
|
}
|
|
}
|
|
|
|
rpc(module,method,parameters,cbfn){
|
|
|
|
var rpcCall = {
|
|
module: module,
|
|
method: method,
|
|
parameters: parameters,
|
|
id: this.rpcNextID++,
|
|
};
|
|
|
|
if (this.websocket.readyState != 1)
|
|
{
|
|
setTimeout(function(){
|
|
LN().rpc(module,method,parameters,cbfn);
|
|
},250);
|
|
} else {
|
|
this.rpcCallbacks.push( { id: rpcCall.id, cbfn: cbfn } );
|
|
this.websocket.send(
|
|
JSON.stringify(rpcCall)
|
|
);
|
|
}
|
|
}
|
|
|
|
close(){
|
|
this.websocket.close();
|
|
}
|
|
|
|
|
|
|
|
|
|
constructURL(){
|
|
var pageURI = window.location;
|
|
|
|
var scheme = pageURI.scheme == "https" ? "wss:" : "ws:";
|
|
var host = pageURI.host;
|
|
|
|
return scheme + "//" + host + "/socket";
|
|
}
|
|
|
|
|
|
}
|
|
|
|
return function(options){
|
|
if (!appInterface)
|
|
appInterface = new LNInterface(options);
|
|
return appInterface;
|
|
};
|
|
})();
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Object.values = function(o) {
|
|
var values = [];
|
|
for(var property in o) {
|
|
values.push(o[property]);
|
|
}
|
|
return values;
|
|
}
|
|
|
|
function encodeID( t )
|
|
{
|
|
return ("" + t).replace( /[\.\/]/g, "_");
|
|
}
|
|
|
|
var lagDetector = null;
|
|
|
|
function updateState(state)
|
|
{
|
|
try
|
|
{
|
|
if (lagDetector)
|
|
clearTimeout(lagDetector);
|
|
|
|
$("#ServerTime").text("ServerTime: " + moment(state.currentTime).format());
|
|
|
|
lagDetector = setTimeout(function(){
|
|
$("#ServerTime").text("Server lag detected");
|
|
}, 2000);
|
|
|
|
} catch (e)
|
|
{
|
|
$("#ServerTime").text("Server state unexpected!");
|
|
}
|
|
}
|
|
|
|
function SKYAPI(baseurl){
|
|
|
|
this.baseurl = baseurl;
|
|
this.refresh = []
|
|
|
|
this.websocket = new WebSocket("ws://localhost:8080/socket");
|
|
this.websocket.onerror = function(e){
|
|
alert("WebSocket Error: " + e);
|
|
}
|
|
this.websocket.onmessage = function(e){
|
|
var j = JSON.parse(e.data);
|
|
if (j.state){
|
|
updateState(j.state);
|
|
}
|
|
}
|
|
|
|
this.setBaseURL = function(url){ this.baseurl = url; }
|
|
this.addRefresh = function( rh, seconds = null ){ this.refresh.push( { interval: seconds ? seconds : 5, refresh: rh } ); }
|
|
|
|
this.get = function(page, json, handler = null){ return this.__request("GET", page, json, handler); }
|
|
this.post = function(page, json, handler = null){ return this.__request("POST", page, json, handler); }
|
|
this.put = function(page, json, handler = null){ return this.__request("PUT", page, json, handler); }
|
|
|
|
this.__request = function(method, page, json, handler = null){
|
|
if (page[0] == '/')
|
|
page = page.substr(1);
|
|
|
|
var x = new XMLHttpRequest();
|
|
if (handler != null)
|
|
{
|
|
x.onload = function(){
|
|
var responseText = x.responseText;
|
|
if (json && !content)
|
|
handler( JSON.parse( responseText ) );
|
|
else
|
|
handler( responseText );
|
|
}
|
|
}
|
|
x.open(method, this.baseurl + page);
|
|
|
|
if (json)
|
|
x.send(JSON.stringify(json));
|
|
else
|
|
x.send();
|
|
}
|
|
|
|
this.getJson = function(page, handler){
|
|
var j = function(t){
|
|
handler(JSON.parse(t));
|
|
};
|
|
return this.get( page, null, j );
|
|
}
|
|
|
|
this.call = function(endpoint,method,parameters = [], receiver = null){
|
|
var x = new XMLHttpRequest();
|
|
|
|
x.open("POST", this.baseurl + endpoint, (receiver != null));
|
|
x.setRequestHeader("content-type","application/json");
|
|
|
|
if (receiver)
|
|
{
|
|
x.onload = function(){ var r = JSON.parse(this.responseText).Result; receiver(r); }
|
|
x.onerror = function(){ receiver(false); }
|
|
}
|
|
|
|
var methodCall = {
|
|
"MethodName": method,
|
|
"Parameters": parameters
|
|
}
|
|
|
|
x.send(JSON.stringify(methodCall));
|
|
|
|
if (!receiver)
|
|
{
|
|
var result = JSON.parse(x.responseText);
|
|
if (result.Exception != null)
|
|
throw result.Exception;
|
|
|
|
return result.Result;
|
|
}
|
|
return x;
|
|
}
|
|
|
|
this.loadPage = function (page) {
|
|
if (page[0] == '/')
|
|
page = page.substr(1);
|
|
|
|
var x = new XMLHttpRequest();
|
|
|
|
x.open("GET", this.baseurl + page);
|
|
x.setRequestHeader("x-template-unframed","unframed");
|
|
x.onload = function()
|
|
{
|
|
$("#content").empty();
|
|
$("#content").append(this.responseText);
|
|
history.pushState(null, page, skyapi().baseurl + page);
|
|
}
|
|
|
|
this.refresh = []
|
|
|
|
x.send();
|
|
|
|
return false;
|
|
}
|
|
|
|
this.fireOnLoad = function(element){
|
|
if (element.onload != null)
|
|
{
|
|
element.onload();
|
|
}
|
|
|
|
for (var n=0;n<element.children.length;n++)
|
|
this.fireOnLoad(element.children[n]);
|
|
}
|
|
|
|
this.__refresh_index = 0;
|
|
this.UIRefresh = function(){
|
|
|
|
this.__refresh_index++;
|
|
|
|
for (var n=0;n<this.refresh.length;n++)
|
|
{
|
|
var r = this.refresh[n];
|
|
if ((this.__refresh_index % r.interval)==0)
|
|
r.refresh();
|
|
}
|
|
|
|
}
|
|
|
|
setInterval( function(){ skyapi().UIRefresh(); }, 1000 );
|
|
}
|
|
|
|
function showStatistics(stats)
|
|
{
|
|
try
|
|
{
|
|
$("#ServerTime").text("ServerTime: " + stats.ServerTime);
|
|
|
|
$("#indHttpServer").attr("state",stats.States.HttpServer);
|
|
$("#indManager").attr("state",stats.States.Manager);
|
|
$("#indCrawler").attr("state",stats.States.Crawler);
|
|
$("#indChecks").attr("state",stats.States.Checks);
|
|
$("#indDispatcher").attr("state",stats.States.Dispatcher);
|
|
|
|
$("#indHttpServer").attr("title",stats.States.HttpServer);
|
|
$("#indManager").attr("title",stats.States.Manager);
|
|
$("#indCrawler").attr("title",stats.States.Crawler);
|
|
$("#indChecks").attr("title",stats.States.Checks);
|
|
$("#indDispatcher").attr("title",stats.States.Dispatcher);
|
|
|
|
} catch (e)
|
|
{
|
|
$("#ServerTime").text("Server unreachable");
|
|
|
|
$("#indHttpServer").attr("state",3);
|
|
$("#indManager").attr("state",0);
|
|
$("#indCrawler").attr("state",0);
|
|
$("#indChecks").attr("state",0);
|
|
$("#indDispatcher").attr("state",0);
|
|
|
|
$("#indHttpServer").attr("title","UNKNOWN");
|
|
$("#indManager").attr("title","UNKNOWN");
|
|
$("#indCrawler").attr("title","UNKNOWN");
|
|
$("#indChecks").attr("title","UNKNOWN");
|
|
$("#indDispatcher").attr("title","UNKNOWN");
|
|
}
|
|
}
|
|
|
|
function updateStatistics()
|
|
{
|
|
try
|
|
{
|
|
var request = skyapi().call("api/management","GetStatistics",[],showStatistics);
|
|
} catch (e)
|
|
{
|
|
showStatistics(false);
|
|
}
|
|
}
|
|
|
|
|
|
var __skyapi = new SKYAPI("/");
|
|
|
|
function skyapi()
|
|
{
|
|
return __skyapi;
|
|
}
|
|
|
|
|
|
|
|
function ScaleSI(value)
|
|
{
|
|
if (value > 1000000000)
|
|
return ((value / 1000000000) | 0) + "G";
|
|
if (value > 1000000)
|
|
return ((value / 1000000) | 0) + "M";
|
|
if (value > 1000)
|
|
return ((value / 1000) | 0) + "k";
|
|
return value;
|
|
}
|
|
|
|
|
|
*/
|
|
|