This repository has been archived by the owner on Jun 25, 2020. It is now read-only.
forked from homebridge/HAP-NodeJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AccessoryController.js
executable file
·113 lines (111 loc) · 3.56 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
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
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;
};
}
},
accessoryJsonPresentation: function accessoryJsonPresentation() {
var servicesObjects = [];
for (var i = 0; i < this.services.length; i++) {
var service = this.services[i];
servicesObjects.push(service.objectsPresentation());
};
var accessory = {
aid: this.accessoryID,
services: servicesObjects
}
return accessory;
},
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: this.accessoryID,
services: servicesObjects
}
var dict = {
accessories: [accessory]
}
return JSON.stringify(dict);
},
jsonForCharacteristicUpdate: function jsonForCharacteristicUpdate(aid, iid, callback) {
var charObject = this.objects[iid];
var charValue = charObject.valueForUpdate(function(value){
var respDict = {
characteristics: [
{
aid: aid,
iid: iid,
value: value
}
]
}
callback(JSON.stringify(respDict));
});
},
processSingleCharacteristicsValueWrite: function processSingleCharacteristicsValueWrite(update, peer) {
var update_char = update;
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);
}
},
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);
} else if (this.bridgeController !== undefined) {
this.bridgeController.broadcastEvent(data, subscribedPeers, peer);
}
}
}
function AccessoryController() {
if (!(this instanceof AccessoryController)) {
return new AccessoryController();
}
this.accessoryID = 1;
this.instanceID = 1;
this.objects = {};
this.services = [];
}
module.exports = {
AccessoryController: AccessoryController
};