Skip to content

Commit

Permalink
Always store date as raw, tree, json.
Browse files Browse the repository at this point in the history
  • Loading branch information
MyHomeMyData committed Dec 3, 2023
1 parent f172eba commit 1f6e75d
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 89 deletions.
67 changes: 15 additions & 52 deletions admin/jsonConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,13 @@
"default" : "e380",
"newLine": true
},
"e380_tree": {
"e380_active": {
"type": "checkbox",
"label": "tree states",
"tooltip": "Show data as tree of obejcts",
"label": "active",
"tooltip": "Activate communication",
"default" : true,
"newLine": false
},
"e380_json": {
"type": "checkbox",
"label": "json states",
"tooltip": "Show data as JSON obejcts",
"default" : false,
"newLine": false
},
"e380_delay": {
"type": "number",
"label": "Min. update time (s)",
Expand Down Expand Up @@ -161,24 +154,14 @@
},
{
"type": "checkbox",
"attr": "collect_tree_states",
"width": "10% ",
"title": "tree states",
"tooltip": "Show data as tree of obejcts",
"attr": "collect_active",
"width": "5% ",
"title": "active",
"tooltip": "Activate communication",
"filter": false,
"sort": false,
"default": true
},
{
"type": "checkbox",
"attr": "collect_json_states",
"width": "10% ",
"title": "json states",
"tooltip": "Show data as JSON obejcts",
"filter": false,
"sort": false,
"default": false
},
{
"type": "number",
"attr": "collect_delay_time",
Expand Down Expand Up @@ -259,24 +242,14 @@
},
{
"type": "checkbox",
"attr": "uds_tree_states",
"width": "10% ",
"title": "tree states",
"tooltip": "Show data as tree of obejcts",
"attr": "uds_active",
"width": "5% ",
"title": "active",
"tooltip": "Activate communication",
"filter": false,
"sort": false,
"default": true
},
{
"type": "checkbox",
"attr": "uds_json_states",
"width": "10% ",
"title": "json states",
"tooltip": "Show data as JSON obejcts",
"filter": false,
"sort": false,
"default": false
},
{
"type": "text",
"attr": "uds_dev_addr",
Expand Down Expand Up @@ -372,24 +345,14 @@
},
{
"type": "checkbox",
"attr": "collect_tree_states",
"width": "10% ",
"title": "tree states",
"tooltip": "Show data as tree of obejcts",
"attr": "collect_active",
"width": "5% ",
"title": "active",
"tooltip": "Activate communication",
"filter": false,
"sort": false,
"default": true
},
{
"type": "checkbox",
"attr": "collect_json_states",
"width": "10% ",
"title": "json states",
"tooltip": "Show data as JSON obejcts",
"filter": false,
"sort": false,
"default": false
},
{
"type": "number",
"attr": "collect_delay_time",
Expand Down
5 changes: 2 additions & 3 deletions lib/canUds.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ class uds {
}

async addSchedule(ctx, schedule, dids) {
// x
const obj = await ctx.getStateAsync(this.schedulesId);
const schedules = JSON.parse(obj.val);
if (!Object.keys(schedules).includes(schedule)) {
Expand Down Expand Up @@ -132,7 +131,7 @@ class uds {
}
await ctx.setStateAsync(this.cmndQueueId, {val: JSON.stringify(cmnds), ack: true});
}
await this.sleep(100);
await this.sleep(50);
}
}

Expand All @@ -156,7 +155,7 @@ class uds {
await this.pushCmnd(ctx, 'read', dids); }
}
}
await this.sleep(100);
await this.sleep(50);
}
}

Expand Down
65 changes: 43 additions & 22 deletions lib/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,46 @@ class storage {
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: {},
});
}
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: {},
});
}
await ctx.setObjectNotExistsAsync(this.config.stateBase+'.tree', {
type: 'channel',
common: {
name: this.config.stateBase+' TREE',
role: 'device'
},
native: {},
});

await ctx.setObjectNotExistsAsync(this.config.stateBase+'.raw', {
type: 'channel',
common: {
name: this.config.stateBase+' RAW',
role: 'device'
},
native: {},
});
}

toHex(d) {
// Convert integer to hex string of length len
return ('00'+(Number(d).toString(16))).slice(-2);
}

arr2Hex(arr) {
// Convert byte array to hex string
let hs = '';
for (const v in arr) { hs += this.toHex(arr[v]); }
return hs;
}

async decodeDataCAN(ctx, did, data) {
async function storeObjectJson(ctx, stateId, obj) {
await ctx.setObjectNotExistsAsync(stateId, {
Expand Down Expand Up @@ -58,13 +76,16 @@ class storage {
}
}
const idStr = didsDict[this.config.device][did].id;
const raw = this.arr2Hex(data);
const val = didsDict[this.config.device][did].decode(data);
if (val) {
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) { await storeObjectTree(ctx, stateIdTree, val); }
if (this.config.doJson) { await storeObjectJson(ctx, stateIdJson, val); }
const stateIdRaw = this.config.stateBase+'.raw.'+didStr.slice(-4)+'_'+idStr;
await storeObjectTree(ctx, stateIdTree, val);
await storeObjectJson(ctx, stateIdJson, val);
await storeObjectJson(ctx, stateIdRaw, raw);
}
}

Expand Down
20 changes: 8 additions & 12 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,42 +93,39 @@ class E3oncan extends utils.Adapter {
// ===========================================

// Setup E380 collect:
if ( (this.config.e380_tree) || (this.config.e380_json) ) {
if (this.config.e380_active) {
this.e380Collect = new collect.collect(
{ '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});
'active': this.config.e380_active});
await this.e380Collect.initStates(this);
}
// Setup all configured devices for collect:
for (const dev of Object.values(this.config.table_collect_ext)) {
if ( (dev.collect_tree_states) || (dev.collect_json_states) ) {
if (dev.collect_active) {
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,
'active': dev.collect_active,
'channel': this.channelExt});
this.E3CollectExt.push(Collect);
await Collect.initStates(this); }
}

// Setup all configured devices for UDS:
for (const dev of Object.values(this.config.table_uds)) {
if ( (dev.uds_tree_states) || (dev.uds_json_states) ) {
if (dev.uds_active) {
if (!(Object.keys(this.udsDevices).includes(dev.uds_dev_addr))) {
const Uds = new uds.uds(
{ 'canID': [Number(dev.uds_dev_addr)],
'stateBase': dev.uds_dev_name,
'device': 'common',
'delay': 0,
'doTree': dev.uds_tree_states,
'doJSON': dev.uds_json_states,
'active': dev.uds_active,
'channel': this.channelExt,
'timeout': 2 // Commuication timeout (s)
});
Expand All @@ -148,14 +145,13 @@ class E3oncan extends utils.Adapter {

// Setup all configured devices for collect:
for (const dev of Object.values(this.config.table_collect_int)) {
if ( (dev.collect_tree_states) || (dev.collect_json_states) ) {
if (dev.collect_active) {
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,
'active': dev.collect_active,
'channel': this.channelInt});
this.E3CollectInt.push(Collect);
await Collect.initStates(this); }
Expand Down

0 comments on commit 1f6e75d

Please sign in to comment.