forked from homebridge/HAP-NodeJS
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAccessoryController.js
executable file
·84 lines (82 loc) · 2.49 KB
/
AccessoryController.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
AccessoryController.prototype = {
addService: function addService(service) {
service.instanceID = this.instanceID;
service.accessoryController = this;
this.objects[service.instanceID] = service;
this.instanceID += 1;
this.services.push(service);
if (service.characteristics.length > 0) {
for (var i = 0; i < service.characteristics.length; i++) {
var characteristic = service.characteristics[i];
characteristic.instanceID = this.instanceID;
characteristic.accessoryID = 1;
characteristic.accessoryController = this;
this.objects[characteristic.instanceID] = characteristic;
this.instanceID += 1;
};
}
},
jsonPresentation: function jsonPresentation() {
var servicesObjects = [];
for (var i = 0; i < this.services.length; i++) {
var service = this.services[i];
servicesObjects.push(service.objectsPresentation());
};
var accessory = {
aid: 1,
services: servicesObjects
}
var dict = {
accessories: [accessory]
}
return JSON.stringify(dict);
},
jsonForCharacteristicUpdate: function jsonForCharacteristicUpdate(aid, iid) {
var charObject = this.objects[iid];
var charValue = charObject.valueForUpdate();
var respDict = {
characteristics: [
{
aid: aid,
iid: iid,
value: charValue
}
]
}
return JSON.stringify(respDict);
},
processCharacteristicsValueWrite: function processCharacteristicsValueWrite(updates, peer) {
var updates_objects = JSON.parse(updates.toString());
console.log(updates_objects);
var update_characteristics = updates_objects["characteristics"];
for (var i = 0; i < update_characteristics.length; i++) {
var update_char = update_characteristics[i];
var update_char_iid = update_char["iid"];
var update_char_value = update_char["value"];
var update_char_event = update_char["ev"];
var charObject = this.objects[update_char_iid];
if (update_char_value !== undefined) {
charObject.updateCharacteristicValue(update_char_value, peer);
}
if (update_char_event !== undefined) {
charObject.updateCharacteristicEvent(update_char_event, peer);
}
}
},
broadcastEvent: function broadcastEvent(data, subscribedPeers, peer) {
if (this.tcpServer !== undefined) {
this.tcpServer.broadcastEvent(data, subscribedPeers, peer);
}
}
}
function AccessoryController() {
if (!(this instanceof AccessoryController)) {
return new AccessoryController();
}
this.instanceID = 1;
this.objects = {};
this.services = [];
}
module.exports = {
AccessoryController: AccessoryController
};