diff --git a/lib/modules/directorieshandler.js b/lib/modules/directorieshandler.js index 4980f27..b290f0c 100644 --- a/lib/modules/directorieshandler.js +++ b/lib/modules/directorieshandler.js @@ -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"; @@ -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 ******************************** * ******************************************************************/ @@ -241,20 +269,27 @@ 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)}`; @@ -262,24 +297,22 @@ class directorieshandlerClass { } } // 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"){