Skip to content

Commit

Permalink
change concept of assigning roles, values and writecommands
Browse files Browse the repository at this point in the history
  • Loading branch information
BenAhrdt committed Mar 2, 2024
1 parent c855eba commit c178d0d
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 119 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ For now there is documentation in English here: http://www.hafenmeister.com/Lora
Placeholder for the next version (at the beginning of the line):
### **WORK IN PROGRESS**
-->
### **WORK IN PROGRESS**
* (BenAhrdt) change concept of assigning roles, values and writecommands

### 0.5.5 (2024-03-01)
* (BenAhrdt) first step of handling with date

Expand Down
8 changes: 0 additions & 8 deletions lib/modules/commands/write.json

This file was deleted.

127 changes: 49 additions & 78 deletions lib/modules/directorieshandler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const { isDeepStrictEqual } = require("util");
const fs = require("fs");

class directorieshandlerClass {
constructor(adapter) {
Expand Down Expand Up @@ -29,33 +28,61 @@ class directorieshandlerClass {
joinRaw: "join.raw"
};

// Assign definitions for special states
this.assigns = {
time: (value,common)=>{
return this.timestringToDate(value,common);
},
gwTime: (value,common)=>{
return this.timestringToDate(value,common);
},
nsTime: (value,common)=>{
return this.timestringToDate(value,common);
},
timestamp: (value,common)=>{
return this.timestringToDate(value,common);
/*********************************************************************
* ********************* Definition of Assigns **********************
* ******************************************************************/

// Definition of Assign timestamps
this.timestringToDate = (value,common) =>{
common.role = "date";
if(typeof value === "string"){
common.type = "number";
return new Date(value).valueOf();
}
else{
return value;
}
};

//define path for uplink roles
this.uplinkRolesPath = "/lib/modules/roles/uplinks.json";
// Assign definitions for special states
this.assigns = {
time: this.timestringToDate,
gwTime: this.timestringToDate,
nsTime: this.timestringToDate,
timestamp: this.timestringToDate
};

// get roles
this.uplinkRoles = this.getJsonFromDirectoryfile(`${this.adapter.adapterDir}${this.uplinkRolesPath}`);
/*********************************************************************
* ************************** writecommands *************************
* ******************************************************************/

this.executeWritecommand = async (elementName,sourceId,value) =>{
// Check, if there is a writetrigger and the stateval is not euqal its releaseVal(still present)
const writecommand = this.writeCommand[elementName];
if(writecommand && writecommand.releaseValue !== value){
const baseInfo = this.adapter.getBaseDeviceInfo(sourceId);
const subfolder = sourceId.slice(baseInfo.objectStartDirectory.length + 1,sourceId.length - baseInfo.changedState.length - 1);
if(!writecommand.approvedFolders || writecommand.approvedFolders[subfolder]){
const destinationId = `${baseInfo.objectStartDirectory}.${writecommand.destination}`;
const destinationState = await this.adapter.getStateAsync(destinationId);
if(destinationState?.val === writecommand.releaseValue){
this.adapter.log.debug(`writetrigger ${elementName} recogniced. value ${value} will be written into ${this.writeCommand[elementName].destination}`);
await this.adapter.setStateAsync(destinationId,value);
}
}
}
};

//define path for devicetype difinitions
this.writeCommandPath = "/lib/modules/commands/write.json";
this.writeCommandDeviceType = {destination:"configuration.devicetype",releaseValue:"",approvedFolders:{"uplink.decoded":true}};

// get devicetype definitions
this.writeCommand = this.getJsonFromDirectoryfile(`${this.adapter.adapterDir}${this.writeCommandPath}`);
this.writeCommand = {
Devicetype: this.writeCommandDeviceType,
devicetype: this.writeCommandDeviceType,
Device: this.writeCommandDeviceType,
device: this.writeCommandDeviceType,
Hardware_mode: this.writeCommandDeviceType,
Hardware_Mode: this.writeCommandDeviceType
};

// declare the directory structre
this.directories = {
Expand Down Expand Up @@ -137,38 +164,6 @@ class directorieshandlerClass {
};
}

/*********************************************************************
* *************************** General ******************************
* ******************************************************************/

// Caclculation of timestring into number (role = date)
timestringToDate(value,common){
common.role = "date";
common.type = "number";
if(typeof value === "string"){
return new Date(value).valueOf();
}
else{
return value;
}
}

/*********************************************************************
* **************************** Roles ********************************
* ******************************************************************/

getJsonFromDirectoryfile(file){
const activeFunction = "getJsonFromDirectoryfiles";
this.adapter.log.silly(`the file ${file} will be readout`);
try{
return JSON.parse(fs.readFileSync(file, "utf-8"));
}
catch(error){
this.adapter.log.error(`error at ${activeFunction}: ` + error);
return undefined;
}
}

/*********************************************************************
* ************************ Objectstring *****************************
* ******************************************************************/
Expand Down Expand Up @@ -296,10 +291,6 @@ class directorieshandlerClass {
objectId = `${startDirectory}.${internalObjectId}`;
}
}
// Check for roles
/*if(this.uplinkRoles[elementName]){
common.role = this.uplinkRoles[elementName];
}*/
// Check for id dont starts with "."
if(objectId.indexOf(".") === 0){
objectId.substring(1,objectId.length);
Expand Down Expand Up @@ -333,26 +324,6 @@ class directorieshandlerClass {
}
}

/*********************************************************************
* ************************* writecommand ****************************
* ******************************************************************/

async executeWritecommand(writecommand,sourceId,value){
// Check, if there is a writetrigger and the stateval is not euqal its releaseVal(still present)
if(this.writeCommand[writecommand] && this.writeCommand[writecommand].releaseValue !== value){
const baseInfo = this.adapter.getBaseDeviceInfo(sourceId);
const subfolder = sourceId.slice(baseInfo.objectStartDirectory.length + 1,sourceId.length - baseInfo.changedState.length - 1);
if(!this.writeCommand[writecommand].approvedFolders || this.writeCommand[writecommand].approvedFolders[subfolder]){
const destinationId = `${baseInfo.objectStartDirectory}.${this.writeCommand[writecommand].destination}`;
const destinationState = await this.adapter.getStateAsync(destinationId);
if(destinationState?.val === this.writeCommand[writecommand].releaseValue){
this.adapter.log.debug(`writetrigger ${writecommand} recogniced. value ${value} will be written into ${this.writeCommand[writecommand].destination}`);
await this.adapter.setStateAsync(destinationId,value);
}
}
}
}

/*********************************************************************
* ************************** Attribute ******************************
* ******************************************************************/
Expand Down
40 changes: 20 additions & 20 deletions lib/modules/downlinkConfighandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,26 @@ class downlinkConfighandlerClass {
};

this.downlinkParameterAttributs = {
"name": "",
"port": 1,
"priority": "NORMAL",
"type": "number",
"confirmed": false,
"front": "",
"end": "",
"lengthInByte": 3,
"on": "",
"off": "",
"onClick": "",
"multiplyfaktor": 1,
"unit": "",
"crc": "noCrc",
"limitMin": false,
"limitMinValue": 0,
"limitMax": false,
"limitMaxValue": 0,
"swap": false,
"decimalPlaces": 0
name: "",
port: 1,
priority: "NORMAL",
type: "number",
confirmed: false,
front: "",
end: "",
lengthInByte: 3,
on: "",
off: "",
onClick: "",
multiplyfaktor: 1,
unit: "",
crc: "noCrc",
limitMin: false,
limitMinValue: 0,
limitMax: false,
limitMaxValue: 0,
swap: false,
decimalPlaces: 0
};
}

Expand Down
15 changes: 10 additions & 5 deletions lib/modules/messagehandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ class messagehandlerClass {
this.adapter = adapter;
this.directoryhandler = new directorieshandlerClass(this.adapter);

//define path for uplink roles
this.downlinkRolesPath = "/lib/modules/roles/downlinks.json";

// get roles
this.downlinkRoles = this.directoryhandler.getJsonFromDirectoryfile(`${this.adapter.adapterDir}${this.downlinkRolesPath}`);
/*********************************************************************
* *************************** Roles ********************************
* ******************************************************************/

// Assign definitions for special states
this.downlinkRoles = {
button: "button",
boolean: "switch",
json: "json"
};
}


Expand Down
5 changes: 0 additions & 5 deletions lib/modules/roles/downlinks.json

This file was deleted.

3 changes: 0 additions & 3 deletions lib/modules/roles/uplinks.json

This file was deleted.

0 comments on commit c178d0d

Please sign in to comment.