divera_status/base.js
2024-09-23 22:45:58 +02:00

129 lines
5.5 KiB
JavaScript

const PluginBaseTemplate = require('./../../templates/pluginBaseTemplate');
const manifest = require('./manifest.json');
const Basedata = require('../../../models/basedata');
const Operation = require('../../../models/core/core_operation');
const Organizations = require('../../../models/organizations');
const Alertdevices = require('../../../models/alertdevices');
const Configuration = require('../../../models/configuration');
const FCMTokens = require('../../../models/fctmtokens');
/**
* Class for DIVERA_STATUS communication
* @class plugins/inbound/divera_status
* @namespace plugins/inbound/divera_status
* @memberof plugins/inbound/divera_status
*/
class divera_status extends PluginBaseTemplate {
/**
* Constructor
*
* Sets up everything, starts service DIVERA_STATUS
*/
constructor() {
super(manifest, __dirname);
this.basedata = new Basedata(global.plgManager.logger);
this.organizations = new Organizations(global.plgManager.logger);
this.alertdevices = new Alertdevices(global.plgManager.logger);
this.configuration = new Configuration(global.plgManager.logger);
this.fcmTokens = new FCMTokens(global.plgManager.logger);
}
/**
* Plugin-specific implementation
*/
newFeedback(feedbackInfo) {
global.plgManager.logger.info('Received new Feedback from REST API', {label: this.logIdentifier});
global.plgManager.event_new_feedback_received(feedbackInfo.operationUUID, feedbackInfo.basedataUUID, feedbackInfo.stateId);
}
/**
* reconfigures DIVERA_STATUS plugin with new config settings
* @param {function} callback success, true or false
* @memberof plugins/inbound/divera_status
*/
initializePlugin(callback) {
this.config = {
AUTOLOGIN_KEY: this.getConfigValue('AUTOLOGIN_KEY'),
STATUS_EB: this.getConfigValue('STATUS_EB'),
STATUS_NEB: this.getConfigValue('STATUS_NEB'),
MONITOR_ID: this.getConfigValue('MONITOR_ID'),
loglevel: 'debug'
}
if (
(typeof this.config.AUTOLOGIN_KEY !== 'undefined') &&
(typeof this.config.status_mapping_use_text !== 'undefined')
//|| (typeof this.config.httpsPort !== 'undefined') && (typeof this.config.runOnHTTPS !== 'undefined')
) {
global.plgManager.logger.info('Properly inititlized settings', {label: this.logIdentifier});
if (this.config.AUTOLOGIN_KEY != '') {
callback(true);
} else {
global.plgManager.logger.error('Could not initialize settings', {label: this.logIdentifier});
callback(false);
}
} else {
global.plgManager.logger.error('Could not initialize settings', {label: this.logIdentifier});
callback(false);
}
}
/**
* Wires messages from service to event model of plugin
* All Messages in service need to be wired here to be recognised in plugin
* @memberof plugins/inbound/divera_status
*/
wireServiceCommunication() {
this.subProcess.on('message', function (msg) {
var payload = [];
try {
payload = JSON.parse(msg);
} catch (err) {
payload = [];
}
if (typeof payload.logmessage != 'undefined') {
msg = payload.logmessage;
}
global.plgManager.logger.info(JSON.stringify(msg), {label: this.logIdentifier});
var payload = null;
try {
payload = JSON.parse(msg);
} catch (err) {
payload = err.message;
}
if (typeof payload.feedbackState != 'undefined') {
var basedataUUID = payload.feedbackState.basedataUUID;
var operationUUID = payload.feedbackState.operationUUID;
var feedbackState = payload.feedbackState.feedbackState;
global.plgManager.event_new_feedback_received(operationUUID, basedataUUID, feedbackState);
global.plgManager.logger.info('Sending new Feedback to PluginManager', {label: this.logIdentifier});
}
if (typeof payload.logmessage != 'undefined') {
global.plgManager.logger.info(payload.logmessage, { label: this.logIdentifier });
} else if (typeof payload.event != 'undefined') {
// service send structured data
if (payload.event.event === 'event_new_feedback') {
var message = payload.event.feedback;
global.plgManager.logger.info('NEW FEEDBACK', {label: this.logIdentifier});
//this.event_new_feedback_received()
} else if (payload.event.event === 'error') {
global.plgManager.logger.error('PLUGIN | RESTAPI | SERVICE | ERROR',{label: this.logIdentifier});
global.plgManager.logger.error('ERROR ' + JSON.stringify(payload.event.error), {label: this.logIdentifier});
global.plgManager.event_new_admin_notification('REST API Fehler: ' + JSON.stringify(payload.event.error));
this.executeServiceRestartRoutine();
}
} else {
global.plgManager.logger.info('UNSOLICITED MESSAGE: ' + JSON.stringify(msg), {label: this.logIdentifier});
}
}.bind(this));
}
}
module.exports = divera_status;