Skip to content

Commit

Permalink
Added minimum delay for did evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
MyHomeMyData committed Nov 28, 2023
1 parent dc4e9e5 commit ce02009
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
27 changes: 19 additions & 8 deletions lib/canCollect.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -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: {
Expand All @@ -51,30 +54,35 @@ 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;
const val = didsDict[this.device][did].decode(data);
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) {
Expand All @@ -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;
}
}
}
Expand All @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit ce02009

Please sign in to comment.