Skip to content

Commit 5632154

Browse files
author
BenAhrdt
committed
first try of valueparsing
1 parent 96eea0b commit 5632154

File tree

5 files changed

+130
-85
lines changed

5 files changed

+130
-85
lines changed

admin/jsonConfig.json

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,34 @@
22
"i18n": true,
33
"type": "panel",
44
"items": {
5-
"option1": {
6-
"type": "text",
7-
"label": "option1",
8-
"newLine": true
9-
},
10-
"option2": {
11-
"type": "text",
12-
"label": "option2",
13-
"newLine": true
5+
"statesTable":{
6+
"type":"table",
7+
"sm":12,
8+
"items":[
9+
{
10+
"type": "text",
11+
"attr": "application",
12+
"title": "application",
13+
"tooltip": "applicationTooltip",
14+
"default": "",
15+
"sm":12
16+
},{
17+
"type": "text",
18+
"attr": "username",
19+
"title": "username",
20+
"tooltip": "usernameTooltip",
21+
"default": "",
22+
"sm":12
23+
},
24+
{
25+
"type": "password",
26+
"attr": "password",
27+
"title": "password",
28+
"tooltip": "passwordTooltip",
29+
"default": "",
30+
"sm":12
31+
}
32+
]
1433
}
1534
}
1635
}

