-
-
Notifications
You must be signed in to change notification settings - Fork 278
/
devicesInRoom.js
154 lines (149 loc) · 5.37 KB
/
devicesInRoom.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
import { RequestStatus } from '../../../utils/consts';
import createBoxActions from '../boxActions';
import createDeviceActions from '../../device';
import update from 'immutability-helper';
import get from 'get-value';
const { DEVICE_FEATURE_TYPES, DEVICE_FEATURE_CATEGORIES } = require('../../../../../server/utils/constants');
const BOX_KEY = 'DevicesInRoom';
const getLightStatus = room => {
let roomLightStatus = 0;
let hasBinaryLightDeviceFeature = false;
room.devices.forEach(device => {
device.features.forEach(feature => {
// if it's a light
const isLight =
feature.category === DEVICE_FEATURE_CATEGORIES.LIGHT && feature.type === DEVICE_FEATURE_TYPES.LIGHT.BINARY;
// if it's a light and it's turned on, we consider that the light
// is on in the room
if (isLight && feature.last_value === 1) {
roomLightStatus = 1;
}
if (isLight) {
hasBinaryLightDeviceFeature = true;
}
});
});
return {
roomLightStatus,
hasBinaryLightDeviceFeature
};
};
function createActions(store) {
const boxActions = createBoxActions(store);
const deviceActions = createDeviceActions(store);
const actions = {
async getDevicesInRoom(state, box, x, y) {
boxActions.updateBoxStatus(state, BOX_KEY, x, y, RequestStatus.Getting);
try {
const room = await state.httpClient.get(`/api/v1/room/${box.room}?expand=devices`);
// we test if there are lights ON/OFF device features to control in this room
const { hasBinaryLightDeviceFeature, roomLightStatus } = getLightStatus(room);
boxActions.mergeBoxData(state, BOX_KEY, x, y, {
room,
hasBinaryLightDeviceFeature,
roomLightStatus
});
boxActions.updateBoxStatus(state, BOX_KEY, x, y, RequestStatus.Success);
} catch (e) {
boxActions.updateBoxStatus(state, BOX_KEY, x, y, RequestStatus.Error);
}
},
async changeAllLightsStatusRoom(state, x, y, action) {
boxActions.mergeBoxData(state, BOX_KEY, x, y, {
roomLightStatus: action
});
const data = boxActions.getBoxData(state, BOX_KEY, x, y);
const promises = [];
data.room.devices.forEach(device => {
device.features.forEach(feature => {
const isLightBinary =
feature.category === DEVICE_FEATURE_CATEGORIES.LIGHT && feature.type === DEVICE_FEATURE_TYPES.LIGHT.BINARY;
if (isLightBinary) {
promises.push(deviceActions.setValue(state, feature.selector, action));
feature.last_value = action;
feature.last_value_changed = new Date();
}
});
});
boxActions.mergeBoxData(state, BOX_KEY, x, y, {
room: data.room
});
await Promise.all(promises);
},
async updateValue(state, x, y, device, deviceFeature, deviceIndex, featureIndex, action) {
await deviceActions.setValue(state, deviceFeature.selector, action);
const data = boxActions.getBoxData(state, BOX_KEY, x, y);
const newData = update(data, {
room: {
devices: {
[deviceIndex]: {
features: {
[featureIndex]: {
last_value: {
$set: action
},
last_value_changed: {
$set: new Date()
}
}
}
}
}
}
});
const { hasBinaryLightDeviceFeature, roomLightStatus } = getLightStatus(newData.room);
boxActions.mergeBoxData(state, BOX_KEY, x, y, {
room: newData.room,
hasBinaryLightDeviceFeature,
roomLightStatus
});
},
deviceFeatureWebsocketEvent(state, x, y, payload) {
const data = boxActions.getBoxData(state, BOX_KEY, x, y);
const devices = get(data, 'room.devices');
if (devices) {
let found = false;
let currentDeviceIndex = 0;
let currentFeatureIndex = 0;
while (!found && currentDeviceIndex < devices.length) {
while (!found && currentFeatureIndex < devices[currentDeviceIndex].features.length) {
if (
devices[currentDeviceIndex].features[currentFeatureIndex].selector === payload.device_feature_selector
) {
found = true;
const newData = update(data, {
room: {
devices: {
[currentDeviceIndex]: {
features: {
[currentFeatureIndex]: {
last_value: {
$set: payload.last_value
},
last_value_changed: {
$set: payload.last_value_changed
}
}
}
}
}
}
});
const { hasBinaryLightDeviceFeature, roomLightStatus } = getLightStatus(newData.room);
boxActions.mergeBoxData(state, BOX_KEY, x, y, {
room: newData.room,
hasBinaryLightDeviceFeature,
roomLightStatus
});
}
currentFeatureIndex += 1;
}
currentDeviceIndex += 1;
currentFeatureIndex = 0;
}
}
}
};
return Object.assign({}, actions, boxActions);
}
export default createActions;