Skip to content

Commit

Permalink
change time states from string to number and role date
Browse files Browse the repository at this point in the history
  • Loading branch information
BenAhrdt committed Mar 1, 2024
1 parent bf378b1 commit cecb94f
Showing 1 changed file with 53 additions and 20 deletions.
73 changes: 53 additions & 20 deletions lib/modules/directorieshandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@ 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);
}
};

//define path for uplink roles
this.uplinkRolesPath = "/lib/modules/roles/uplinks.json";

Expand Down Expand Up @@ -125,6 +141,18 @@ 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 ********************************
* ******************************************************************/
Expand Down Expand Up @@ -241,45 +269,50 @@ class directorieshandlerClass {
await this.generateRekursivObjects(obj[elementName],objectId,topic,message);
}
else{
let stateCommonType = typeof obj[elementName];
let stateCommonName = "";
let stateCommonWrite = false;
let stateCommonRole = "value";
const common = {
type: typeof obj[elementName],
name: "",
role: "value",
unit: obj[elementName]? obj[elementName].CommonStateUnit? obj[elementName].CommonStateUnit : "" : "",
write: false
};
//let stateCommonType = typeof obj[elementName];
//let stateCommonName = "";
//let stateCommonWrite = false;
//let stateCommonRole = "value";
let stateVal = obj[elementName];
let objectId = `${startDirectory}.${elementName}`;
let internalObjectId = elementName;
if(obj[elementName]){
if(obj[elementName].isState){
stateVal = obj[elementName].stateVal !== undefined? obj[elementName].stateVal: undefined;
stateCommonType = obj[elementName].stateCommonType? obj[elementName].stateCommonType : typeof stateVal;
stateCommonName = obj[elementName].stateCommonName ? obj[elementName].stateCommonName : stateCommonName;
stateCommonWrite = obj[elementName].stateCommonWrite ? obj[elementName].stateCommonWrite : stateCommonWrite;
stateCommonRole = obj[elementName].stateCommonRole ? obj[elementName].stateCommonRole : stateCommonRole;
common.type = obj[elementName].stateCommonType? obj[elementName].stateCommonType : typeof stateVal;
common.name = obj[elementName].stateCommonName ? obj[elementName].stateCommonName : common.name;
common.write = obj[elementName].stateCommonWrite ? obj[elementName].stateCommonWrite : common.write;
common.role = obj[elementName].stateCommonRole ? obj[elementName].stateCommonRole : common.role;
}
if(obj[elementName].objectId){
internalObjectId = `${obj[elementName].objectId(topic,message)}`;
objectId = `${startDirectory}.${internalObjectId}`;
}
}
// Check for roles
if(this.uplinkRoles[elementName]){
stateCommonRole = this.uplinkRoles[elementName];
}
/*if(this.uplinkRoles[elementName]){
common.role = this.uplinkRoles[elementName];
}*/
// Check for id dont starts with "."
if(objectId.indexOf(".") === 0){
objectId.substring(1,objectId.length);
}
// Check for an assign definition (calculation and / or role assignment)
if(this.assigns[elementName]){
stateVal = this.assigns[elementName](stateVal,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: "";
await this.adapter.setObjectNotExistsAsync(objectId,{
type: "state",
common: {
type: stateCommonType !== undefined? stateCommonType!== "object"? stateCommonType: "mixed": "mixed",
name: stateCommonName,
role: stateCommonRole,
read: stateCommonRole !== "button",
unit: obj[elementName]? obj[elementName].CommonStateUnit? obj[elementName].CommonStateUnit : "" : "",
def: obj[elementName] || typeof obj[elementName] === "boolean" ? obj[elementName].stateCommonDef? obj[elementName].stateCommonDef: stateCommonType === "boolean"? false : stateCommonType === "number"? 0: "": stateCommonType === "number"? 0: "",
write: stateCommonWrite
},
common: common,
native: {},
});
if(typeof stateVal === "object"){
Expand Down

0 comments on commit cecb94f

Please sign in to comment.