Skip to content

Commit

Permalink
Admin config test version
Browse files Browse the repository at this point in the history
  • Loading branch information
MyHomeMyData committed Nov 28, 2023
1 parent 2593233 commit ff14409
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 40 deletions.
114 changes: 109 additions & 5 deletions admin/jsonConfig.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,120 @@
{
"i18n": true,
"i18n": false,
"type": "panel",
"items": {
"option1": {
"can_header": {
"type": "header",
"text": "CAN Adapter",
"size": 2,
"newLine": false
},
"can_name": {
"type": "text",
"label": "can bus",
"default" : "vcan0",
"newLine": false
},
"e380_header": {
"type": "header",
"text": "Energy Meter E380",
"size": 2,
"newLine": true
},
"e380_name": {
"type": "text",
"label": "Name",
"default" : "e380",
"newLine": false
},
"e380_tree": {
"type": "checkbox",
"label": "tree states",
"default" : true,
"newLine": false
},
"e380_json": {
"type": "checkbox",
"label": "option1",
"label": "json states",
"default" : false,
"newLine": false
},
"e380_delay": {
"type": "text",
"label": "Min. update time (s)",
"default" : 5,
"newLine": false
},
"dev1_header": {
"type": "header",
"text": "Viessmann Device 1",
"size": 2,
"newLine": true
},
"option2": {
"dev1_name": {
"type": "text",
"label": "option2",
"label": "Name",
"default" : "vitocal",
"newLine": false
},
"dev1_tree": {
"type": "checkbox",
"label": "tree states",
"default" : true,
"newLine": false
},
"dev1_json": {
"type": "checkbox",
"label": "json states",
"default" : false,
"newLine": false
},
"dev1_delay": {
"type": "text",
"label": "Min. update time (s)",
"default" : 5,
"newLine": false
},
"dev1_canid": {
"type": "text",
"label": "CAN ID",
"default" : "0x693",
"newLine": false
},
"dev2_header": {
"type": "header",
"text": "Viessmann Device 2",
"size": 2,
"newLine": true
},
"dev2_name": {
"type": "text",
"label": "Name",
"default" : "vx3",
"newLine": false
},
"dev2_tree": {
"type": "checkbox",
"label": "tree states",
"default" : true,
"newLine": false
},
"dev2_json": {
"type": "checkbox",
"label": "json states",
"default" : false,
"newLine": false
},
"dev2_delay": {
"type": "text",
"label": "Min. update time (s)",
"default" : 5,
"newLine": false
},
"dev2_canid": {
"type": "text",
"label": "CAN ID",
"default" : "0x451",
"newLine": false
}
}
}
2 changes: 0 additions & 2 deletions io-package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@
]
},
"native": {
"option1": true,
"option2": "42"
},
"objects": [],
"instanceObjects": [
Expand Down
44 changes: 25 additions & 19 deletions lib/canCollect.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
const didsDict = require('./didsE3').dids;

class collect {
constructor(canID, stateBase, device, delay) {
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;
this.ts = {};
this.dids = null;
this.didwatch = 7777;
Expand All @@ -21,23 +23,27 @@ class collect {
};
}
async initStates(ctx) {
await ctx.setObjectNotExistsAsync(this.stateBase+'.json', {
type: 'channel',
common: {
name: this.stateBase+' JSON',
role: 'device'
},
native: {},
});
if (this.doJson) {
await ctx.setObjectNotExistsAsync(this.stateBase+'.json', {
type: 'channel',
common: {
name: this.stateBase+' JSON',
role: 'device'
},
native: {},
});
}

await ctx.setObjectNotExistsAsync(this.stateBase+'.tree', {
type: 'channel',
common: {
name: this.stateBase+' TREE',
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) {
Expand Down Expand Up @@ -69,8 +75,8 @@ 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;
storeObjectTree(ctx, stateIdTree, val);
storeObjectJson(ctx, stateIdJson, val);
if (this.doTree) { storeObjectTree(ctx, stateIdTree, val); }
if (this.doJson) { storeObjectJson(ctx, stateIdJson, val); }
}

async msgCollect(ctx, msg) {
Expand Down
47 changes: 33 additions & 14 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,44 +25,62 @@ class E3oncan extends utils.Adapter {
...options,
name: 'e3oncan',
});
//this.on('install', this.onInstall.bind(this));
this.on('ready', this.onReady.bind(this));
this.on('stateChange', this.onStateChange.bind(this));
// this.on('objectChange', this.onObjectChange.bind(this));
// this.on('message', this.onMessage.bind(this));
this.on('unload', this.onUnload.bind(this));

this.vx3Collect = new collect.collect([0x451], 'vx3', 'common', 5);
this.e380Collect = new collect.collect([0x250,0x252,0x254,0x256,0x258,0x25A,0x25C], 'e380', 'e380', 5);
this.dev2Collect = null;
this.e380Collect = null;

this.channel = null;
}
/*
async onInstall() {
console.log('onInstall()');
console.log(this.config);
}
*/
/**
* Is called when databases are connected and adapter received configuration.
*/
async onReady() {
// Initialize your adapter here
try {
this.channel = can.createRawChannel('vcan0', true);
this.channel = can.createRawChannel(this.config.can_name, true);
this.channel.addListener('onMessage', this.onCanMsg, this);
} catch (e) {
console.error(e);
this.channel = null;
}
}

/**
* Is called when databases are connected and adapter received configuration.
*/
async onReady() {
// Initialize your adapter here
if ( (this.config.dev2_tree) || (this.config.dev2_json) ) {
this.dev2Collect = new collect.collect([Number(this.config.dev2_canid)], this.config.dev2_name, 'common', this.config.dev2_delay, this.config.dev2_tree, this.config.dev2_json);
}
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);
}

// Reset the connection indicator during startup
this.setState('info.connection', false, true);

// The adapters config (in the instance object everything under the attribute "native") is accessible via
// this.config:
this.log.info('config option1: ' + this.config.option1);
this.log.info('config option2: ' + this.config.option2);
//this.log.info('config option1: ' + this.config.option1);
//this.log.info('config option2: ' + this.config.option2);

/*
For every state in the system there has to be also an object of type state
Here a simple template for a boolean variable named "testVariable"
Because every adapter instance uses its own unique namespace variable names can't collide with other adapters variables
*/
await this.vx3Collect.initStates(this);
console.log(this.config);

if (this.vx3Collect) { await this.vx3Collect.initStates(this); }
if (this.e380Collect) { await this.e380Collect.initStates(this); }

await this.setObjectNotExistsAsync('testVariable', {
type: 'state',
common: {
Expand Down Expand Up @@ -184,11 +202,12 @@ class E3oncan extends utils.Adapter {
// }

onCanMsg(msg) {
if (this.vx3Collect.canID.includes(msg.id)) { this.vx3Collect.msgCollect(this, msg); }
if (this.e380Collect.canID.includes(msg.id)) { this.e380Collect.msgCollect(this, msg); }
if ( (this.dev2Collect) && (this.dev2Collect.canID.includes(msg.id)) ) { this.dev2Collect.msgCollect(this, msg); }
if ( (this.e380Collect) && (this.e380Collect.canID.includes(msg.id)) ) { this.e380Collect.msgCollect(this, msg); }
}
}


if (require.main !== module) {
// Export the constructor in compact mode
/**
Expand Down

0 comments on commit ff14409

Please sign in to comment.