-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
178 lines (142 loc) · 6.95 KB
/
index.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
const SwitchAccessory = require('./lib/SwitchAccessory');
const OutletAccessory = require('./lib/OutletAccessory');
const scanner = require('./lib/scanner');
const PLUGIN_NAME = 'homebridge-switchmate-ble';
const PLATFORM_NAME = 'SwitchmateBLE';
const SWITCH_TYPE = scanner.SWITCH;
const OUTLET_TYPE = scanner.OUTLET;
const CLASS_DEF = {[OUTLET_TYPE]: OutletAccessory, [SWITCH_TYPE]: SwitchAccessory};
let Characteristic, PlatformAccessory, Service, Categories, UUID;
module.exports = function(homebridge) {
({
platformAccessory: PlatformAccessory,
hap: {Characteristic, Service, Accessory: {Categories}, uuid: UUID}
} = homebridge);
homebridge.registerPlatform(PLUGIN_NAME, PLATFORM_NAME, SwitchmateBLE, true);
};
class SwitchmateBLE {
constructor(...props) {
[this.log, this.config, this.api] = [...props];
this.cachedAccessories = new Map();
if (this.config.timeout) scanner.timeout = this.config.timeout;
if (this.config.gap) scanner.gap = this.config.gap;
if (!isFinite(this.config.http) || this.config.http < 80) this.config.http = false;
if (this.config.http) require('./lib/WebControl').start(this);
this.groups = [];
this.devices = {};
this.config.devices.forEach(config => {
const {group, id, authCode, ...context} = config;
const devices = (Array.isArray(group) ? group : [{id: id, authCode: authCode}]).filter(device => {
if (/^[0-9a-f]{12}$/i.test(device.id)) return true;
this.log.error('Invalid device id: %s in %s', device.id, JSON.stringify(config));
});
if (devices.length > 0) {
const idx = this.groups.length;
const ids = [];
devices.forEach(device => {
this.devices[device.id] = {name: 'switchmate-' + device.id.slice(8), ...device, _group: idx};
ids.push(device.id);
});
ids.sort();
const shortIds = ids.map(id => id.slice(8)).join('.');
this.groups.push({
model: shortIds,
hwid: ids.join('.'),
...context,
devices: devices,
UUID: UUID.generate(PLUGIN_NAME + ':' + ids.join('-')),
name: config.name || shortIds
});
}
});
this.api.on('didFinishLaunching', () => {
this.discoverDevices();
});
}
discoverDevices() {
const connectedDevices = [];
const deviceIds = Object.keys(this.devices);
if (deviceIds.length === 0) return this.log.error('No valid configured devices found.');
scanner.on('discover', device => {
if (!device || !device.id) return;
if (connectedDevices.includes(device.id)) return;
if (!this.devices[device.id]) return this.log.warn('Discovered a device that has not been configured yet (%s).', device.id);
connectedDevices.push(device.id);
const group = this.groups[this.devices[device.id]._group];
let locatedCount = 0;
group.devices.forEach((config, idx) => {
if (device.id === config.id) {
device._config = config;
group.devices[idx]._located = device;
locatedCount++;
} else if (config._located) locatedCount++;
});
this.log.info('Discovered %s:%s (%d of %d)', group.name, device.id, locatedCount, group.devices.length);
if (group.devices.length === locatedCount && locatedCount > 0) {
const {devices, ...context} = group;
this.addAccessory({
...context,
devices: devices.map(device => device._located)
});
}
});
scanner.start(null, Object.keys(this.devices));
}
registerPlatformAccessories(platformAccessories) {
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, Array.isArray(platformAccessories) ? platformAccessories : [platformAccessories]);
}
configureAccessory(accessory) {
if (accessory instanceof PlatformAccessory) {
this.cachedAccessories.set(accessory.UUID, accessory);
accessory.services.forEach(service => {
if (service.UUID === Service.AccessoryInformation.UUID) return;
service.characteristics.some(characteristic => {
if (!characteristic.props ||
!Array.isArray(characteristic.props.perms) ||
characteristic.props.perms.length !== 3 ||
!(characteristic.props.perms.includes(Characteristic.Perms.WRITE) && characteristic.props.perms.includes(Characteristic.Perms.NOTIFY))
) return;
this.log.info('Marked %s unreachable by faulting Service.%s.%s', accessory.displayName, service.displayName, characteristic.displayName);
characteristic.updateValue(new Error('Unreachable'));
return true;
});
});
} else {
this.log.debug('Unregistering', accessory.displayName);
this.api.unregisterPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
}
}
addAccessory(group) {
if (group.devices.length > 1 && group.devices.some(device => device.type === scanner.OUTLET)) {
this.log.debug('Outlets cannot participate in groups (%s)', group.name);
return;
} else {
group.type = group.devices[0].type;
}
const Accessory = CLASS_DEF[group.type];
let accessory = this.cachedAccessories.get(group.UUID),
isCached = true;
if (!accessory) {
accessory = new PlatformAccessory(group.name, group.UUID, Accessory.getCategory(Categories));
accessory.getService(Service.AccessoryInformation)
.setCharacteristic(Characteristic.Manufacturer, 'Switchmate')
.setCharacteristic(Characteristic.Model, group.model)
.setCharacteristic(Characteristic.SerialNumber, '1.0');
isCached = false;
} else {
// Destroy the old one
accessory.destroy && accessory.destroy();
}
this.cachedAccessories.set(group.UUID, new Accessory(this, accessory, group, !isCached));
}
removeAccessory(homebridgeAccessory) {
if (!homebridgeAccessory) return;
delete this.cachedAccessories[homebridgeAccessory.deviceId];
this.api.unregisterPlatformAccessories(PLATFORM_NAME, PLATFORM_NAME, [homebridgeAccessory]);
}
removeAccessoryByUUID(uuid) {
if (!uuid || !this.cachedAccessories.has(uuid)) return;
this.api.unregisterPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [this.cachedAccessories.get(uuid)]);
this.cachedAccessories.delete(uuid);
}
}