iobroker.lorawan 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/main.js ADDED
@@ -0,0 +1,208 @@
1
+ "use strict";
2
+
3
+
4
+ /*
5
+ * Created with @iobroker/create-adapter v2.6.0
6
+ */
7
+
8
+ // The adapter-core module gives you access to the core ioBroker functions
9
+ // you need to create an adapter
10
+ const utils = require("@iobroker/adapter-core");
11
+ const mqttClientClass = require("./lib/modules/mqttclient");
12
+ const messagehandlerClass = require("./lib/modules/messagehandler");
13
+
14
+ // Load your modules here, e.g.:
15
+ // const fs = require("fs");
16
+
17
+ class Lorawan extends utils.Adapter {
18
+
19
+ /**
20
+ * @param {Partial<utils.AdapterOptions>} [options={}]
21
+ */
22
+ constructor(options) {
23
+ super({
24
+ ...options,
25
+ name: "lorawan",
26
+ });
27
+ this.on("ready", this.onReady.bind(this));
28
+ this.on("stateChange", this.onStateChange.bind(this));
29
+ // this.on("objectChange", this.onObjectChange.bind(this));
30
+ // this.on("message", this.onMessage.bind(this));
31
+ this.on("unload", this.onUnload.bind(this));
32
+ this.mqttClient = {};
33
+ this.changeInfo = {};
34
+ }
35
+
36
+ /**
37
+ * Is called when databases are connected and adapter received configuration.
38
+ */
39
+ async onReady() {
40
+ /*
41
+ let a = {b:"",c:"2"};
42
+ a.val = JSON.parse(JSON.stringify(a));
43
+ this.log.debug(JSON.stringify(a));
44
+ delete a.val;
45
+ this.log.debug(JSON.stringify(a));
46
+ return;*/
47
+ // create new messagehandler
48
+ this.messagehandler = new messagehandlerClass(this);
49
+
50
+ // Set all mqtt clients
51
+ this.mqttClient = new mqttClientClass(this,this.config);
52
+ /*
53
+ // Subscribe all States (given from messagehandler)
54
+ this.subscibeableStates = this.messagehandler.getSubscribeableStates(undefined);
55
+ if(this.subscibeableStates){
56
+ for(const subscibeableState of Object.values(this.subscibeableStates)){
57
+ this.subscribeStatesAsync(`*.${subscibeableState}`);
58
+ }
59
+ }
60
+ */
61
+ this.subscribeStatesAsync(`*.push`);
62
+ this.subscribeStatesAsync(`*.replace`);
63
+ /*
64
+ setTimeout(() => {
65
+ this.mqttClient[1]?.publish("R/c0619ab24727/keepalive",null);
66
+ }, 1000);*/
67
+ // Reset the connection indicator during startup
68
+ /*setTimeout(() => {
69
+ this.mqttClient?.publish("v3/hafi-ttn-lorawan@ttn/devices/eui-lobaro-modbus/down/push",JSON.stringify({"downlinks":[{"f_port": 128,"frm_payload":"Pw==","priority": "NORMAL"}]}));
70
+ }, 5000);
71
+ this.setState("info.connection", false, true);*/
72
+ }
73
+
74
+ /**
75
+ * Is called when adapter shuts down - callback has to be called under any circumstances!
76
+ * @param {() => void} callback
77
+ */
78
+ onUnload(callback) {
79
+ try {
80
+ // Here you must clear all timeouts or intervals that may still be active
81
+ // clearTimeout(timeout1);
82
+ // clearTimeout(timeout2);
83
+ // ...
84
+ // clearInterval(interval1);
85
+
86
+ callback();
87
+ } catch (e) {
88
+ callback();
89
+ }
90
+ }
91
+
92
+ // If you need to react to object changes, uncomment the following block and the corresponding line in the constructor.
93
+ // You also need to subscribe to the objects with `this.subscribeObjects`, similar to `this.subscribeStates`.
94
+ // /**
95
+ // * Is called if a subscribed object changes
96
+ // * @param {string} id
97
+ // * @param {ioBroker.Object | null | undefined} obj
98
+ // */
99
+ // onObjectChange(id, obj) {
100
+ // if (obj) {
101
+ // // The object was changed
102
+ // this.log.info(`object ${id} changed: ${JSON.stringify(obj)}`);
103
+ // } else {
104
+ // // The object was deleted
105
+ // this.log.info(`object ${id} deleted`);
106
+ // }
107
+ // }
108
+
109
+ /**
110
+ * Is called if a subscribed state changes
111
+ * @param {string} id
112
+ * @param {ioBroker.State | null | undefined} state
113
+ */
114
+ async onStateChange(id, state) {
115
+ if (state) {
116
+ // this.log.debug(`state ${id} changed: val: ${state.val} - ack: ${state.ack}`);
117
+ // The state was changed => only states with ack = false will be processed, others will be ignored
118
+ if(!state.ack){
119
+ // get information of the changing state
120
+ // @ts-ignore
121
+ this.changeInfo = this.getChangeInfo(id);
122
+ let appending = "";
123
+ if(this.changeInfo.changedState === "push"){
124
+ // @ts-ignore
125
+ appending = "push";
126
+ const downlinkTopic = this.messagehandler?.getDownlinkTopic(this.changeInfo,`/down/${appending}`);
127
+ //this.sendDownlink(downlinkTopic,JSON.stringify(state.val));
128
+ this.sendDownlink(downlinkTopic,state.val);
129
+ this.setStateAsync(id,state.val,true);
130
+ }
131
+ else if(this.changeInfo.changedState === "replace"){
132
+ // @ts-ignore
133
+ appending = "replace";
134
+ const downlinkTopic = this.messagehandler?.getDownlinkTopic(this.changeInfo,`/down/${appending}`);
135
+ this.sendDownlink(downlinkTopic,state.val);
136
+ this.setStateAsync(id,state.val,true);
137
+ }
138
+ }
139
+ } else {
140
+ // The state was deleted
141
+ this.log.info(`state ${id} deleted`);
142
+ }
143
+ }
144
+
145
+ sendDownlink(topic,message){
146
+ this.mqttClient?.publish(topic,message);
147
+ }
148
+
149
+ getChangeInfo(id){
150
+ // Select datahandling in case of origin
151
+ if(this.config.ttn){
152
+ return this.getTtnChangeInfo(id);
153
+ }
154
+ else if(this.config.chirpstack){
155
+ // this.handleChirpstack(topic,message);
156
+ }
157
+ }
158
+
159
+ getTtnChangeInfo(id){
160
+ id = id.substring(this.namespace.length + 1,id.length);
161
+ const idElements = id.split(".");
162
+ const changeInfo = {
163
+ applicationId : idElements[0],
164
+ dev_uid : idElements[3],
165
+ device_id : idElements[2],
166
+ changedState : idElements[idElements.length - 1],
167
+ allElements : idElements
168
+ };
169
+ return changeInfo;
170
+ }
171
+
172
+ /* getStringprefix(source,searchstring,times){
173
+ let position = 0;
174
+ for(let index = 0; index < times ; index++){
175
+ position = source.indexOf(searchstring,position) + 1;
176
+ }
177
+ return source.substring(0,position-1);
178
+ }*/
179
+ // If you need to accept messages in your adapter, uncomment the following block and the corresponding line in the constructor.
180
+ // /**
181
+ // * Some message was sent to this instance over message box. Used by email, pushover, text2speech, ...
182
+ // * Using this method requires "common.messagebox" property to be set to true in io-package.json
183
+ // * @param {ioBroker.Message} obj
184
+ // */
185
+ // onMessage(obj) {
186
+ // if (typeof obj === "object" && obj.message) {
187
+ // if (obj.command === "send") {
188
+ // // e.g. send email or pushover or whatever
189
+ // this.log.info("send command");
190
+
191
+ // // Send response in callback if required
192
+ // if (obj.callback) this.sendTo(obj.from, obj.command, "Message received", obj.callback);
193
+ // }
194
+ // }
195
+ // }
196
+
197
+ }
198
+
199
+ if (require.main !== module) {
200
+ // Export the constructor in compact mode
201
+ /**
202
+ * @param {Partial<utils.AdapterOptions>} [options={}]
203
+ */
204
+ module.exports = (options) => new Lorawan(options);
205
+ } else {
206
+ // otherwise start the instance directly
207
+ new Lorawan();
208
+ }
package/package.json ADDED
@@ -0,0 +1,78 @@
1
+ {
2
+ "name": "iobroker.lorawan",
3
+ "version": "0.0.2",
4
+ "description": "converts the desired lora gateway data to a ioBroker structure",
5
+ "author": {
6
+ "name": "BenAhrdt",
7
+ "email": "bsahrdt@gmail.com"
8
+ },
9
+ "contributors": [
10
+ {
11
+ "name": "J-Paul0815"
12
+ }
13
+ ],
14
+ "homepage": "https://github.com/BenAhrdt/ioBroker.lorawan",
15
+ "license": "MIT",
16
+ "keywords": [
17
+ "LoRaWAN"
18
+ ],
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "https://github.com/BenAhrdt/ioBroker.lorawan.git"
22
+ },
23
+ "engines": {
24
+ "node": ">= 18"
25
+ },
26
+ "dependencies": {
27
+ "@iobroker/adapter-core": "^3.0.4",
28
+ "mqtt": "^5.3.4"
29
+ },
30
+ "devDependencies": {
31
+ "@alcalzone/release-script": "^3.7.0",
32
+ "@alcalzone/release-script-plugin-iobroker": "^3.7.0",
33
+ "@alcalzone/release-script-plugin-license": "^3.7.0",
34
+ "@alcalzone/release-script-plugin-manual-review": "^3.7.0",
35
+ "@iobroker/adapter-dev": "^1.2.0",
36
+ "@iobroker/testing": "^4.1.0",
37
+ "@tsconfig/node18": "^18.2.2",
38
+ "@types/chai": "^4.3.11",
39
+ "@types/chai-as-promised": "^7.1.8",
40
+ "@types/mocha": "^10.0.6",
41
+ "@types/node": "^20.10.6",
42
+ "@types/proxyquire": "^1.3.31",
43
+ "@types/sinon": "^17.0.2",
44
+ "@types/sinon-chai": "^3.2.12",
45
+ "chai-as-promised": "^7.1.1",
46
+ "chai": "^4.4.0",
47
+ "eslint": "^8.56.0",
48
+ "mocha": "^10.2.0",
49
+ "proxyquire": "^2.1.3",
50
+ "sinon": "^17.0.1",
51
+ "sinon-chai": "^3.7.0",
52
+ "typescript": "~5.3.3"
53
+ },
54
+ "main": "main.js",
55
+ "files": [
56
+ "admin{,/!(src)/**}/!(tsconfig|tsconfig.*|.eslintrc).{json,json5}",
57
+ "admin{,/!(src)/**}/*.{html,css,png,svg,jpg,js}",
58
+ "lib/",
59
+ "www/",
60
+ "io-package.json",
61
+ "LICENSE",
62
+ "main.js"
63
+ ],
64
+ "scripts": {
65
+ "test:js": "mocha --config test/mocharc.custom.json \"{!(node_modules|test)/**/*.test.js,*.test.js,test/**/test!(PackageFiles|Startup).js}\"",
66
+ "test:package": "mocha test/package --exit",
67
+ "test:integration": "mocha test/integration --exit",
68
+ "test": "npm run test:js && npm run test:package",
69
+ "check": "tsc --noEmit -p tsconfig.check.json",
70
+ "lint": "eslint .",
71
+ "translate": "translate-adapter",
72
+ "release": "release-script"
73
+ },
74
+ "bugs": {
75
+ "url": "https://github.com/BenAhrdt/ioBroker.lorawan/issues"
76
+ },
77
+ "readmeFilename": "README.md"
78
+ }