forked from ilcato/homebridge-blynk
-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
290 lines (280 loc) · 9.48 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// Blynk Platform plugin for HomeBridge
//
// Remember to add platform to config.json. Example:
// "platforms": [
// {
// "platform": "Blynk",
// "name": "Blynk",
// "server": "PUT THE ADDRESS OF THE LOCAL BLYNK SERVER HERE",
// "appPort": "PUT THE PORT OF THE LOCAL BLYNK SERVER HERE, TIPICALLY 8443",
// "username": "PUT THE VALUE OF THE USERNAME HERE",
// "password": "PUT THE VALUE OF THE PASSWORD HERE",
// "dashboardName": "PUT THE DASHBOARD NAME HERE",
// "accessories": [
// {
// "name": "SwitchD5",
// "widget": "Switch",
// "mode": "SWITCH",
// "caption": "Lamp 1",
// "pin": "D5"
// },
// {
// "name": "ContactSensor1",
// "widget": "ContactSensor",
// "caption": "Door 1",
// "pin": "D4"
// },
// {
// "name": "TemperatureSensor1",
// "widget": "TemperatureSensor",
// "caption": "Temperature 1",
// "pin": "V2"
// }
// ]
// }
// ],
//
// When you attempt to add a device, it will ask for a "PIN code".
// The default code for all HomeBridge accessories is 031-45-154.
'use strict';
// TODO: remove when using a full signed certificate
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
var Service, Characteristic;
var request = require("request");
var blynk = require("blynk-app-client");
const UPDATE_PERIOD = 2000;
const INITIAL_UPDATE_PERIOD = 10000;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerPlatform("homebridge-blynk", "Blynk", BlynkPlatform);
}
function BlynkPlatform(log, config) {
this.log = log;
this.server = config["server"];
this.appPort = config["appPort"];
this.username = config["username"];
this.password = config["password"];
this.dashboardName = config["dashboardName"];
this.BlynkAccessories = config["accessories"];
this.blynkServer = blynk.createClient(this.server, this.appPort);
}
BlynkPlatform.prototype.accessories = function(callback) {
this.log("Loading accessories...");
var that = this;
var foundAccessories = [];
if (this.BlynkAccessories == null || this.BlynkAccessories.length == 0) {
callback(foundAccessories);
return;
}
this.blynkServer.connect(this.username, this.password)
.then(function (status) {
return that.blynkServer.loadProfileGzipped();
})
.then(function (profile) {
var dashBoards = JSON.parse(profile).dashBoards;
dashBoards.map(function(dashboard) {
if (dashboard.name == that.dashboardName) {
that.dashboard = dashboard;
}
});
if (that.dashboard == undefined) {
callback(foundAccessories);
return;
}
that.blynkServer.activate(that.dashboard.id)
.then(function (status) {
return that.blynkServer.getToken(that.dashboard.id);
})
.then(function (token) {
that.token = token;
})
.catch (function (err) {
that.log("Error getting token");
callback(foundAccessories);
return;
});
that.BlynkAccessories.map(function(s) {
that.log("Found: " + s.name);
var accessory = null;
var services = [];
var service = null;
switch (s.widget) {
case "Switch":
service = {
controlService: new Service.Switch(s.caption),
characteristics: [Characteristic.On]
};
service.controlService.mode = s.mode;
break;
case "ContactSensor":
service = {
controlService: new Service.ContactSensor(s.caption),
characteristics: [Characteristic.ContactSensorState]
};
break;
case "TemperatureSensor":
service = {
controlService: new Service.TemperatureSensor(s.caption),
characteristics: [Characteristic.CurrentTemperature]
};
break;
default:
break;
}
if (service != null) {
service.controlService.widget = s.widget;
service.controlService.pin = s.pin;
services.push(service);
accessory = new BlynkAccessory(services);
accessory.getServices = function() {
return that.getServices(accessory);
};
accessory.platform = that;
accessory.remoteAccessory = s;
accessory.name = s.name;
accessory.model = "Blynk";
accessory.manufacturer = "Blynk";
accessory.serialNumber = "<unknown>";
foundAccessories.push(accessory);
}
}
)
callback(foundAccessories);
})
.catch(function (error) {
this.log("Error: " + error);
callback(foundAccessories);
return;
});
}
BlynkPlatform.prototype.hardwareWrite = function(pinString, value) {
var pinType = pinString.substr(0,1).toLowerCase();
var pin = pinString.substr(1);
var that = this;
this.blynkServer.hardware(this.dashboard.id, pinType, "w", pin, value ? 1 : 0)
.catch(function (err) {
that.log("There was a problem sending hardware write command on: " + pinString);
});
}
BlynkPlatform.prototype.hardwareRead = function(callback, characteristic, service) {
var pinString = service.controlService.pin;
var pinType = pinString.substr(0,1).toLowerCase();
var pin = parseInt(pinString.substr(1));
var that = this;
this.blynkServer.hardware(this.dashboard.id, pinType, "r", pin)
.then(function (fields) {
var pinValue = fields[3];
var retValue = null;
switch (service.controlService.widget) {
case "Switch":
retValue = pinValue == "1" ? true : false;
break;
case "ContactSensor":
retValue = parseInt(pinValue);
break;
case "TemperatureSensor":
retValue = parseFloat(pinValue);
break;
default:
break;
}
if (callback)
callback(undefined, retValue);
})
.catch(function (err) {
that.log("There was a problem sending hardware read command on: " + pinString + " - " + err);
if (callback)
callback(undefined, retValue);
});
}
BlynkPlatform.prototype.getInformationService = function(homebridgeAccessory) {
var informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Name, homebridgeAccessory.name)
.setCharacteristic(Characteristic.Manufacturer, homebridgeAccessory.manufacturer)
.setCharacteristic(Characteristic.Model, homebridgeAccessory.model)
.setCharacteristic(Characteristic.SerialNumber, homebridgeAccessory.serialNumber);
return informationService;
}
BlynkPlatform.prototype.bindCharacteristicEvents = function(characteristic, service, homebridgeAccessory) {
setTimeout( function() {
this.updateWidget(service, characteristic)
}.bind(this), INITIAL_UPDATE_PERIOD);
characteristic
.on('set', function(value, callback, context) {
if(context !== 'fromSetValue' && context !== 'fromPoller') {
switch (service.controlService.widget) {
case "Switch":
var pin = service.controlService.pin;
this.hardwareWrite(pin, value, homebridgeAccessory);
if (service.controlService.subtype == "PUSH") {
// In order to behave like a push button reset the status to off
setTimeout( function(){
characteristic.setValue(false, undefined, 'fromSetValue');
}, 100 );
}
break;
default:
break;
}
}
callback();
}.bind(this) );
characteristic
.on('get', function(callback) {
this.hardwareRead(callback, characteristic, service)
}.bind(this) );
}
BlynkPlatform.prototype.getServices = function(homebridgeAccessory) {
var services = [];
var informationService = homebridgeAccessory.platform.getInformationService(homebridgeAccessory);
services.push(informationService);
for (var s = 0; s < homebridgeAccessory.services.length; s++) {
var service = homebridgeAccessory.services[s];
for (var i=0; i < service.characteristics.length; i++) {
var characteristic = service.controlService.getCharacteristic(service.characteristics[i]);
if (characteristic == undefined)
characteristic = service.controlService.addCharacteristic(service.characteristics[i]);
homebridgeAccessory.platform.bindCharacteristicEvents(characteristic, service, homebridgeAccessory);
}
services.push(service.controlService);
}
return services;
}
BlynkPlatform.prototype.updateWidget = function(service, characteristic) {
var pinString = service.controlService.pin;
var pinType = pinString.substr(0,1).toLowerCase();
var pin = parseInt(pinString.substr(1));
this.blynkServer.hardware(this.dashboard.id, pinType, "r", pin)
.then(function (fields) {
var pinValue = fields[3];
var retValue = null;
switch (service.controlService.widget) {
case "Switch":
retValue = pinValue == "1" ? true : false;
break;
case "ContactSensor":
retValue = parseInt(pinValue);
break;
case "TemperatureSensor":
retValue = parseFloat(pinValue);
break;
default:
break;
}
characteristic.setValue(retValue, undefined, 'fromPoller');
setTimeout( function() {
this.updateWidget(service, characteristic)
}.bind(this), UPDATE_PERIOD);
}.bind(this))
.catch(function (err) {
this.log("There was a problem sending hardware read command on: " + pinString + " - " + err);
setTimeout( function() {
this.updateWidget(service, characteristic)
}.bind(this), UPDATE_PERIOD);
}.bind(this));
}
function BlynkAccessory(services) {
this.services = services;
}