This repository has been archived by the owner on Apr 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
query.service.ts
69 lines (65 loc) · 2.63 KB
/
query.service.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
import { Injectable } from '@andrei-tatar/ts-ioc';
import { QueryDevice, QueryDevices, QueryInput, QueryPayload } from '../../google';
import { Device } from '../../models';
import { DevicesRepository } from '../../services/devices.repository';
@Injectable()
export class QueryService {
constructor(
private devices: DevicesRepository,
) {
}
query(input: QueryInput): QueryPayload {
const queryIds = input.payload.devices.map(d => d.id);
const userDevices = this.devices.getDevicesById(queryIds);
const devices: QueryDevices = {};
for (const [index, device] of userDevices.entries()) {
const id = queryIds[index];
if (!device) {
devices[id] = { online: false };
} else {
const state: QueryDevice = {
online: device.state.online,
};
this.updateQueryState(state, device);
devices[id] = state;
}
}
return { devices };
}
private updateQueryState(state: QueryDevice, device: Device) {
switch (device.type) {
case 'outlet':
case 'switch':
state.on = device.state.on;
break;
case 'light':
state.on = device.state.on;
if (device.brightnessControl) {
state.brightness = device.state.brightness || 100;
}
if (device.colorControl) {
state.color = device.state.color;
}
break;
case 'thermostat':
state.thermostatMode = device.state.thermostatMode;
state.thermostatTemperatureAmbient = device.state.thermostatTemperatureAmbient;
state.thermostatHumidityAmbient = device.state.thermostatHumidityAmbient;
if (device.state.thermostatMode === 'heatcool') {
state.thermostatTemperatureSetpointHigh = device.state.thermostatTemperatureSetpointHigh;
state.thermostatTemperatureSetpointLow = device.state.thermostatTemperatureSetpointLow;
} else {
state.thermostatTemperatureSetpoint = device.state.thermostatTemperatureSetpoint;
}
break;
case 'speaker':
state.on = device.state.on;
state.currentVolume = device.state.currentVolume;
state.isMuted = device.state.isMuted;
break;
case 'blinds':
state.openPercent = device.state.openPercent;
break;
}
}
}