/**
* WALLBOARD Service
*
* description: This class is called in a separate node process
* in Plugin "WALLBOARD". This service opens
* offers a websocket for the wallboard clients
*
* author: Jens Dinstühler 2020
* version: 1.0.0
*
*/
// required modules for WebSocket
const express = require('express')
const app = express();
const http = require('http');
const server = http.Server(app);
const socketIO = require('socket.io');
const io = socketIO(server);
const port = process.env.PORT || 5020;
// required for core integrationd and communication
const Database = require('../../../models/database');
const Feedback = require('../../../models/feedbackstates');
// initialization of general properties
var coreDb = null;
global.appRoot = __dirname + '/../../..';
global.coreDb = null;
var serviceState = 0; // 0 == not configured, 1 == configured but not connected, 2 == configured and connected
/**
* Starts the coredatabase connection
*/
function startCoredatabase() {
coreDb = new Database();
coreDb.startDatabase((successDatabase) => {
if (successDatabase) {
global.coreDb = coreDb.db;
LogAtMain('Core Database Connection established');
// Database started, start server now
} else {
// Database could not be started. -> Log error to console
LogAtMain('ERROR: Core Database Connection could not be established');
}
});
}
/**
* Sends an operation object for testing purposes
* @param {object} aSocket socket.io socket object
*/
function sendOperation(aSocket) {
var operation = {
keyword: 'THL 3 - VU',
location: 'Reichenschwand',
street: 'Am Saueracker 36',
community: 'Reichenschwand',
object: 'Haus Dinstühler',
section: 'Kreuzung',
lat: 49.12344,
lon: 11.34233
}
aSocket.emit('operation', operation);
}
/**
* Log Function for inter process communication
*/
function LogAtMain(msg) {
process.send('{"logmessage" : "' + msg + '"}');
}
function routeIncomingIPCCommands(msg) {
var payload = [];
try {
payload = JSON.parse(msg);
} catch (err) {
payload = [];
console.log('WALLBOARD | WEBSOCKET | Unknown IPC Message from parent process: ' + msg);
}
// NOW CHECK FOR COMMAND
if (typeof msg.event_new_alarm != 'undefined') {
console.log('WALLBOARD | WEBSOCKET | SIGNAL | event_new_alarm');
}
if (typeof msg.event_closed_alarm != 'undefined') {
console.log('WALLBOARD | WEBSOCKET | SIGNAL | event_closed_alarm');
}
if (typeof msg.event_new_feedback != 'undefined') {
console.log('WALLBOARD | WEBSOCKET | SIGNAL | event_new_feedback');
}
}
/**
* Wire messages coming from Plugin base.js
*/
process.on('message', (msg) => {
if (typeof msg.config != 'undefined') {
config = msg.config;
serviceState = 1;
LogAtMain('Recieved Configuration from CORE');
}
if (typeof msg.quit != 'undefined') {
if(msg.quit === 1) {
if (serviceState === 2) {
// KatSys Service is connected, so stop it
console.log('WALLBOARD | WEBSOCKET | Shutting down Wallboard WEBSOCKET');
katsys.closeConnection();
serviceState = 1;
}
console.log('WALLBOARD | WEBSOCKET | Destroying Wallboard plugin');
process.quit();
}
}
routeIncomingIPCCommands(msg);
});
// GENERIC Error Handling
process.on('unhandledRejection', (reason, p) => {
console.log('WALLBOARD | WEBSOCKET | Unhandled Rejection at: Promise', p, 'reason:', reason.stack);
LogAtMain('Unhandled Rejection at: Promise', p, 'reason:', reason.stack);
});
process.on('uncaughtException', function (err) {
console.log('WALLBOARD | WEBSOCKET | Caught exception: ' + err);
process.send('{"error" : "' + err.message + '"}');
});
startCoredatabase();
io.on('connection', (socket) => {
console.log('WALLBOARD | WEBSOCKET | Benutzer nun verbunden');
socket.emit('updateData', '-');
sendOperation(socket);
var ipAddress = socket.handshake.address;
process.send('{"newConnection":"' + ipAddress + '"}');
socket.on('disconnect', () => {
console.log('WALLBOARD | WEBSOCKET | Benutzerverbindung abgebaut');
process.send('{"lostConnection":1}');
});
socket.on('message', (msg) => {
console.log('WALLBOARD | WEBSOCKET | incoming message: ' + msg);
socket.emit('updateData', msg);
});
});
server.listen(port, () => {
console.log(`PLUGIN | WALLBOARD | SERVICE | WEBSOCKET | started at port: ${port}`);
});