Skip to content

Commit

Permalink
change deviceInformations (keep old values in structure)
Browse files Browse the repository at this point in the history
  • Loading branch information
BenAhrdt committed May 28, 2024
1 parent 155529b commit e4a5f18
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ For now there is documentation in English here: https://wiki.hafenmeister.de
Placeholder for the next version (at the beginning of the line):
### **WORK IN PROGRESS**
-->
### **WORK IN PROGRESS**
* (BenAhrdt) change deviceInformations (keep old values in structure)

### 1.1.1 (2024-05-15)
* (BenAhrdt) mqtt dependencies updated for better keepalive

Expand Down
56 changes: 49 additions & 7 deletions lib/modules/messagehandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,49 @@ class messagehandlerClass {
try{
const adapterObjectsAtStart = await this.adapter.getAdapterObjectsAsync();
// Generate Infos of all devices and decoded folders
const decodedData = {};
for(const adapterObject of Object.values(adapterObjectsAtStart)){
if(adapterObject._id.endsWith(`${this.directoryhandler.reachableSubfolders.uplinkRaw}.json`)){
const uplinkState = await this.adapter.getStateAsync(adapterObject._id);
await this.assignDeviceInformation(adapterObject._id,JSON.parse(uplinkState.val));
if(adapterObject._id.indexOf(`${this.directoryhandler.reachableSubfolders.uplinkDecoded}.`) !== -1){
const decodedState = await this.adapter.getStateAsync(adapterObject._id);
const changeInfo = await this.adapter.getChangeInfo(adapterObject._id);
if(!decodedData[changeInfo.deviceEUI]){
decodedData[changeInfo.deviceEUI] ={};
decodedData[changeInfo.deviceEUI].decoded = {};
decodedData[changeInfo.deviceEUI].id = adapterObject._id;
}
decodedData[changeInfo.deviceEUI].decoded[changeInfo.changedState] = decodedState.val;
}
if(adapterObject._id.endsWith(`rx_metadata.0.time`) || adapterObject._id.endsWith(`rxInfo.0.nsTime`)){
const timeData = await this.adapter.getStateAsync(adapterObject._id);
const changeInfo = await this.adapter.getChangeInfo(adapterObject._id);
if(!decodedData[changeInfo.deviceEUI]){
decodedData[changeInfo.deviceEUI] ={};
decodedData[changeInfo.deviceEUI].decoded = {};
decodedData[changeInfo.deviceEUI].id = adapterObject._id;
}
decodedData[changeInfo.deviceEUI].time = timeData.val;
}
}

// Check all devEUI and convert
for(const devEUI in decodedData){
// Check origin
switch(this.adapter.config.origin){
case this.adapter.origin.ttn:
decodedData[devEUI].uplink_message = {};
decodedData[devEUI].uplink_message.decoded_payload = decodedData[devEUI].decoded;
decodedData[devEUI].uplink_message.rx_metadata = [];
decodedData[devEUI].uplink_message.rx_metadata[0] = {};
decodedData[devEUI].uplink_message.rx_metadata[0].time = decodedData[devEUI].time;
break;
case this.adapter.origin.chirpstack:
decodedData[devEUI].object = decodedData[devEUI].decoded;
decodedData[devEUI].uplink_message.rxInfo = [];
decodedData[devEUI].uplink_message.rxInfo[0] = {};
decodedData[devEUI].uplink_message.rxInfo[0].nsTime = decodedData[devEUI].time;
break;
}
this.assignDeviceInformation(decodedData[devEUI].id,decodedData[devEUI]);
}
}
catch(error){
Expand Down Expand Up @@ -165,14 +203,18 @@ class messagehandlerClass {
// Check origin
switch(this.adapter.config.origin){
case this.adapter.origin.ttn:
if(message.uplink_message && message.uplink_message){
this.deviceinformations[changeInfo.deviceEUI].uplink.decoded = message.uplink_message.decoded_payload;
if(message.uplink_message && message.uplink_message.rx_metadata[0]){
for(const attr in message.uplink_message.decoded_payload){
this.deviceinformations[changeInfo.deviceEUI].uplink.decoded[attr] = message.uplink_message.decoded_payload[attr];
}
this.deviceinformations[changeInfo.deviceEUI].uplink.time = message.uplink_message.rx_metadata[0].time;
}
break;
case this.adapter.origin.chirpstack:
if(message.object){
this.deviceinformations[changeInfo.deviceEUI].uplink.decoded = message.object;
if(message.object && message.rxInfo[0].nsTime){
for(const attr in message.object){
this.deviceinformations[changeInfo.deviceEUI].uplink.decoded[attr] = message.object[attr];
}
this.deviceinformations[changeInfo.deviceEUI].uplink.time = message.rxInfo[0].nsTime;
}
break;
Expand Down

0 comments on commit e4a5f18

Please sign in to comment.