Send to rado platform; show_dbdata, db loads after start
This commit is contained in:
parent
ed0fe5b15d
commit
f63ac50497
22 changed files with 6380 additions and 2279 deletions
|
|
@ -20,9 +20,9 @@ key:string|weight:string|sk:string|en:string
|
|||
+|power_supply_works_correctly|NOTICE|Napájací zdroj pracuje správne|Power supply works correctly|...............
|
||||
+|battery_level_is_low|ERROR|Batéria má nízku úroveň napätia|Battery level is low|...............
|
||||
+|battery_level_is_ok|NOTICE|Batéria má správnu úroveň napätia|Battery level is OK|...............
|
||||
+|door_has_been_open|NOTICE|Dvere boli otvorené|Door has been open|...............
|
||||
+|door_has_been_closed|NOTICE|Dvere boli zatvorené|Door has been closed|...............
|
||||
+|door_has_been_open_without_permision_alarm_is_on|WARNING|Dvere boli otvorené bez povolania - zapnutá siréna|Door has been open without permision - alarm is on|...............
|
||||
+|door_opened|NOTICE|Dvere boli otvorené|Door has been opeed|...............
|
||||
+|door_closed|NOTICE|Dvere boli zatvorené|Door has been closed|...............
|
||||
+|door_opened_without_permission|WARNING|Dvere boli otvorené bez povoeania - zapnutá siréna|Door has been oed without permision - alarm is on|...............
|
||||
+|state_of_contactor_for_line|INFORMATIONAL|Stav stýkača pre líniu č. ${line} je ${value}|State of contactor for line no. ${line} is ${value}|...............
|
||||
+|local_database_is_corrupted|CRITICAL|||...............
|
||||
+|electrometer_nok|ERROR|Elektromer neodpovedá|Electrometer is not responding|...............
|
||||
|
|
@ -34,4 +34,4 @@ key:string|weight:string|sk:string|en:string
|
|||
+|twilight_sensor_ok|NOTICE|Sensor súmraku znovu odpovedá|Twilight sensor is responding again|...............
|
||||
+|lamps_have_turned_on|NOTICE|Lampy sa zapli|Lamps have turned on|...............
|
||||
+|lamps_have_turned_off|NOTICE|Lampy sa vypli|Lamps have turned off|...............
|
||||
+|flow_restart|NOTICE|Restart flowu|Flow has been restarted|...............
|
||||
+|flow_restart|NOTICE|Restart flowu|Flow has been restarted|...............
|
||||
|
|
@ -1,2 +1,2 @@
|
|||
rvo_name:string|lang:string|temperature_adress:string|latitude:number|longitude:number|mqtt_host:string|mqtt_clientid:string|mqtt_username:string|mqtt_port:number|maintanace_mode:boolean|projects_id:number|controller_type:string|serial_port:string|backup_on_failure:boolean|restore_from_backup:number|restore_backup_wait:number
|
||||
+|rvo_senica_39_10.0.0.132|en|28.427B45920702|48.70826502|17.28455203|192.168.252.1|rvo_senica_39_10.0.0.132|qzSNuCNrLP4OL1v47YEe|1883|0|68|unipi|ttyUSB0|1|20|5|...........................................
|
||||
rvo_name:string|lang:string|temperature_adress:string|latitude:number|longitude:number|mqtt_host:string|mqtt_clientid:string|mqtt_username:string|mqtt_port:number|maintanace_mode:boolean|project_id:number|controller_type:string|serial_port:string|backup_on_failure:boolean|restore_from_backup:number|restore_backup_wait:number|node_status_nok_time:number|phases:number
|
||||
+|rvo_senica_42_10.0.0.118|en|28.427B45920702|48.70826502|17.28455203|192.168.252.1|rvo_senica_42_10.0.0.118|QMvF7etEvbYMMr8Q6baP|1883|0|59|unipi|ttyUSB0|1|20|5|6|3|..............
|
||||
|
|
|
|||
0
databases/tbdatacloud.nosql
Normal file
0
databases/tbdatacloud.nosql
Normal file
404
flow/cloudmqttconnect.js
Normal file
404
flow/cloudmqttconnect.js
Normal file
|
|
@ -0,0 +1,404 @@
|
|||
exports.id = 'cloudmqttconnect';
|
||||
exports.title = 'Cloud connect mqtt';
|
||||
exports.group = 'MQTT';
|
||||
exports.color = '#888600';
|
||||
exports.version = '1.0.2';
|
||||
exports.icon = 'sign-out';
|
||||
exports.input = 1;
|
||||
exports.output = 2;
|
||||
exports.options = { host: 'tb-stage.worksys.io', port: 1883, clientid: "", username: "" };
|
||||
|
||||
exports.html = `<div class="padding">
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
<div data-jc="textbox" data-jc-path="host" data-jc-config="placeholder:test.mosquitto.org;required:false" class="m">Hostname or IP address (if not empty - setting will override db setting)</div>
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<div data-jc="textbox" data-jc-path="port" data-jc-config="placeholder:1883;required:true" class="m">Port</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
<div data-jc="textbox" data-jc-path="clientid">@(Client id)</div>
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<div data-jc="textbox" data-jc-path="topic" data-jc-config="required:true" class="m">topic</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>`;
|
||||
|
||||
|
||||
|
||||
const { promisifyBuilder } = require('./helper/db_helper');
|
||||
const fs = require('fs');
|
||||
const mqtt = require('mqtt');
|
||||
|
||||
const SEND_TO = {
|
||||
debug: 0,
|
||||
rpcCall: 1,
|
||||
}
|
||||
|
||||
//CONFIG
|
||||
let saveTelemetryOnError = true;//backup_on_failure overrides this value
|
||||
//------------------------
|
||||
|
||||
const noSqlFileSizeLimit = 4194304;//use 5MB - 4194304
|
||||
let insertNoSqlCounter = 0;
|
||||
let insertBackupNoSqlCounter = 0;
|
||||
let processingData = false;
|
||||
|
||||
let backup_on_failure = true;//== saveTelemetryOnError - create backup client send failure
|
||||
let restore_from_backup = 50; //how many rows process at once?
|
||||
let restore_backup_wait = 3;//wait seconds
|
||||
let lastRestoreTime = 0;
|
||||
|
||||
// if there is an error in client connection, flow logs to monitor.txt. Not to log messages every second, we use sendClientError variable
|
||||
let sendClientError = true;
|
||||
|
||||
|
||||
const nosql = NOSQL('tbdatacloud');
|
||||
|
||||
|
||||
exports.install = function(instance) {
|
||||
|
||||
var client;
|
||||
var opts;
|
||||
var clientReady = false;
|
||||
|
||||
let o = null; //options
|
||||
|
||||
function main()
|
||||
{
|
||||
loadSettings();
|
||||
}
|
||||
|
||||
//set opts according to db settings
|
||||
function loadSettings()
|
||||
{
|
||||
|
||||
o = instance.options;
|
||||
opts = {
|
||||
host: o.host,
|
||||
port: o.port,
|
||||
clientId: o.clientid,
|
||||
username: o.username,
|
||||
rejectUnauthorized: false,
|
||||
resubscribe: false
|
||||
};
|
||||
|
||||
|
||||
console.log("wsmqttpublich -> loadSettings from instance.options", instance.options);
|
||||
|
||||
connectToTbServer();
|
||||
}
|
||||
|
||||
function connectToTbServer()
|
||||
{
|
||||
var url = "mqtt://" + opts.host + ":" + opts.port;
|
||||
console.log("MQTT URL: ", url);
|
||||
|
||||
client = mqtt.connect(url, opts);
|
||||
|
||||
client.on('connect', function() {
|
||||
client.subscribe(`${o.topic}_backward`, (err) => {
|
||||
if (!err) {
|
||||
console.log("MQTT subscribed");
|
||||
}
|
||||
});
|
||||
instance.status("Connected", "green");
|
||||
clientReady = true;
|
||||
sendClientError = true;
|
||||
});
|
||||
|
||||
client.on('reconnect', function() {
|
||||
client.subscribe(`${o.topic}_backward`, (err) => {
|
||||
if (!err) {
|
||||
console.log("MQTT subscribed");
|
||||
}
|
||||
});
|
||||
instance.status("Reconnecting", "yellow");
|
||||
clientReady = false;
|
||||
});
|
||||
|
||||
client.on('message', function(topic, message) {
|
||||
// message is type of buffer
|
||||
message = message.toString();
|
||||
if (message[0] === '{') {
|
||||
TRY(function() {
|
||||
|
||||
message = JSON.parse(message);
|
||||
if (message.hasOwnProperty("device") && message.hasOwnProperty("data") && message.data.hasOwnProperty("id")) {
|
||||
client.publish(`${o.topic}_forward`, `{"device": "${message.device}", "id": ${message.data.id}, "data": {"success": true}}`, {qos:1});
|
||||
instance.send(SEND_TO.rpcCall, {"device": message.device, "id": message.data.id, "RPC response": {"success": true}});
|
||||
}
|
||||
|
||||
}, () => instance.debug('MQTT: Error parsing data', message));
|
||||
}
|
||||
|
||||
instance.send(SEND_TO.rpcCall, {"topic":o.topic, "content":message });
|
||||
});
|
||||
|
||||
client.on('close', function(err) {
|
||||
clientReady = false;
|
||||
|
||||
if (err && err.toString().indexOf('Error')) {
|
||||
instance.status("Err: "+err.code, "red");
|
||||
instance.send(SEND_TO.debug, {"message":"Client CLOSE signal received !", "error":err, "opt":opts });
|
||||
} else {
|
||||
instance.status("Disconnected", "red");
|
||||
instance.send(SEND_TO.debug, {"message":"Client CLOSE signal received !", "error":err, "opt":opts });
|
||||
}
|
||||
|
||||
client.reconnect();
|
||||
});
|
||||
|
||||
client.on('error', function(err) {
|
||||
instance.status("Err: "+ err.code, "red");
|
||||
instance.send(SEND_TO.debug, {"message":"Client ERROR signal received !", "error":err, "opt":opts });
|
||||
if(sendClientError) {
|
||||
console.log('MQTT client error', err);
|
||||
sendClientError = false;
|
||||
}
|
||||
clientReady = false;
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
||||
instance.on('0', function(data) {
|
||||
|
||||
if(clientReady)
|
||||
{
|
||||
//do we have some data in backup file?
|
||||
//if any, process data from database
|
||||
if(saveTelemetryOnError)
|
||||
{
|
||||
//read telemetry data and send back to server
|
||||
if(!processingData) processDataFromDatabase();
|
||||
}
|
||||
}
|
||||
|
||||
if(clientReady)
|
||||
{
|
||||
client.publish(`${o.topic}_forward`, data.data, {qos: 1});
|
||||
//console.log("ondata..",data.data)
|
||||
|
||||
|
||||
// Pokad ten error na 38 chápem tak tento parameter MUSÍ byt string...
|
||||
// Tak to musim dekódovat na strane Cloudu zas
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
//logger.debug("Client unavailable. Data not sent !", JSON.stringify(data.data));
|
||||
instance.send(SEND_TO.debug, {"message":"Client unavailable. Data not sent !", "data": data.data });
|
||||
|
||||
if(saveTelemetryOnError)
|
||||
{
|
||||
try {
|
||||
let d = JSON.parse(data.data);
|
||||
//create new file from tbdata.nosql, if file size exceeds given limit, and clear tbdata.nosql
|
||||
makeBackupFromDbFile();
|
||||
|
||||
//write to tb
|
||||
d.id = UID();
|
||||
nosql.insert(d);
|
||||
} catch(e) {
|
||||
console.log("cloudconnect - unable to parse data from wsmqtt");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
instance.close = function(done) {
|
||||
if(clientReady){
|
||||
client.end();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
function getDbBackupFileCounter(type)
|
||||
{
|
||||
var files = fs.readdirSync(__dirname + "/../databases");
|
||||
|
||||
let counter = 0;
|
||||
for(var i = 0; i < files.length; i++)
|
||||
{
|
||||
|
||||
if(files[i] == "tbdatacloud.nosql") continue;
|
||||
|
||||
if(files[i].endsWith(".nosql"))
|
||||
{
|
||||
|
||||
let pos = files[i].indexOf(".");
|
||||
if(pos > -1)
|
||||
{
|
||||
|
||||
let fileCounter = counter;
|
||||
let firstDigit = files[i].slice(0, pos);
|
||||
|
||||
fileCounter = parseInt(firstDigit);
|
||||
if(isNaN(fileCounter)) fileCounter = 0;
|
||||
//console.log("getDbBackupFileCounter digit:", files[i], firstDigit, fileCounter, isNaN(fileCounter), type);
|
||||
|
||||
if(type == "max")
|
||||
{
|
||||
if(fileCounter > counter)
|
||||
{
|
||||
counter = fileCounter;
|
||||
}
|
||||
}
|
||||
else if(type == "min")
|
||||
{
|
||||
if(counter == 0) counter = fileCounter;
|
||||
|
||||
if(fileCounter < counter)
|
||||
{
|
||||
counter = fileCounter;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if(type == "max") counter++;
|
||||
|
||||
return counter;
|
||||
}
|
||||
|
||||
const makeBackupFromDbFile = async () => {
|
||||
|
||||
if(!saveTelemetryOnError) return;
|
||||
|
||||
//to avoid large file: tbdata.nosql
|
||||
|
||||
//init value is 0!
|
||||
if(insertNoSqlCounter > 0)
|
||||
{
|
||||
--insertNoSqlCounter;
|
||||
return;
|
||||
}
|
||||
|
||||
insertNoSqlCounter = 100;
|
||||
|
||||
let source = __dirname + "/../databases/tbdatacloud.nosql";
|
||||
|
||||
var stats = fs.statSync(source);
|
||||
var fileSizeInBytes = stats.size;
|
||||
|
||||
if(fileSizeInBytes > noSqlFileSizeLimit)
|
||||
{
|
||||
|
||||
let counter = 1;
|
||||
counter = getDbBackupFileCounter("max");
|
||||
|
||||
let destination = __dirname + "/../databases/" + counter + "." + "tbdatacloud.nosql";
|
||||
|
||||
//make backup file
|
||||
fs.copyFileSync(source, destination);
|
||||
//fs.renameSync(p, p + "." + counter);
|
||||
|
||||
//clear tbdata.nosql
|
||||
fs.writeFileSync(source, "");
|
||||
fs.truncateSync(source, 0);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
const processDataFromDatabase = async () => {
|
||||
|
||||
if(restore_from_backup <= 0) return;
|
||||
|
||||
//calculate diff
|
||||
const now = new Date();
|
||||
let currentTime = now.getTime();
|
||||
let diff = currentTime - lastRestoreTime;
|
||||
|
||||
if( (diff / 1000) < restore_backup_wait)
|
||||
{
|
||||
//console.log("*********restore_backup_wait", diff, restore_backup_wait);
|
||||
return;
|
||||
}
|
||||
|
||||
processingData = true;
|
||||
|
||||
//get filename to process
|
||||
let counter = getDbBackupFileCounter("min");
|
||||
|
||||
//we have some backup files
|
||||
let dataBase = 'tbdata';
|
||||
|
||||
var nosql;
|
||||
if(counter == 0) dataBase = 'tbdatacloud';
|
||||
else dataBase = counter + "." + 'tbdatacloud';
|
||||
|
||||
nosql = NOSQL(dataBase);
|
||||
|
||||
//select all data - use limit restore_from_backup
|
||||
let records = await promisifyBuilder(nosql.find().take(restore_from_backup));
|
||||
|
||||
for(let i = 0; i < records.length; i++)
|
||||
{
|
||||
if(clientReady) {
|
||||
|
||||
let item = records[i];
|
||||
let id = item.id;
|
||||
|
||||
if(id !== undefined)
|
||||
{
|
||||
//console.log("------------processDataFromDatabase - remove", id, dataBase, i);
|
||||
|
||||
try {
|
||||
|
||||
let o = JSON.parse(JSON.stringify(item));
|
||||
delete o.id;
|
||||
let message = JSON.stringify(o);
|
||||
|
||||
client.publish(`${o.topic}_forward`, message, {qos:1});
|
||||
//console.log("db...", message)
|
||||
//remove from database
|
||||
await promisifyBuilder(nosql.remove().where("id", id));
|
||||
|
||||
} catch(error) {
|
||||
//process error
|
||||
console.log("processDataFromDatabase", error);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
processingData = false;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if(records.length > 0)
|
||||
{
|
||||
//clean backup file
|
||||
if(counter > 0) nosql.clean();
|
||||
}
|
||||
|
||||
//no data in db, remove
|
||||
if(records.length == 0)
|
||||
{
|
||||
if(counter > 0) nosql.drop();
|
||||
}
|
||||
|
||||
const d = new Date();
|
||||
lastRestoreTime = d.getTime();
|
||||
|
||||
processingData = false;
|
||||
|
||||
}
|
||||
|
||||
instance.on('options', main);
|
||||
main();
|
||||
|
||||
};
|
||||
2109
flow/cmd_manager.js
2109
flow/cmd_manager.js
File diff suppressed because it is too large
Load diff
3603
flow/cmd_manager131.js
Normal file
3603
flow/cmd_manager131.js
Normal file
File diff suppressed because it is too large
Load diff
108
flow/db_init.js
Normal file
108
flow/db_init.js
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
exports.id = 'db_init';
|
||||
exports.title = 'DB Initialization';
|
||||
exports.group = 'Worksys';
|
||||
exports.color = '#888600';
|
||||
exports.version = '1.0.2';
|
||||
exports.icon = 'sign-out';
|
||||
exports.input = 1;
|
||||
exports.output = ["blue"];
|
||||
|
||||
exports.html = `<div class="padding">
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
<div data-jc="textbox" data-jc-path="host" data-jc-config="placeholder:test.mosquitto.org;required:false" class="m">Hostname or IP address (if not empty - setting will override db setting)</div>
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<div data-jc="textbox" data-jc-path="port" data-jc-config="placeholder:1883;required:true" class="m">Port</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
<div data-jc="textbox" data-jc-path="clientid">@(Client id)</div>
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<div data-jc="textbox" data-jc-path="username" class="m">@(Username)</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>`;
|
||||
|
||||
|
||||
exports.readme = `
|
||||
# DB initialization
|
||||
`;
|
||||
|
||||
const { promisifyBuilder, makeMapFromDbResult } = require('./helper/db_helper.js');
|
||||
const { initNotification } = require('./helper/notification_reporter');
|
||||
|
||||
|
||||
exports.install = async function(instance) {
|
||||
|
||||
const dbNodes = TABLE("nodes");
|
||||
const dbRelays = TABLE("relays");
|
||||
const dbSettings = TABLE("settings");
|
||||
const dbStatus = TABLE("status");
|
||||
const dbPins = TABLE("pins");
|
||||
const dbNotifications = TABLE("notifications");
|
||||
|
||||
FLOW.GLOBALS = {};
|
||||
const dbs = FLOW.GLOBALS;
|
||||
|
||||
const responseSettings = await promisifyBuilder(dbSettings.find());
|
||||
const responseNodes = await promisifyBuilder(dbNodes.find());
|
||||
const responsePins = await promisifyBuilder(dbPins.find());
|
||||
const responseStatus = await promisifyBuilder(dbStatus.find());
|
||||
const responseRelays = await promisifyBuilder(dbRelays.find());
|
||||
const response = await promisifyBuilder(dbNotifications.find());
|
||||
|
||||
dbs.pinsData = makeMapFromDbResult(responsePins, "pin");
|
||||
dbs.relaysData = makeMapFromDbResult(responseRelays, "line");
|
||||
dbs.nodesData = makeMapFromDbResult(responseNodes, "node");
|
||||
dbs.statusData = responseStatus[0];
|
||||
dbs.notificationsData = makeMapFromDbResult(response, "key");
|
||||
|
||||
//+|354|nodesdata.....+|482|nodesdata....
|
||||
//for some reason, if last line in nodes.table is not empty, flow wrote more nodes data in one row,
|
||||
//so we have to add empty line at the bottom of nodes table to avoid this.
|
||||
//now, remove empty lines from nodesData database:
|
||||
if(dbs.nodesData.hasOwnProperty("0")) delete dbs.nodesData["0"];
|
||||
|
||||
dbs.settings = {
|
||||
edge_fw_version : "2024-11-04", //rok-mesiac-den
|
||||
language : responseSettings[0]["lang"],
|
||||
rvo_name : responseSettings[0]["rvo_name"],
|
||||
project_id : responseSettings[0]["project_id"],
|
||||
rvoTbName : dbs.relaysData[0]["tbname"],
|
||||
temperature_address : responseSettings[0]["temperature_address"],
|
||||
controller_type : responseSettings[0]["controller_type"],
|
||||
serial_port : responseSettings[0]["serial_port"],
|
||||
node_status_nok_time : responseSettings[0]["node_status_nok_time"] * 60 * 60 * 1000 ,// hour * minutes *
|
||||
latitude : responseSettings[0]["latitude"],
|
||||
longitude : responseSettings[0]["longitude"],
|
||||
no_voltage : new Set(),//modbus_citysys - elektromer
|
||||
backup_on_failure : responseSettings[0]["backup_on_failure"],
|
||||
restore_from_backup : responseSettings[0]["restore_from_backup"],
|
||||
restore_backup_wait : responseSettings[0]["restore_backup_wait"],
|
||||
mqtt_host : responseSettings[0]["mqtt_host"],
|
||||
mqtt_clientid : responseSettings[0]["mqtt_clientid"],
|
||||
mqtt_username : responseSettings[0]["mqtt_username"],
|
||||
mqtt_port : responseSettings[0]["mqtt_port"],
|
||||
phases: responseSettings[0]["phases"],
|
||||
|
||||
//dynamic values
|
||||
masterNodeIsResponding : true, //cmd_manager
|
||||
maintenance_mode : false,
|
||||
}
|
||||
|
||||
FLOW.dbLoaded = true;
|
||||
initNotification();
|
||||
|
||||
setTimeout(()=> {
|
||||
console.log("DB_INIT - data loaded");
|
||||
instance.send(0, "_")
|
||||
}, 5000)
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load diff
File diff suppressed because it is too large
Load diff
|
|
@ -49,13 +49,8 @@ class DataToTbHandler {
|
|||
for(let i = 0; i < arrayOfValues.length; i++)
|
||||
{
|
||||
ts = arrayOfValues[i].ts;
|
||||
|
||||
//console.log("sendToTb------------>before", arrayOfValues[i].values, tbname);
|
||||
|
||||
let values = this.prepareValuesForTb(tbname, ts, arrayOfValues[i].values);
|
||||
|
||||
//console.log("sendToTb------------>after", values);
|
||||
|
||||
if(!this.isEmptyObject(values))
|
||||
{
|
||||
arrayOfValuesToSend.push({ts: ts, values: values});
|
||||
|
|
@ -68,17 +63,6 @@ class DataToTbHandler {
|
|||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
let dataToTb = {
|
||||
[tbname]: [
|
||||
{
|
||||
"ts": Date.now(),
|
||||
"values": values
|
||||
}
|
||||
]
|
||||
}
|
||||
*/
|
||||
|
||||
this.messageCounter++;
|
||||
|
||||
let dataToTbModified = {
|
||||
|
|
|
|||
|
|
@ -97,7 +97,7 @@ class ErrorToServiceHandler
|
|||
console.log("ErrorToServiceHandler------------------------>send to service", dataToInfoSender);
|
||||
|
||||
//TODO UGLY!!!
|
||||
if(this.projects_id === undefined) this.projects_id = FLOW.OMS_projects_id;
|
||||
if(this.projects_id === undefined) this.projects_id = FLOW.GLOBALS.settings.project_id;
|
||||
|
||||
/*
|
||||
if(this.projects_id === undefined)
|
||||
|
|
|
|||
|
|
@ -1,72 +0,0 @@
|
|||
|
||||
//key is device, value = str
|
||||
let sentValues= {};
|
||||
|
||||
function sendError(func, device, weight, str, extra, tb_output, instance, type) {
|
||||
// if ((weight === ERRWEIGHT.DEBUG) && (instance.CONFIG.debug === false)){
|
||||
// return; // Allow debug messages only if CONFIG.debug is active
|
||||
// }
|
||||
|
||||
let key = device;
|
||||
if(type != undefined) key = type;
|
||||
|
||||
if(sentValues.hasOwnProperty(key))
|
||||
{
|
||||
if(sentValues[key] == str)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
sentValues[key] = str;
|
||||
|
||||
let content = {
|
||||
"type": weight,
|
||||
"status": "new",
|
||||
"source": {
|
||||
"func":func,
|
||||
"component":instance.id,
|
||||
"component_name":instance.name,
|
||||
"edge":device
|
||||
},
|
||||
"message":str,
|
||||
"message_data": extra
|
||||
};
|
||||
|
||||
let msg = {};
|
||||
msg[device] = [
|
||||
{
|
||||
"ts": Date.now(),
|
||||
"values": {
|
||||
"_event":content
|
||||
}
|
||||
}
|
||||
];
|
||||
|
||||
// Msg can be outputted from components only after configuration
|
||||
/*if (canSendErrData()){
|
||||
sendBufferedErrors();
|
||||
} else {
|
||||
bufferError(msg);
|
||||
}*/
|
||||
instance.send(tb_output, msg); // Even if error server is unavailable, send this message to output, for other possible component connections
|
||||
|
||||
}
|
||||
|
||||
|
||||
let ERRWEIGHT = {
|
||||
EMERGENCY: "emergency", // System unusable
|
||||
ALERT: "alert", // Action must be taken immidiately
|
||||
CRITICAL: "critical", // Component unable to function
|
||||
ERROR: "error", // Error, but component able to recover from it
|
||||
WARNING: "warning", // Possibility of error, system running futher
|
||||
NOTICE: "notice", // Significant message but not an error, things user might want to know about
|
||||
INFO: "informational", // Info
|
||||
DEBUG: "debug" // Debug - only if CONFIG.debug is enabled
|
||||
};
|
||||
|
||||
|
||||
module.exports = {
|
||||
sendError,
|
||||
ERRWEIGHT
|
||||
}
|
||||
|
|
@ -1,56 +0,0 @@
|
|||
const ERRWEIGHT = {
|
||||
EMERGENCY: "emergency", // System unusable
|
||||
ALERT: "alert", // Action must be taken immidiately
|
||||
CRITICAL: "critical", // Component unable to function
|
||||
ERROR: "error", // Error, but component able to recover from it
|
||||
WARNING: "warning", // Possibility of error, system running futher
|
||||
NOTICE: "notice", // Significant message but not an error, things user might want to know about
|
||||
INFO: "informational", // Info
|
||||
DEBUG: "debug" // Debug - only if CONFIG.debug is enabled
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
function sendError(func, device, weight, str, extra){
|
||||
if ((weight === ERRWEIGHT.DEBUG) && (instance.CONFIG.debug === false)){
|
||||
return; // Allow debug messages only if CONFIG.debug is active
|
||||
}
|
||||
|
||||
let content = {
|
||||
"type": weight,
|
||||
"status": "new",
|
||||
"source": {
|
||||
"func":func,
|
||||
"component":instance.id,
|
||||
"component_name":instance.name,
|
||||
"edge":myEdge
|
||||
},
|
||||
"message":str,
|
||||
"message_data": extra
|
||||
};
|
||||
let msg = {};
|
||||
msg[device] = [
|
||||
{
|
||||
"ts": Date.now(),
|
||||
"values": {
|
||||
"_event":content
|
||||
}
|
||||
}
|
||||
];
|
||||
|
||||
// Msg can be outputted from components only after configuration
|
||||
/*if (canSendErrData()){
|
||||
sendBufferedErrors();
|
||||
} else {
|
||||
bufferError(msg);
|
||||
}*/
|
||||
instance.send(0, msg); // Even if error server is unavailable, send this message to output, for other possible component connections
|
||||
|
||||
}
|
||||
|
||||
|
||||
module.exports = {
|
||||
sendError,
|
||||
ERRWEIGHT,
|
||||
}
|
||||
30
flow/helper/logger.js
Normal file
30
flow/helper/logger.js
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
//https://github.com/log4js-node/log4js-node/blob/master/examples/example.js
|
||||
//file: { type: 'file', filename: path.join(__dirname, 'log/file.log') }
|
||||
|
||||
var log4js = require("log4js");
|
||||
var path = require('path');
|
||||
|
||||
log4js.configure({
|
||||
appenders: {
|
||||
errLogs: { type: 'file', compress:true, daysToKeep: 2, maxLogSize: 1048576, backups: 1, keepFileExt: true, filename: path.join(__dirname + "/../../", 'err.txt') },
|
||||
monitorLogs: { type: 'file', compress:true, daysToKeep: 2, maxLogSize: 1048576, backups: 1, keepFileExt: true, filename: path.join(__dirname + "/../../", 'monitor.txt') },
|
||||
console: { type: 'console' }
|
||||
},
|
||||
categories: {
|
||||
errLogs: { appenders: ['console', 'errLogs'], level: 'error' },
|
||||
monitorLogs: { appenders: ['console', 'monitorLogs'], level: 'trace' },
|
||||
//another: { appenders: ['console'], level: 'trace' },
|
||||
default: { appenders: ['console'], level: 'trace' }
|
||||
}
|
||||
});
|
||||
|
||||
const errLogger = log4js.getLogger("errLogs");
|
||||
const logger = log4js.getLogger();
|
||||
const monitor = log4js.getLogger("monitorLogs");
|
||||
|
||||
//USAGE
|
||||
//logger.debug("text")
|
||||
//monitor.info('info');
|
||||
//errLogger.error("some error");
|
||||
|
||||
module.exports = { errLogger, logger, monitor };
|
||||
|
|
@ -1,10 +1,6 @@
|
|||
|
||||
const { promisifyBuilder, makeMapFromDbResult } = require('./db_helper.js');
|
||||
const dbNotifications = TABLE("notifications");
|
||||
|
||||
//key is device, value = str
|
||||
let sentValues= {};
|
||||
let notificationsData = {};
|
||||
let notificationsData = null;
|
||||
|
||||
let ERRWEIGHT = {
|
||||
EMERGENCY: "emergency", // System unusable
|
||||
|
|
@ -24,12 +20,9 @@ function getKey(map, val) {
|
|||
//https://stackoverflow.com/questions/41117799/string-interpolation-on-variable
|
||||
var template = (tpl, args) => tpl.replace(/\${(\w+)}/g, (_, v) => args[v]);
|
||||
|
||||
async function initNotifications()
|
||||
{
|
||||
let response = await promisifyBuilder(dbNotifications.find());
|
||||
notificationsData = makeMapFromDbResult(response, "key");
|
||||
|
||||
console.log("initNotifications done" );
|
||||
function initNotification() {
|
||||
notificationsData = FLOW.GLOBALS.notificationsData;
|
||||
}
|
||||
|
||||
function sendNotification(func, device, key, params, extra, tb_output, instance, saveKey) {
|
||||
|
|
@ -39,7 +32,7 @@ function sendNotification(func, device, key, params, extra, tb_output, instance,
|
|||
let storeToSendValues = true;
|
||||
if(saveKey == undefined) storeToSendValues = false;
|
||||
|
||||
let lang = FLOW.OMS_language;
|
||||
let lang = FLOW.GLOBALS.settings.language;
|
||||
if(lang != "en" || lang != "sk") lang = "en";
|
||||
|
||||
let tpl = key;
|
||||
|
|
@ -55,7 +48,8 @@ function sendNotification(func, device, key, params, extra, tb_output, instance,
|
|||
}
|
||||
else
|
||||
{
|
||||
console.error("sendNotification: Notifications: undefined key", key, func, notificationsData);
|
||||
//console.error("sendNotification: Notifications: undefined key", key, func, notificationsData);
|
||||
console.error("sendNotification: Notifications: undefined key", key, func );
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
@ -91,7 +85,7 @@ function sendNotification(func, device, key, params, extra, tb_output, instance,
|
|||
|
||||
if(storeToSendValues) sentValues[saveKey] = tpl;
|
||||
|
||||
let str = FLOW.OMS_rvo_name;
|
||||
let str = FLOW.GLOBALS.settings.rvo_name;
|
||||
if(str != "") str = str + ": ";
|
||||
str = str + tpl;
|
||||
|
||||
|
|
@ -132,6 +126,6 @@ function sendNotification(func, device, key, params, extra, tb_output, instance,
|
|||
|
||||
module.exports = {
|
||||
sendNotification,
|
||||
initNotifications,
|
||||
ERRWEIGHT
|
||||
}
|
||||
ERRWEIGHT,
|
||||
initNotification
|
||||
}
|
||||
|
|
|
|||
|
|
@ -40,18 +40,17 @@ async function writeData(port, data, readbytes, timeout){
|
|||
return new Promise((resolve, reject) => {
|
||||
|
||||
// If first item in data array is 255, we just write broadcast command to rsPort
|
||||
// We wait 3 seconds and resolve([ "b", "r", "o", "a", "d", "c", "a", "s", "t" ])
|
||||
// We wait 3 seconds and resolve(["broadcast"])
|
||||
// It is important to resolve with array
|
||||
if(data[0] == 255) {
|
||||
|
||||
port.write(Buffer.from(data), function(err) {
|
||||
if (err) {
|
||||
port.removeListener('data', callback);
|
||||
reject(err.message);
|
||||
}
|
||||
});
|
||||
|
||||
setTimeout(resolve, 3000, [ "b", "r", "o", "a", "d", "c", "a", "s", "t" ]);
|
||||
setTimeout(resolve, 3000, ["broadcast"]);
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -3,12 +3,9 @@ exports.title = 'Info sender';
|
|||
exports.version = '1.0.0';
|
||||
exports.group = 'Worksys';
|
||||
exports.color = '#2134B0';
|
||||
exports.input = 1;
|
||||
exports.input = 2;
|
||||
exports.output = 1
|
||||
exports.click = false;
|
||||
exports.author = 'oms-is';
|
||||
exports.icon = 'bolt';
|
||||
exports.options = { edge: "undefined" };
|
||||
|
||||
const { networkInterfaces } = require('os');
|
||||
|
||||
|
|
@ -22,13 +19,12 @@ exports.html = `<div class="padding">
|
|||
|
||||
exports.readme = `# send all data to projects.worksys.io, required to monitor status of controller(unipi)`;
|
||||
|
||||
const fs = require('fs');
|
||||
var path = require('path');
|
||||
exports.install = function(instance) {
|
||||
|
||||
exports.install = async function(instance) {
|
||||
|
||||
let id;
|
||||
let allValues = {};
|
||||
let sendAllValuesInterval;
|
||||
let configured = false;
|
||||
|
||||
let now = new Date();
|
||||
console.log(exports.title, "INSTALLED", now.toLocaleString("sk-SK"));
|
||||
|
|
@ -47,23 +43,19 @@ exports.install = async function(instance) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function sendValues()
|
||||
{
|
||||
const id = FLOW.OMS_projects_id;
|
||||
if(!configured) return;
|
||||
|
||||
if(Object.keys(allValues).length > 0)
|
||||
{
|
||||
if(id !== undefined)
|
||||
if(id)
|
||||
{
|
||||
delete allValues.__force__;
|
||||
let dataToSend = {...allValues};
|
||||
dataToSend.id = id;
|
||||
dataToSend.ipAddresses = ipAddresses;
|
||||
//dataToSend.notify_date = new Date().toISOString().slice(0, 19).replace('T', ' ');
|
||||
|
||||
//console.log(exports.title, "------------>sendValues", dataToSend);
|
||||
|
||||
instance.send(0, dataToSend);
|
||||
|
||||
|
|
@ -71,7 +63,7 @@ exports.install = async function(instance) {
|
|||
}
|
||||
else
|
||||
{
|
||||
console.log(exports.title, "unable to send data, id is undefined");
|
||||
console.log(exports.title, "unable to send data, no id");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -81,10 +73,14 @@ exports.install = async function(instance) {
|
|||
clearInterval(sendAllValuesInterval);
|
||||
})
|
||||
|
||||
instance.on("data", (flowdata) => {
|
||||
instance.on("0", _ => {
|
||||
id = FLOW.GLOBALS.settings.project_id;
|
||||
configured = true;
|
||||
})
|
||||
|
||||
instance.on("1", flowdata => {
|
||||
|
||||
allValues = { ...allValues, ...flowdata.data};
|
||||
|
||||
//console.log("DATA RECEIVED", flowdata.data);
|
||||
|
||||
//__force__
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ exports.title = 'Modbus reader';
|
|||
exports.version = '2.0.0';
|
||||
exports.group = 'Worksys';
|
||||
exports.color = '#2134B0';
|
||||
exports.input = 1;
|
||||
exports.output = ["red", "white", "yellow"];
|
||||
exports.click = false;
|
||||
exports.author = 'Rastislav Kovac';
|
||||
|
|
@ -31,7 +32,10 @@ const SEND_TO = {
|
|||
const numberOfNotResponding = {};
|
||||
let tbName = null;
|
||||
let mainSocket;
|
||||
|
||||
//number of phases inRVO
|
||||
let phases;
|
||||
//phases where voltage is 0 (set)
|
||||
let noVoltage;
|
||||
|
||||
exports.install = function(instance) {
|
||||
|
||||
|
|
@ -55,10 +59,19 @@ exports.install = function(instance) {
|
|||
// lampSwitchNotification helper variables
|
||||
this.onNotificationSent = false;
|
||||
this.offNotificationSent = false;
|
||||
this.phases = this.buildPhases();
|
||||
|
||||
this.startSocket();
|
||||
}
|
||||
|
||||
buildPhases = () => {
|
||||
let a = [];
|
||||
for (let i = 1; i<= phases; i++) {
|
||||
a.push(`Phase_${i}_voltage`)
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
startSocket = () => {
|
||||
|
||||
let obj = this;
|
||||
|
|
@ -228,9 +241,7 @@ exports.install = function(instance) {
|
|||
this.allValues = {};
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
setNewStream = () =>
|
||||
|
|
@ -282,7 +293,7 @@ exports.install = function(instance) {
|
|||
if(!(values.hasOwnProperty("Phase_1_voltage") || values.hasOwnProperty("Phase_2_voltage") || values.hasOwnProperty("Phase_3_voltage"))) return;
|
||||
|
||||
Object.keys(values).map(singleValue => {
|
||||
if (["Phase_1_voltage", "Phase_2_voltage", "Phase_3_voltage"].includes(singleValue))
|
||||
if (this.phases.includes(singleValue))
|
||||
{
|
||||
let l = singleValue.split("_");
|
||||
let phase = parseInt(l[1]);
|
||||
|
|
@ -291,13 +302,13 @@ exports.install = function(instance) {
|
|||
|
||||
if(values[singleValue] == 0)
|
||||
{
|
||||
FLOW.OMS_no_voltage.add(phase);
|
||||
noVoltage.add(phase);
|
||||
sendNotification("modbus_reader: checkNullVoltage", tbName, "no_voltage_on_phase", {phase: phase}, "", SEND_TO.tb, instance, "voltage" + phase );
|
||||
// console.log('no voltage')
|
||||
}
|
||||
else
|
||||
{
|
||||
FLOW.OMS_no_voltage.delete(phase);
|
||||
noVoltage.delete(phase);
|
||||
// console.log('voltage detected')
|
||||
sendNotification("modbus_reader: checkNullVoltage", tbName, "voltage_on_phase_restored", {phase: phase}, "", SEND_TO.tb, instance, "voltage" + phase);
|
||||
}
|
||||
|
|
@ -330,18 +341,23 @@ exports.install = function(instance) {
|
|||
}
|
||||
|
||||
const isObjectEmpty = (objectName) => {
|
||||
return Object.keys(objectName).length === 0 && objectName.constructor === Object;
|
||||
return Object.keys(objectName).length === 0 && objectName.constructor === Object;
|
||||
}
|
||||
|
||||
setTimeout(() => {
|
||||
|
||||
function main() {
|
||||
|
||||
phases = FLOW.GLOBALS.settings.phases;
|
||||
tbName = FLOW.GLOBALS.settings.rvoTbName;
|
||||
noVoltage = FLOW.GLOBALS.settings.no_voltage;
|
||||
mainSocket = new SocketWithClients();
|
||||
tbName = FLOW.OMS_rvo_tbname;
|
||||
|
||||
// this notification is to show, that flow (unipi) has been restarted
|
||||
sendNotification("modbus_reader", tbName, "flow_restart", {}, "", SEND_TO.slack, instance);
|
||||
}
|
||||
|
||||
}, 25000);
|
||||
instance.on("0", function(_) {
|
||||
main();
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
241
flow/show_dbdata.js
Normal file
241
flow/show_dbdata.js
Normal file
|
|
@ -0,0 +1,241 @@
|
|||
exports.id = 'showdb';
|
||||
exports.title = 'Show db data';
|
||||
exports.group = 'Worksys';
|
||||
exports.color = '#888600';
|
||||
exports.version = '1.0.2';
|
||||
exports.icon = 'sign-out';
|
||||
exports.input = 7;
|
||||
exports.output = 1;
|
||||
|
||||
const { exec } = require('child_process');
|
||||
|
||||
exports.install = function(instance) {
|
||||
|
||||
instance.on("0", _ => {
|
||||
instance.send(0, FLOW.GLOBALS.settings);
|
||||
})
|
||||
instance.on("1", _ => {
|
||||
instance.send(0, FLOW.GLOBALS.relaysData);
|
||||
})
|
||||
instance.on("2", _ => {
|
||||
instance.send(0, FLOW.GLOBALS.nodesData);
|
||||
})
|
||||
instance.on("3", _ => {
|
||||
instance.send(0, FLOW.GLOBALS.pinsData);
|
||||
})
|
||||
instance.on("4", _ => {
|
||||
instance.send(0, {rpcSwitchOffLine, rpcSetNodeDimming, rpcLineProfile, rpcNodeProfile, sunCalcExample, dataFromTerminalBroadcast})
|
||||
})
|
||||
instance.on("5", _ => {
|
||||
exec("sudo tail -n 25 monitor.txt" , (err, stdout, stderr) => {
|
||||
if (err || stderr) instance.send(0,{err, stderr});
|
||||
else instance.send(0,stdout);
|
||||
})
|
||||
})
|
||||
instance.on("6", _ => {
|
||||
exec("sudo tail -n 25 err.txt" , (err, stdout, stderr) => {
|
||||
if (err || stderr) instance.send(0,{err, stderr});
|
||||
else instance.send(0,stdout);
|
||||
})
|
||||
})
|
||||
};
|
||||
|
||||
|
||||
|
||||
const rpcSwitchOffLine =
|
||||
{
|
||||
"topic": "v1/gateway/rpc",
|
||||
"content": {
|
||||
"device": "jbN4q7JPZmexgdnz2yKbWGDYAWwO0Q3BMX6ERLoV",
|
||||
"data": {
|
||||
"id": 8,
|
||||
"method": "set_command",
|
||||
"params": {
|
||||
"entities": [
|
||||
{
|
||||
"entity_type": "edb_line",
|
||||
"tb_name": "MgnK93rkoAazbqdQ4yB2Q0yZ1YXGx6pmwBeVEP2O"
|
||||
}
|
||||
],
|
||||
"command": "switch",
|
||||
"payload": {
|
||||
"value": false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const rpcSetNodeDimming =
|
||||
{
|
||||
"topic": "v1/gateway/rpc",
|
||||
"content": {
|
||||
"device": "jbN4q7JPZmexgdnz2yKbWGDYAWwO0Q3BMX6ERLoV",
|
||||
"data": {
|
||||
"id": 10,
|
||||
"method": "set_command",
|
||||
"params": {
|
||||
"entities": [
|
||||
{
|
||||
"entity_type": "street_luminaire",
|
||||
"tb_name": "jbN4q7JPZmexgdnz2yKbWdDYAWwO0Q3BMX6ERLoV"
|
||||
}
|
||||
],
|
||||
"command": "dimming",
|
||||
"payload": {
|
||||
"value": 5
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const rpcLineProfile =
|
||||
{
|
||||
"topic": "v1/gateway/rpc",
|
||||
"content": {
|
||||
"device": "jbN4q7JPZmexgdnz2yKbWGDYAWwO0Q3BMX6ERLoV",
|
||||
"data": {
|
||||
"id": 9,
|
||||
"method": "set_profile",
|
||||
"params": {
|
||||
"entities": [
|
||||
{
|
||||
"entity_type": "edb_line",
|
||||
"tb_name": "MgnK93rkoAazbqdQ4yB2Q0yZ1YXGx6pmwBeVEP2O"
|
||||
}
|
||||
],
|
||||
"payload": {
|
||||
"intervals": [
|
||||
{
|
||||
"value": 0,
|
||||
"end_time": "20:00",
|
||||
"start_time": "13:00"
|
||||
},
|
||||
{
|
||||
"value": 1,
|
||||
"end_time": "05:30",
|
||||
"start_time": "20:00"
|
||||
},
|
||||
{
|
||||
"value": 0,
|
||||
"end_time": "13:00",
|
||||
"start_time": "05:30"
|
||||
}
|
||||
],
|
||||
"astro_clock": true,
|
||||
"dawn_lux_sensor": false,
|
||||
"dusk_lux_sensor": false,
|
||||
"dawn_lux_sensor_value": 5,
|
||||
"dusk_lux_sensor_value": 5,
|
||||
"dawn_astro_clock_offset": 0,
|
||||
"dusk_astro_clock_offset": 0,
|
||||
"dawn_lux_sensor_time_window": 30,
|
||||
"dusk_lux_sensor_time_window": 30,
|
||||
"dawn_astro_clock_time_window": 60,
|
||||
"dusk_astro_clock_time_window": 60
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const rpcNodeProfile =
|
||||
{
|
||||
"topic": "v1/gateway/rpc",
|
||||
"content": {
|
||||
"device": "jbN4q7JPZmexgdnz2yKbWGDYAWwO0Q3BMX6ERLoV",
|
||||
"data": {
|
||||
"id": 11,
|
||||
"method": "set_profile",
|
||||
"params": {
|
||||
"entities": [
|
||||
{
|
||||
"entity_type": "street_luminaire",
|
||||
"tb_name": "jbN4q7JPZmexgdnz2yKbWdDYAWwO0Q3BMX6ERLoV"
|
||||
}
|
||||
],
|
||||
"payload": {
|
||||
"intervals": [
|
||||
{
|
||||
"cct": 3000,
|
||||
"value": 0,
|
||||
"end_time": "17:50",
|
||||
"start_time": "13:00"
|
||||
},
|
||||
{
|
||||
"cct": 3000,
|
||||
"value": 100,
|
||||
"end_time": "21:30",
|
||||
"start_time": "17:50"
|
||||
},
|
||||
{
|
||||
"cct": 3000,
|
||||
"value": 0,
|
||||
"end_time": "13:00",
|
||||
"start_time": "07:10"
|
||||
},
|
||||
{
|
||||
"cct": 3000,
|
||||
"value": 50,
|
||||
"end_time": "00:00",
|
||||
"start_time": "21:30"
|
||||
},
|
||||
{
|
||||
"cct": 3000,
|
||||
"value": 10,
|
||||
"end_time": "04:30",
|
||||
"start_time": "00:00"
|
||||
},
|
||||
{
|
||||
"cct": 3000,
|
||||
"value": 100,
|
||||
"end_time": "07:10",
|
||||
"start_time": "04:30"
|
||||
}
|
||||
],
|
||||
"astro_clock": true,
|
||||
"dawn_lux_sensor": false,
|
||||
"dusk_lux_sensor": false,
|
||||
"dawn_lux_sensor_value": 5,
|
||||
"dusk_lux_sensor_value": 5,
|
||||
"dawn_astro_clock_offset": 30,
|
||||
"dusk_astro_clock_offset": 20,
|
||||
"dawn_lux_sensor_time_window": 30,
|
||||
"dusk_lux_sensor_time_window": 30,
|
||||
"dawn_astro_clock_time_window": 60,
|
||||
"dusk_astro_clock_time_window": 60
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const sunCalcExample = {
|
||||
dusk_no_offset: '20:18',
|
||||
dawn_no_offset: '05:19',
|
||||
dusk: '20:18',
|
||||
dusk_hours: 20,
|
||||
dusk_minutes: 18,
|
||||
dawn: '05:19',
|
||||
dawn_hours: 5,
|
||||
dawn_minutes: 19,
|
||||
dusk_time: 1715278688962,
|
||||
dawn_time: 1715224744357,
|
||||
dusk_astro_clock_offset: 0,
|
||||
dawn_astro_clock_offset: 0
|
||||
}
|
||||
|
||||
|
||||
const dataFromTerminalBroadcast = {
|
||||
address: 4294967295,
|
||||
byte1: 0,
|
||||
byte2: 0,
|
||||
byte3: 0,
|
||||
byte4: 96,
|
||||
name: "Time Schedule settings",
|
||||
recipient: 2,
|
||||
register: 8,
|
||||
rw: 1
|
||||
}
|
||||
|
|
@ -11,9 +11,6 @@ exports.options = { 'name':'', 'types': '["emergency", "critical", "error", "ale
|
|||
|
||||
exports.html = `<div class="padding">
|
||||
<div class="row">
|
||||
<div class="col-md-12">
|
||||
<div data-jc="textbox" data-jc-path="name" data-jc-config="required:true">@(Name of this server)</div>
|
||||
</div>
|
||||
<div class="col-md-12">
|
||||
<div data-jc="textbox" data-jc-path="slack_channel" data-jc-config="required:false">@(Slack channel to receive the alerts)</div>
|
||||
</div>
|
||||
|
|
@ -170,11 +167,12 @@ exports.install = function(instance) {
|
|||
FLOW["savedSlackMessages"] = [];
|
||||
}
|
||||
|
||||
instance.options.name = FLOW.GLOBALS.settings.rvo_name;
|
||||
if (instance.options.name) {
|
||||
instance.status('Running');
|
||||
running = true;
|
||||
} else {
|
||||
instance.status('Please enter name', 'red');
|
||||
instance.status('Please run options again', 'red');
|
||||
running = false;
|
||||
}
|
||||
} catch (e) {
|
||||
|
|
@ -183,5 +181,7 @@ exports.install = function(instance) {
|
|||
};
|
||||
|
||||
instance.on('options', instance.reconfigure);
|
||||
instance.reconfigure();
|
||||
};
|
||||
setTimeout(instance.reconfigure, 10000);
|
||||
|
||||
|
||||
};
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ exports.id = 'thermometer';
|
|||
exports.title = 'Thermometer';
|
||||
exports.group = 'Worksys';
|
||||
exports.color = '#5CB36D';
|
||||
exports.input = 1;
|
||||
exports.version = '1.0.3';
|
||||
exports.output = ["red", "white", "blue"];
|
||||
exports.author = 'Rastislav Kovac';
|
||||
|
|
@ -9,7 +10,9 @@ exports.icon = 'thermometer-three-quarters';
|
|||
|
||||
exports.readme = `# Getting temperature values for RVO. In case of LM, you need device address. In case of unipi, evok sends values, in case thermometer is installed`;
|
||||
|
||||
const instanceSendTo = {
|
||||
const { errLogger, logger, monitor } = require('./helper/logger');
|
||||
|
||||
const SEND_TO = {
|
||||
debug: 0,
|
||||
tb: 1,
|
||||
dido_controller: 2
|
||||
|
|
@ -18,56 +21,16 @@ const instanceSendTo = {
|
|||
//read temperature - frequency
|
||||
let timeoutMin = 5;//minutes
|
||||
|
||||
var path = require('path');
|
||||
var log4js = require("log4js");
|
||||
|
||||
log4js.configure({
|
||||
appenders: {
|
||||
errLogs: { type: 'file', filename: path.join(__dirname + "/../", 'err.txt') },
|
||||
monitorLogs: { type: 'file', compress:true, daysToKeep: 2, maxLogSize: 1048576, backups: 1, keepFileExt: true, filename: path.join(__dirname + "/../", 'monitor.txt') },
|
||||
console: { type: 'console' }
|
||||
},
|
||||
categories: {
|
||||
errLogs: { appenders: ['console', 'errLogs'], level: 'error' },
|
||||
monitorLogs: { appenders: ['console', 'monitorLogs'], level: 'trace' },
|
||||
//another: { appenders: ['console'], level: 'trace' },
|
||||
default: { appenders: ['console'], level: 'trace' }
|
||||
}
|
||||
});
|
||||
|
||||
const errLogger = log4js.getLogger("errLogs");
|
||||
const logger = log4js.getLogger();
|
||||
const monitor = log4js.getLogger("monitorLogs");
|
||||
|
||||
//logger.debug("text")
|
||||
//monitor.info('info');
|
||||
//errLogger.error("some error");
|
||||
|
||||
const { promisifyBuilder, makeMapFromDbResult } = require('./helper/db_helper');
|
||||
const dbSettings = TABLE("settings");
|
||||
let temperatureAddress = "";
|
||||
|
||||
async function loadSettings()
|
||||
{
|
||||
//todo global FLOW.OMS_edgeName is making problem, so we load it here as well, it should not be
|
||||
let responseSettings = await promisifyBuilder(dbSettings.find());
|
||||
temperatureAddress = responseSettings[0]["temperature_adress"];
|
||||
}
|
||||
|
||||
loadSettings();
|
||||
|
||||
|
||||
exports.install = function(instance) {
|
||||
|
||||
const { exec } = require('child_process');
|
||||
const { sendNotification, ERRWEIGHT } = require('./helper/notification_reporter');
|
||||
const { sendNotification } = require('./helper/notification_reporter');
|
||||
|
||||
let startRead;
|
||||
let dataToTb;
|
||||
let counter = 0;
|
||||
|
||||
let edgeName = "";
|
||||
|
||||
let rvoTbName = "";
|
||||
let temperatureAddress = "";
|
||||
|
||||
logger.debug(exports.title, "installed");
|
||||
|
||||
|
|
@ -76,11 +39,11 @@ exports.install = function(instance) {
|
|||
})
|
||||
|
||||
|
||||
const start = function() {
|
||||
const main = function() {
|
||||
|
||||
try {
|
||||
|
||||
if(FLOW.OMS_controller_type === "unipi")
|
||||
if(FLOW.GLOBALS.settings.controller_type === "unipi")
|
||||
{
|
||||
clearInterval(startRead);
|
||||
return;
|
||||
|
|
@ -88,71 +51,23 @@ exports.install = function(instance) {
|
|||
|
||||
if(temperatureAddress === "") throw "gettemperature: temperatureAddress is not defined";
|
||||
|
||||
logger.debug("FLOW.OMS_temperature_adress", FLOW.OMS_temperature_adress);
|
||||
|
||||
exec(`owread -C ${temperatureAddress}/temperature`, (error, stdout, stderr) => {
|
||||
|
||||
edgeName = FLOW.OMS_edgeName;
|
||||
|
||||
if(edgeName !== "")
|
||||
if(!error)
|
||||
{
|
||||
if(error)
|
||||
{
|
||||
|
||||
if(FLOW.OMS_brokerready == undefined)
|
||||
{
|
||||
logger.debug("gettemparature - FLOW.OMS_brokerready is undefined");
|
||||
|
||||
setTimeout(function(){
|
||||
start();
|
||||
}, 3000);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if(FLOW.OMS_brokerready)
|
||||
{
|
||||
//sendNotification("start", edgeName, ERRWEIGHT.WARNING, "Thermometer is not responding", {"Error": error}, instanceSendTo.tb, instance, "thermometer");
|
||||
sendNotification("start", edgeName, "thermometer_is_not_responding", {}, {"Error": error}, instanceSendTo.tb, instance, "thermometer");
|
||||
}
|
||||
|
||||
let status = "NOK";
|
||||
dataToTb = {
|
||||
[edgeName]: [
|
||||
{
|
||||
"ts": Date.now(),
|
||||
"values": {
|
||||
"status": status
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
monitor.info("Thermometer is not responding", error, FLOW.OMS_brokerready);
|
||||
|
||||
// instance.send(instanceSendTo.tb, dataToTb); // poslat stav nok do tb, ak to handluje dido_controller ??
|
||||
instance.send(instanceSendTo.dido_controller, {status: "NOK-thermometer"});
|
||||
}
|
||||
else parseData(stdout);
|
||||
}
|
||||
else
|
||||
{
|
||||
monitor.info("gettemperature: edgeName is not defined", FLOW.OMS_edgeName);
|
||||
|
||||
setTimeout(function(){
|
||||
start();
|
||||
}, 3000);
|
||||
|
||||
parseData(stdout)
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
//instance.send({"Temp":stdout,"stderr":stderr,"err":error});
|
||||
sendNotification("main", rvoTbName, "thermometer_is_not_responding", {}, {"Error": error}, SEND_TO.tb, instance, "thermometer");
|
||||
monitor.info("Thermometer is not responding", error);
|
||||
instance.send(SEND_TO.dido_controller, {status: "NOK-thermometer"});
|
||||
});
|
||||
|
||||
}
|
||||
catch(err) {
|
||||
errLogger.error(exports.title, err);
|
||||
clearInterval(startRead);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -166,30 +81,17 @@ exports.install = function(instance) {
|
|||
|
||||
if(counter > 290)
|
||||
{
|
||||
instance.send(instanceSendTo.debug, "[Get temperature component] - temperature data are comming again from RVO after more than 1 day break");
|
||||
|
||||
//sendNotification("parseData", edgeName, ERRWEIGHT.NOTICE, "Thermometer is working again", "", instanceSendTo.tb, instance, "thermometer");
|
||||
if(FLOW.OMS_brokerready) sendNotification("parseData", edgeName, "thermometer_is_responding_again", {}, "", instanceSendTo.tb, instance, "thermometer");
|
||||
instance.send(SEND_TO.debug, "[Get temperature component] - temperature data are comming again from RVO after more than 1 day break");
|
||||
sendNotification("parseData", rvoTbName, "thermometer_is_responding_again", {}, "", SEND_TO.tb, instance, "thermometer");
|
||||
}
|
||||
|
||||
logger.debug("gettemperature", data);
|
||||
|
||||
const values = {
|
||||
"temperature": Number(data.toFixed(2)),
|
||||
"status": "OK"
|
||||
}
|
||||
|
||||
dataToTb = {
|
||||
[edgeName]: [
|
||||
{
|
||||
"ts": Date.now(),
|
||||
"values":values
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
instance.send(instanceSendTo.tb, dataToTb);
|
||||
instance.send(instanceSendTo.dido_controller, values);
|
||||
|
||||
instance.send(SEND_TO.dido_controller, {values: values});
|
||||
counter = 0;
|
||||
|
||||
} else {
|
||||
|
|
@ -200,24 +102,21 @@ exports.install = function(instance) {
|
|||
//ked je problem 1 den
|
||||
let day = 24 * 60 / timeoutMin;
|
||||
if ( counter > day && counter < day + 2 ) {
|
||||
//sendNotification("parseData", edgeName, ERRWEIGHT.WARNING, "Thermometer receives invalid data", "", instanceSendTo.tb, instance, "thermometer");
|
||||
sendNotification("parseData", edgeName, "thermometer_sends_invalid_data", {}, "", instanceSendTo.tb, instance, "thermometer");
|
||||
//sendNotification("parseData", rvoTbName, ERRWEIGHT.WARNING, "Thermometer receives invalid data", "", SEND_TO.tb, instance, "thermometer");
|
||||
sendNotification("parseData", rvoTbName, "thermometer_sends_invalid_data", {}, "", SEND_TO.tb, instance, "thermometer");
|
||||
|
||||
instance.send(instanceSendTo.debug, "[Get temperature component] - no temperature data from RVO for more than 1 day");
|
||||
instance.send(instanceSendTo.dido_controller, {status: "NOK-thermometer"});
|
||||
instance.send(SEND_TO.debug, "[Get temperature component] - no temperature data from RVO for more than 1 day");
|
||||
instance.send(SEND_TO.dido_controller, {status: "NOK-thermometer"});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
setTimeout(function(){
|
||||
start();
|
||||
}, 10000);
|
||||
|
||||
startRead = setInterval(start, timeoutMin * 1000 * 60);
|
||||
|
||||
//testing
|
||||
//setInterval(() => {instance.send(instanceSendTo.dido_controller, {status: "NOK-thermometer"})}, 180000);
|
||||
|
||||
instance.on("data", _ => {
|
||||
temperatureAddress = FLOW.GLOBALS.settings.temperature_address;
|
||||
rvoTbName = FLOW.GLOBALS.settings.rvoTbName;
|
||||
startRead = setInterval(main, timeoutMin * 1000 * 60);
|
||||
main();
|
||||
})
|
||||
};
|
||||
|
|
@ -4,30 +4,27 @@ exports.group = 'MQTT';
|
|||
exports.color = '#888600';
|
||||
exports.version = '1.0.2';
|
||||
exports.icon = 'sign-out';
|
||||
exports.input = 1;
|
||||
exports.output = ["red", "white", "blue"];
|
||||
exports.author = 'Daniel Segeš';
|
||||
exports.input = 2;
|
||||
exports.output = 4;
|
||||
exports.options = { host: 'tb-stage.worksys.io', port: 1883, clientid: "", username: "" };
|
||||
exports.npm = ['mqtt'];
|
||||
|
||||
|
||||
exports.html = `<div class="padding">
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
<div data-jc="textbox" data-jc-path="host" data-jc-config="placeholder:test.mosquitto.org;required:false" class="m">Hostname or IP address (if not empty - setting will override db setting)</div>
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<div data-jc="textbox" data-jc-path="port" data-jc-config="placeholder:1883;required:true" class="m">Port</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
<div data-jc="textbox" data-jc-path="clientid">@(Client id)</div>
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<div data-jc="textbox" data-jc-path="username" class="m">@(Username)</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
<div data-jc="textbox" data-jc-path="host" data-jc-config="placeholder:test.mosquitto.org;required:false" class="m">Hostname or IP address (if not empty - setting will override db setting)</div>
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<div data-jc="textbox" data-jc-path="port" data-jc-config="placeholder:1883;required:true" class="m">Port</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
<div data-jc="textbox" data-jc-path="clientid">@(Client id)</div>
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<div data-jc="textbox" data-jc-path="username" class="m">@(Username)</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>`;
|
||||
|
||||
|
||||
|
|
@ -41,21 +38,22 @@ Added:
|
|||
- rpc response
|
||||
`;
|
||||
|
||||
const instanceSendTo = {
|
||||
const { promisifyBuilder } = require('./helper/db_helper');
|
||||
const { errLogger, monitor } = require('./helper/logger');
|
||||
const fs = require('fs');
|
||||
const mqtt = require('mqtt');
|
||||
|
||||
const SEND_TO = {
|
||||
debug: 0,
|
||||
rpcCall: 1,
|
||||
services: 2
|
||||
}
|
||||
|
||||
const { promisifyBuilder, makeMapFromDbResult } = require('./helper/db_helper.js');
|
||||
|
||||
//CONFIG
|
||||
let useLog4js = true;
|
||||
let createTelemetryBackup = true;
|
||||
let saveTelemetryOnError = true;//backup_on_failure overrides this value
|
||||
//------------------------
|
||||
|
||||
var fs = require('fs');
|
||||
let rollers;
|
||||
if(createTelemetryBackup) rollers = require('streamroller');
|
||||
|
||||
|
|
@ -64,56 +62,18 @@ let insertNoSqlCounter = 0;
|
|||
let insertBackupNoSqlCounter = 0;
|
||||
let processingData = false;
|
||||
|
||||
let backup_on_failure = false;//== saveTelemetryOnError - create backup broker send failure
|
||||
let backup_on_failure = false;//== saveTelemetryOnError - create backup client send failure
|
||||
let restore_from_backup = 0; //how many rows process at once?
|
||||
let restore_backup_wait = 0;//wait seconds
|
||||
let lastRestoreTime = 0;
|
||||
|
||||
let errLogger;
|
||||
let logger;
|
||||
let monitor;
|
||||
|
||||
//TODO brokerready and sendBrokerError seems to be the same. Moreover, we use FLOW_OMS_brokerready variable!!
|
||||
//
|
||||
// if there is an error in broker connection, flow logs to monitor.txt. Not to log messages every second, we use sendBrokerError variable
|
||||
let sendBrokerError = true;
|
||||
|
||||
if(useLog4js)
|
||||
{
|
||||
var path = require('path');
|
||||
var log4js = require("log4js");
|
||||
|
||||
log4js.configure({
|
||||
appenders: {
|
||||
errLogs: { type: 'file', filename: path.join(__dirname + "/../", 'err.txt') },
|
||||
monitorLogs: { type: 'file', compress:true, daysToKeep: 2, maxLogSize: 1048576, backups: 1, keepFileExt: true, filename: path.join(__dirname + "/../", 'monitor.txt') },
|
||||
console: { type: 'console' }
|
||||
},
|
||||
categories: {
|
||||
errLogs: { appenders: ['console', 'errLogs'], level: 'error' },
|
||||
monitorLogs: { appenders: ['console', 'monitorLogs'], level: 'trace' },
|
||||
//another: { appenders: ['console'], level: 'trace' },
|
||||
default: { appenders: ['console'], level: 'trace' }
|
||||
}
|
||||
});
|
||||
|
||||
errLogger = log4js.getLogger("errLogs");
|
||||
logger = log4js.getLogger();
|
||||
monitor = log4js.getLogger("monitorLogs");
|
||||
|
||||
//USAGE
|
||||
//logger.debug("text");
|
||||
//monitor.info('info');
|
||||
//errLogger.error("some error");
|
||||
}
|
||||
// if there is an error in client connection, flow logs to monitor.txt. Not to log messages every second, we use sendClientError variable
|
||||
let sendClientError = true;
|
||||
|
||||
process.on('uncaughtException', function (err) {
|
||||
|
||||
if(errLogger)
|
||||
{
|
||||
errLogger.error('uncaughtException:', err.message)
|
||||
errLogger.error(err.stack);
|
||||
}
|
||||
errLogger.error('uncaughtException:', err.message)
|
||||
errLogger.error(err.stack);
|
||||
|
||||
//TODO
|
||||
//send to service
|
||||
|
|
@ -127,13 +87,9 @@ const nosqlBackup = NOSQL('/backup/tbdata');
|
|||
|
||||
exports.install = function(instance) {
|
||||
|
||||
var broker;
|
||||
var client;
|
||||
var opts;
|
||||
var brokerready = false;
|
||||
|
||||
instance.on('options', loadSettings);
|
||||
|
||||
mqtt = require('mqtt');
|
||||
var clientReady = false;
|
||||
|
||||
// wsmqtt status for notification purposes on projects.worksys.io database
|
||||
let wsmqttName = null;
|
||||
|
|
@ -142,21 +98,28 @@ exports.install = function(instance) {
|
|||
|
||||
function getWsmqttName(host)
|
||||
{
|
||||
if(host == "tb-demo.worksys.io" || host == '192.168.252.4') return 'wsmqtt_demo';
|
||||
else if(host == "tb-qas01.worksys.io" || host == '192.168.252.5') return 'wsmqtt_qas01';
|
||||
else if(host == "tb-prod01.worksys.io" || host == '192.168.252.1') return 'wsmqtt_prod01';
|
||||
if(host == "tb-demo.worksys.io" || host == '192.168.252.4') return 'wsmqtt_demo';
|
||||
else if(host == "tb-qas01.worksys.io" || host == '192.168.252.5') return 'wsmqtt_qas01';
|
||||
else if(host == "tb-prod01.worksys.io" || host == '192.168.252.1') return 'wsmqtt_prod01';
|
||||
}
|
||||
|
||||
function sendWsStatus()
|
||||
{
|
||||
instance.send(instanceSendTo.services, {[wsmqttName]: wsmqtt_status});
|
||||
instance.send(SEND_TO.services, {[wsmqttName]: wsmqtt_status});
|
||||
}
|
||||
|
||||
sendWsStatusVar = setInterval(sendWsStatus, 180000);
|
||||
|
||||
function main()
|
||||
{
|
||||
if(!FLOW.dbLoaded) return;
|
||||
|
||||
loadSettings();
|
||||
clearInterval(sendWsStatus);
|
||||
sendWsStatusVar = setInterval(sendWsStatus, 180000);
|
||||
}
|
||||
|
||||
//set opts according to db settings
|
||||
async function loadSettings()
|
||||
function loadSettings()
|
||||
{
|
||||
|
||||
if(instance.options.host !== "")
|
||||
|
|
@ -179,21 +142,17 @@ exports.install = function(instance) {
|
|||
else
|
||||
{
|
||||
|
||||
const dbSettings = TABLE("settings");
|
||||
let responseSettings = await promisifyBuilder(dbSettings.find());
|
||||
|
||||
backup_on_failure = responseSettings[0]["backup_on_failure"];
|
||||
const SETTINGS = FLOW.GLOBALS.settings;
|
||||
backup_on_failure = SETTINGS.backup_on_failure;
|
||||
saveTelemetryOnError = backup_on_failure;
|
||||
|
||||
restore_from_backup = responseSettings[0]["restore_from_backup"];
|
||||
restore_backup_wait = responseSettings[0]["restore_backup_wait"];
|
||||
restore_from_backup = SETTINGS.restore_from_backup;
|
||||
restore_backup_wait = SETTINGS.restore_backup_wait;
|
||||
|
||||
let mqtt_host = responseSettings[0]["mqtt_host"];
|
||||
let mqtt_clientid = responseSettings[0]["mqtt_clientid"];
|
||||
let mqtt_username = responseSettings[0]["mqtt_username"];
|
||||
let mqtt_port = responseSettings[0]["mqtt_port"];
|
||||
|
||||
console.log("wsmqttpublich -> loadSettings from db", responseSettings[0]);
|
||||
let mqtt_host = SETTINGS.mqtt_host;
|
||||
let mqtt_clientid = SETTINGS.mqtt_clientid;
|
||||
let mqtt_username = SETTINGS.mqtt_username;
|
||||
let mqtt_port = SETTINGS.mqtt_port;
|
||||
|
||||
opts = {
|
||||
host: mqtt_host,
|
||||
|
|
@ -216,27 +175,23 @@ exports.install = function(instance) {
|
|||
var url = "mqtt://" + opts.host + ":" + opts.port;
|
||||
console.log("MQTT URL: ", url);
|
||||
|
||||
broker = mqtt.connect(url, opts);
|
||||
client = mqtt.connect(url, opts);
|
||||
|
||||
broker.on('connect', function() {
|
||||
client.on('connect', function() {
|
||||
instance.status("Connected", "green");
|
||||
monitor.info("MQTT broker connected");
|
||||
monitor.info("MQTT client connected");
|
||||
|
||||
sendBrokerError = true;
|
||||
|
||||
brokerready = true;
|
||||
FLOW.OMS_brokerready = brokerready;
|
||||
sendClientError = true;
|
||||
clientReady = true;
|
||||
wsmqtt_status = 'connected';
|
||||
});
|
||||
|
||||
broker.on('reconnect', function() {
|
||||
client.on('reconnect', function() {
|
||||
instance.status("Reconnecting", "yellow");
|
||||
brokerready = false;
|
||||
|
||||
FLOW.OMS_brokerready = brokerready;
|
||||
clientReady = false;
|
||||
});
|
||||
|
||||
broker.on('message', function(topic, message) {
|
||||
client.on('message', function(topic, message) {
|
||||
// message is type of buffer
|
||||
message = message.toString();
|
||||
if (message[0] === '{') {
|
||||
|
|
@ -244,50 +199,53 @@ exports.install = function(instance) {
|
|||
|
||||
message = JSON.parse(message);
|
||||
if (message.hasOwnProperty("device") && message.hasOwnProperty("data") && message.data.hasOwnProperty("id")) {
|
||||
broker.publish(topic, `{"device": ${message.device}, "id": ${message.data.id}, "data": {"success": true}}`, {qos:1});
|
||||
instance.send(instanceSendTo.rpcCall, {"device": message.device, "id": message.data.id, "RPC response": {"success": true}});
|
||||
client.publish(topic, `{"device": ${message.device}, "id": ${message.data.id}, "data": {"success": true}}`, {qos:1});
|
||||
instance.send(SEND_TO.rpcCall, {"device": message.device, "id": message.data.id, "RPC response": {"success": true}});
|
||||
}
|
||||
|
||||
}, () => instance.debug('MQTT: Error parsing data', message));
|
||||
}
|
||||
|
||||
instance.send(instanceSendTo.rpcCall, {"topic":topic, "content":message });
|
||||
instance.send(SEND_TO.rpcCall, {"topic":topic, "content":message });
|
||||
});
|
||||
|
||||
broker.on('close', function(err) {
|
||||
brokerready = false;
|
||||
FLOW.OMS_brokerready = brokerready;
|
||||
client.on('close', function(err) {
|
||||
clientReady = false;
|
||||
wsmqtt_status = 'disconnected';
|
||||
|
||||
if (err && err.toString().indexOf('Error')) {
|
||||
instance.status("Err: "+err.code, "red");
|
||||
instance.send(instanceSendTo.debug, {"message":"Broker CLOSE signal received !", "error":err, "opt":opts });
|
||||
instance.send(SEND_TO.debug, {"message":"Client CLOSE signal received !", "error":err, "opt":opts });
|
||||
} else {
|
||||
instance.status("Disconnected", "red");
|
||||
instance.send(instanceSendTo.debug, {"message":"Broker CLOSE signal received !", "error":err, "opt":opts });
|
||||
instance.send(SEND_TO.debug, {"message":"Client CLOSE signal received !", "error":err, "opt":opts });
|
||||
}
|
||||
|
||||
broker.reconnect();
|
||||
client.reconnect();
|
||||
});
|
||||
|
||||
broker.on('error', function(err) {
|
||||
client.on('error', function(err) {
|
||||
instance.status("Err: "+ err.code, "red");
|
||||
instance.send(instanceSendTo.debug, {"message":"Broker ERROR signal received !", "error":err, "opt":opts });
|
||||
if(sendBrokerError) {
|
||||
monitor.info('MQTT broker error', err);
|
||||
sendBrokerError = false;
|
||||
instance.send(SEND_TO.debug, {"message":"Client ERROR signal received !", "error":err, "opt":opts });
|
||||
if(sendClientError) {
|
||||
monitor.info('MQTT client error', err);
|
||||
sendClientError = false;
|
||||
}
|
||||
brokerready = false;
|
||||
FLOW.OMS_brokerready = brokerready;
|
||||
clientReady = false;
|
||||
wsmqtt_status = 'disconnected';
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
instance.on('data', function(data) {
|
||||
|
||||
if (brokerready)
|
||||
instance.on("0", _ => {
|
||||
main();
|
||||
})
|
||||
|
||||
|
||||
instance.on('1', function(data) {
|
||||
|
||||
if(clientReady)
|
||||
{
|
||||
//do we have some data in backup file?
|
||||
//if any, process data from database
|
||||
|
|
@ -296,13 +254,14 @@ exports.install = function(instance) {
|
|||
//read telemetry data and send back to server
|
||||
if(!processingData) processDataFromDatabase();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (brokerready)
|
||||
if(clientReady)
|
||||
{
|
||||
let stringifiedJson = JSON.stringify(data.data);
|
||||
broker.publish("v1/gateway/telemetry", stringifiedJson, {qos: 1});
|
||||
client.publish("v1/gateway/telemetry", stringifiedJson, {qos: 1});
|
||||
|
||||
instance.send(3, stringifiedJson);
|
||||
|
||||
//backup telemetry
|
||||
if(createTelemetryBackup)
|
||||
|
|
@ -327,8 +286,8 @@ exports.install = function(instance) {
|
|||
else
|
||||
{
|
||||
|
||||
if(logger) logger.debug("Broker unavailable. Data not sent !", JSON.stringify(data.data));
|
||||
instance.send(instanceSendTo.debug, {"message":"Broker unavailable. Data not sent !", "data": data.data });
|
||||
//logger.debug("Client unavailable. Data not sent !", JSON.stringify(data.data));
|
||||
instance.send(SEND_TO.debug, {"message":"Client unavailable. Data not sent !", "data": data.data });
|
||||
|
||||
if(saveTelemetryOnError)
|
||||
{
|
||||
|
|
@ -344,9 +303,9 @@ exports.install = function(instance) {
|
|||
});
|
||||
|
||||
|
||||
instance.close = function(done) {
|
||||
if (brokerready){
|
||||
broker.end();
|
||||
instance.close = function(done) {
|
||||
if(clientReady){
|
||||
client.end();
|
||||
clearInterval(sendWsStatusVar);
|
||||
}
|
||||
};
|
||||
|
|
@ -373,7 +332,7 @@ exports.install = function(instance) {
|
|||
let firstDigit = files[i].slice(0, pos);
|
||||
|
||||
fileCounter = parseInt(firstDigit);
|
||||
if (isNaN(fileCounter)) fileCounter = 0;
|
||||
if(isNaN(fileCounter)) fileCounter = 0;
|
||||
//console.log("getDbBackupFileCounter digit:", files[i], firstDigit, fileCounter, isNaN(fileCounter), type);
|
||||
|
||||
if(type == "max")
|
||||
|
|
@ -443,10 +402,7 @@ exports.install = function(instance) {
|
|||
|
||||
const processDataFromDatabase = async () => {
|
||||
|
||||
if(restore_from_backup <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if(restore_from_backup <= 0) return;
|
||||
|
||||
//calculate diff
|
||||
const now = new Date();
|
||||
|
|
@ -478,7 +434,7 @@ exports.install = function(instance) {
|
|||
|
||||
for(let i = 0; i < records.length; i++)
|
||||
{
|
||||
if (brokerready) {
|
||||
if(clientReady) {
|
||||
|
||||
let item = records[i];
|
||||
let id = item.id;
|
||||
|
|
@ -487,18 +443,19 @@ exports.install = function(instance) {
|
|||
{
|
||||
//console.log("------------processDataFromDatabase - remove", id, dataBase, i);
|
||||
|
||||
try{
|
||||
try {
|
||||
|
||||
let o = JSON.parse(JSON.stringify(item));
|
||||
delete o.id;
|
||||
let message = JSON.stringify(o);
|
||||
|
||||
broker.publish("v1/gateway/telemetry", message, {qos:1});
|
||||
client.publish("v1/gateway/telemetry", message, {qos:1});
|
||||
instance.send(3, message);
|
||||
|
||||
//remove from database
|
||||
await promisifyBuilder(nosql.remove().where("id", id));
|
||||
|
||||
} catch (error) {
|
||||
} catch(error) {
|
||||
//process error
|
||||
console.log("processDataFromDatabase", error);
|
||||
}
|
||||
|
|
@ -533,8 +490,6 @@ exports.install = function(instance) {
|
|||
|
||||
}
|
||||
|
||||
loadSettings();
|
||||
|
||||
//instance.on('options', instance.reconfigure);
|
||||
//instance.reconfigure();
|
||||
instance.on('options', main);
|
||||
//instance.reconfigure();
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue