forked from krvarma/homebridge-particle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
266 lines (210 loc) · 7.53 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
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
var request = require("request");
var eventSource = require('eventsource');
var Service, Characteristic;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerPlatform("homebridge-particle", "Particle", ParticlePlatform);
}
function ParticlePlatform(log, config){
this.log = log;
this.accessToken = config["access_token"];
this.deviceId = config["deviceid"];
this.url = config["cloudurl"];
this.devices = config["devices"];
}
ParticlePlatform.prototype = {
accessories: function(callback){
var foundAccessories = [];
var count = this.devices.length;
for(index=0; index< count; ++index){
var accessory = new ParticleAccessory(
this.log,
this.url,
this.accessToken,
this.devices[index]);
foundAccessories.push(accessory);
}
callback(foundAccessories);
}
};
function ParticleAccessory(log, url, access_token, device) {
this.log = log;
this.name = device["name"],
this.args = device["args"];
this.deviceId = device["deviceid"];
this.type = device["type"];
this.functionName = device["function_name"];
this.eventName = device["event_name"];
this.sensorType = device["sensorType"];
this.key = device["key"];
this.accessToken = access_token;
this.url = url;
this.value = 20;
console.log(this.name + " = " + this.sensorType);
this.services = [];
this.informationService = new Service.AccessoryInformation();
this.informationService
.setCharacteristic(Characteristic.Manufacturer, "Particle")
.setCharacteristic(Characteristic.Model, "Photon")
.setCharacteristic(Characteristic.SerialNumber, "AA098BB09");
this.services.push(this.informationService);
if(this.type === "LIGHT"){
this.lightService = new Service.Lightbulb(this.name);
this.lightService
.getCharacteristic(Characteristic.On)
.on('set', this.setState.bind(this));
this.services.push(this.lightService);
}
else if(this.type === "SWITCH"){
this.switchService = new Service.Switch(this.name);
this.switchService
.getCharacteristic(Characteristic.On)
.on('set', this.setState.bind(this));
this.services.push(this.switchService);
}
else if(this.type === "SENSOR"){
var service;
console.log("Sensor Type: " + this.sensorType.toLowerCase());
if(this.sensorType.toLowerCase() === "temperature"){
console.log("Temperature Sensor");
service = new Service.TemperatureSensor(this.name);
service
.getCharacteristic(Characteristic.CurrentTemperature)
.on('get', this.getDefaultValue.bind(this));
}
else if(this.sensorType.toLowerCase() === "humidity"){
console.log("Humidity Sensor");
service = new Service.HumiditySensor(this.name);
service
.getCharacteristic(Characteristic.CurrentRelativeHumidity)
.on('get', this.getDefaultValue.bind(this));
}
else if(this.sensorType.toLowerCase() === "light"){
console.log("Light Sensor");
service = new Service.LightSensor(this.name);
service
.getCharacteristic(Characteristic.CurrentAmbientLightLevel)
.on('get', this.getDefaultValue.bind(this));
}
else if(this.sensorType.toLowerCase() === "motion"){
console.log("Motion Sensor");
service = new Service.MotionSensor(this.name);
service
.getCharacteristic(Characteristic.MotionDetected)
.on('get', this.getDefaultValue.bind(this));
}
else if(this.sensorType.toLowerCase() === "contact"){
console.log("Contact Sensor");
service = new Service.ContactSensor(this.name);
service
.getCharacteristic(Characteristic.MotionDetected)
.on('get', this.getDefaultValue.bind(this));
}
if(service != undefined){
console.log("Initializing " + service.displayName + ", " + this.sensorType);
var eventUrl = this.url + this.deviceId + "/events/" + this.eventName + "?access_token=" + this.accessToken;
var es = new eventSource(eventUrl);
console.log(eventUrl);
es.onerror = function() {
console.log('ERROR!');
};
es.addEventListener(this.eventName,
this.processEventData.bind(this), false);
this.services.push(service);
}
console.log("Service Count: " + this.services.length);
}
}
ParticleAccessory.prototype.setState = function(state, callback) {
this.log.info("Getting current state...");
this.log.info("URL: " + this.url);
this.log.info("Device ID: " + this.deviceId);
var onUrl = this.url + this.deviceId + "/" + this.functionName;
var argument = this.args.replace("{STATE}", (state ? "1" : "0"));
this.log.info("Calling function: " + onUrl + "?" + argument);
request.post(
onUrl, {
form: {
access_token: this.accessToken,
args: argument
}
},
function(error, response, body) {
//console.log(response);
if (!error) {
callback();
} else {
callback(error);
}
}
);
}
ParticleAccessory.prototype.processEventData = function(e){
var data = JSON.parse(e.data);
var tokens = data.data.split('=');
console.log(tokens[0] + " = " + tokens[1] + ", " + this.services[1].displayName + ", " + this.sensorType + ", " + this.key.toLowerCase() + ", " + tokens[0].toLowerCase());
console.log(this.services[1] != undefined && this.key.toLowerCase() === tokens[0].toLowerCase());
if(this.services[1] != undefined && this.key.toLowerCase() === tokens[0].toLowerCase()){
if (tokens[0].toLowerCase() === "temperature") {
this.value = parseFloat(tokens[1]);
this.services[1]
.getCharacteristic(Characteristic.CurrentTemperature)
.setValue(parseFloat(tokens[1]));
}
else if (tokens[0].toLowerCase() === "humidity") {
this.value = parseFloat(tokens[1]);
this.services[1]
.getCharacteristic(Characteristic.CurrentRelativeHumidity)
.setValue(parseFloat(tokens[1]));
}
else if (tokens[0].toLowerCase() === "light") {
this.value = parseFloat(tokens[1]);
this.services[1]
.getCharacteristic(Characteristic.CurrentAmbientLightLevel)
.setValue(parseFloat(tokens[1]));
}
else if (tokens[0].toLowerCase() === "switch") {
this.value = parseFloat(tokens[1]);
this.services[1]
.getCharacteristic(Characteristic.On)
.setValue(parseFloat(tokens[1]));
}
else if (tokens[0].toLowerCase() === "motion") {
this.value = parseFloat(tokens[1]);
this.log('Received ' + this.value);
if (this.value === '1.00' || this.value === 1.00 || this.value === 'true' || this.value === 'TRUE') this.value = true;
else if (this.value === '0.00' || this.value === 0.00 || this.value === 'false' || this.value === 'FALSE') this.value = false;
if (this.value !== true && this.value !== false) {
this.log('Received value is not valid.');
} else {
this.services[1]
.getCharacteristic(Characteristic.MotionDetected)
.setValue(this.value);
}
else if (tokens[0].toLowerCase() === "contact") {
this.value = parseFloat(tokens[1]);
this.log('Received ' + this.value);
if (this.value === '1.00' || this.value === 1.00 || this.value === 'true' || this.value === 'TRUE') this.value = true;
else if (this.value === '0.00' || this.value === 0.00 || this.value === 'false' || this.value === 'FALSE') this.value = false;
if (this.value !== true && this.value !== false) {
this.log('Received value is not valid.');
} else {
this.services[1]
.getCharacteristic(Characteristic.ContactDetected)
.setValue(this.value);
}
}
}
}
}
ParticleAccessory.prototype.getDefaultValue = function(callback) {
callback(null, this.value);
}
ParticleAccessory.prototype.setCurrentValue = function(value, callback) {
console.log("Value: " + value);
callback(null, value);
}
ParticleAccessory.prototype.getServices = function() {
return this.services;
}