-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathSmartBridge.ts
252 lines (229 loc) · 9.77 KB
/
SmartBridge.ts
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
import debug from 'debug';
import * as retry from 'async-retry';
import { EventEmitter } from 'events';
import { LeapClient } from './LeapClient';
import { Response, ResponseWithTag } from './Messages';
import {
BodyType,
ButtonDefinition,
ButtonGroupDefinition,
DeviceDefinition,
ExceptionDetail,
Href,
MultipleDeviceDefinition,
OneButtonDefinition,
OneButtonGroupDefinition,
OneDeviceDefinition,
OneZoneStatus,
MultipleOccupancyGroupStatus,
} from './MessageBodyTypes';
import TypedEmitter from 'typed-emitter';
const logDebug = debug('leap:bridge');
export const LEAP_PORT = 8081;
const PING_INTERVAL_MS = 300000;
const PING_TIMEOUT_MS = 10000;
const CONNECT_MAX_RETRY = 20;
export interface BridgeInfo {
firmwareRevision: string;
manufacturer: string;
model: string;
name: string;
serialNumber: string;
}
type SmartBridgeEvents = {
unsolicited: (bridgeID: string, response: Response) => void;
disconnected: () => void;
};
export class SmartBridge extends (EventEmitter as new () => TypedEmitter<SmartBridgeEvents>) {
private pingLooper: ReturnType<typeof setInterval> | null = null;
public bridgeReconfigInProgress: boolean;
constructor(public readonly bridgeID: string, public client: LeapClient) {
super();
logDebug('new bridge', bridgeID, 'being constructed');
this.bridgeReconfigInProgress = false;
client.on('unsolicited', this._handleUnsolicited.bind(this));
client.on('disconnected', this._handleDisconnect.bind(this));
this.startPingLoop();
}
public async reconfigureBridge(newClient: LeapClient) {
this.bridgeReconfigInProgress = true;
const oldClient = this.client;
// close the old client's connections and remove its references to the bridge so it can be GC'd
this.pingLooper = null;
oldClient.drain();
// replace the old client with the new
this.client = newClient;
this.client.on('unsolicited', this._handleUnsolicited.bind(this));
this.client.on('disconnected', this._handleDisconnect.bind(this));
// Make a new connection with the bridge, retry to make sure we get it
// A freshly boot bridge will refuses the connection during several seconds
await retry(
async () => {
logDebug('Connecting ...');
await this.client.connect();
logDebug('Connected');
},
{ retries: CONNECT_MAX_RETRY, factor: 1 }
);
// Send disconnect signal for re-subscribing
this.emit('disconnected');
this.startPingLoop();
this.bridgeReconfigInProgress = false;
}
private startPingLoop(): void {
this.pingLooper = setInterval((): void => {
const pingPromise = this.client.request('ReadRequest', '/server/1/status/ping');
const timeoutPromise = new Promise((resolve, reject): void => {
setTimeout((): void => {
reject('Ping timeout');
}, PING_TIMEOUT_MS);
});
Promise.race([pingPromise, timeoutPromise])
.then((resp) => {
// if the ping succeeds, there's not really anything to do.
logDebug('Ping succeeded', resp);
})
.catch((e) => {
// if it fails, however, what do we do? the client's
// behavior is to attempt to re-open the connection if it's
// lost. that means calling `this.client.close()` might
// clobber in-flight requests made between the ping timing
// out and the attempt to close it. that's bad.
//
// I think the answer is: nothing. future attempts to use
// the client will block (and potentially eventually time
// out), and we don't ever want to prevent that happening
// unless specifically requested.
logDebug('Ping failed:', e);
});
}, PING_INTERVAL_MS);
}
public start(): void {
// not much to do here, but it needs to exist if close exists.
if (this.pingLooper === null) {
logDebug('Bridge starting');
this.startPingLoop();
}
}
public close(): void {
// much as with LeapClient.close, this method will not actually prevent
// some caller from causing the client to reconnect. all this really
// does is tell the client to close the socket, and kills the
// keep-alive loop.
logDebug('bridge id', this.bridgeID, 'closing');
if (this.pingLooper !== null) {
clearTimeout(this.pingLooper);
this.pingLooper = null;
}
this.client.close();
}
public async ping(): Promise<Response> {
return await this.client.request('ReadRequest', '/server/1/status/ping');
}
public async getHref(href: Href): Promise<BodyType> {
logDebug(`client getting href ${href.href}`);
const raw = await this.client.request('ReadRequest', href.href);
return raw.Body!;
}
public async getBridgeInfo(): Promise<BridgeInfo> {
logDebug('getting bridge information');
const raw = await this.client.request('ReadRequest', '/device/1');
if ((raw.Body! as OneDeviceDefinition).Device) {
const device = (raw.Body! as OneDeviceDefinition).Device;
return {
firmwareRevision: device.FirmwareImage.Firmware.DisplayName,
manufacturer: 'Lutron Electronics Co., Inc',
model: device.ModelNumber,
name: device.FullyQualifiedName.join(' '),
serialNumber: device.SerialNumber,
};
}
throw new Error('Got bad response to bridge info request');
}
public async getDeviceInfo(): Promise<DeviceDefinition[]> {
logDebug('getting info about all devices');
const raw = await this.client.request('ReadRequest', '/device');
if ((raw.Body! as MultipleDeviceDefinition).Devices) {
const devices = (raw.Body! as MultipleDeviceDefinition).Devices;
return devices;
}
throw new Error('got bad response to all device list request');
}
public async setBlindsTilt(device: DeviceDefinition, value: number): Promise<void> {
const href = device.LocalZones[0].href + '/commandprocessor';
logDebug('setting href', href, 'to value', value);
this.client.request('CreateRequest', href, {
Command: {
CommandType: 'GoToTilt',
TiltParameters: {
Tilt: Math.round(value),
},
},
});
}
public async readBlindsTilt(device: DeviceDefinition): Promise<number> {
const resp = await this.client.request('ReadRequest', device.LocalZones[0].href + '/status');
const val = (resp.Body! as OneZoneStatus).ZoneStatus.Tilt;
logDebug('read tilt for device', device.FullyQualifiedName.join(' '), 'at', val);
return val;
}
/* A device has a list of ButtonGroup Hrefs. This method maps them to
* (promises for) the actual ButtonGroup objects themselves.
*/
public async getButtonGroupsFromDevice(
device: DeviceDefinition,
): Promise<(ButtonGroupDefinition | ExceptionDetail)[]> {
return Promise.all(
device.ButtonGroups.map((bgHref: Href) =>
this.client.request('ReadRequest', bgHref.href).then((resp: Response) => {
switch (resp.CommuniqueType) {
case 'ExceptionResponse':
return resp.Body! as ExceptionDetail;
case 'ReadResponse':
return (resp.Body! as OneButtonGroupDefinition).ButtonGroup;
default:
throw new Error('Unexpected communique type');
}
}),
),
);
}
/* Similar to getButtonGroupsFromDevice, a ButtonGroup contains a list of
* Button Hrefs. This maps them to (promises for) the actual Button
* objects themselves.
*/
public async getButtonsFromGroup(bgroup: ButtonGroupDefinition): Promise<ButtonDefinition[]> {
return Promise.all(
bgroup.Buttons.map((button: Href) =>
this.client
.request('ReadRequest', button.href)
.then((resp: Response) => (resp.Body! as OneButtonDefinition).Button),
),
);
}
public subscribeToButton(button: ButtonDefinition, cb: (r: Response) => void) {
this.client.subscribe(button.href + '/status/event', cb);
}
/* Because we can't subscribe to individual occupancysensors, we have to
* subscribe to everything and handle routing elsewhere. As such, this will
* call `cb` every time any sensor changes.
*/
public async subscribeToOccupancy(cb: (r: Response) => void): Promise<MultipleOccupancyGroupStatus> {
this.client.subscribe('/occupancygroup/status', cb).catch((e) => {
logDebug('ignoring failed subscription because response is not tagged');
});
return this.client
.request('ReadRequest', '/occupancygroup/status')
.then((resp: Response) => resp.Body! as MultipleOccupancyGroupStatus);
}
private _handleUnsolicited(response: Response) {
logDebug('bridge', this.bridgeID, 'got unsolicited message:');
logDebug(response);
this.emit('unsolicited', this.bridgeID, response);
}
private _handleDisconnect(): void {
// nothing to do here
logDebug('bridge id', this.bridgeID, 'disconnected.');
this.emit('disconnected');
}
}