-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
MyHomeMyData
committed
Dec 1, 2023
1 parent
7d10e91
commit 8a4d7c5
Showing
3 changed files
with
116 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
|
||
const didsDict = require('./didsE3').dids; | ||
|
||
class storage { | ||
constructor(config) { | ||
this.config = config; | ||
} | ||
async initStates(ctx) { | ||
if (this.config.doJson) { | ||
await ctx.setObjectNotExistsAsync(this.config.stateBase+'.json', { | ||
type: 'channel', | ||
common: { | ||
name: this.config.stateBase+' JSON', | ||
role: 'device' | ||
}, | ||
native: {}, | ||
}); | ||
} | ||
|
||
if (this.config.doTree) { | ||
await ctx.setObjectNotExistsAsync(this.config.stateBase+'.tree', { | ||
type: 'channel', | ||
common: { | ||
name: this.config.stateBase+' TREE', | ||
role: 'device' | ||
}, | ||
native: {}, | ||
}); | ||
} | ||
} | ||
async decodeDataCAN(ctx, did, data) { | ||
async function storeObjectJson(ctx, stateId, obj) { | ||
await ctx.setObjectNotExistsAsync(stateId, { | ||
type: 'state', | ||
common: { | ||
name: idStr, | ||
type: 'string', | ||
role: 'state', | ||
read: true, | ||
write: true, | ||
}, | ||
native: {}, | ||
}); | ||
await ctx.setStateAsync(stateId, JSON.stringify(obj), true); | ||
} | ||
async function storeObjectTree(ctx, stateId, obj) { | ||
if (typeof(obj) == 'object') { | ||
for (const [key, itm] of Object.entries(obj)) { | ||
storeObjectTree(ctx, String(stateId)+'.'+String(key),itm); | ||
} | ||
} else { | ||
//ctx.log.debug(String(stateId)+': '+JSON.stringify(obj)); | ||
storeObjectJson(ctx, stateId, obj); | ||
} | ||
} | ||
const idStr = didsDict[this.config.device][did].id; | ||
const val = didsDict[this.config.device][did].decode(data); | ||
const didStr = '000'+String(did); | ||
const stateIdJson = this.config.stateBase+'.json.'+didStr.slice(-4)+'_'+idStr; | ||
const stateIdTree = this.config.stateBase+'.tree.'+didStr.slice(-4)+'_'+idStr; | ||
if (this.config.doTree) { storeObjectTree(ctx, stateIdTree, val); } | ||
if (this.config.doJson) { storeObjectJson(ctx, stateIdJson, val); } | ||
} | ||
|
||
} | ||
|
||
module.exports = { | ||
storage | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters