From 86dd141b6348280decf918d7af62484bf82d2fbc Mon Sep 17 00:00:00 2001 From: BenAhrdt Date: Sun, 25 Aug 2024 09:35:21 +0200 Subject: [PATCH] change in function to assign properties --- README.md | 2 +- lib/modules/directorieshandler.js | 41 +++++++++++++++++++++---------- main.js | 8 +++--- 3 files changed, 33 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 6921195..12d156c 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ For now there is documentation in English here: https://wiki.hafenmeister.de ### **WORK IN PROGRESS** --> ### **WORK IN PROGRESS** -* (BenAhrdt) function to assign roles +* (BenAhrdt) function to assign properties ### 1.3.1 (2024-07-30) * (BenAhrdt) implements example image diff --git a/lib/modules/directorieshandler.js b/lib/modules/directorieshandler.js index d7108b1..34c8ddb 100644 --- a/lib/modules/directorieshandler.js +++ b/lib/modules/directorieshandler.js @@ -28,6 +28,12 @@ class directorieshandlerClass { joinRaw: "join.raw" }; + this.assignableRoles = { + sensor: { + main: "sensor" + } + }; + this.icons = { wifi: "wifi" }; @@ -38,15 +44,15 @@ class directorieshandlerClass { * **************** Definition of Extendaed Assigns ***************** * ******************************************************************/ - this.executeExtendedAssign = (id,assigndata,options)=>{ - const activeFunction = "executeExtendedAssign"; + this.executeAssign = (id,value,assigndata,options)=>{ + const activeFunction = "executeAssign"; this.adapter.log.debug(`Function ${activeFunction} started.`); try{ // check folder const baseInfo = this.adapter.getBaseDeviceInfo(id); const subfolder = id.slice(baseInfo.objectStartDirectory.length + 1,id.length - baseInfo.changedState.length - 1); - if(!assigndata.approvedFolders || assigndata.approvedFolders[subfolder]){ - assigndata.assignfunction(assigndata,options); + if(assigndata.approvedFolders && assigndata.approvedFolders[subfolder]){ + return assigndata.approvedFolders[subfolder].assignfunction(id,value,assigndata.approvedFolders[subfolder],options); } } catch(error){ @@ -54,15 +60,24 @@ class directorieshandlerClass { } }; - this.commonAssign = (assigndata,options)=>{ + this.commonAssign = (id,value,assigndata,options)=>{ for(const element in assigndata.common){ options.common[element] = assigndata.common[element]; } + return value; }; - this.extendedAssign = { - DOOR_CLOSE_STATUS: {assignfunction: this.commonAssign, approvedFolders:{"uplink.decoded":true}, common: {role:"sensor.door"}}, - WINDOW_CLOSE_STATUS: {assignfunction: this.commonAssign, approvedFolders:{"uplink.decoded":true}, common: {role:"sensor.window"}}, + this.assign = { + OPEN: { + approvedFolders:{ + "uplink.decoded": { + assignfunction: this.commonAssign, + common: { + role: this.assignableRoles.sensor.main + } + } + }, + } }; /********************************************************************* @@ -338,7 +353,7 @@ class directorieshandlerClass { const common = { type: typeof obj[elementName], name: "", - role: "value", + role: "state", unit: obj[elementName]? obj[elementName].CommonStateUnit? obj[elementName].CommonStateUnit : "" : "", write: false }; @@ -383,10 +398,10 @@ class directorieshandlerClass { stateVal = this.assigns[elementName](stateVal,common); } - // Check for extended assigns - if(this.extendedAssign[elementName] && !options?.dontAssign){ - this.adapter.log.debug(`the state with the id ${objectId} will be assigned by internal extend-assign function`); - this.executeExtendedAssign(objectId,this.extendedAssign[elementName],{common:common}); + // Check for assign (new implemented function) + if(this.assign[elementName] && !options?.dontAssign){ + this.adapter.log.debug(`the state with the id ${objectId} will be assigned by internal assign function`); + stateVal = this.executeAssign(objectId,stateVal,this.assign[elementName],{common:common}); } common.read = common.role !== "button"; common.def = obj[elementName] || typeof obj[elementName] === "boolean" ? obj[elementName].stateCommonDef? obj[elementName].stateCommonDef: common.type === "boolean"? false : common.type === "number"? 0: "": common.type === "number"? 0: ""; diff --git a/main.js b/main.js index e8aa816..6abb0eb 100644 --- a/main.js +++ b/main.js @@ -119,16 +119,16 @@ class Lorawan extends utils.Adapter { //const message = {"deduplicationId":"4a91b00d-b5e1-4955-b085-ba21b9318213","time":"2024-01-26T20:18:45.299871+00:00","deviceInfo":{"tenantId":"52f14cd4-c6f1-4fbd-8f87-4025e1d49242","tenantName":"ChirpStack","applicationId":"59bcc5a7-59e2-4481-9615-fc4e58791915","applicationName":"Mclimate_Vicki","deviceProfileId":"3a9bc28f-3664-4bdf-b3be-a20d1eb32dc8","deviceProfileName":"Mclimate_Vicki","deviceName":"MClimate_Vicki_Heizkoerperventil_001","devEui":"70b3d52dd300ed31","deviceClassEnabled":"CLASS_A","tags":{}},"margin":7,"externalPowerSource":false,"batteryLevelUnavailable":false,"batteryLevel":85.826775}; // UP - //const topic = "application/59bcc5a7-59e2-4481-9615-fc4e58791915/device/70b3d52dd300ed31/event/up"; - //const message = {"deduplicationId":"c14f77c3-cfe5-42f3-9c43-651d3ca4cf45","time":"2024-01-27T12:00:05.267780+00:00","deviceInfo":{"tenantId":"52f14cd4-c6f1-4fbd-8f87-4025e1d49242","tenantName":"ChirpStack","applicationId":"59bcc5a7-59e2-4481-9615-fc4e58791915","applicationName":"Mclimate_Vicki","deviceProfileId":"3a9bc28f-3664-4bdf-b3be-a20d1eb32dc8","deviceProfileName":"Mclimate_Vicki","deviceName":"MClimate_Vicki_Heizkoerperventil_001","devEui":"70b3d52dd300ed31","deviceClassEnabled":"CLASS_A","tags":{}},"devAddr":"01343968","adr":true,"dr":5,"fCnt":11129,"fPort":2,"confirmed":false,"data":"gQyOiP39EdAw","object":{"sensorTemperature":20.06,"targetTemperature":12.0,"motorRange":509.0,"childLock":false,"batteryVoltage":3.3,"attachedBackplate":true,"lowMotorConsumption":false,"motorPosition":509.0,"reason":81.0,"highMotorConsumption":false,"calibrationFailed":false,"relativeHumidity":53.13,"perceiveAsOnline":true,"openWindow":false,"brokenSensor":false},"rxInfo":[{"gatewayId":"50313953530a4750","uplinkId":47105,"gwTime":"2024-01-27T12:00:05.267780+00:00","nsTime":"2024-01-27T12:00:05.314616473+00:00","rssi":-68,"snr":9.25,"channel":7,"location":{"latitude":53.55485739669679,"longitude":9.921609163284304},"context":"nnL1/A==","metadata":{"region_common_name":"EU868","region_config_id":"eu868"},"crcStatus":"CRC_OK"}],"txInfo":{"frequency":867900000,"modulation":{"lora":{"bandwidth":125000,"spreadingFactor":7,"codeRate":"CR_4_5"}}}}; + const topic = "application/e91e66ba-1aa7-4bdf-af88-f1246e0b8d75/device/a84041263188b787/event/up"; + const message = {"deduplicationId":"ce1ca35d-35c7-4f60-844c-c2b2810fd74b","time":"2024-08-25T07:10:47.758298+00:00","deviceInfo":{"tenantId":"52f14cd4-c6f1-4fbd-8f87-4025e1d49242","tenantName":"ChirpStack","applicationId":"e91e66ba-1aa7-4bdf-af88-f1246e0b8d75","applicationName":"Türen","deviceProfileId":"431c5895-68e2-478d-945f-f0e9a6f5f9f5","deviceProfileName":"Dragino Türsensoren / Fenstersensoren","deviceName":"Flurtüre","devEui":"a84041263188b787","deviceClassEnabled":"CLASS_A","tags":{}},"devAddr":"0061ebd4","adr":true,"dr":5,"fCnt":8264,"fPort":10,"confirmed":false,"data":"DAYBAA+IAAAAAA==","object":{"ALARM":0.0,"BAT_V":3.078,"CONTACT":true,"OPEN_TIMES":3976.0,"MOD":1.0,"LAST_OPEN_DURATION":0.0,"OPEN":false,"devicetype":"Dragino"},"rxInfo":[{"gatewayId":"503035416e314750","uplinkId":64001,"gwTime":"2024-08-25T07:10:47.758298+00:00","nsTime":"2024-08-25T07:11:29.787667701+00:00","rssi":-68,"snr":9.25,"channel":6,"location":{"latitude":50.69350130173554,"longitude":8.476821184158327},"context":"fp1WbA==","metadata":{"region_common_name":"EU868","region_config_id":"eu868"},"crcStatus":"CRC_OK"}],"txInfo":{"frequency":867700000,"modulation":{"lora":{"bandwidth":125000,"spreadingFactor":7,"codeRate":"CR_4_5"}}}}; // LOG //const topic = "application/59bcc5a7-59e2-4481-9615-fc4e58791915/device/70b3d52dd300ed31/event/up"; //const message = {"time":"2024-01-27T10:29:58.221817559+00:00","deviceInfo":{"tenantId":"52f14cd4-c6f1-4fbd-8f87-4025e1d49242","tenantName":"ChirpStack","applicationId":"59bcc5a7-59e2-4481-9615-fc4e58791915","applicationName":"Mclimate_Vicki","deviceProfileId":"3a9bc28f-3664-4bdf-b3be-a20d1eb32dc8","deviceProfileName":"Mclimate_Vicki","deviceName":"MClimate_Vicki_Heizkoerperventil_001","devEui":"70b3d52dd300ed31","deviceClassEnabled":"CLASS_A","tags":{}},"level":"ERROR","code":"UPLINK_CODEC","description":"Exception generated by quickjs","context":{"deduplication_id":"c44e7e25-09ce-4c95-b96f-5a298c5c6440"}}; // JOIN - const topic = "application/59bcc5a7-59e2-4481-9615-fc4e58791915/device/70b3d52dd300ed31/event/join"; - const message = {"deduplicationId":"44cef56d-1b8d-45fc-a762-03b98b620db2","time":"2023-12-12T03:13:21.551178+00:00","deviceInfo":{"tenantId":"52f14cd4-c6f1-4fbd-8f87-4025e1d49242","tenantName":"ChirpStack","applicationId":"59bcc5a7-59e2-4481-9615-fc4e58791915","applicationName":"Mclimate_Vicki","deviceProfileId":"3a9bc28f-3664-4bdf-b3be-a20d1eb32dc8","deviceProfileName":"Mclimate_Vicki","deviceName":"MClimate_Vicki_Heizkoerperventil_001","devEui":"70b3d52dd300ed31","deviceClassEnabled":"CLASS_A","tags":{}},"devAddr":"01009400"}; + //const topic = "application/59bcc5a7-59e2-4481-9615-fc4e58791915/device/70b3d52dd300ed31/event/join"; + //const message = {"deduplicationId":"44cef56d-1b8d-45fc-a762-03b98b620db2","time":"2023-12-12T03:13:21.551178+00:00","deviceInfo":{"tenantId":"52f14cd4-c6f1-4fbd-8f87-4025e1d49242","tenantName":"ChirpStack","applicationId":"59bcc5a7-59e2-4481-9615-fc4e58791915","applicationName":"Mclimate_Vicki","deviceProfileId":"3a9bc28f-3664-4bdf-b3be-a20d1eb32dc8","deviceProfileName":"Mclimate_Vicki","deviceName":"MClimate_Vicki_Heizkoerperventil_001","devEui":"70b3d52dd300ed31","deviceClassEnabled":"CLASS_A","tags":{}},"devAddr":"01009400"}; // DOWN //const topic = "application/59bcc5a7-59e2-4481-9615-fc4e58791915/device/70b3d52dd300ed31/command/down";