Skip to content

Commit

Permalink
first try of valueparsing
Browse files Browse the repository at this point in the history
  • Loading branch information
BenAhrdt committed Jan 7, 2024
1 parent 96eea0b commit 5632154
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 85 deletions.
37 changes: 28 additions & 9 deletions admin/jsonConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,34 @@
"i18n": true,
"type": "panel",
"items": {
"option1": {
"type": "text",
"label": "option1",
"newLine": true
},
"option2": {
"type": "text",
"label": "option2",
"newLine": true
"statesTable":{
"type":"table",
"sm":12,
"items":[
{
"type": "text",
"attr": "application",
"title": "application",
"tooltip": "applicationTooltip",
"default": "",
"sm":12
},{
"type": "text",
"attr": "username",
"title": "username",
"tooltip": "usernameTooltip",
"default": "",
"sm":12
},
{
"type": "password",
"attr": "password",
"title": "password",
"tooltip": "passwordTooltip",
"default": "",
"sm":12
}
]
}
}
}
3 changes: 1 addition & 2 deletions io-package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@
]
},
"native": {
"option1": "",
"option2": ""
"statesTable": []
},
"objects": [],
"instanceObjects": [
Expand Down
90 changes: 90 additions & 0 deletions lib/modules/messagehandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
class messagehandlerClass {
constructor(adapter) {
this.adapter = adapter;
this.level = {
application:{
name: "Application",
commonName: "Application",
type: "channel"
},
devices:{
name: "Devices",
commonName: "Devices",
type: "folder"
},
device:{
name: "Device",
type: "device"
}
};

this.units = {
"pressure": "mBar",
"battery": "V",
"battery_level": "%",
"humidity": "%",
"Batterie": "V",
"Temperatur": "°C",
"airhumidity": "%",
"volt": "V",
"temperatur" : "°C",
"airtemperature" : "°C"
};
}

async handleMessage(application,topic,message){
message = JSON.parse(message);
//const stateId = this.generateDeviceString(message.end_device_ids);
// Generate internal folder for the smoothed values values
try{
for(const level of Object.values(this.level)){
// @ts-ignore
const objectId = this.gernerateObjectString(message.end_device_ids,level.name);
await this.adapter.setObjectNotExistsAsync(objectId,{
// @ts-ignore
type: level.type,
common: {
// @ts-ignore
name: level.commonName ? level.commonName : `addr. ${message.end_device_ids.dev_addr}`
},
native : {},
});
}
for(const endpoint in message["uplink_message"]["decoded_payload"]){
const stateId = this.gernerateObjectString(message.end_device_ids,this.level.device.name);
// @ts-ignore
await this.adapter.setObjectNotExistsAsync(`${stateId}.${endpoint}`,{
type: "state",
common: {
name: "",
type: "mixed",
role: "value",
unit: this.units[endpoint] ? this.units[endpoint] : undefined,
read: true,
write: false
},
native: {},
});
await this.adapter.setStateAsync(`${stateId}.${endpoint}`,message["uplink_message"]["decoded_payload"][endpoint],true);
}
}
catch(error){
this.adapter.log.warn("check: " + error);
}
}

gernerateObjectString(end_device_ids,resolvetype){
switch(resolvetype){
case this.level.application.name:
return end_device_ids.application_ids.application_id;

case this.level.devices.name:
return `${end_device_ids.application_ids.application_id}.devices`;

case this.level.device.name:
return `${end_device_ids.application_ids.application_id}.devices.${end_device_ids.dev_eui}`;
}
}
}

module.exports = messagehandlerClass;
8 changes: 4 additions & 4 deletions lib/modules/mqttclient.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const mqtt = require("mqtt");

class mqttClientClass {
constructor(adapter, ip, port, username, password) {
constructor(adapter, ip, port, connectionSettings) {
this.adapter = adapter;
this.url = "mqtts";
this.client = mqtt.connect(`${this.url}://${ip}`, {
port: port,
username: username,
password: password,
username: connectionSettings.username,
password: connectionSettings.password,
clientId: `iobroker_${this.adapter.namespace}`,
});
this.client.on("connect", () => {
Expand Down Expand Up @@ -57,7 +57,7 @@ class mqttClientClass {
}
}
this.adapter.log.debug(`${topic}: ${type} - ${typeof value == "object" ? JSON.stringify(value) : value}`);*/
await this.adapter.handleMessage(topic, message);
await this.adapter.handleMessage(connectionSettings.application, topic, message);
});
}
async publish(topic, message, opt) {
Expand Down
77 changes: 7 additions & 70 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// you need to create an adapter
const utils = require("@iobroker/adapter-core");
const mqttClientClass = require("./lib/modules/mqttclient");
const messagehandlerClass = require("./lib/modules/messagehandler");

// Load your modules here, e.g.:
// const fs = require("fs");
Expand Down Expand Up @@ -36,89 +37,25 @@ class Lorawan extends utils.Adapter {
async onReady() {
// Initialize your adapter here
// declare mqtt CLient
this.mqttClient = new mqttClientClass(this,"eu1.cloud.thethings.network","8883",this.config.option1,this.config.option2);
/* this.mqttClient = new mqttClientClass(this,"192.168.2.56","1883","","");
// @ts-ignore
this.messagehandler = new messagehandlerClass(this);
this.mqttClient = new mqttClientClass(this,"eu1.cloud.thethings.network","8883",this.config.statesTable[0]);
/* this.mqttClient = new mqttClientClass(this,"192.168.2.56","1883","","");
setTimeout(() => {
this.mqttClient?.publish("R/c0619ab24727/keepalive",null);
}, 1000);*/
// Reset the connection indicator during startup
this.setState("info.connection", false, true);

// The adapters config (in the instance object everything under the attribute "native") is accessible via
// this.config:
this.log.info("config option1: " + this.config.option1);
this.log.info("config option2: " + this.config.option2);

}

async handleMessage(topic,value){
value = JSON.parse(value);
const stateId = this.generateDeviceString(value.end_device_ids);
// Generate internal folder for the smoothed values values
await this.setObjectNotExistsAsync(this.gernerateObjectString(value.end_device_ids,"application"),{
"type": "folder",
"common": {
"name": "application"
},
native : {},
});
await this.setObjectNotExistsAsync(this.gernerateObjectString(value.end_device_ids,"devices"),{
"type": "channel",
"common": {
"name": "devices"
},
native : {},
});
await this.setObjectNotExistsAsync(this.gernerateObjectString(value.end_device_ids,"device"),{
"type": "device",
"common": {
"name": `addr. ${value.end_device_ids.dev_addr}`
},
native : {},
});
try{
for(const endpoint in value["uplink_message"]["decoded_payload"]){
// @ts-ignore
await this.setObjectNotExistsAsync(`${stateId}.${endpoint}`,{
type: "state",
common: {
name: "",
type: "number",
role: "value",
read: true,
write: false
},
native: {},
});
await this.setStateAsync(`${this.gernerateObjectString(value.end_device_ids,"device")}.${endpoint}`,JSON.stringify(value["uplink_message"]["decoded_payload"][endpoint]),true);
}
}
catch(e){
this.log.warn(e);
}
async handleMessage(application,topic,message){
this.messagehandler?.handleMessage(application,topic,message);
}

gernerateObjectString(end_device_ids,resolvetype){
switch(resolvetype){
case "application":
return end_device_ids.application_ids.application_id;

case "devices":
return `${end_device_ids.application_ids.application_id}.devices`;

case "device":
return `${end_device_ids.application_ids.application_id}.devices.${end_device_ids.dev_eui}`;
}
}

generateDeviceString(end_device_ids){
this.log.debug(JSON.stringify(end_device_ids));
return `${end_device_ids.application_ids.application_id}.devices.${end_device_ids.dev_eui}`;
}

generateStateString(topic){
return topic.replace(/\//g, ".");
}
/**
* Is called when adapter shuts down - callback has to be called under any circumstances!
* @param {() => void} callback
Expand Down

0 comments on commit 5632154

Please sign in to comment.