Skip to content

Commit

Permalink
New class storage.js
Browse files Browse the repository at this point in the history
  • Loading branch information
MyHomeMyData committed Dec 1, 2023
1 parent 7d10e91 commit 8a4d7c5
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 99 deletions.
79 changes: 11 additions & 68 deletions lib/canCollect.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@

//const codecs = require('./codecs');
const didsDict = require('./didsE3').dids;
const storageCol = require('./storage');

class collect {
constructor(canID, stateBase, device, delay, doTree, doJson) {
this.canID = canID;
this.stateBase = stateBase;
this.device = device;
this.delay = delay; // Minumum time delay (s) for evaluation of specific did
this.doTree = doTree;
this.doJson = doJson;
constructor(config) {
this.config = config;
this.storage = new storageCol.storage(this.config);
this.ts = {};
this.dids = null;
this.didwatch = 7777;
Expand All @@ -22,72 +17,20 @@ class collect {
'D0' : 0x21
};
}
async initStates(ctx) {
if (this.doJson) {
await ctx.setObjectNotExistsAsync(this.stateBase+'.json', {
type: 'channel',
common: {
name: this.stateBase+' JSON',
role: 'device'
},
native: {},
});
}

if (this.doTree) {
await ctx.setObjectNotExistsAsync(this.stateBase+'.tree', {
type: 'channel',
common: {
name: this.stateBase+' TREE',
role: 'device'
},
native: {},
});
}
}
async decodeDataCAN(ctx, did, data) {
async function storeObjectJson(ctx, stateId, obj) {
await ctx.setObjectNotExistsAsync(stateId, {
type: 'state',
common: {
name: idStr,
type: 'string',
role: 'state',
read: true,
write: true,
},
native: {},
});
await ctx.setStateAsync(stateId, JSON.stringify(obj), true);
}
async function storeObjectTree(ctx, stateId, obj) {
if (typeof(obj) == 'object') {
for (const key in Object.keys(obj)) {
const itm = obj[Object.keys(obj)[key]];
storeObjectTree(ctx, String(stateId)+'.'+String(Object.keys(obj)[key]),itm);
}
} else {
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;
if (this.doTree) { storeObjectTree(ctx, stateIdTree, val); }
if (this.doJson) { storeObjectJson(ctx, stateIdJson, val); }
async initStates(ctx) {
this.storage.initStates(ctx);
}

async msgCollect(ctx, msg) {
const candata = msg.data.toJSON().data;
const canid = msg.id;
const tsNow = new Date().getTime();

if (this.device == 'e380') {
if (this.config.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);
if ( (this.config.delay > 0) && ((tsNow-this.ts[canid]) < this.config.delay*1000) ) { return; }
this.storage.decodeDataCAN(ctx,msg.id,candata);
this.ts[canid] = tsNow;
} else {
//ctx.log.debug(JSON.stringify(candata));
Expand All @@ -107,7 +50,7 @@ class collect {
ctx.log.debug(JSON.stringify(this.data));
}
this.data.collecting = false;
this.decodeDataCAN(ctx, this.data.did, this.data.databytes.slice(0,this.data.len));
this.storage.decodeDataCAN(ctx, this.data.did, this.data.databytes.slice(0,this.data.len));
this.ts[this.data.did] = tsNow;
}
}
Expand All @@ -134,7 +77,7 @@ class collect {
}
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; }
if ( (this.config.delay > 0) && ((tsNow-this.ts[this.data.did]) < this.config.delay*1000) ) { return; }
this.data.timestamp = msg.ts_sec*1000+Math.round(msg.ts_usec/1000);
this.data.collecting = true;
}
Expand Down
69 changes: 69 additions & 0 deletions lib/storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@

const didsDict = require('./didsE3').dids;

class storage {
constructor(config) {
this.config = config;
}
async initStates(ctx) {
if (this.config.doJson) {
await ctx.setObjectNotExistsAsync(this.config.stateBase+'.json', {
type: 'channel',
common: {
name: this.config.stateBase+' JSON',
role: 'device'
},
native: {},
});
}

if (this.config.doTree) {
await ctx.setObjectNotExistsAsync(this.config.stateBase+'.tree', {
type: 'channel',
common: {
name: this.config.stateBase+' TREE',
role: 'device'
},
native: {},
});
}
}
async decodeDataCAN(ctx, did, data) {
async function storeObjectJson(ctx, stateId, obj) {
await ctx.setObjectNotExistsAsync(stateId, {
type: 'state',
common: {
name: idStr,
type: 'string',
role: 'state',
read: true,
write: true,
},
native: {},
});
await ctx.setStateAsync(stateId, JSON.stringify(obj), true);
}
async function storeObjectTree(ctx, stateId, obj) {
if (typeof(obj) == 'object') {
for (const [key, itm] of Object.entries(obj)) {
storeObjectTree(ctx, String(stateId)+'.'+String(key),itm);
}
} else {
//ctx.log.debug(String(stateId)+': '+JSON.stringify(obj));
storeObjectJson(ctx, stateId, obj);
}
}
const idStr = didsDict[this.config.device][did].id;
const val = didsDict[this.config.device][did].decode(data);
const didStr = '000'+String(did);
const stateIdJson = this.config.stateBase+'.json.'+didStr.slice(-4)+'_'+idStr;
const stateIdTree = this.config.stateBase+'.tree.'+didStr.slice(-4)+'_'+idStr;
if (this.config.doTree) { storeObjectTree(ctx, stateIdTree, val); }
if (this.config.doJson) { storeObjectJson(ctx, stateIdJson, val); }
}

}

module.exports = {
storage
};
67 changes: 36 additions & 31 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,41 +64,46 @@ class E3oncan extends utils.Adapter {
// Setup E380 collect:
if ( (this.config.e380_tree) || (this.config.e380_json) ) {
this.e380Collect = new collect.collect(
[0x250,0x252,0x254,0x256,0x258,0x25A,0x25C],
this.config.e380_name, this.config.e380_name,
this.config.e380_delay, this.config.e380_tree,
this.config.e380_json);
{ 'canID': [0x250,0x252,0x254,0x256,0x258,0x25A,0x25C],
'stateBase': this.config.e380_name,
'device': this.config.e380_name,
'delay': this.config.e380_delay,
'doTree': this.config.e380_tree,
'doJSON': this.config.e380_json});
await this.e380Collect.initStates(this);
}
// Setup all configured devices for collect:
for (let i=0; i< this.config.table_collect_ext.length;i++) {
const dev = this.config.table_collect_ext[i];
//for (const [key, dev] of Object.values(this.config.table_collect_ext)) {
// this.log.debug(String(key)+': '+JSON.stringify(dev));
//}
for (const dev of Object.values(this.config.table_collect_ext)) {
if ( (dev.collect_tree_states) || (dev.collect_json_states) ) {
this.E3CollectExt.push(new collect.collect(
[Number(dev.collect_canid)],
dev.collect_dev_name,
'common',
dev.collect_delay_time,
dev.collect_tree_states,
dev.collect_json_states));
await this.E3CollectExt[i].initStates(this); }
const Collect = new collect.collect(
{ 'canID': [Number(dev.collect_canid)],
'stateBase': dev.collect_dev_name,
'device': 'common',
'delay': dev.collect_delay_time,
'doTree': dev.collect_tree_states,
'doJSON': dev.collect_json_states});
this.E3CollectExt.push(Collect);
await Collect.initStates(this); }
}

// Evaluate configuration for internal CAN bus
// ===========================================

// Setup all configured devices for collect:
for (let i=0; i< this.config.table_collect_int.length;i++) {
const dev = this.config.table_collect_int[i];
for (const dev of Object.values(this.config.table_collect_int)) {
if ( (dev.collect_tree_states) || (dev.collect_json_states) ) {
this.E3CollectInt.push(new collect.collect(
[Number(dev.collect_canid)],
dev.collect_dev_name,
'common',
dev.collect_delay_time,
dev.collect_tree_states,
dev.collect_json_states));
await this.E3CollectInt[i].initStates(this); }
const Collect = new collect.collect(
{ 'canID': [Number(dev.collect_canid)],
'stateBase': dev.collect_dev_name,
'device': 'common',
'delay': dev.collect_delay_time,
'doTree': dev.collect_tree_states,
'doJSON': dev.collect_json_states});
this.E3CollectInt.push(Collect);
await Collect.initStates(this); }
}


Expand Down Expand Up @@ -263,18 +268,18 @@ class E3oncan extends utils.Adapter {
// }

onCanMsgExt(msg) {
if ( (this.e380Collect) && (this.e380Collect.canID.includes(msg.id)) ) { this.e380Collect.msgCollect(this, msg); }
for (let i=0; i<this.E3CollectExt.length;i++) {
if ( (this.E3CollectExt[i]) && (this.E3CollectExt[i].canID.includes(msg.id)) ) {
this.E3CollectExt[i].msgCollect(this, msg);
if ( (this.e380Collect) && (this.e380Collect.config.canID.includes(msg.id)) ) { this.e380Collect.msgCollect(this, msg); }
for (const dev of Object.values(this.E3CollectExt)) {
if ( (dev) && (dev.config.canID.includes(msg.id)) ) {
dev.msgCollect(this, msg);
}
}
}

onCanMsgInt(msg) {
for (let i=0; i<this.E3CollectInt.length;i++) {
if ( (this.E3CollectInt[i]) && (this.E3CollectInt[i].canID.includes(msg.id)) ) {
this.E3CollectInt[i].msgCollect(this, msg);
for (const dev of Object.values(this.E3CollectInt)) {
if ( (dev) && (dev.config.canID.includes(msg.id)) ) {
dev.msgCollect(this, msg);
}
}
}
Expand Down

0 comments on commit 8a4d7c5

Please sign in to comment.