-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
253 lines (216 loc) · 7.87 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
242
243
244
245
246
247
248
249
250
251
252
253
"use strict";
/*
* Created with @iobroker/create-adapter v1.30.1
*/
// The adapter-core module gives you access to the core ioBroker functions you need to create an adapter
const utils = require("@iobroker/adapter-core");
const schellenbergBridge = require("./lib/SchellenbergBridge");
const commonDefines = require("./lib/helpers/CommonDefines");
const commandFactory = require("./lib/comunication/CommandFactory");
let gthis = null; // global to 'this' of smartfriends main instance
let SchellenbergBridge = null;
// Default gateway parameters
const defaultPort = 4900;
const defaultShcVersion = "3.6.0";
const defaultShcApiVersion = "3.3";
const defaultCSymbol = "D19033i";
class Smartfriends extends utils.Adapter {
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
constructor(options) {
super({
...options,
name: "smartfriends",
});
this.on("ready", this.onReady.bind(this));
this.on("stateChange", this.onStateChange.bind(this));
this.on("unload", this.onUnload.bind(this));
gthis = this;
}
async checkSettings() {
this.log.debug("Checking adapter settings...");
if (this.config.smartFriendsPort == null || this.config.smartFriendsPort <= 0) {
this.log.warn(`Port was not correctly set. Defaulting to '${defaultPort}'.`);
this.config.smartFriendsPort = defaultPort;
}
if (this.config.smartFriendsIP == null || this.config.smartFriendsIP == "") {
throw new Error("IP address empty! Check settings.");
}
if (this.config.smartFriendsUsername == null || this.config.smartFriendsUsername == "") {
throw new Error("Username empty! Check settings.");
}
if (this.config.smartFriendsPassword == null || this.config.smartFriendsPassword == "") {
throw new Error("Password empty! Check settings.");
}
if (this.config.smartFriendsCSymbol == null || this.config.smartFriendsCSymbol == "") {
this.log.warn(`CSymbol was not correctly set. Defaulting to '${defaultCSymbol}.`);
this.config.smartFriendsCSymbol = defaultCSymbol;
}
if (this.config.smartFriendsShcVersion == null || this.config.smartFriendsShcVersion == "") {
this.log.warn(`SHCVersion was not correctly set. Defaulting to '${defaultShcVersion}'.`);
this.config.smartFriendsShcVersion = defaultShcVersion;
}
if (this.config.smartFriendsShApiVersion == null || this.config.smartFriendsShApiVersion == "") {
this.log.warn(`SHAPIVersion was not correctly set. Defaulting to '${defaultShcApiVersion}'.`);
this.config.smartFriendsShApiVersion = defaultShcApiVersion;
}
}
async initObjects() {
this.log.debug("Initializing objects...");
// info
let infoPrefix = commonDefines.AdapterDatapointIDs.Info;
await this.setObjectNotExistsAsync(infoPrefix, {
type: "channel",
common: {
name: "Adapter information"
},
native: {}
});
infoPrefix += ".";
await this.setObjectNotExistsAsync(infoPrefix + commonDefines.AdapterStateIDs.Connection, {
type: "state",
common: {
name: "Connection to gateway",
type: "boolean",
role: "indicator.connected",
read: true,
write: false,
desc: "Indicates if connection to SmartFriendsBox was successful or not"
},
native: {}
});
// gateway
let gatewayPrefix = commonDefines.AdapterDatapointIDs.Gateway;
await this.setObjectNotExistsAsync(gatewayPrefix, {
type: "channel",
common: {
name: "Gateway information"
},
native: {}
});
gatewayPrefix += ".";
await this.setObjectNotExistsAsync(gatewayPrefix + commonDefines.AdapterStateIDs.HardwareName, {
type: "state",
common: {
name: "Hardware name",
type: "string",
role: "text",
read: true,
write: false,
desc: "Actual Hardware name"
},
native: {}
});
await this.setObjectNotExistsAsync(gatewayPrefix + commonDefines.AdapterStateIDs.MacAddress, {
type: "state",
common: {
name: "Hardware MAC address",
type: "string",
role: "info.mac",
read: true,
write: false,
desc: "Hardware MAC address"
},
native: {}
});
this.setAdapterConnectionState(false);
}
async connectToGateway() {
gthis.log.info("Connecting to gateway and retrieving data...");
gthis.log.debug(`IP: ${this.config.smartFriendsIP} - Port: ${this.config.smartFriendsPort} - Username: ${this.config.smartFriendsUsername} - Password: ${this.config.smartFriendsPassword} - CSymbol: ${this.config.smartFriendsCSymbol} - SHCVersion: ${this.config.smartFriendsShcVersion} - SHAPIVersion: ${this.config.smartFriendsShApiVersion}`);
SchellenbergBridge = new schellenbergBridge.SchellenbergBridge(gthis);
SchellenbergBridge.Connect();
}
async setAdapterConnectionState(isConnected) {
await this.setStateChangedAsync(`${commonDefines.AdapterDatapointIDs.Info}.${commonDefines.AdapterStateIDs.Connection}`, isConnected, true);
await this.setForeignState(`system.adapter.${this.namespace}.connected`, isConnected, true);
}
/**
* Is called when databases are connected and adapter received configuration.
*/
async onReady() {
this.initObjects()
.then(() => this.checkSettings())
.then(() => {
this.connectToGateway();
this.subscribeStates("devices.*.control.*"); // only subsribe to states changes under "devices.X.control."
})
.catch(err => this.log.error(err));
}
/**
* Is called when adapter shuts down - callback has to be called under any circumstances!
* @param {() => void} callback
*/
onUnload(callback) {
try {
if (SchellenbergBridge != null) {
SchellenbergBridge.handleDisconnect();
}
this.setAdapterConnectionState(false);
this.log.info("onUnload(): Cleaned everything up...");
callback();
// eslint-disable-next-line no-unused-vars
} catch (e) {
callback();
}
}
/**
* 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.silly(`state ${id} changed: ${state.val} (ack = ${state.ack})`);
if (state.val == false) {
this.log.silly("Only suscribed to val==true. No need to process changed data.");
return;
}
// ack is true when state was updated by gateway --> in this case, we don't need to send it again
if (state.ack) {
this.log.silly("Updated data was retrieved from gateway. No need to process changed data.");
return;
}
// Only states under "devices.XXX.control" are subscribed --> device settings/modes are changed
let deviceId = id.replace(`${this.namespace}.${commonDefines.AdapterDatapointIDs.Devices}.`, "");
deviceId = deviceId.substring(0, deviceId.indexOf("."));
const controlOption = id.substring(id.lastIndexOf(".") + 1, id.length);
let controlCommand = commonDefines.DeviceCommands.UNDEF;
switch (controlOption) {
case (commonDefines.AdapterStateIDs.MoveDown):
controlCommand = commonDefines.DeviceCommands.MoveDown;
break;
case (commonDefines.AdapterStateIDs.MoveUp):
controlCommand = commonDefines.DeviceCommands.MoveUp;
break;
case (commonDefines.AdapterStateIDs.MoveStop):
controlCommand = commonDefines.DeviceCommands.MoveStop;
break;
default:
this.log.error(`Unsupported control option: ${controlOption} - Please report this to the developer!`);
break;
}
if (deviceId != "" && controlCommand != commonDefines.DeviceCommands.UNDEF) {
this.log.debug(`Sending command '${controlCommand.name}' to device ${deviceId}...`);
SchellenbergBridge.sendAndReceiveCommand(commandFactory.default.createSetDeviceValueCmd(deviceId, controlCommand.value));
this.setStateAsync(id, false, true);
}
} else {
// The state was deleted
this.log.silly(`state ${id} deleted`);
}
}
}
// @ts-ignore parent is a valid property on module
if (module.parent) {
// Export the constructor in compact mode
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
module.exports = (options) => new Smartfriends(options);
} else {
// otherwise start the instance directly
new Smartfriends();
}