homebridge-alexa 0.5.48

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.
@@ -0,0 +1,183 @@
1
+ // Local event based client for alexa
2
+ //
3
+ // Generates events for each Alexa Skill message
4
+ //
5
+
6
+ "use strict";
7
+
8
+ var mqtt = require('mqtt');
9
+ var debug = require('debug')('alexaLocal');
10
+ const packageConfig = require('../package.json');
11
+ var Bottleneck = require("bottleneck");
12
+
13
+ var Validator = require('is-my-json-valid');
14
+ var alexaSchema = require('./alexa_smart_home_message_schema.json');
15
+ var checkAlexaMessage = Validator(alexaSchema, {
16
+ verbose: true
17
+ });
18
+
19
+ var connection = {};
20
+ var count = 0;
21
+ var username;
22
+ var limiter;
23
+ let Characteristic;
24
+
25
+ module.exports = {
26
+ alexaLocal: alexaLocal,
27
+ alexaEvent: alexaEvent,
28
+ alexaPriorityEvent: alexaPriorityEvent
29
+ };
30
+
31
+ function alexaLocal(options) {
32
+ debug("Connecting to Homebridge Smart Home Skill");
33
+ // Throttle event's to match Amazon's Rate API
34
+ // Limit events to one every 30 seconds, and keep at most 5 minutes worth
35
+ limiter = new Bottleneck({
36
+ maxConcurrent: 1,
37
+ highWater: 10,
38
+ minTime: 10000,
39
+ strategy: Bottleneck.strategy.BLOCK
40
+ });
41
+
42
+ if (options.alexaService) {
43
+ Characteristic = options.Characteristic;
44
+ options.alexaService.setCharacteristic(Characteristic.ContactSensorState, Characteristic.ContactSensorState.CONTACT_NOT_DETECTED);
45
+ }
46
+
47
+ limiter.on("dropped", function(dropped) {
48
+ options.log("WARNING: ( homebridge-alexa) Dropped event message, message rate too high.");
49
+ });
50
+
51
+ username = options.username;
52
+ connection.client = mqtt.connect(options);
53
+ // connection.client.setMaxListeners(0);
54
+ connection.client.on('connect', function() {
55
+ debug('connect', "command/" + options.username + "/#");
56
+ if (options.alexaService) {
57
+ options.alexaService.setCharacteristic(Characteristic.ContactSensorState, Characteristic.ContactSensorState.CONTACT_DETECTED);
58
+ }
59
+ connection.client.removeAllListeners('message'); // Clean up event handlers
60
+ connection.client.subscribe("command/" + options.username + "/#");
61
+ connection.client.publish("presence/" + options.username + "/1", JSON.stringify({
62
+ Connected: options.username,
63
+ version: packageConfig.version
64
+ }));
65
+ connection.client.on('message', function(topic, message) {
66
+ var msg = JSON.parse(message.toString());
67
+
68
+ // debug("Count", this.listenerCount(msg.directive.header.namespace.toLowerCase()));
69
+ if (options.eventBus.listenerCount(msg.directive.header.namespace) > 0) {
70
+ debug('Emitting', msg.directive.header.namespace);
71
+ options.eventBus.emit(msg.directive.header.namespace, msg, function(err, response) {
72
+ // TODO: if no message, return error Response
73
+ if (response == null) {
74
+ response = _alexaErrorResponse(msg);
75
+ }
76
+ // debug("Response", JSON.stringify(response));
77
+ connection.client.publish("response/" + options.username + "/1", JSON.stringify(response));
78
+ });
79
+ } else {
80
+ debug('No listener for', msg.directive.header.namespace.toLowerCase());
81
+ var response = _alexaErrorResponse(msg);
82
+ connection.client.publish("response/" + options.username + "/1", JSON.stringify(response));
83
+ debug("Response", JSON.stringify(response, null, 2));
84
+ }
85
+ });
86
+ });
87
+
88
+ connection.client.on('offline', function() {
89
+ debug('offline');
90
+ if (options.alexaService) {
91
+ options.alexaService.setCharacteristic(Characteristic.ContactSensorState, Characteristic.ContactSensorState.CONTACT_NOT_DETECTED);
92
+ }
93
+ });
94
+
95
+ connection.client.on('reconnect', function() {
96
+ count++;
97
+ debug('reconnect');
98
+ if (options.alexaService) {
99
+ options.alexaService.setCharacteristic(Characteristic.ContactSensorState, Characteristic.ContactSensorState.CONTACT_NOT_DETECTED);
100
+ }
101
+ if (count % 5 === 0) options.log("ERROR: ( homebridge-alexa) You have an issue with your installation, please review the README.");
102
+ if (count > 1000) {
103
+ connection.client.end({
104
+ force: true
105
+ });
106
+ if (options.alexaService) {
107
+ options.alexaService.setCharacteristic(Characteristic.ContactSensorState, Characteristic.ContactSensorState.CONTACT_NOT_DETECTED);
108
+ }
109
+ options.log("ERROR: ( homebridge-alexa) Stopping Home Skill connection due to excessive reconnects, please review the README.");
110
+ }
111
+ });
112
+
113
+ connection.client.on('error', function(err) {
114
+ if (options.alexaService) {
115
+ options.alexaService.setCharacteristic(Characteristic.ContactSensorState, Characteristic.ContactSensorState.CONTACT_NOT_DETECTED);
116
+ }
117
+ switch (err.code) {
118
+ case 5:
119
+ options.log("ERROR: ( homebridge-alexa) Login to homebridge.ca failed, please validate your credentials in config.json and restart homebridge.");
120
+ connection.client.end({
121
+ force: true
122
+ });
123
+ break;
124
+ default:
125
+ debug('ERROR: %s -> ', err.message, err.code);
126
+ }
127
+ });
128
+ }
129
+
130
+ function alexaEvent(message) {
131
+ /*
132
+ var status = checkAlexaMessage(message.toString());
133
+ if (!status) {
134
+ debug("WARNING - Bad response message", JSON.stringify(checkAlexaMessage.errors));
135
+ debug("---------------------------- Request --------------------------------");
136
+ debug(JSON.stringify(message));
137
+ }
138
+ */
139
+ var topic = "event/" + username + "/1";
140
+ var publish = function(callback) {
141
+ debug("Sending message", JSON.stringify(message));
142
+ connection.client.publish(topic, JSON.stringify(message), {
143
+ retain: false
144
+ }, callback);
145
+ };
146
+ limiter.submit(publish);
147
+ // debug("publish limit", limiter.counts());
148
+ }
149
+
150
+ function alexaPriorityEvent(message) {
151
+ var topic = "event/" + username + "/1";
152
+ var publish = function(callback) {
153
+ debug("Sending priority message", JSON.stringify(message));
154
+ connection.client.publish(topic, JSON.stringify(message), {
155
+ retain: false
156
+ }, callback);
157
+ };
158
+ limiter.submit({ priority: 4 }, publish);
159
+
160
+ /*
161
+ var topic = "event/" + username + "/1";
162
+ debug("Sending priority message", JSON.stringify(message));
163
+ connection.client.publish(topic, JSON.stringify(message));
164
+ */
165
+ }
166
+
167
+ function _alexaErrorResponse(message) {
168
+ var response = {
169
+ "event": {
170
+ "header": {
171
+ "name": "ErrorResponse",
172
+ "namespace": "Alexa",
173
+ "payloadVersion": "3",
174
+ "messageId": message.directive.header.messageId
175
+ },
176
+ "payload": {
177
+ "type": "INVALID_DIRECTIVE",
178
+ "message": "No listener for directive"
179
+ }
180
+ }
181
+ };
182
+ return (response);
183
+ }