forked from homebridge/HAP-NodeJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCharacteristic.js
101 lines (93 loc) · 2.56 KB
/
Characteristic.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
function Characteristic(options, onUpdate) {
if (!(this instanceof Characteristic)) {
return new Characteristic(options);
}
this.instanceID = 0;
this.accessoryID = 0;
this.type = options.type;
this.value = options.initialValue;
this.perms = options.perms;
this.format = options.format;
this.supportEvents = options.supportEvents;
this.supportBonjour = options.supportBonjour;
this.manfDescription = options.manfDescription;
this.designedMaxLength = options.designedMaxLength;
this.designedMinValue = options.designedMinValue;
this.designedMaxValue = options.designedMaxValue;
this.designedMinStep = options.designedMinStep;
this.unit = options.unit;
this.eventEnabled = false;
this.bonjourEnabled = false;
this.subscribedPeers = {};
this.onUpdate = onUpdate;
}
Characteristic.prototype = {
updateCharacteristicValue: function updateCharacteristicValue(value, peer) {
this.value = value;
this.updateValue(value, peer);
if (this.onUpdate !== null) {
this.onUpdate(value);
} else {
console.log("Update:",value);
}
},
updateCharacteristicEvent: function updateCharacteristicEvent(event, peer) {
console.log("Enable Event:",event);
this.subscribedPeers[peer] = event;
this.eventEnabled = event;
},
objectPresentation: function objectPresentation() {
var object = {
iid: this.instanceID,
type: this.type,
perms: this.perms,
format: this.format,
value: this.value,
events: this.eventEnabled,
bonjour: this.bonjourEnabled,
};
if (this.manfDescription !== undefined) {
object.description = this.manfDescription;
}
if (this.designedMaxLength !== undefined) {
object.maxLen = this.designedMaxLength;
}
if (this.designedMinValue !== undefined) {
object.minValue = this.designedMinValue;
}
if (this.designedMaxValue !== undefined) {
object.maxValue = this.designedMaxValue;
}
if (this.designedMinStep !== undefined) {
object.minStep = this.designedMinStep;
}
if (this.unit !== undefined) {
object.unit = this.unit;
}
return object;
},
updateValue: function updateValue(value, peer) {
this.value = value;
if(this.eventEnabled) {
if (this.accessoryController !== undefined) {
var eventDict = {
characteristics: [
{
aid: this.accessoryID,
iid: this.instanceID,
value: this.value
}
]
}
var eventJSON = JSON.stringify(eventDict);
this.accessoryController.broadcastEvent(eventJSON, this.subscribedPeers, peer);
}
}
},
valueForUpdate: function valueForUpdate() {
return this.value;
}
}
module.exports = {
Characteristic: Characteristic
};