From 2e88ab17e7199b5365aa62a81f511316791096f6 Mon Sep 17 00:00:00 2001 From: MyHomeMyData Date: Sat, 6 Jan 2024 14:00:39 +0100 Subject: [PATCH] Allow WriteByDid only for dids on white list. --- lib/canUds.js | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/lib/canUds.js b/lib/canUds.js index 0b60557..47e8f98 100644 --- a/lib/canUds.js +++ b/lib/canUds.js @@ -171,6 +171,7 @@ class uds { // Stop worker: this.callback = null; if (opMode == 'normal') { + await ctx.unsubscribeStates(ctx.namespace+'.'+this.config.stateBase+'.*'); await ctx.log.info('UDS worker stopped on '+this.config.stateBase); } else { await ctx.log.silly('UDS worker stopped in mode '+opMode+' on '+this.config.stateBase); @@ -309,12 +310,32 @@ class uds { async onUdsStateChange(ctx, ctxWorker, id, state) { + // Change of UDS Writables + // ======================= + if (id.includes(this.storage.storageDids.didsWritablesId)) { + // User requests change of UDS writables + await ctx.log.info('User requested change of UDS dids writable on '+this.config.stateBase); + await this.storage.storageDids.readKnownDids(ctx, await this.storage.getOpMode()); + await ctx.setStateAsync(id, { val: state.val, ack: true }); // Acknowlegde user command + return; + } + + // Change of UDS device specific datapoint definition + // ================================================== + if (id.includes(this.storage.storageDids.didsSpecId)) { + // User requests change of UDS device specific datapoint definition + await ctx.log.info('User requested change of UDS device specific datapoint definition on '+this.config.stateBase); + await this.storage.storageDids.readKnownDids(ctx, await this.storage.getOpMode()); + await ctx.setStateAsync(id, { val: state.val, ack: true }); // Acknowlegde user command + return; + } + // User command ReadByDid // ====================== if (id.includes(this.userReadByDidId)) { // User requests ReadByDid const dids = JSON.parse(state.val); - await ctx.log.debug('UDS user command ReadByDid on '+this.config.stateBase+'. Dids='+JSON.stringify(dids)); + await ctx.log.debug('User command UDS ReadByDid on '+this.config.stateBase+'. Dids='+JSON.stringify(dids)); await this.pushCmnd(ctx, 'read', dids); await ctx.setStateAsync(id, { val: JSON.stringify(dids), ack: true }); // Acknowlegde user command return; @@ -325,15 +346,15 @@ class uds { const dcs = await ctx.idToDCS(id); // Get device, channel and state id if (dcs.state.length < 6) { // Implausible state id - ctx.log.warn('UDS user command WriteByDid on '+this.config.stateBase+': Could not evaluate state change on id '+id); + ctx.log.warn('User command UDS WriteByDid on '+this.config.stateBase+': Could not evaluate state change on id '+id); return; } const did = Number(dcs.state.slice(0,4)); if (!(did in this.storage.storageDids.didsWritable)) { - ctx.log.error('UDS user command WriteByDid on '+this.config.stateBase+'.'+String(did)+': Writing not allowed on this did. Pls. refer to README for further informations.'); + ctx.log.error('User command UDS WriteByDid on '+this.config.stateBase+'.'+String(did)+': Writing not allowed on this did. Pls. refer to README for further informations.'); return; } - await ctx.log.debug('UDS user command WriteByDid on '+this.config.stateBase+'.'+String(did)); + await ctx.log.debug('User command UDS WriteByDid on '+this.config.stateBase+'.'+String(did)); //await ctx.log.debug(JSON.stringify(dcs)+' did='+String(did)+' id='+id+' state='+JSON.stringify(state)); let byteArr=null; // Encoded data let lenBaseId; // Index of start of did state id (did_name ...) in full state id (e3oncan ...) @@ -345,7 +366,7 @@ class uds { await this.pushCmnd(ctx, 'write', [[did,byteArr]]); setTimeout(function(ctxWorker,did){ctxWorker.cmndsQueue.push({'mode':'read', 'did': did});},2500,this,did); // Read value after 2500 ms } else { - ctx.log.error('UDS user command WriteByDid on '+this.config.stateBase+': Encoding of data failed.'); + ctx.log.error('User command UDS WriteByDid on '+this.config.stateBase+': Encoding of data failed.'); } break; case 'raw': @@ -355,7 +376,7 @@ class uds { await this.pushCmnd(ctx, 'write', [[did,byteArr]]); setTimeout(function(ctxWorker,did){ctxWorker.cmndsQueue.push({'mode':'read', 'did': did});},2500,this,did); // Read value after 2500 ms } else { - ctx.log.error('UDS user command WriteByDid on '+this.config.stateBase+': Encoding of data failed.'); + ctx.log.error('User command UDS WriteByDid on '+this.config.stateBase+': Encoding of data failed.'); } break; case 'tree': @@ -369,7 +390,7 @@ class uds { await this.pushCmnd(ctx, 'write', [[did,byteArr]]); setTimeout(function(ctxWorker,did){ctxWorker.cmndsQueue.push({'mode':'read', 'did': did});},2500,this,did); // Read value after 2500 ms } else { - ctx.log.error('UDS user command WriteByDid on '+this.config.stateBase+': Encoding of data failed.'); + ctx.log.error('User command UDS WriteByDid on '+this.config.stateBase+': Encoding of data failed.'); } break; } @@ -417,12 +438,12 @@ class uds { await ctxWorker.pushCmnd(ctx, 'write', [[did,byteArr]]); setTimeout(function(ctxWorker,did){ctxWorker.cmndsQueue.push({'mode':'read', 'did': did});},2500,ctxWorker,did); // Read value after 2500 ms } else { - ctx.log.error('UDS user command WriteByDid on '+ctxWorker.config.stateBase+': Encoding of data failed.'); + ctx.log.error('User command UDS WriteByDid on '+ctxWorker.config.stateBase+': Encoding of data failed.'); } }); break; default: - ctx.log.warn('UDS user command WriteByDid on '+this.config.stateBase+': Could not evaluate state change on id '+id); + ctx.log.warn('User command UDS WriteByDid on '+this.config.stateBase+': Could not evaluate state change on id '+id); } }