forked from homebridge/HAP-NodeJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Characteristic.js
248 lines (209 loc) · 8.54 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
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
var inherits = require('util').inherits;
var EventEmitter = require('events').EventEmitter;
var once = require('./util/once').once;
'use strict';
module.exports = {
Characteristic: Characteristic
}
/**
* Characteristic represents a particular typed variable that can be assigned to a Service. For instance, a
* "Hue" Characteristic might store a 'float' value of type 'arcdegrees'. You could add the Hue Characteristic
* to a Service in order to store that value. A particular Characteristic is distinguished from others by its
* UUID. HomeKit provides a set of known Characteristic UUIDs defined in HomeKitTypes.js along with a
* corresponding concrete subclass.
*
* You can also define custom Characteristics by providing your own UUID. Custom Characteristics can be added
* to any native or custom Services, but Siri will likely not be able to work with these.
*
* Note that you can get the "value" of a Characteristic by accessing the "value" property directly, but this
* is really a "cached value". If you want to fetch the latest value, which may involve doing some work, then
* call getValue().
*
* @event 'get' => function(callback(err, newValue), context) { }
* Emitted when someone calls getValue() on this Characteristic and desires the latest non-cached
* value. If there are any listeners to this event, one of them MUST call the callback in order
* for the value to ever be delivered. The `context` object is whatever was passed in by the initiator
* of this event (for instance whomever called `getValue`).
*
* @event 'set' => function(newValue, callback(err, changedNewValue), context) { }
* Emitted when someone calls setValue() on this Characteristic with a desired new value. If there
* are any listeners to this event, one of them MUST call the callback in order for this.value to
* actually be set. Note that you can optionally set a *different* value than the one requested by
* passing in the changedNewValue argument to the callback. The `context` object is whatever was
* passed in by the initiator of this change (for instance whomever called `setValue`).
*
* @event 'change' => function({ oldValue, newValue, context }) { }
* Emitted after a change in our value has occurred. The new value will also be immediately accessible
* in this.value. The event object contains the new value as well as the context object originally
* passed in by the initiator of this change (if known).
*/
function Characteristic(displayName, UUID, props) {
this.displayName = displayName;
this.UUID = UUID;
this.iid = null; // assigned by our containing Service
this.value = null;
this.props = props || {
format: null,
unit: null,
minValue: null,
maxValue: null,
minStep: null,
perms: []
};
}
inherits(Characteristic, EventEmitter);
// Known HomeKit formats
Characteristic.Formats = {
BOOL: 'bool',
INT: 'int',
FLOAT: 'float',
STRING: 'string',
ARRAY: 'array', // unconfirmed
DICTIONARY: 'dictionary', // unconfirmed
UINT8: 'uint8',
UINT16: 'uint16',
UINT32: 'uint32',
UINT64: 'uint64',
DATA: 'data', // unconfirmed
TLV8: 'tlv8'
}
// Known HomeKit unit types
Characteristic.Units = {
CELSIUS: 'celsius',
FAHRENHEIT: 'fahrenheit',
PERCENTAGE: 'percentage',
ARC_DEGREE: 'arcdegrees',
LUX: 'lux',
SECONDS: 'seconds'
}
// Known HomeKit permission types
Characteristic.Perms = {
READ: 'pr',
WRITE: 'pw',
NOTIFY: 'ev'
}
/**
* Copies the given properties to our props member variable,
* and returns 'this' for chaining.
*
* @param 'props' {
* format: <one of Characteristic.Formats>,
* unit: <one of Characteristic.Units>,
* minValue: <minimum value for numeric characteristics>,
* maxValue: <maximum value for numeric characteristics>,
* minStep: <smallest allowed increment for numeric characteristics>,
* perms: array of [Characteristic.Perms] like [Characteristic.Perms.READ, Characteristic.Perms.WRITE]
* }
*/
Characteristic.prototype.setProps = function(props) {
for (var key in (props || {}))
if (Object.prototype.hasOwnProperty.call(props, key))
this.props[key] = props[key];
return this;
}
Characteristic.prototype.getValue = function(callback, context) {
if (this.listeners('get').length > 0) {
// allow a listener to handle the fetching of this value, and wait for completion
this.emit('get', once(function(err, newValue) {
if (err) {
// pass the error along to our callback
if (callback) callback(err);
}
else {
// getting the value was a success; we can pass it along and also update our cached value
var oldValue = this.value;
this.value = newValue;
if (callback) callback(null, newValue);
// emit a change event if necessary
if (oldValue !== newValue)
this.emit('change', { oldValue:oldValue, newValue:newValue, context:context });
}
}.bind(this)), context);
}
else {
// no one is listening to the 'get' event, so just return the cached value
if (callback)
callback(null, this.value);
}
}
Characteristic.prototype.setValue = function(newValue, callback, context) {
if (this.listeners('set').length > 0) {
// allow a listener to handle the setting of this value, and wait for completion
this.emit('set', newValue, once(function(err, changedNewValue) {
if (err) {
// pass the error along to our callback
if (callback) callback(err);
}
else {
// did the caller specify change value we should be setting?
var actualNewValue = (typeof changedNewValue !== 'undefined') ? changedNewValue : newValue;
// setting the value was a success; so we can cache it now
var oldValue = this.value;
this.value = actualNewValue;
if (callback) callback();
// emit a change event if necessary
if (oldValue !== actualNewValue)
this.emit('change', { oldValue:oldValue, newValue:actualNewValue, context:context });
}
}.bind(this)), context);
}
else {
// no one is listening to the 'set' event, so just assign the value blindly
var oldValue = this.value;
this.value = newValue;
if (callback) callback();
// emit a change event if necessary
if (oldValue !== newValue)
this.emit('change', { oldValue:oldValue, newValue:newValue, context:context });
}
return this; // for chaining
}
Characteristic.prototype.getDefaultValue = function() {
switch (this.format) {
case Characteristic.Formats.BOOL: return false;
case Characteristic.Formats.STRING: return null;
case Characteristic.Formats.ARRAY: return []; // who knows!
case Characteristic.Formats.DICTIONARY: return {}; // who knows!
case Characteristic.Formats.DATA: return null; // who knows!
case Characteristic.Formats.TLV8: return null; // who knows!
default: return this.props.minValue || 0;
}
}
Characteristic.prototype._assignID = function(identifierCache, accessoryName, serviceUUID, serviceSubtype) {
// generate our IID based on our UUID
this.iid = identifierCache.getIID(accessoryName, serviceUUID, serviceSubtype, this.UUID);
}
/**
* Returns a JSON representation of this Accessory suitable for delivering to HAP clients.
*/
Characteristic.prototype.toHAP = function(opt) {
// ensure our value fits within our constraints if present
var value = this.value;
if (this.props.minValue != null && value < this.props.minValue) value = this.props.minValue;
if (this.props.maxValue != null && value > this.props.maxValue) value = this.props.maxValue;
var hap = {
iid: this.iid,
type: this.UUID,
perms: this.props.perms,
format: this.props.format,
value: value,
description: this.displayName
// These properties used to be sent but do not seem to be used:
//
// maxLen: this.format === Characteristic.Formats.STRING ? 255 : 1,
// events: false,
// bonjour: false
};
// extra properties
if (this.props.unit != null) hap.unit = this.props.unit;
if (this.props.maxValue != null) hap.maxValue = this.props.maxValue;
if (this.props.minValue != null) hap.minValue = this.props.minValue;
if (this.props.minStep != null) hap.minStep = this.props.minStep;
// if we're not readable, omit the "value" property - otherwise iOS will complain about non-compliance
if (this.props.perms.indexOf(Characteristic.Perms.READ) == -1)
delete hap.value;
// delete the "value" property anyway if we were asked to
if (opt && opt.omitValues)
delete hap.value;
return hap;
}