diff --git a/lib/canCollect.js b/lib/canCollect.js index 3949e57..5335b61 100644 --- a/lib/canCollect.js +++ b/lib/canCollect.js @@ -1,11 +1,14 @@ + //const codecs = require('./codecs'); const didsDict = require('./didsE3').dids; class collect { - constructor(canID, stateBase, device) { + constructor(canID, stateBase, device, delay) { this.canID = canID; this.stateBase = stateBase; this.device = device; + this.delay = delay; // Minumum time delay (s) for evaluation of specific did + this.ts = {}; this.dids = null; this.didwatch = 7777; this.data = { @@ -37,7 +40,7 @@ class collect { }); } async decodeDataCAN(ctx, did, data) { - async function storeObject(ctx, stateId, obj) { + async function storeObjectJson(ctx, stateId, obj) { await ctx.setObjectNotExistsAsync(stateId, { type: 'state', common: { @@ -51,14 +54,14 @@ class collect { }); await ctx.setStateAsync(stateId, JSON.stringify(obj), true); } - async function objDump(ctx, stateId, obj) { + async function storeObjectTree(ctx, stateId, obj) { if (typeof(obj) == 'object') { for (const key in Object.keys(obj)) { const itm = obj[Object.keys(obj)[key]]; - objDump(ctx, String(stateId)+'.'+String(Object.keys(obj)[key]),itm); + storeObjectTree(ctx, String(stateId)+'.'+String(Object.keys(obj)[key]),itm); } } else { - storeObject(ctx, stateId, obj); + storeObjectJson(ctx, stateId, obj); } } const idStr = didsDict[this.device][did].id; @@ -66,15 +69,20 @@ class collect { const didStr = '000'+String(did); const stateIdJson = this.stateBase+'.json.'+didStr.slice(-4)+'_'+idStr; const stateIdTree = this.stateBase+'.tree.'+didStr.slice(-4)+'_'+idStr; - objDump(ctx, stateIdTree, val); - storeObject(ctx, stateIdJson, val); + storeObjectTree(ctx, stateIdTree, val); + storeObjectJson(ctx, stateIdJson, val); } async msgCollect(ctx, msg) { const candata = msg.data.toJSON().data; - //console.log(canID); + const canid = msg.id; + const tsNow = new Date().getTime(); + if (this.device == 'e380') { + if (!(canid in this.ts)) { this.ts[canid] = 0; } + if ( (this.delay > 0) && ((tsNow-this.ts[canid]) < this.delay*1000) ) { return; } this.decodeDataCAN(ctx,msg.id,candata); + this.ts[canid] = tsNow; } else { //console.log(candata); if (this.data.collecting) { @@ -94,6 +102,7 @@ class collect { } this.data.collecting = false; this.decodeDataCAN(ctx, this.data.did, this.data.databytes.slice(0,this.data.len)); + this.ts[this.data.did] = tsNow; } } } @@ -118,6 +127,8 @@ class collect { console.log(this.data); } if ( (this.data.did > 0) && (this.data.did < 10000) ) { + if (!(this.data.did in this.ts)) { this.ts[this.data.did] = 0; } + if ( (this.delay > 0) && ((tsNow-this.ts[this.data.did]) < this.delay*1000) ) { return; } this.data.timestamp = msg.ts_sec*1000+Math.round(msg.ts_usec/1000); this.data.collecting = true; } diff --git a/main.js b/main.js index e03ebd5..d814bbd 100644 --- a/main.js +++ b/main.js @@ -31,8 +31,8 @@ class E3oncan extends utils.Adapter { // this.on('message', this.onMessage.bind(this)); this.on('unload', this.onUnload.bind(this)); - this.vx3Collect = new collect.collect([0x451], 'vx3', 'common'); - this.e380Collect = new collect.collect([0x250,0x252,0x254,0x256,0x258,0x25A,0x25C], 'e380', 'e380'); + this.vx3Collect = new collect.collect([0x451], 'vx3', 'common', 5); + this.e380Collect = new collect.collect([0x250,0x252,0x254,0x256,0x258,0x25A,0x25C], 'e380', 'e380', 5); try { this.channel = can.createRawChannel('vcan0', true);