-
Notifications
You must be signed in to change notification settings - Fork 16
/
SynologyAccessory.ts
309 lines (269 loc) · 10.2 KB
/
SynologyAccessory.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import {
AccessoryConfig,
AccessoryPlugin,
API,
CharacteristicEventTypes,
CharacteristicSetCallback,
CharacteristicValue,
HAP,
Logging,
Service,
} from 'homebridge';
import PollingService from './PollingService';
import ping from 'ping';
import syno from 'syno';
import wol from 'wol';
interface RawQueryParams {
sessionName: string;
api: string;
method: string;
version: string;
path: string;
}
interface DiskInfo {
temp: number;
}
enum deviceStatus {
Online = 'Online',
Offline = 'Offline',
WakingUp = 'WakingUp',
ShuttingDown = 'ShuttingDown',
}
let hap: HAP;
export = (api: API) => {
hap = api.hap;
api.registerAccessory('synology', SynologyAccessory);
};
class SynologyAccessory implements AccessoryPlugin {
private readonly log: Logging;
private readonly api: API;
private readonly name: string;
private readonly host: string;
private readonly mac: string;
private readonly shutdownTime: number;
private readonly startupTime: number;
private readonly disabled: Array<string>;
private readonly config: Record<string, unknown>;
private dsm?: typeof syno;
private readonly informationService: Service;
private readonly switchService?: Service;
private readonly temperatureService?: Service;
private readonly diskTemperatureService?: Service;
private state: deviceStatus;
constructor(log: Logging, config: AccessoryConfig, api: API) {
this.log = log;
this.api = api;
this.name = config.name;
this.mac = config.mac;
this.host = config.host;
this.shutdownTime = config.shutdownTime || 60;
this.startupTime = config.startupTime || 60;
this.disabled = config.disabled || [];
this.state = deviceStatus.Offline;
this.config = {
ignoreCertificateErrors: true,
host: config.host,
port: config.port || '5000',
account: config.username,
passwd: config.password,
protocol: config.protocol,
apiVersion: config.version || '6.2.2',
otp: config.otp || undefined,
};
this.informationService = new hap.Service.AccessoryInformation()
.setCharacteristic(hap.Characteristic.Manufacturer, 'Synology')
.setCharacteristic(hap.Characteristic.Model, config.model || 'Diskstation')
.setCharacteristic(hap.Characteristic.FirmwareRevision, config.version || '6.2.2')
.setCharacteristic(hap.Characteristic.SerialNumber, config.serial || 'n/a');
this.switchService = new hap.Service.Switch(this.name);
this.switchService.getCharacteristic(hap.Characteristic.On)
.on(CharacteristicEventTypes.SET, this.setState.bind(this));
this.startStatePolling();
if (!this.disabled.includes('temperature')) {
this.temperatureService = new hap.Service.TemperatureSensor(`${this.name} Temperature`, 'systemp');
this.startTemperaturePolling();
}
if (!this.disabled.includes('diskTemperature')) {
this.diskTemperatureService = new hap.Service.TemperatureSensor(`${this.name} Average Disk Temperature`, 'disktemp');
this.startDiskTemperaturePolling();
}
this.log.info(`Synology ${this.name} finished initializing!`);
}
startStatePolling(): void {
const pollStateService = new PollingService(async () => {
const res = await ping.promise.probe(this.host);
pollStateService.emit('state', res.alive);
}, 5000);
pollStateService.on('state', (state: boolean) => {
if ((this.state === deviceStatus.Online && state)
|| (this.state === deviceStatus.Offline && !state)
|| (this.state === deviceStatus.WakingUp && !state)
|| (this.state === deviceStatus.ShuttingDown && state)
) {
return;
}
this.switchService!.updateCharacteristic(hap.Characteristic.On, state);
this.state = state ? deviceStatus.Online : deviceStatus.Offline;
this.log.info(`state changed to ${this.state}; updated characteristic`);
});
pollStateService.start();
this.api.on('shutdown', (): void => pollStateService.stop());
}
startTemperaturePolling(): void {
const pollTemperatureService = new PollingService(async () => {
// don't poll temperature if device is not online
if (this.state !== deviceStatus.Online) {
this.temperatureService!.updateCharacteristic(hap.Characteristic.StatusActive, false);
this.log.debug('Device is not online; Polling temperature disabled.');
return;
}
try {
const res = await this.query('dsm', 'getInfo');
pollTemperatureService.emit('change', res.temperature);
} catch (err) {
this.temperatureService!.updateCharacteristic(hap.Characteristic.StatusActive, false);
this.log.warn(`Can't get temperature, ${err}`);
}
}, 30000);
pollTemperatureService.on('change', (temperature: number) => {
this.log.debug(`updating temperature to ${temperature} °C`);
this.temperatureService!.updateCharacteristic(hap.Characteristic.CurrentTemperature, temperature);
this.temperatureService!.updateCharacteristic(hap.Characteristic.StatusActive, true);
});
pollTemperatureService.start();
this.api.on('shutdown', (): void => pollTemperatureService.stop());
}
startDiskTemperaturePolling(): void {
const pollDiskTemperatureService = new PollingService(async () => {
// don't poll disk temperature if device is not online
if (this.state !== deviceStatus.Online) {
this.diskTemperatureService!.updateCharacteristic(hap.Characteristic.StatusActive, false);
this.log.debug('Device is not online; Polling disk temperature disabled.');
return;
}
try {
const res = await this.rawQuery({
sessionName: 'StorageInfo',
api: 'SYNO.Storage.CGI.Storage',
version: '1',
path: 'entry.cgi',
method: 'load_info',
});
const avgDiskTemp = (res.disks as DiskInfo[])
.map((d: DiskInfo) => d.temp)
.reduce((a: number, b: number) => a + b) / (res.disks as DiskInfo[]).length;
pollDiskTemperatureService.emit('change', Math.round(avgDiskTemp * 10) / 10);
} catch (err) {
this.diskTemperatureService!.updateCharacteristic(hap.Characteristic.StatusActive, false);
this.log.warn(`Can't get average disk temperature, ${err}`);
}
}, 30000);
pollDiskTemperatureService.on('change', (temperature: number) => {
this.log.debug(`updating average disk temperature to ${temperature} °C`);
this.diskTemperatureService!.updateCharacteristic(hap.Characteristic.CurrentTemperature, temperature);
this.diskTemperatureService!.updateCharacteristic(hap.Characteristic.StatusActive, true);
});
pollDiskTemperatureService.start();
this.api.on('shutdown', (): void => pollDiskTemperatureService.stop());
}
async setState(value: CharacteristicValue, callback: CharacteristicSetCallback) {
// no change if device is waking up or shutting down
if (this.state === deviceStatus.WakingUp || this.state === deviceStatus.ShuttingDown) {
return callback();
}
// return if states matches
if (this.state === deviceStatus.Online && value as boolean) {
return callback();
}
if (value as boolean) {
this.log.debug(`wake up ${this.name}.`);
wol.wake(this.mac, (err) => {
if (err) {
this.log.warn(`Could not wake up ${this.name}: ${err}`);
this.state = deviceStatus.Offline;
return;
}
this.log.info(`${this.name} woke up!`);
this.state = deviceStatus.WakingUp;
setTimeout(() => {
if (this.state !== deviceStatus.WakingUp) {
return;
}
this.log.error(`Startup time of ${this.startupTime}s expired, reverting state to offline`);
this.state = deviceStatus.Offline;
}, this.startupTime * 1000);
});
callback();
} else {
try {
this.log.info(`Shutting down ${this.name}`);
await this.query('dsm', 'shutdownSystem');
this.state = deviceStatus.ShuttingDown;
this.dsm = undefined;
setTimeout(() => {
if (this.state !== deviceStatus.ShuttingDown) {
return;
}
this.log.error(`Shutdown time of ${this.shutdownTime}s expired, reverting state to online`);
this.state = deviceStatus.Online;
}, this.shutdownTime * 1000);
} catch(err) {
this.log.error(`Can't shutdown ${this.name}: ${err}`);
this.state = deviceStatus.Online;
}
callback();
}
}
query(api: string, method: string, params:Record<string, unknown> = {}) {
return new Promise<Record<string, unknown>>((resolve, reject) => {
if (this.state !== deviceStatus.Online) {
return reject(new Error(`${this.name} is offline, waking up or shutting down. No query possible`));
}
if (!this.dsm) {
this.dsm = new syno(this.config);
}
this.dsm[api][method](params, (err: Record<string, unknown>, data: Record<string, unknown>) => {
if (err) {
return reject(err);
}
resolve(data);
});
});
}
rawQuery(params: RawQueryParams) {
return new Promise<Record<string, unknown>>((resolve, reject) => {
if (this.state !== deviceStatus.Online) {
return reject(new Error(`${this.name} is offline, waking up or shutting down. No query possible`));
}
if (!this.dsm) {
this.dsm = new syno(this.config);
}
this.dsm.dsm.requestAPI({
params: null,
done(err: Record<string, unknown>, data: Record<string, unknown>) {
if (err) {
return reject(err);
}
resolve(data);
},
apiInfos: params,
});
});
}
identify(): void {
this.log(`Identifying Synology ${this.name}`);
}
getServices(): Service[] {
const services: Service[] = [this.informationService];
if (!this.disabled.includes('switch') && this.switchService) {
services.push(this.switchService);
}
if (!this.disabled.includes('temperature') && this.temperatureService) {
services.push(this.temperatureService);
}
if (!this.disabled.includes('diskTemperature') && this.diskTemperatureService) {
services.push(this.diskTemperatureService);
}
return services;
}
}