Good morning, I need your help. I am receiving data coming from the serial port "/ dev / ttyUSB0", my goal "is to recover some information (temperature and humidity) and replace it on the object enoceanRecords
.
// Constants
const DEFAULT_SERIAL_PORT = "/dev/ttyUSB0";
// Dependancies
var fs = require('fs');
var vm = require('vm');
var events = require('events');
var cache = require('./plugins_cache.js');
var util = require('util');
var pk = require('node-enocean')();
// Type de données
var enoceanRecords = {
Humidity: { 'value': '', 'type': 'TEXT', 'unit': '' },
Temperature: { 'value': '', 'type': 'INTEGER', 'unit': 'A' }
};
// Includes
vm.runInThisContext(fs.readFileSync(__dirname + "/" + "plugins_table.js"));
var table = new Table();
// Class
var PluginEnocean = function () {
this.infos = { name: "enocean",
version: "0.0.1"
};
};
PluginEnocean.prototype.query = function () {
return this.infos;
}
PluginEnocean.prototype.init = function (config) {
var options = config.get('plugin:enocean');
var path = DEFAULT_SERIAL_PORT;
if(options !== undefined) {
if(options['path'] !== undefined) {
path = options['path'];
}
}
// Open serial port
console.log(path);
pk.listen(path);
// Set table name
table.setName(this.infos['name']);
// Collect data
pk.on('data', function (data) {
// =====================
var temp = enoceanRecords['Temperature'];
var humid = enoceanRecords['Humidity'] ;
var valTemp = (data['values'])[1];
var valHumid = (data['values'])[0];
temp['value'] = valTemp['value'];
temp['type'] = valTemp['type'];
temp['unit'] = valTemp['unit'];
humid['value'] = valHumid['value'];
humid['type'] = valHumid['type'];
humid['unit'] = valHumid['unit'];
//=====================
var uuid = data['senderId'] ;
for(key in enoceanRecords) {
table.setRecord(key, enoceanRecords[key]);
}
// Feed data
if(uuid !== undefined) {
table.setUuid(uuid);
table.setRecord(key, enoceanRecords[key]);
table.setStamp();
cache.push(table);
}
});
};
PluginEnocean.prototype.run = function () {
};
PluginEnocean.prototype.exit = function () {
};
var enocean = module.exports = new PluginEnocean();