-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
241 lines (207 loc) · 9.71 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
"use strict";
/*
* Created with @iobroker/create-adapter v1.21.0
*/
// The adapter-core module gives you access to the core ioBroker functions
// you need to create an adapter
const utils = require("@iobroker/adapter-core");
// Load your modules here, e.g.:
// const fs = require("fs");
const mapHelper = require("./lib/mapHelper");
class BoriswernerTestadapter extends utils.Adapter {
/**
* @param {Partial<ioBroker.AdapterOptions>} [options={}]
*/
constructor(options) {
super({
...options,
name: "boriswerner_testadapter",
});
this.on("ready", this.onReady.bind(this));
this.on("objectChange", this.onObjectChange.bind(this));
this.on("stateChange", this.onStateChange.bind(this));
// this.on("message", this.onMessage.bind(this));
this.on("unload", this.onUnload.bind(this));
}
/**
* Is called when databases are connected and adapter received configuration.
*/
async onReady() {
// Initialize your adapter here
// 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);
/*
For every state in the system there has to be also an object of type state
Here a simple template for a boolean variable named "testVariable"
Because every adapter instance uses its own unique namespace variable names can"t collide with other adapters variables
// */
// await this.setObjectAsync("testVariable", {
// type: "state",
// common: {
// name: "testVariable",
// type: "boolean",
// role: "indicator",
// read: true,
// write: true,
// },
// native: {},
// });
this.createChannelNotExists("map", "Maps");
let json = {};
//initial map setup
json = { "maps":[{ "mapID": "1260001017", "mapIndex": 0, "mapStatus": 1, "mapIsCurrentMap": 0, "mapIsBuilt": 1, "mapName": "EG" }
, { "mapID": "1307200506", "mapIndex": 1, "mapStatus": 1, "mapIsCurrentMap": 1, "mapIsBuilt": 1, "mapName": "OG" }] };
mapHelper.processMaps(this, json);
json = { "mapID": "1260001017", "mapSetID": "208", "mapSpotAreas": [{ "mapSpotAreaID": "1" }, { "mapSpotAreaID": "4" }] } ;
mapHelper.processSpotAreas(this, json);
json = { "mapID": "1307200506", "mapSetID": "207", "mapSpotAreas": [{ "mapSpotAreaID": "0" }, { "mapSpotAreaID": "1" }, { "mapSpotAreaID": "2" }] } ;
mapHelper.processSpotAreas(this, json);
json = { "mapSpotAreaID": "1", "mapSpotAreaName": "Wohnzimmer", "mapID": "1260001017" } ;
mapHelper.processSpotAreaInfo(this, json);
json = { "mapSpotAreaID": "4", "mapSpotAreaName": "Flur", "mapID": "1260001017" } ;
mapHelper.processSpotAreaInfo(this, json);
json = { "mapSpotAreaID": "0", "mapSpotAreaName": "A", "mapID": "1307200506" } ;
mapHelper.processSpotAreaInfo(this, json);
json = { "mapSpotAreaID": "1", "mapSpotAreaName": "B", "mapID": "1307200506" } ;
mapHelper.processSpotAreaInfo(this, json);
json = { "mapSpotAreaID": "2", "mapSpotAreaName": "C", "mapID": "1307200506" } ;
mapHelper.processSpotAreaInfo(this, json);
// //change map name of 1307200506 and remove map 1260001017
// json = { "maps":[{ "mapID": "1307200506", "mapIndex": 1, "mapStatus": 1, "mapIsCurrentMap": 1, "mapIsBuilt": 1, "mapName": "Obergeschoss" }] };
// mapHelper.processMaps(this, json);
// //remove spotArea 1 and rename spotArea 2
// json = { "mapID": "1307200506", "mapSetID": "207", "mapSpotAreas": [{ "mapSpotAreaID": "0" }, { "mapSpotAreaID": "2" }] } ;
// mapHelper.processSpotAreas(this, json);
// json = { "mapSpotAreaID": "0", "mapSpotAreaName": "A", "mapID": "1307200506" } ;
// mapHelper.processSpotAreaInfo(this, json);
// json = { "mapSpotAreaID": "2", "mapSpotAreaName": "Schlafzimmer", "mapID": "1307200506" } ;
// mapHelper.processSpotAreaInfo(this, json);
// //reactivate map 1260001017 with different status and Name, spotArea 4 (Flur) removed and 3 (Küche) added
// json = { "maps":[{ "mapID": "1260001017", "mapIndex": 0, "mapStatus": 2, "mapIsCurrentMap": 0, "mapIsBuilt": 1, "mapName": "Erdgeschoss" }
// , { "mapID": "1307200506", "mapIndex": 1, "mapStatus": 1, "mapIsCurrentMap": 1, "mapIsBuilt": 1, "mapName": "Obergeschoss" }] };
// mapHelper.processMaps(this, json);
// json = { "mapID": "1260001017", "mapSetID": "208", "mapSpotAreas": [{ "mapSpotAreaID": "1" }, { "mapSpotAreaID": "3" }] } ;
// mapHelper.processSpotAreas(this, json);
// json = { "mapSpotAreaID": "1", "mapSpotAreaName": "Wohnzimmer", "mapID": "1260001017" } ;
// mapHelper.processSpotAreaInfo(this, json);
// json = { "mapSpotAreaID": "3", "mapSpotAreaName": "Küche", "mapID": "1260001017" } ;
// mapHelper.processSpotAreaInfo(this, json);
// //reactivate spotArea 4 (Flur) and 3 (Küche) removed
// json = { "mapID": "1260001017", "mapSetID": "208", "mapSpotAreas": [{ "mapSpotAreaID": "1" }, { "mapSpotAreaID": "4" }] } ;
// mapHelper.processSpotAreas(this, json);
// json = { "mapSpotAreaID": "1", "mapSpotAreaName": "Wohnzimmer", "mapID": "1260001017" } ;
// mapHelper.processSpotAreaInfo(this, json);
// json = { "mapSpotAreaID": "4", "mapSpotAreaName": "Flur", "mapID": "1260001017" } ;
// mapHelper.processSpotAreaInfo(this, json);
// in this template all states changes inside the adapters namespace are subscribed
this.subscribeStates("*");
/*
setState examples
you will notice that each setState will cause the stateChange event to fire (because of above subscribeStates cmd)
*/
// the variable testVariable is set to true as command (ack=false)
// await this.setStateAsync("testVariable", true);
// // same thing, but the value is flagged "ack"
// // ack should be always set to true if the value is received from or acknowledged from the target system
// await this.setStateAsync("testVariable", { val: true, ack: true });
// // same thing, but the state is deleted after 30s (getState will return null afterwards)
// await this.setStateAsync("testVariable", { val: true, ack: true, expire: 30 });
// // examples for the checkPassword/checkGroup functions
// let result = await this.checkPasswordAsync("admin", "iobroker");
// this.log.info("check user admin pw iobroker: " + result);
// result = await this.checkGroupAsync("admin", "admin");
// this.log.info("check group user admin group admin: " + result);
}
/**
* Is called when adapter shuts down - callback has to be called under any circumstances!
* @param {() => void} callback
*/
onUnload(callback) {
try {
this.log.info("cleaned everything up...");
callback();
} catch (e) {
callback();
}
}
/**
* Is called if a subscribed object changes
* @param {string} id
* @param {ioBroker.Object | null | undefined} obj
*/
onObjectChange(id, obj) {
if (obj) {
// The object was changed
this.log.info(`object ${id} changed: ${JSON.stringify(obj)}`);
} else {
// The object was deleted
this.log.info(`object ${id} deleted`);
}
}
/**
* Is called if a subscribed state changes
* @param {string} id
* @param {ioBroker.State | null | undefined} state
*/
onStateChange(id, state) {
if (state) {
// The state was changed
this.log.info(`state ${id} changed: ${state.val} (ack = ${state.ack})`);
} else {
// The state was deleted
this.log.info(`state ${id} deleted`);
}
}
// /**
// * Some message was sent to this instance over message box. Used by email, pushover, text2speech, ...
// * Using this method requires "common.message" property to be set to true in io-package.json
// * @param {ioBroker.Message} obj
// */
// onMessage(obj) {
// if (typeof obj === "object" && obj.message) {
// if (obj.command === "send") {
// // e.g. send email or pushover or whatever
// this.log.info("send command");
// // Send response in callback if required
// if (obj.callback) this.sendTo(obj.from, obj.command, "Message received", obj.callback);
// }
// }
// }
async createChannelNotExists(id, name) {
this.setObjectNotExists(id, {
type: "channel",
common: {
name: name
},
native: {}
});
}
async createObjectNotExists(id, name, type, role, write, def, unit) {
this.setObjectNotExists(id, {
type: "state",
common: {
name: name,
type: type,
role: role,
read: true,
write: write,
def: def,
unit: unit
},
native: {}
});
}
}
// @ts-ignore parent is a valid property on module
if (module.parent) {
// Export the constructor in compact mode
/**
* @param {Partial<ioBroker.AdapterOptions>} [options={}]
*/
module.exports = (options) => new BoriswernerTestadapter(options);
} else {
// otherwise start the instance directly
new BoriswernerTestadapter();
}