/**
* KatSys Konnector Service
*
* description: This class is called in a separate node process
* in Plugin "KatSys Konnector". This service opens
* a connection to KatSys Cloud (ws) and recieves
* Status, Alarms and ZVEI Code Alarms.
*
* author: Jens Dinstühler 2020
* version: 1.0.0
*
*/
var serviceState = 0; // 0 == not configured, 1 == configured but not connected, 2 == configured and connected
var config = null; // holds configuration from plugin config store
/**
* Log Function for inter process communication
*/
function LogAtMain(msg) {
process.send('{"logmessage" : "' + msg + '"}');
}
/**
*
* 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('Closing connection to KatSys Cloud');
katsys.closeConnection();
serviceState = 1;
}
console.log('Destroying KatSys plugin');
process.quit();
}
}
if (typeof msg.start != 'undefined') {
if(msg.start === 1) {
if (serviceState === 1) {
// KatSys Service is configured, so start it
LogAtMain('Starting connection to KatSys Cloud');
connectToKatsys();
serviceState = 2;
} else {
LogAtMain('Serice not in configured and stopped state. Not starting service');
}
}
}
if (typeof msg.testalarm !== 'undefined') {
if (msg.testalarm === true) {
katsys._parser.TestparseRAWAlarmData();
}
}
});
// GENERIC Error Handling
process.on('unhandledRejection', (reason, p) => {
console.log('PLUGIN | KATSYS | Unhandled Rejection at: Promise', p, 'reason:', reason.stack);
LogAtMain('Unhandled Rejection at: Promise', p, 'reason:', reason.stack);
});
process.on('uncaughtException', function (err) {
console.log('PLUGIN | KATSYS | Caught exception: ' + err);
process.send('{"error" : "' + err.message + '"}');
});
const KatSysCloud = require(__dirname + '/katsys.js');
var katsys = new KatSysCloud();
/**
* configures katsys object and starts connection
*/
function connectToKatsys() {
console.log('CONFIG KATSYS:');
console.log(config);
console.log('SUBTOKEN:');
console.log(config.subToken);
if (serviceState === 1) {
katsys.masterToken = config.masterToken;
katsys.subToken = config.subToken;
katsys.pemFile = config.pemFile;
katsys.loglevel = config.loglevel;
katsys.openConnection();
} else {
LogAtMain('Could not start KatSys Connection as service is not configured');
}
}
/**
* Wires events fired by katsys object to plugin-process
* This is communication between two seperate node processes as the
* plugin itself lives in main thread, the service lives in its own thread.
*/
function registerEvents() {
katsys.on('connectionStateChanged', (newState) => {
// Connection State of Katsys Connection has changed
process.send('{ "event" : { "event" : "connectionStateChanged", "newState" : ' + newState + ' } }');
})
katsys.on('statusParsed', (statusInfo) => {
// Connection State of Katsys Connection has changed
process.send('{ "event" : { "event" : "statusParsed", "statusInfo" : ' + JSON.stringify(statusInfo) + ' } }');
})
katsys.on('zveiParsed', (zveiInfo) => {
process.send('{ "event" : { "event" : "zveiParsed", "zveiInfo" : ' + JSON.stringify(zveiInfo) + ' } }');
})
katsys.on('alarmParsed', (alarmInfo) => {
process.send('{ "event" : { "event" : "alarmParsed", "alarmInfo" : ' + JSON.stringify(alarmInfo) + ' } }');
})
katsys.on('katsys_try_reconnect', (tryCount) => {
process.send(`{ "event" : { "event" : "katsys_try_reconnect", "tryCount" : ${tryCount} } }`);
})
katsys.on('ws_error', (error) => {
process.send('{ "event" : { "event" : "katsys_error", "error" : ' + JSON.stringify(error) + ' } }');
})
}
/**
* call register events
*/
registerEvents();