forked from KhaosT/homebridge-amazondash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
224 lines (199 loc) · 6.54 KB
/
index.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
var dash_button = require('node-dash-button');
var Accessory, Service, Characteristic, UUIDGen;
module.exports = function(homebridge) {
Accessory = homebridge.platformAccessory;
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
UUIDGen = homebridge.hap.uuid;
homebridge.registerPlatform("homebridge-amazondash", "AmazonDash", DashPlatform, true);
}
function DashPlatform(log, config, api) {
var self = this;
self.log = log;
self.config = config || { "platform": "AmazonDash" };
self.buttons = self.config.buttons || [];
self.accessories = {}; // MAC -> Accessory
if (api) {
self.api = api;
self.api.on('didFinishLaunching', self.didFinishLaunching.bind(this));
}
}
DashPlatform.prototype.configureAccessory = function(accessory) {
var self = this;
accessory.reachable = true;
accessory
.getService(Service.StatelessProgrammableSwitch)
.getCharacteristic(Characteristic.ProgrammableSwitchEvent)
.setValue(0);
var accessoryMAC = accessory.context.mac;
self.accessories[accessoryMAC] = accessory;
}
DashPlatform.prototype.didFinishLaunching = function() {
var self = this;
for (var i in self.buttons) {
var button = self.buttons[i];
if (!self.accessories[button.mac]) {
self.addAccessory(button.mac, button.name);
}
}
var registedMACs = Object.keys(self.accessories);
if (registedMACs.length > 0) {
self.dash = dash_button(registedMACs);
self.dash.on('detected', function(dash_id) {
var accessory = self.accessories[dash_id];
if (accessory) {
self.dashEventWithAccessory(accessory);
}
});
}
}
DashPlatform.prototype.dashEventWithAccessory = function(accessory) {
var targetChar = accessory
.getService(Service.StatelessProgrammableSwitch)
.getCharacteristic(Characteristic.ProgrammableSwitchEvent);
targetChar.setValue(1);
setTimeout(function(){targetChar.setValue(0);}, 10000);
}
DashPlatform.prototype.addAccessory = function(mac, name) {
var self = this;
var uuid = UUIDGen.generate(mac);
var newAccessory = new Accessory(name, uuid, 15);
newAccessory.reachable = true;
newAccessory.context.mac = mac;
newAccessory.addService(Service.StatelessProgrammableSwitch, name);
newAccessory
.getService(Service.AccessoryInformation)
.setCharacteristic(Characteristic.Manufacturer, "Amazon")
.setCharacteristic(Characteristic.Model, "JK76PL")
.setCharacteristic(Characteristic.SerialNumber, mac);
this.accessories[mac] = newAccessory;
this.api.registerPlatformAccessories("homebridge-amazondash", "AmazonDash", [newAccessory]);
var dashButton = dash_button(mac);
dashButton.on('detected', function() {
self.dashEventWithAccessory(newAccessory);
});
}
DashPlatform.prototype.removeAccessory = function(accessory) {
if (accessory) {
var mac = accessory.context.mac;
this.api.unregisterPlatformAccessories("homebridge-amazondash", "AmazonDash", [accessory]);
delete this.accessories[mac];
}
}
DashPlatform.prototype.configurationRequestHandler = function(context, request, callback) {
if (request && request.type === "Terminate") {
return;
}
if (!context.step) {
var instructionResp = {
"type": "Interface",
"interface": "instruction",
"title": "Before You Start...",
"detail": "Please make sure homebridge is running with elevated privileges and you have setup the dependency follow the tutorial.",
"showNextButton": true,
"buttonText": "View Tutorial",
"actionURL": "https://github.com/hortinstein/node-dash-button"
}
context.step = 1;
callback(instructionResp);
} else {
switch (context.step) {
case 1:
var respDict = {
"type": "Interface",
"interface": "list",
"title": "What do you want to do?",
"items": [
"Add New Dash Button",
"Disassociate Existed Dash Button"
]
}
context.step = 2;
callback(respDict);
break;
case 2:
var selection = request.response.selections[0];
if (selection === 0) {
//Setup New
var respDict = {
"type": "Interface",
"interface": "input",
"title": "New Dash Button",
"items": [
{
"id": "name",
"title": "Name",
"placeholder": "Orange Dash"
},
{
"id": "mac",
"title": "MAC Address (lowercase)",
"placeholder": "11:22:33:44:aa:ff"
}
]
}
context.step = 4;
callback(respDict);
} else {
//Remove Exist
var self = this;
var buttons = Object.keys(this.accessories).map(function(k){return self.accessories[k]});
var names = buttons.map(function(k){return k.displayName});
var respDict = {
"type": "Interface",
"interface": "list",
"title": "Which Dash Button do you want to remove?",
"items": names
}
context.buttons = buttons;
context.step = 6;
callback(respDict);
}
break;
case 4:
var userInputs = request.response.inputs;
var name = userInputs.name;
var MACAddress = userInputs.mac;
this.addAccessory(MACAddress, name);
var respDict = {
"type": "Interface",
"interface": "instruction",
"title": "Success",
"detail": "The new dash button is now added.",
"showNextButton": true
}
context.step = 5;
callback(respDict);
break;
case 5:
var self = this;
delete context.step;
var newConfig = this.config;
var newButtons = Object.keys(this.accessories).map(function(k){
var accessory = self.accessories[k];
var button = {
'name': accessory.displayName,
'mac': accessory.context.mac
};
return button;
});
newConfig.buttons = newButtons;
callback(null, "platform", true, newConfig);
break;
case 6:
var selection = request.response.selections[0];
var accessory = context.buttons[selection];
this.removeAccessory(accessory);
var respDict = {
"type": "Interface",
"interface": "instruction",
"title": "Success",
"detail": "The dash button is now removed.",
"showNextButton": true
}
context.step = 5;
callback(respDict);
break;
}
}
}