-
Notifications
You must be signed in to change notification settings - Fork 506
/
hue.cpp
336 lines (304 loc) · 11.7 KB
/
hue.cpp
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
* Handle Hue-specific FC03 cluster.
*/
#include <QString>
#include <math.h>
#include "de_web_plugin.h"
#include "de_web_plugin_private.h"
#define HUE_EFFECTS_CLUSTER_ID 0xFC03
struct code {
quint8 value;
QString name;
};
code effects[] = {
{ 0x01, QLatin1String("candle") },
{ 0x02, QLatin1String("fire") },
{ 0x03, QLatin1String("prism") },
{ 0x09, QLatin1String("sunrise") },
{ 0x0a, QLatin1String("sparkle") },
{ 0x0b, QLatin1String("opal") },
{ 0x0c, QLatin1String("glisten") },
{ 0x0d, QLatin1String("sunset") },
{ 0x0e, QLatin1String("underwater") },
{ 0x0f, QLatin1String("cosmos") },
{ 0x10, QLatin1String("sunbeam") },
{ 0x11, QLatin1String("enchant") }
};
quint8 effectNameToValue(QString &effectName)
{
for (auto &e: effects)
{
if (e.name == effectName)
{
return e.value;
}
}
return 0xFF;
}
/*! Return a list of effect names corresponding to the bitmap of supported effects.
\param effectBitmap - the bitmap with supported effects (from 0x0011)
\return QStringList of effect names
*/
QStringList DeRestPluginPrivate::getHueEffectNames(quint64 effectBitmap, bool colorloop)
{
QStringList names = { QLatin1String("none") };
if (colorloop)
{
names.append(QLatin1String("colorloop"));
}
for (auto &e: effects) {
if (effectBitmap & (0x01 << e.value))
{
names.append(e.name);
}
}
return names;
};
code styles[] = {
{ 0x00, QLatin1String("linear") }, // interpolated_palette
{ 0x02, QLatin1String("scattered") }, // random_pixelated
{ 0x04, QLatin1String("mirrored") } // interpolated_palette_mirrored
};
quint8 styleNameToValue(QString &styleName)
{
for (auto &s: styles)
{
if (s.name == styleName)
{
return s.value;
}
}
return 0xFF;
}
/*! Return a list of style names corresponding to the bitmap of supported styles.
\param styleBitmap - the bitmap with supported styles (from 0x0013)
\return QStringList of style names
*/
QStringList DeRestPluginPrivate::getHueGradientStyleNames(quint16 styleBitmap)
{
QStringList names = {};
for (auto &s: styles) {
if (styleBitmap & (0x01 << (s.value >> 1)))
{
names.append(s.name);
}
}
return names;
};
const double maxX = 0.7347;
const double maxY = 0.8431;
/*! Add a Hue effect task to the queue.
\param task - the task item
\param effectName - the effect
\return true - on success
false - on error
*/
bool DeRestPluginPrivate::addTaskHueEffect(TaskItem &task, QString &effectName)
{
task.taskType = TaskHueEffect;
task.req.setClusterId(HUE_EFFECTS_CLUSTER_ID);
task.req.setProfileId(HA_PROFILE_ID);
task.zclFrame.payload().clear();
task.zclFrame.setSequenceNumber(zclSeq++);
task.zclFrame.setCommandId(0x00);
task.zclFrame.setManufacturerCode(VENDOR_PHILIPS);
task.zclFrame.setFrameControl(deCONZ::ZclFCClusterCommand |
deCONZ::ZclFCManufacturerSpecific |
deCONZ::ZclFCDirectionClientToServer |
deCONZ::ZclFCDisableDefaultResponse);
{ // payload
QDataStream stream(&task.zclFrame.payload(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
stream << (quint16) 0x0020; // set effect
// stream << (quint16) 0x0021; // set effect (with on/off)
// stream << (quint8) 1; // on
if (effectName == "none")
{
stream << (quint8) 0; // none
}
else
{
stream << effectNameToValue(effectName);
}
}
{ // ZCL frame
task.req.asdu().clear(); // cleanup old request data if there is any
QDataStream stream(&task.req.asdu(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
task.zclFrame.writeToStream(stream);
}
return addTask(task);
}
bool DeRestPluginPrivate::validateHueGradient(const ApiRequest &req, ApiResponse &rsp, QVariantMap &gradient, quint16 styleBitmap = 0x0001)
{
QString id = req.path[3];
bool ok = true;
bool check;
if (gradient["points"].isNull())
{
rsp.list.append(errorToMap(ERR_MISSING_PARAMETER, QString("/lights/%1/state").arg(id), QString("missing parameter, gradient/points, for parameter, gradient")));
return false;
}
if (gradient["points"].type() != QVariant::List)
{
rsp.list.append(errorToMap(ERR_INVALID_VALUE, QString("/lights/%1/state").arg(id), QString("invalid value, %1, for parameter, gradient/points").arg(gradient["points"].toString())));
return false;
}
QVariantList &points = *reinterpret_cast<QVariantList *>(gradient["points"].data()); // Create reference instead of copy
const quint8 length = points.length();
if (length < 2 || length > 9)
{
rsp.list.append(errorToMap(ERR_INVALID_VALUE, QString("/lights/%1/state").arg(id), QString("invalid length, %1, for parameter, gradient/points").arg(length)));
return false;
}
if (gradient["segments"].isNull()) gradient["segments"] = length;
if (gradient["color_adjustment"].isNull()) gradient["color_adjustment"] = 0;
if (gradient["offset"].isNull()) gradient["offset"] = 0;
if (gradient["offset_adjustment"].isNull()) gradient["offset_adjustment"] = 0;
if (gradient["style"].isNull()) gradient["style"] = "linear";
for (QVariantMap::const_iterator p = gradient.begin(); p != gradient.end(); p++)
{
QString param = p.key();
if (param == "points")
{
int i = -1;
for (auto &point : points)
{
i++;
if (point.type() != QVariant::List)
{
rsp.list.append(errorToMap(ERR_INVALID_VALUE, QString("/lights/%1/state").arg(id), QString("invalid value, %1, for parameter, gradient/points/%2").arg(point.toString()).arg(i)));
ok = false;
continue;
}
QVariantList &xy = *reinterpret_cast<QVariantList *>(point.data()); // Create reference instead of copy
if (xy.length() != 2)
{
rsp.list.append(errorToMap(ERR_INVALID_VALUE, QString("/lights/%1/state").arg(id), QString("invalid length, %1, for parameter, gradient/points/%2").arg(xy.length()).arg(i)));
ok = false;
continue;
}
double x = xy[0].toDouble(&check);
if (!check || x < 0 || x > 1)
{
rsp.list.append(errorToMap(ERR_INVALID_VALUE, QString("/lights/%1/state").arg(id), QString("invalid value, %1, for parameter, gradient/points/%2/0").arg(xy[0].toString()).arg(i)));
ok = false;
}
if (x > maxX) xy[0] = maxX; // This is why we needed a reference
double y = xy[1].toDouble(&check);
if (!check || y < 0 || y > 1)
{
rsp.list.append(errorToMap(ERR_INVALID_VALUE, QString("/lights/%1/state").arg(id), QString("invalid value, %1, for parameter, gradient/points/%2/1").arg(xy[1].toString()).arg(i)));
ok = false;
}
if (y > maxY) xy[1] = maxY; // This is why we needed a reference
}
}
else if (param == "segments" || param == "offset")
{
quint8 value = gradient[param].toUInt(&check);
if (!check || value > 0x1F)
{
rsp.list.append(errorToMap(ERR_INVALID_VALUE, QString("/lights/%1/state").arg(id), QString("invalid value, %1, for parameter, gradient/%2").arg(gradient[param].toString()).arg(param)));
ok = false;
}
}
else if (param == "style")
{
QString styleName = gradient[param].toString();
bool valid = false;
for (auto &s: styles)
{
if (styleName == s.name && (styleBitmap & (0x01 << (s.value >> 1)))) {
valid = true;
break;
}
}
if (!valid)
{
rsp.list.append(errorToMap(ERR_INVALID_VALUE, QString("/lights/%1/state").arg(id), QString("invalid value, %1, for parameter, gradient/%2").arg(gradient[param].toString()).arg(param)));
ok = false;
}
}
else if (param == "color_adjustment" || param == "offset_adjustment")
{
quint8 value = gradient[param].toUInt(&check);
if (!check || value > 0x07)
{
rsp.list.append(errorToMap(ERR_INVALID_VALUE, QString("/lights/%1/state").arg(id), QString("invalid value, %1, for parameter, gradient/%2").arg(gradient[param].toString()).arg(param)));
ok = false;
}
}
else
{
rsp.list.append(errorToMap(ERR_PARAMETER_NOT_AVAILABLE, QString("/lights/%1/state").arg(id), QString("parameter, gradient/%1, not available").arg(param)));
ok = false;
}
}
return ok;
}
void streamPoint(QDataStream &stream, double x, double y)
{
const quint16 rawX = (x >= maxX) ? 4095 : floor(x * 4095 / maxX);
const quint16 rawY = (y >= maxY) ? 4095 : floor(y * 4095 / maxY);
stream << (quint8) (rawX & 0x0FF);
stream << (quint8) (((rawX & 0xF00) >> 8) | ((rawY & 0x00F) << 4));
stream << (quint8) ((rawY & 0xFF0) >> 4);
}
/*! Add a Hue gradient task to the queue.
\param task - the task item
\param gradient - the gradient
\return true - on success
false - on error
*/
bool DeRestPluginPrivate::addTaskHueGradient(TaskItem &task, QVariantMap &gradient)
{
task.taskType = TaskHueGradient;
task.req.setClusterId(HUE_EFFECTS_CLUSTER_ID);
task.req.setProfileId(HA_PROFILE_ID);
task.zclFrame.payload().clear();
task.zclFrame.setSequenceNumber(zclSeq++);
task.zclFrame.setCommandId(0x00);
task.zclFrame.setManufacturerCode(VENDOR_PHILIPS);
task.zclFrame.setFrameControl(deCONZ::ZclFCClusterCommand |
deCONZ::ZclFCManufacturerSpecific |
deCONZ::ZclFCDirectionClientToServer |
deCONZ::ZclFCDisableDefaultResponse);
QVariantList points = gradient["points"].toList();
QVariantList point;
quint8 style = 0xFF;
for (auto &s: styles)
{
if (gradient["style"] == s.name)
{
style = s.value;
break;
}
}
{ // payload
QDataStream stream(&task.zclFrame.payload(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
stream << (quint16) 0x0150; // set gradient
stream << (quint16) 0x0004; // transitiontime
const quint8 nPoints = points.length();
stream << (quint8) (1 + 3 * (nPoints + 1));
stream << (quint8) (nPoints << 4);
stream << (quint8) style;
stream << (quint8) 0;
stream << (quint8) 0;
for (auto &point : points)
{
QVariantList xy = point.toList();
streamPoint(stream, xy[0].toDouble(), xy[1].toDouble());
}
stream << (quint8) ((gradient["segments"].toUInt() << 3) | gradient["color_adjustment"].toUInt());
stream << (quint8) ((gradient["offset"].toUInt() << 3)| gradient["offset_adjustment"].toUInt());
}
{ // ZCL frame
task.req.asdu().clear(); // cleanup old request data if there is any
QDataStream stream(&task.req.asdu(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
task.zclFrame.writeToStream(stream);
}
return addTask(task);
}