io-package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@
7979
]
8080
},
8181
"native": {
82-
"option1": "",
83-
"option2": ""
82+
"statesTable": []
8483
},
8584
"objects": [],
8685
"instanceObjects": [

lib/modules/messagehandler.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
class messagehandlerClass {
2+
constructor(adapter) {
3+
this.adapter = adapter;
4+
this.level = {
5+
application:{
6+
name: "Application",
7+
commonName: "Application",
8+
type: "channel"
9+
},
10+
devices:{
11+
name: "Devices",
12+
commonName: "Devices",
13+
type: "folder"
14+
},
15+
device:{
16+
name: "Device",
17+
type: "device"
18+
}
19+
};
20+
21+
this.units = {
22+
"pressure": "mBar",
23+
"battery": "V",
24+
"battery_level": "%",
25+
"humidity": "%",
26+
"Batterie": "V",
27+
"Temperatur": "°C",
28+
"airhumidity": "%",
29+
"volt": "V",
30+
"temperatur" : "°C",
31+
"airtemperature" : "°C"
32+
};
33+
}
34+
35+
async handleMessage(application,topic,message){
36+
message = JSON.parse(message);
37+
//const stateId = this.generateDeviceString(message.end_device_ids);
38+
// Generate internal folder for the smoothed values values
39+
try{
40+
for(const level of Object.values(this.level)){
41+
// @ts-ignore
42+
const objectId = this.gernerateObjectString(message.end_device_ids,level.name);
43+
await this.adapter.setObjectNotExistsAsync(objectId,{
44+
// @ts-ignore
45+
type: level.type,
46+
common: {
47+
// @ts-ignore
48+
name: level.commonName ? level.commonName : `addr. ${message.end_device_ids.dev_addr}`
49+
},
50+
native : {},
51+
});
52+
}
53+
for(const endpoint in message["uplink_message"]["decoded_payload"]){
54+
const stateId = this.gernerateObjectString(message.end_device_ids,this.level.device.name);
55+
// @ts-ignore
56+
await this.adapter.setObjectNotExistsAsync(`${stateId}.${endpoint}`,{
57+
type: "state",
58+
common: {
59+
name: "",
60+
type: "mixed",
61+
role: "value",
62+
unit: this.units[endpoint] ? this.units[endpoint] : undefined,
63+
read: true,
64+
write: false
65+
},
66+
native: {},
67+
});
68+
await this.adapter.setStateAsync(`${stateId}.${endpoint}`,message["uplink_message"]["decoded_payload"][endpoint],true);
69+
}
70+
}
71+
catch(error){
72+
this.adapter.log.warn("check: " + error);
73+
}
74+
}
75+
76+
gernerateObjectString(end_device_ids,resolvetype){
77+
switch(resolvetype){
78+
case this.level.application.name:
79+
return end_device_ids.application_ids.application_id;
80+
81+
case this.level.devices.name:
82+
return `${end_device_ids.application_ids.application_id}.devices`;
83+
84+
case this.level.device.name:
85+
return `${end_device_ids.application_ids.application_id}.devices.${end_device_ids.dev_eui}`;
86+
}
87+
}
88+
}
89+
90+
module.exports = messagehandlerClass;

lib/modules/mqttclient.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
const mqtt = require("mqtt");
22

33
class mqttClientClass {
4-
constructor(adapter, ip, port, username, password) {
4+
constructor(adapter, ip, port, connectionSettings) {
55
this.adapter = adapter;
66
this.url = "mqtts";
77
this.client = mqtt.connect(`${this.url}://${ip}`, {
88
port: port,
9-
username: username,
10-
password: password,
9+
username: connectionSettings.username,
10+
password: connectionSettings.password,
1111
clientId: `iobroker_${this.adapter.namespace}`,
1212
});
1313
this.client.on("connect", () => {
@@ -57,7 +57,7 @@ class mqttClientClass {
5757
}
5858
}
5959
this.adapter.log.debug(`${topic}: ${type} - ${typeof value == "object" ? JSON.stringify(value) : value}`);*/
60-
await this.adapter.handleMessage(topic, message);
60+
await this.adapter.handleMessage(connectionSettings.application, topic, message);
6161
});
6262
}
6363
async publish(topic, message, opt) {

main.js

Lines changed: 7 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// you need to create an adapter
99
const utils = require("@iobroker/adapter-core");
1010
const mqttClientClass = require("./lib/modules/mqttclient");
11+
const messagehandlerClass = require("./lib/modules/messagehandler");
1112

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

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

5251
}
5352

54-
async handleMessage(topic,value){
55-
value = JSON.parse(value);
56-
const stateId = this.generateDeviceString(value.end_device_ids);
57-
// Generate internal folder for the smoothed values values
58-
await this.setObjectNotExistsAsync(this.gernerateObjectString(value.end_device_ids,"application"),{
59-
"type": "folder",
60-
"common": {
61-
"name": "application"
62-
},
63-
native : {},
64-
});
65-
await this.setObjectNotExistsAsync(this.gernerateObjectString(value.end_device_ids,"devices"),{
66-
"type": "channel",
67-
"common": {
68-
"name": "devices"
69-
},
70-
native : {},
71-
});
72-
await this.setObjectNotExistsAsync(this.gernerateObjectString(value.end_device_ids,"device"),{
73-
"type": "device",
74-
"common": {
75-
"name": `addr. ${value.end_device_ids.dev_addr}`
76-
},
77-
native : {},
78-
});
79-
try{
80-
for(const endpoint in value["uplink_message"]["decoded_payload"]){
81-
// @ts-ignore
82-
await this.setObjectNotExistsAsync(`${stateId}.${endpoint}`,{
83-
type: "state",
84-
common: {
85-
name: "",
86-
type: "number",
87-
role: "value",
88-
read: true,
89-
write: false
90-
},
91-
native: {},
92-
});
93-
await this.setStateAsync(`${this.gernerateObjectString(value.end_device_ids,"device")}.${endpoint}`,JSON.stringify(value["uplink_message"]["decoded_payload"][endpoint]),true);
94-
}
95-
}
96-
catch(e){
97-
this.log.warn(e);
98-
}
53+
async handleMessage(application,topic,message){
54+
this.messagehandler?.handleMessage(application,topic,message);
9955
}
10056

101-
gernerateObjectString(end_device_ids,resolvetype){
102-
switch(resolvetype){
103-
case "application":
104-
return end_device_ids.application_ids.application_id;
10557

106-
case "devices":
107-
return `${end_device_ids.application_ids.application_id}.devices`;
10858

109-
case "device":
110-
return `${end_device_ids.application_ids.application_id}.devices.${end_device_ids.dev_eui}`;
111-
}
112-
}
113-
114-
generateDeviceString(end_device_ids){
115-
this.log.debug(JSON.stringify(end_device_ids));
116-
return `${end_device_ids.application_ids.application_id}.devices.${end_device_ids.dev_eui}`;
117-
}
118-
119-
generateStateString(topic){
120-
return topic.replace(/\//g, ".");
121-
}
12259
/**
12360
* Is called when adapter shuts down - callback has to be called under any circumstances!
12461
* @param {() => void} callback

0 commit comments

Comments
 (0)