-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.js
203 lines (171 loc) · 6.95 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
"use strict";
const utils = require("@iobroker/adapter-core"); // Get common adapter utils
const ioBLib = require("@strathcole/iob-lib").ioBLib;
const tahoma = require("./lib/tahoma");
const packageJson = require("./package.json");
const adapterName = packageJson.name.split(".").pop();
const adapterVersion = packageJson.version;
const patchVersion = ".3";
let adapter;
let polling;
let pollingTime;
let controller;
function startAdapter (options) {
options = options || {};
Object.assign(options, {
"name": "tahoma"
});
adapter = new utils.Adapter(options);
ioBLib.init(adapter);
adapter.on("unload", function (callback) {
if (polling)
clearTimeout(polling);
(controller && controller.logout(function (err, data) {
if (err)
controller.context.log.warn("Error during logout: " + err);
controller.unload(function () {
adapter.setState("info.connection", false, true);
callback();
});
})) || callback();
});
adapter.on("stateChange", function (id, state) {
// Warning, state can be null if it was deleted
try {
adapter.log.debug("stateChange " + id + " " + JSON.stringify(state));
if (!id)
return;
if (state && id.substr(0, adapter.namespace.length + 1) !== adapter.namespace + ".") {
// processStateChangeForeign(id, state);
return;
}
id = id.substring(adapter.namespace.length + 1); // remove instance name and id
if (state && state.ack)
return;
state = state.val;
adapter.log.debug("id=" + id);
if (typeof state !== "undefined" && state !== null)
processStateChange(id, state);
} catch (e) {
adapter.log.info("Error processing stateChange: " + e);
}
});
adapter.on("ready", function () {
if (!adapter.config.username)
adapter.log.warn("[START] Username not set");
else if (!adapter.config.password)
adapter.log.warn("[START] Password not set");
else {
adapter.log.info("[START] Starting adapter " + adapterName + " v" + adapterVersion + "" + patchVersion);
main();
}
});
return adapter;
}
function main () {
const deviceUsername = adapter.config.username;
const devicePassword = adapter.config.password;
const gatewaypin = adapter.config.gatewaypin;
const useMDNS = adapter.config.usemdns;
let localUrl;
if (gatewaypin)
localUrl = "https://gateway-" + gatewaypin + ((useMDNS) ? ".local" : "") + ":8443/enduser-mobile-web/1/enduserAPI/";
else
localUrl = "";
let tahomalinkUrl;
if (localUrl)
tahomalinkUrl = adapter.config.devapi_authurl;
else
tahomalinkUrl = adapter.config.tahomalinkurl;
const loginOptions = {
"maxAttempts": adapter.config.loginattempts | 3,
"delayAttempts": adapter.config.delaybetweenloginattempts | 30,
"delayAfterFailure": adapter.config.delayafterfailedlogin | 120
};
const applyOptions = {
"delayRetry": adapter.config.delayretryapplyqueue | 1500
};
pollingTime = adapter.config.pollinterval || 10000;
if (pollingTime < 5000)
pollingTime = 5000;
adapter.log.info("Configured polling interval: " + pollingTime);
adapter.log.debug("[START] Started Adapter");
adapter.subscribeStates("*");
ioBLib.setOrUpdateState("update", "Update device states", false, "", "boolean", "button.refresh");
controller = new tahoma.Tahoma(deviceUsername, devicePassword, tahomalinkUrl, localUrl, loginOptions, applyOptions, adapter);
controller.login(function (err, obj) {
if (!err)
pollStates();
});
}
function pollStates () {
adapter.log.debug("Starting state polling");
if (polling) {
clearTimeout(polling);
polling = null;
}
if (controller.isConnected()) {
controller.getAllStates(function () {
if (new Date().getTime() - controller.lastEventTime > 5 * 60 * 1000) {
// no events within last 5 minutes
controller.logout(function () { });
}
});
}
polling = setTimeout(function () {
pollStates();
}, pollingTime);
}
function processStateChange (id, value) {
adapter.log.debug("StateChange: " + JSON.stringify([id, value]));
if (id === "update") {
if (value) {
pollStates();
adapter.setState("update", false, true);
}
} else if (id.match(/^devices.*\.states\.core:ClosureState$/))
controller.onClosureStateChange(id, value);
else if (id.match(/^devices.*\.states\.core:TargetClosureState$/))
controller.onClosureStateChange(id, value);
else if (id.match(/^devices.*\.states\.core:ClosureState:slow$/))
controller.onClosureStateChange(id, value, true);
else if (id.match(/^devices.*\.states\.core:TargetClosureState:slow$/))
controller.onClosureStateChange(id, value, true);
else if (id.match(/^devices.*\.states\.core:TargetTemperatureState$/))
controller.onTemperatureStateChange(id, value);
else if (id.match(/^devices.*\.states\.core:DeploymentState$/))
controller.onDeploymentStateChange(id, value);
else if (id.match(/^devices.*\.states\.core:TargetDeploymentState$/))
controller.onDeploymentStateChange(id, value);
else if (id.match(/^devices.*\.states\.core:OnOffState$/))
controller.onOnOffStateChange(id, value);
else if (id.match(/^devices.*\.states\.core:SlateOrientationState$/))
controller.onSetOrientation(id, value);
else if (id.match(/^actionGroups.*\.commands\.execute/) && value)
controller.onExecuteCommand(id, value);
else if (id.match(/^devices.*\.states\.core:LightIntensityState$/))
controller.onIntensityStateChange(id, value);
else if (id.match(/^devices.*\.commands\./) && value) {
if (id.endsWith(":slow")) {
adapter.log.debug("Triggered command with slow mode: " + id);
id = id.substr(0, id.length - 5);
const cmd = id.substr(id.lastIndexOf(".") + 1);
if (cmd === "up" || cmd === "open") {
id = id.replace(/\.commands\.[a-z]+$/, ".states.core:ClosureState");
controller.onClosureStateChange(id, 0, true);
} else if (cmd === "down" || cmd === "close") {
id = id.replace(/\.commands\.[a-z]+$/, ".states.core:ClosureState");
controller.onClosureStateChange(id, 100, true);
} else
adapter.log.warn("Unknown slow command: " + id);
} else
controller.onExecuteDeviceCommand(id, false);
}
}
// If started as allInOne/compact mode => return function to create instance
if (module.parent)
module.exports = startAdapter;
else {
// or start the instance directly
startAdapter();
} // endElse