scenes.js
4.38 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
'use strict';
const ApiEndpoint = require('./endpoint')
, SceneIdPlaceholder = require('../../../placeholders/SceneIdPlaceholder')
, LightIdPlacehoder = require('../../../placeholders/LightIdPlaceholder')
, model = require('@peter-murray/hue-bridge-model').model
, instanceChecks = model.instanceChecks
, SceneLightState = model.lightStates.SceneLightState
, ApiError = require('../../../ApiError')
, util = require('../../../util')
;
const SCENE_ID_PLACEHOLDER = new SceneIdPlaceholder();
module.exports = {
getAll: new ApiEndpoint()
.get()
.acceptJson()
.uri('/<username>/scenes')
.pureJson()
.postProcess(buildScenesResult),
createScene: new ApiEndpoint()
.post()
.acceptJson()
.uri('/<username>/scenes')
.pureJson()
.payload(getCreateScenePayload)
.postProcess(buildCreateSceneResult),
updateScene: new ApiEndpoint()
.put()
.acceptJson()
.uri('/<username>/scenes/<id>')
.placeholder(new SceneIdPlaceholder())
.pureJson()
.payload(buildBasicSceneUpdatePayload)
.postProcess(util.extractUpdatedAttributes),
updateSceneLightState: new ApiEndpoint()
.put()
.acceptJson()
.uri('/<username>/scenes/<id>/lightstates/<lightStateId>')
.placeholder(SCENE_ID_PLACEHOLDER)
.placeholder(new LightIdPlacehoder('lightStateId'))
.pureJson()
.payload(buildUpdateSceneLightStatePayload)
.postProcess(util.extractUpdatedAttributes),
getScene: new ApiEndpoint()
.get()
.acceptJson()
.uri('/<username>/scenes/<id>')
.placeholder(SCENE_ID_PLACEHOLDER)
.pureJson()
.postProcess(buildSceneResult),
deleteScene: new ApiEndpoint()
.delete()
.acceptJson()
.uri('/<username>/scenes/<id>')
.placeholder(SCENE_ID_PLACEHOLDER)
.pureJson()
.postProcess(validateSceneDeletion),
};
function buildScenesResult(result) {
let scenes = [];
Object.keys(result).forEach(function (id) {
const data = result[id]
, type = data.type.toLowerCase()
;
const scene = model.createFromBridge(type, id, data);
scenes.push(scene);
});
return scenes;
}
function buildSceneResult(data, requestParameters) {
const type = data.type.toLowerCase()
, id = SCENE_ID_PLACEHOLDER.getValue(requestParameters)
;
return model.createFromBridge(type, id, data);
}
function validateSceneDeletion(result) {
if (!util.wasSuccessful(result)) {
throw new ApiError(util.parseErrors(result).join(', '));
}
return true;
}
function getCreateScenePayload(parameters) {
const scene = parameters.scene;
if (!scene) {
throw new ApiError('No scene provided');
} else if (!instanceChecks.isSceneInstance(scene)) {
throw new ApiError('Must provide a valid Scene object');
}
const body = scene.getHuePayload();
// Remove properties that are not used is creation
delete body.id;
delete body.locked;
delete body.owner;
delete body.lastupdated;
delete body.version;
return {
type: 'application/json',
body: body
};
}
//TODO
function buildUpdateSceneLightStatePayload(parameters) {
const lightState = parameters.lightState;
if (!lightState) {
throw new ApiError('No SceneLightState provided');
} else if (!(lightState instanceof SceneLightState)) {
throw new ApiError('Must provide a valid SceneLightState object');
}
//TODO need to validate this object to protect ourselves here
const body = lightState.getPayload();
return {
type: 'application/json',
body: body
};
}
function buildBasicSceneUpdatePayload(parameters) {
const scene = parameters.scene;
if (!scene) {
throw new ApiError('No scene provided');
} else if (!instanceChecks.isSceneInstance(scene)) {
throw new ApiError('Must provide a valid Scene object');
}
const scenePayload = scene.getHuePayload()
, body = {}
;
// Extract the properties that we are allowed to update as per the API docs
['name', 'lights', 'lightstates', 'storelightstate'].forEach(key => {
const value = scenePayload[key];
if (value !== null) {
body[key] = value;
}
});
return {
type: 'application/json',
body: body
};
}
function buildCreateSceneResult(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 scene: ${hueErrors[0].description}`, hueErrors[0]);
}
return {id: result[0].success.id};
}