forked from NorthernMan54/homebridge-alexa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb.js
349 lines (310 loc) · 12 KB
/
web.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
// {
// "platform": "Alexa",
// "name": "Alexa",
// "username": "....",
// "password": "...."
// }
"use strict";
var Accessory, Service, Characteristic, UUIDGen;
var http = require('http');
var debug = require('debug')('AlexaPlugin');
var alexaLocal = require('./lib/alexaLocal.js').alexaLocal;
var alexaHAP = require('./lib/alexaHAP.js');
var alexaTranslator = require('./lib/alexaTranslator.js');
var mqtt = require('mqtt');
var alexa;
var options = {};
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
Accessory = homebridge.platformAccessory;
UUIDGen = homebridge.hap.uuid;
homebridge.registerPlatform("homebridge-alexa", "Alexa", alexahome);
};
function alexahome(log, config, api) {
this.log = log;
this.config = config;
this.pin = config['pin'] || "031-45-154";
this.username = config['username'] || false;
this.password = config['password'] || false;
this.filter = config['filter'];
this.refresh = config['refresh'] || 60 * 15; // Update every 15 minute's
if (!this.username || !this.password)
this.log.error("Missing username and password");
if (api) {
this.api = api;
this.api.on('didFinishLaunching', this.didFinishLaunching.bind(this));
}
}
alexahome.prototype = {
accessories: function(callback) {
this.log("accessories");
callback();
}
};
alexahome.prototype.didFinishLaunching = function() {
options = {
username: this.username,
password: this.password,
clientId: this.username,
reconnectPeriod: 5000,
servers: [{
protocol: 'mqtts',
host: 'homebridge.cloudwatch.net',
port: 8883
},
{
protocol: 'mqtt',
host: 'homebridge.cloudwatch.net',
port: 1883
}
]
};
alexaHAP.HAPDiscovery({
"pin": this.pin,
"refresh": this.refresh
});
// init(this);
alexa = new alexaLocal(options);
alexa.on('alexa', _alexaMessage.bind(this));
alexa.on('alexa.discovery', _alexaDiscovery.bind(this));
alexa.on('alexa.powercontroller', _alexaPowerController.bind(this));
alexa.on('alexa.powerlevelcontroller', _alexaPowerLevelController.bind(this));
alexa.on('alexa.colorcontroller', _alexaColorController.bind(this));
alexa.on('alexa.colortemperaturecontroller', _alexaColorTemperatureController.bind(this));
}
alexahome.prototype.configureAccessory = function(accessory) {
this.log("configureAccessory");
callback();
}
function _alexaDiscovery(message, callback) {
alexaHAP.HAPs(function(endPoints) {
var response = alexaTranslator.endPoints(message, endPoints, this.filter);
if (response.event.payload.endpoints.length < 1) {
this.log("ERROR: HAP Discovery failed, please review config");
} else {
this.log("alexaDiscovery - returned %s devices", response.event.payload.endpoints.length);
}
//debug("Discovery Response", JSON.stringify(response, null, 4));
callback(null, response);
}.bind(this))
}
function _alexaColorTemperatureController(message, callback) {
var action = message.directive.header.name;
var endpointId = message.directive.endpoint.endpointId;
var colorTemperature;
try {
var haAction = JSON.parse(message.directive.endpoint.cookie[action]);
} catch (e) {
this.log.error("_alexaColorTemperatureController missing action", action, e.message, message.directive.endpoint.cookie);
callback(e);
return;
}
switch (action.toLowerCase()) {
case "decreasecolortemperature":
case "increasecolortemperature":
// This characteristic describes color temperature which is represented in the reciprocal megakelvin (MK-1) or mirek scale. MK = 1,000,000 / K where MK is the desired mirek value and K is temperature in Kelvins.
alexaHAP.HAPstatus(haAction.host, haAction.port, "?id=" + haAction.aid + "." + haAction.iid, function(err, status) {
this.log("ColorTemperatureController-get", action, haAction.host, haAction.port, status, err);
var colorTemperatureDelta = 40;
if ( action.toLowerCase() = "decreasecolortemperature" )
colorTemperatureDelta = -40;
colorTemperature = status.characteristics[0].value + colorTemperatureDelta > 500 ? 500 : status.characteristics[0].value + colorTemperatureDelta;
colorTemperature = colorTemperature < 140 ? 140 : colorTemperature;
var body = {
"characteristics": [{
"aid": haAction.aid,
"iid": haAction.iid,
"value": colorTemperature
}]
};
alexaHAP.HAPcontrol(haAction.host, haAction.port, JSON.stringify(body), function(err, status) {
this.log("ColorTemperatureController-change", action, haAction.host, haAction.port, status, body, err);
var response = alexaTranslator.alexaResponse(message, status, err, _round(1000000/colorTemperature));
callback(err, response);
}.bind(this));
}.bind(this));
break;
case "setcolortemperature":
// No need to do anything
colorTemperature = _round(1000000/message.directive.payload.colorTemperatureInKelvin);
var body = {
"characteristics": [{
"aid": haAction.aid,
"iid": haAction.iid,
"value": colorTemperature
}]
};
alexaHAP.HAPcontrol(haAction.host, haAction.port, JSON.stringify(body), function(err, status) {
this.log("ColorTemperatureController-set", action, haAction.host, haAction.port, status, body, err);
var response = alexaTranslator.alexaResponse(message, status, err, message.directive.payload.colorTemperatureInKelvin);
callback(err, response);
}.bind(this));
break;
}
}
function _alexaPowerController(message, callback) {
var action = message.directive.header.name;
var endpointId = message.directive.endpoint.endpointId;
try {
var haAction = JSON.parse(message.directive.endpoint.cookie[action]);
} catch (e) {
this.log.error("_alexaPowerController missing action", action, e.message, message.directive.endpoint.cookie);
callback(e);
return;
}
var body = {
"characteristics": [{
"aid": haAction.aid,
"iid": haAction.iid,
"value": haAction.value
}]
};
alexaHAP.HAPcontrol(haAction.host, haAction.port, JSON.stringify(body), function(err, status) {
this.log("PowerController", action, haAction.host, haAction.port, status, err);
var response = alexaTranslator.alexaResponse(message, status, err);
callback(err, response);
}.bind(this));
}
function _alexaColorController(message, callback) {
var action = message.directive.header.name;
var endpointId = message.directive.endpoint.endpointId;
try {
var haAction = JSON.parse(message.directive.endpoint.cookie[action]);
} catch (e) {
this.log.error("_alexaColorController missing action", action, e.message, message.directive.endpoint.cookie);
callback(e);
return;
}
debug("action", haAction, message.directive.payload);
var body = {
"characteristics": [{
"aid": haAction.hue.aid,
"iid": haAction.hue.iid,
"value": message.directive.payload.color.hue
}, {
"aid": haAction.saturation.aid,
"iid": haAction.saturation.iid,
"value": message.directive.payload.color.saturation * 100
}, {
"aid": haAction.brightness.aid,
"iid": haAction.brightness.iid,
"value": message.directive.payload.color.brightness * 100
}]
};
debug("color HB command", body);
alexaHAP.HAPcontrol(haAction.host, haAction.port, JSON.stringify(body), function(err, status) {
this.log("ColorController", action, haAction.host, haAction.port, status, err);
var response = alexaTranslator.alexaResponse(message, status, err);
callback(err, response);
}.bind(this));
}
function _alexaPowerLevelController(message, callback) {
//debug(JSON.stringify(message, null, 4));
var action = message.directive.header.name;
var endpointId = message.directive.endpoint.endpointId;
var powerLevel, haAction;
try {
haAction = JSON.parse(message.directive.endpoint.cookie[action]);
} catch (e) {
this.log.error("_alexaPowerLevelController missing action", action, e.message, message.directive.endpoint.cookie);
callback(e);
return;
}
switch (action.toLowerCase()) {
case "adjustpowerlevel":
// Need to get current value prior to dimming
alexaHAP.HAPstatus(haAction.host, haAction.port, "?id=" + haAction.aid + "." + haAction.iid, function(err, status) {
this.log("PowerLevelController-get", action, haAction.host, haAction.port, status, err);
var powerLevelDelta = message.directive.payload.powerLevelDelta;
powerLevel = status.characteristics[0].value + powerLevelDelta > 100 ? 100 : status.characteristics[0].value + powerLevelDelta;
powerLevel = powerLevel < 0 ? 0 : powerLevel;
var body = {
"characteristics": [{
"aid": haAction.aid,
"iid": haAction.iid,
"value": powerLevel
}]
};
alexaHAP.HAPcontrol(haAction.host, haAction.port, JSON.stringify(body), function(err, status) {
this.log("PowerLevelController-set", action, haAction.host, haAction.port, status, body, err);
var response = alexaTranslator.alexaResponse(message, status, err, powerLevel);
callback(err, response);
}.bind(this));
}.bind(this));
break;
case "setpowerlevel":
// No need to do anything
powerLevel = message.directive.payload.powerLevel;
var body = {
"characteristics": [{
"aid": haAction.aid,
"iid": haAction.iid,
"value": powerLevel
}]
};
alexaHAP.HAPcontrol(haAction.host, haAction.port, JSON.stringify(body), function(err, status) {
this.log("PowerLevelController", action, haAction.host, haAction.port, status, body, err);
var response = alexaTranslator.alexaResponse(message, status, err, powerLevel);
callback(err, response);
}.bind(this));
break;
}
}
function _alexaMessage(message, callback) {
var now = new Date();
switch (message.directive.header.name.toLowerCase()) {
case "reportstate": // aka getStatus
var action = message.directive.header.name;
var endpointId = message.directive.endpoint.endpointId;
try {
var reportState = JSON.parse(message.directive.endpoint.cookie[action]);
} catch (e) {
this.log.error("_alexaMessage missing action", action, e.message, message.directive.endpoint.cookie);
callback(e);
return;
}
var body = "?id=";
var spacer = ""; // No spacer for first element
var host, port;
reportState.forEach(function(element) {
host = element.host;
port = element.port;
if (element.interface == "Alexa.ColorController") {
body = body + spacer + element.hue.aid + "." + element.hue.iid;
spacer = ",";
body = body + spacer + element.saturation.aid + "." + element.saturation.iid;
body = body + spacer + element.brightness.aid + "." + element.brightness.iid;
} else {
body = body + spacer + element.aid + "." + element.iid;
spacer = ",";
}
});
// For performance HAP GET Characteristices supports getting multiple in one call
alexaHAP.HAPstatus(host, port, body, function(err, status) {
//this.log("reportState", action, host, port, status, err);
var response = alexaTranslator.alexaStateResponse(message, reportState, status, err);
callback(err, response);
}.bind(this));
break;
default:
this.log.error("Unhandled _alexaMessage Directive", message.directive.header.name);
var response = {
"event": {
"header": {
"name": "ErrorResponse",
"namespace": "Alexa",
"payloadVersion": "3",
"messageId": message.directive.header.messageId
},
"payload": {
"endpoints": []
}
}
};
}
}
function _round(value, precision) {
var multiplier = Math.pow(10, precision || 0);
return Math.round(value * multiplier) / multiplier;
}