2018-06-30 16:20:54 +02:00
|
|
|
import * as store from './store';
|
|
|
|
import config from './config';
|
|
|
|
import {singelton as notify} from './element/notify';
|
|
|
|
import {render} from './gui';
|
2017-05-08 19:13:29 +02:00
|
|
|
|
2018-06-30 16:20:54 +02:00
|
|
|
const RECONNECT_AFTER = 5000,
|
|
|
|
RETRY_QUERY = 300,
|
|
|
|
query = [],
|
|
|
|
eventMSGID = {},
|
|
|
|
eventTo = {};
|
2017-05-08 19:13:29 +02:00
|
|
|
|
2018-06-30 16:20:54 +02:00
|
|
|
let connectionID = localStorage.getItem('session'),
|
2018-07-19 21:54:13 +02:00
|
|
|
socket = null,
|
|
|
|
connectionEstablished = false;
|
2017-05-09 01:03:44 +02:00
|
|
|
|
2018-06-30 16:20:54 +02:00
|
|
|
function newUUID () {
|
|
|
|
/* eslint-disable */
|
|
|
|
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
|
|
|
|
const r = Math.random() * 16 | 0,
|
|
|
|
v = c === 'x' ? r : r & 0x3 | 0x8;
|
|
|
|
return v.toString(16);
|
|
|
|
});
|
|
|
|
/* eslint-enable */
|
|
|
|
}
|
|
|
|
|
|
|
|
function correctMSG (obj) {
|
|
|
|
if (!obj.id) {
|
|
|
|
obj.id = newUUID();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function onerror (err) {
|
|
|
|
console.warn(err);
|
|
|
|
// eslint-disable-next-line no-magic-numbers
|
|
|
|
if (socket.readyState !== 3) {
|
|
|
|
notify.send({
|
|
|
|
'header': 'Verbindung',
|
|
|
|
'type': 'error'
|
|
|
|
}, 'Verbindung zum Server unterbrochen!');
|
|
|
|
}
|
|
|
|
render();
|
|
|
|
socket.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
function onopen () {
|
2018-07-19 21:54:13 +02:00
|
|
|
connectionEstablished = true;
|
2018-06-30 16:20:54 +02:00
|
|
|
render();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function sendjson (obj, callback) {
|
|
|
|
if (socket.readyState !== 1) {
|
|
|
|
query.push({
|
|
|
|
'callback': callback,
|
|
|
|
'obj': obj
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
correctMSG(obj);
|
|
|
|
const socketMSG = JSON.stringify(obj);
|
|
|
|
socket.send(socketMSG);
|
|
|
|
if (typeof callback === 'function') {
|
|
|
|
eventMSGID[obj.id] = callback;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function onmessage (raw) {
|
|
|
|
const msg = JSON.parse(raw.data),
|
|
|
|
msgFunc = eventMSGID[msg.id],
|
|
|
|
eventFuncs = eventTo[msg.subject];
|
|
|
|
|
|
|
|
if (msg.subject === 'session_init') {
|
|
|
|
if (connectionID === null) {
|
|
|
|
connectionID = newUUID();
|
|
|
|
localStorage.setItem('session', connectionID);
|
2017-05-16 19:18:35 +02:00
|
|
|
}
|
2018-06-30 16:20:54 +02:00
|
|
|
msg.id = connectionID;
|
|
|
|
sendjson(msg);
|
|
|
|
render();
|
|
|
|
return;
|
2017-05-16 19:18:35 +02:00
|
|
|
}
|
2017-05-08 19:13:29 +02:00
|
|
|
|
2018-06-30 16:20:54 +02:00
|
|
|
if (msgFunc) {
|
|
|
|
msgFunc(msg);
|
|
|
|
delete eventMSGID[msg.id];
|
|
|
|
render();
|
|
|
|
return;
|
2017-05-16 19:18:35 +02:00
|
|
|
}
|
2017-05-08 19:13:29 +02:00
|
|
|
|
2018-06-30 16:20:54 +02:00
|
|
|
if (typeof eventFuncs === 'object' && eventFuncs.length > 0) {
|
|
|
|
// eslint-disable-next-line guard-for-in
|
|
|
|
for (const key in eventFuncs) {
|
|
|
|
const func = eventFuncs[key];
|
|
|
|
if (func) {
|
|
|
|
func(msg);
|
2017-05-16 19:18:35 +02:00
|
|
|
}
|
|
|
|
}
|
2018-06-30 16:20:54 +02:00
|
|
|
render();
|
|
|
|
return;
|
2017-05-16 19:18:35 +02:00
|
|
|
}
|
2017-05-09 01:03:44 +02:00
|
|
|
|
2018-06-30 16:20:54 +02:00
|
|
|
notify.send('warning', `unable to identify message: ${msg.subject}`);
|
|
|
|
render();
|
|
|
|
}
|
|
|
|
|
|
|
|
function onclose () {
|
|
|
|
console.log('socket closed by server');
|
2018-07-19 21:54:13 +02:00
|
|
|
if (connectionEstablished) {
|
|
|
|
notify.send({
|
|
|
|
'header': 'Verbindung',
|
|
|
|
'type': 'warning'
|
|
|
|
}, 'Verbindung zum Server beendet!');
|
|
|
|
}
|
2018-06-30 16:20:54 +02:00
|
|
|
render();
|
2018-07-19 21:54:13 +02:00
|
|
|
connectionEstablished = false;
|
2018-06-30 16:20:54 +02:00
|
|
|
// eslint-disable-next-line no-use-before-define
|
|
|
|
window.setTimeout(connect, RECONNECT_AFTER);
|
|
|
|
}
|
|
|
|
|
|
|
|
function connect () {
|
|
|
|
socket = new window.WebSocket(config.backend);
|
|
|
|
socket.onopen = onopen;
|
|
|
|
socket.onerror = onerror;
|
|
|
|
socket.onmessage = onmessage;
|
|
|
|
socket.onclose = onclose;
|
|
|
|
sendjson({'subject': 'auth_status'});
|
|
|
|
sendjson({'subject': 'connect'});
|
|
|
|
}
|
|
|
|
|
|
|
|
window.setInterval(() => {
|
|
|
|
const queryEntry = query.pop();
|
|
|
|
if (queryEntry) {
|
|
|
|
sendjson(queryEntry.obj, queryEntry.callback);
|
2017-05-16 19:18:35 +02:00
|
|
|
}
|
2018-06-30 16:20:54 +02:00
|
|
|
}, RETRY_QUERY);
|
2017-05-09 01:03:44 +02:00
|
|
|
|
2017-05-16 19:18:35 +02:00
|
|
|
|
2018-06-30 16:20:54 +02:00
|
|
|
export function getStatus () {
|
|
|
|
if (socket) {
|
|
|
|
return socket.readyState;
|
2017-05-30 14:35:11 +02:00
|
|
|
}
|
2018-06-30 16:20:54 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2017-05-16 19:18:35 +02:00
|
|
|
|
2018-06-30 16:20:54 +02:00
|
|
|
export function setEvent (to, func) {
|
|
|
|
eventTo[to] = [func];
|
|
|
|
}
|
2017-05-16 19:18:35 +02:00
|
|
|
|
2018-06-30 16:20:54 +02:00
|
|
|
export function addEvent (to, func) {
|
|
|
|
if (typeof eventTo[to] !== 'object') {
|
|
|
|
eventTo[to] = [];
|
2017-05-16 19:18:35 +02:00
|
|
|
}
|
2018-06-30 16:20:54 +02:00
|
|
|
eventTo[to].push(func);
|
|
|
|
}
|
2017-05-16 19:18:35 +02:00
|
|
|
|
2018-06-30 16:20:54 +02:00
|
|
|
export function delEvent (to, func) {
|
|
|
|
if (typeof eventTo[to] === 'object' && eventTo[to].length > 1) {
|
|
|
|
eventTo[to].pop(func);
|
|
|
|
} else {
|
|
|
|
eventTo[to] = [];
|
2017-05-16 19:18:35 +02:00
|
|
|
}
|
2018-06-30 16:20:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function sendnode(node, callback) {
|
|
|
|
sendjson({'subject':'node-system','body': node}, (msg) => {
|
2018-07-17 23:13:51 +02:00
|
|
|
if(!msg.body) {
|
2018-06-30 16:20:54 +02:00
|
|
|
notify.send({
|
|
|
|
'header': 'Speichern',
|
|
|
|
'type': 'error',
|
|
|
|
}, `Einstellungen für '${node.node_id}' wurden nicht gespeichert.`);
|
|
|
|
}
|
|
|
|
if (typeof callback === 'function') {
|
|
|
|
callback(msg);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
setEvent('auth_status', (msg) => {
|
|
|
|
if (msg.body) {
|
|
|
|
store.isLogin = true;
|
|
|
|
|
|
|
|
notify.send({
|
|
|
|
'header': 'Login',
|
|
|
|
'type': 'success'
|
|
|
|
},'Willkommen zurück!');
|
|
|
|
} else {
|
|
|
|
store.isLogin = false;
|
|
|
|
}
|
|
|
|
render();
|
|
|
|
});
|
|
|
|
|
2018-07-15 21:55:13 +02:00
|
|
|
addEvent('node-system', (msg) => {
|
2018-06-30 16:20:54 +02:00
|
|
|
store.updateNode(msg.body, true);
|
|
|
|
});
|
2018-07-15 21:55:13 +02:00
|
|
|
addEvent('node-current', (msg) => {
|
2018-06-30 16:20:54 +02:00
|
|
|
store.updateNode(msg.body);
|
|
|
|
});
|
2018-07-15 21:55:13 +02:00
|
|
|
addEvent('channels_wifi24', (msg) => {
|
|
|
|
store.channelsWifi24 = msg.body.sort((a, b) => a - b);
|
|
|
|
});
|
|
|
|
addEvent('channels_wifi5', (msg) => {
|
|
|
|
store.channelsWifi5 = msg.body.sort((a, b) => a - b);
|
|
|
|
});
|
2017-05-16 19:18:35 +02:00
|
|
|
|
2018-06-30 16:20:54 +02:00
|
|
|
connect();
|