groups.js
4.62 KB
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
'use strict';
const ApiEndpoint = require('./endpoint')
, ApiError = require('../../../ApiError')
, util = require('../../../util')
, GroupIdPlaceholder = require('../../../placeholders/GroupIdPlaceholder')
, model = require('@peter-murray/hue-bridge-model').model
, GroupState = model.GroupState
, instanceChecks = model.instanceChecks
;
const GROUP_ID_PLACEHOLDER = new GroupIdPlaceholder();
module.exports = {
getAllGroups: new ApiEndpoint()
.get()
.uri('/<username>/groups')
.acceptJson()
.pureJson()
.postProcess(buildGroupsResult),
createGroup: new ApiEndpoint()
.post()
.uri('/<username>/groups')
.payload(buildGroupBody)
.acceptJson()
.pureJson()
.postProcess(buildCreateGroupResult),
getGroupAttributes: new ApiEndpoint()
.get()
.uri('/<username>/groups/<id>')
.placeholder(GROUP_ID_PLACEHOLDER)
.acceptJson()
.pureJson()
.postProcess(buildGroup),
setGroupAttributes: new ApiEndpoint()
.put()
.uri('/<username>/groups/<id>')
.placeholder(GROUP_ID_PLACEHOLDER)
.acceptJson()
.payload(buildGroupAttributeBody)
.pureJson()
.postProcess(util.wasSuccessful),
setGroupState: new ApiEndpoint()
.put()
.uri('/<username>/groups/<id>/action')
.placeholder(GROUP_ID_PLACEHOLDER)
.payload(buildGroupStateBody)
.pureJson()
.postProcess(util.wasSuccessful),
deleteGroup: new ApiEndpoint()
.delete()
.uri('/<username>/groups/<id>')
.placeholder(GROUP_ID_PLACEHOLDER)
.pureJson()
.postProcess(validateGroupDeletion),
setStreaming: new ApiEndpoint()
.put()
.uri('/<username>/groups/<id>')
.placeholder(GROUP_ID_PLACEHOLDER)
.payload(buildStreamBody)
.pureJson()
.postProcess(util.wasSuccessful),
};
function buildGroupsResult(result) {
const groups = [];
Object.keys(result).forEach(groupId => {
const payload = result[groupId]
, type = payload.type.toLowerCase()
, group = model.createFromBridge(type, groupId, payload)
;
groups.push(group);
});
return groups;
}
//TODO this is questionable
function buildCreateGroupResult(result) {
const hueErrors = util.parseErrors(result); //TODO not sure if this still gets called as the request handles some of this
if (hueErrors) {
throw new ApiError(`Error creating group: ${hueErrors[0].description}`, hueErrors[0]);
}
return {id: Number(result[0].success.id)};
}
function buildGroupStateBody(data) {
if (!data || !data.state) {
throw new ApiError('A GroupState must be provided');
}
let state;
if (data.state instanceof GroupState) {
state = data.state;
} else {
state = new GroupState().populate(data.state);
}
return {
type: 'application/json',
body: state.getPayload()
};
}
function buildGroupBody(parameters) {
const group = parameters.group;
if (! group) {
throw new ApiError('A group must be provided');
}
if (!instanceChecks.isGroupInstance(group)) {
throw new ApiError('group parameter must be an instance of a Group');
}
const result = {
type: 'application/json',
body: {
name: group.name,
type: group.type,
}
};
if (group.lights) {
result.body.lights = group.lights;
} else if (group.type === 'Entertainment') {
// Entertainment requires a empty array to be passed in if no lights defined.
result.body.lights = [];
}
if (group.class) {
result.body.class = group.class;
} else {
result.body.recycle = group.recycle;
}
return result;
}
function buildGroupAttributeBody(parameters) {
const body = {}
, group = parameters.group
;
if (!group) {
throw new ApiError('A group is required to update attributes')
}
//TODO if you have an entertainment group and are updating the lights, they must be capabilities.streaming.renderer = true
let payload;
if (instanceChecks.isGroupInstance(group)) {
payload = group.getHuePayload();
} else {
payload = group;
}
['name', 'lights', 'class'].forEach(key => {
if (payload[key]) {
body[key] = payload[key];
}
});
return {
type: 'application/json',
body: body
};
}
function buildStreamBody(parameters) {
const body = {
stream: {
active: !!parameters.active
}
};
return {
type: 'application/json',
body: body
};
}
function buildGroup(data, requestParameters) {
const type = data.type.toLowerCase()
, id = GROUP_ID_PLACEHOLDER.getValue(requestParameters)
;
return model.createFromBridge(type, id, data);
}
function validateGroupDeletion(result) {
if (!util.wasSuccessful(result)) {
throw new ApiError(util.parseErrors(result).join(', '));
}
return true;
}