forked from dresden-elektronik/deconz-rest-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
device_setup.cpp
executable file
·422 lines (357 loc) · 15 KB
/
device_setup.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
* device_setup.cpp
*
* Configures ubisys manufacturer specific Device_Setup Cluster 0xFC00 on Endpoint 232 (0xE8).
*
* It can be used to control the behavior of inputs (i.e. permanent switches, push-button switches,
* normally open vs. normally closed, on/off, level control or scene selection, etc.)
* Example usage:
* -X PUT -d '{ "mode": "momentary" }' /sensor/xx/config
* -X PUT -d '{ "mode": "rocker" }' /sensor/xx/config
*
* In addition to predefined configurations for momentary switches and rocker switches,
* this implementation allows custom configurations for a more complex move or recall scene command.
* Example usage:
* -X PUT -d '{ "mode": "custom_41020006000D0306000206010D04060002" }' /sensor/xx/config
* writes ZCL raw data "41020006000D0306000206010D04060002" to attribute 0x0001 on cluster 0xFC00
*
* Implements support for:
* - ubisys S1 power switch
* - ubisys S2 power switch
* - ubisys J1 shutter control
*
*/
#include "de_web_plugin.h"
#include "de_web_plugin_private.h"
#define UBISYS_J1 0x01
#define UBISYS_S1 0x02
#define UBISYS_S2 0x03
static bool addPayloadJ1(TaskItem &task, ResourceItem *item)
{ // payload
QDataStream stream(&task.zclFrame.payload(), QIODevice::ReadWrite);
stream.setByteOrder(QDataStream::LittleEndian);
if (item->toString() == QLatin1String("momentary")) // default configuration for J1
{
stream << (quint16)0x0001; // attribute id InputActions
stream << (quint8)0x48; // attribute type array
stream << (quint8)0x41; // attribute datatype: octed string
stream << (quint16)0x0004; // element count: 0x0004 (4 entries)
stream << (quint8)0x06; // element #1: length: 6
stream << (quint8)0x00; // InputAndOptions: 0x00 = first physical input
stream << (quint8)0x0D; // transition: released -> pressed
stream << (quint8)0x02; // Source: Endpoint #2 (hosts window covering client cluster on J1)
stream << (quint16)0x0102; // Cluster ID: 0x0102 – window covering
stream << (quint8)0x00; // ZCL command template: Move up/open
stream << (quint8)0x06; // element #2
stream << (quint8)0x00;
stream << (quint8)0x07; // transition: pressed -> released
stream << (quint8)0x02;
stream << (quint16)0x0102;
stream << (quint8)0x02; // ZCL command template: Stop
stream << (quint8)0x06; // element #3
stream << (quint8)0x01; // InputAndOptions: 0x01 = second physical input
stream << (quint8)0x0D; // transition: released -> pressed
stream << (quint8)0x02;
stream << (quint16)0x0102;
stream << (quint8)0x01; // ZCL command template: Move down/close
stream << (quint8)0x06; // element #4
stream << (quint8)0x01;
stream << (quint8)0x07; // transition: pressed -> released
stream << (quint8)0x02;
stream << (quint16)0x0102;
stream << (quint8)0x02; // ZCL command template: Stop
}
else if (item->toString() == QLatin1String("rocker"))
{
stream << (quint16)0x0001; // attribute id InputActions
stream << (quint8)0x48; // attribute type array
stream << (quint8)0x41; // attribute datatype: octed string
stream << (quint16)0x0004; // element count: 0x0004 (4 entries)
stream << (quint8)0x06; // element #1: length 6
stream << (quint8)0x00; // InputAndOptions: 0x00
stream << (quint8)0x0D; // transition: released -> pressed
stream << (quint8)0x02; // Source: Endpoint #2 (hosts window covering client cluster on J1)
stream << (quint16)0x0102; // Cluster ID: 0x0102 – window covering
stream << (quint8)0x00; // ZCL command template: Move up/open
stream << (quint8)0x06; // element #2
stream << (quint8)0x00; // InputAndOptions: 0x00
stream << (quint8)0x03; // transition: any state -> released
stream << (quint8)0x02;
stream << (quint16)0x0102;
stream << (quint8)0x02; // ZCL command template: Stop
stream << (quint8)0x06; // element #3
stream << (quint8)0x01; // InputAndOptions: 0x01
stream << (quint8)0x0D; // transition: released -> pressed
stream << (quint8)0x02;
stream << (quint16)0x0102;
stream << (quint8)0x01; // ZCL command template: Move down/close
stream << (quint8)0x06; // element #4
stream << (quint8)0x01;
stream << (quint8)0x03; // transition: any state -> released
stream << (quint8)0x02;
stream << (quint16)0x0102;
stream << (quint8)0x02; // ZCL command template: Stop
}
else if (item->toString().startsWith("custom_"))
{
stream << (quint16)0x0001; // attribute id InputActions
stream << (quint8)0x48; // attribute type array
// Example J1 momentary: "custom_41040006000d020201000600070202010206010d0202010106010702020102"
// Example J1 rocker: "custom_41040006000d020201000600030202010206010d0202010106010302020102"
// ^^ ^^
QString subStr("custom");
QByteArray command = item->toString().toUtf8();
command.remove(0, subStr.size() + 1); // remove custom_
QByteArray payload = QByteArray::fromHex(command);
stream.writeRawData(payload.data(), payload.size());
item->setValue(subStr); // setValue "custom"
}
else
{
return false; // do nothing
}
return true;
}
static bool addPayloadS1(TaskItem &task, ResourceItem *item)
{ // payload
QDataStream stream(&task.zclFrame.payload(), QIODevice::ReadWrite);
stream.setByteOrder(QDataStream::LittleEndian);
if (item->toString() == QLatin1String("momentary"))
{
stream << (quint16)0x0001; // attribute id InputActions
stream << (quint8)0x48; // attribute type array
stream << (quint8)0x41; // attribute datatype: octed string
stream << (quint16)0x0002; // element count: 0x0001 (1 entries)
stream << (quint8)0x06; // element #1: length: 6
stream << (quint8)0x00; // InputAndOptions: 0x00 = first physical input
stream << (quint8)0x0D; // transition: released -> pressed
stream << (quint8)0x02; // Source: Endpoint #2 (hosts primary on/off client cluster on S1)
stream << (quint16)0x0006; // Cluster ID: 0x0006 - on/off
stream << (quint8)0x02; // ZCL command template: Toggle
}
else if (item->toString() == QLatin1String("rocker")) // default configuration for S1
{
stream << (quint16)0x0001; // attribute id InputActions
stream << (quint8)0x48; // attribute type array
stream << (quint8)0x41; // attribute datatype: octed string
stream << (quint16)0x0004; // element count: 0x0002 (2 entries)
stream << (quint8)0x06; // element #1: length: 6
stream << (quint8)0x00; // InputAndOptions: 0x00 = first physical input
stream << (quint8)0x0D; // transition: released -> pressed
stream << (quint8)0x02; // Source: Endpoint #2 (hosts primary on/off client cluster on S1)
stream << (quint16)0x0006; // Cluster ID: 0x0006 - on/off
stream << (quint8)0x02; // ZCL command template: Toggle
stream << (quint8)0x06; // element #2: length: 6
stream << (quint8)0x00; // InputAndOptions: 0x00 = first physical input
stream << (quint8)0x03; // transition: any state -> released
stream << (quint8)0x02; // Source: Endpoint #2 (hosts primary on/off client cluster on S1)
stream << (quint16)0x0006; // Cluster ID: 0x0006 - on/off
stream << (quint8)0x02; // ZCL command template: Toggle
}
else if (item->toString().startsWith("custom_"))
{
stream << (quint16)0x0001; // attribute id InputActions
stream << (quint8)0x48; // attribute type array
QString subStr("custom");
QByteArray command = item->toString().toUtf8();
command.remove(0, subStr.size() + 1); // remove custom_
QByteArray payload = QByteArray::fromHex(command);
stream.writeRawData(payload.data(), payload.size());
item->setValue(subStr); // setValue "custom"
}
else
{
return false; // do nothing
}
return true;
}
static bool addPayloadS2(TaskItem &task, ResourceItem *item)
{ // payload
QDataStream stream(&task.zclFrame.payload(), QIODevice::ReadWrite);
stream.setByteOrder(QDataStream::LittleEndian);
if (item->toString() == QLatin1String("momentary")) // default configuration for S2
{
stream << (quint16)0x0001; // attribute id InputActions
stream << (quint8)0x48; // attribute type array
stream << (quint8)0x41; // attribute datatype: octed string
stream << (quint16)0x0002; // element count: 0x0002 (2 entries)
stream << (quint8)0x06; // element #1: length: 6
stream << (quint8)0x00; // InputAndOptions: 0x00 = first physical input
stream << (quint8)0x0D; // transition: released -> pressed
stream << (quint8)0x03; // Source: Endpoint #3 (hosts primary on/off client cluster on S2)
stream << (quint16)0x0006; // Cluster ID: 0x0006 - on/off
stream << (quint8)0x02; // ZCL command template: Toggle
stream << (quint8)0x06; // element #2
stream << (quint8)0x01; // InputAndOptions: 0x01 = second physical input
stream << (quint8)0x0D; // transition: released -> pressed
stream << (quint8)0x04; // Endpoint #4 (hosts secondary on/off client cluster on S2)
stream << (quint16)0x0006;
stream << (quint8)0x02; // ZCL command template: Toggle
}
else if (item->toString() == QLatin1String("rocker"))
{
stream << (quint16)0x0001; // attribute id InputActions
stream << (quint8)0x48; // attribute type array
stream << (quint8)0x41; // attribute datatype: octed string
stream << (quint16)0x0004; // element count: 0x0004 (4 entries)
stream << (quint8)0x06; // element #1: length: 6
stream << (quint8)0x00; // InputAndOptions: 0x00 = first physical input
stream << (quint8)0x0D; // transition: released -> pressed
stream << (quint8)0x03; // Source: Endpoint #3 (hosts primary on/off client cluster on S2)
stream << (quint16)0x0006; // Cluster ID: 0x0006 - on/off
stream << (quint8)0x02; // ZCL command template: Toggle
stream << (quint8)0x06; // element #2
stream << (quint8)0x01; // InputAndOptions: 0x01 = second physical input
stream << (quint8)0x0D; // transition: released -> pressed
stream << (quint8)0x04; // Endpoint #4 (hosts secondary on/off client cluster on S2)
stream << (quint16)0x0006;
stream << (quint8)0x02; // ZCL command template: Toggle
stream << (quint8)0x06; // element #3: length: 6
stream << (quint8)0x00; // InputAndOptions: 0x00 = first physical input
stream << (quint8)0x03; // transition: any state -> released
stream << (quint8)0x03; // Source: Endpoint #3 (hosts primary on/off client cluster on S2)
stream << (quint16)0x0006; // Cluster ID: 0x0006 - on/off
stream << (quint8)0x02; // ZCL command template: Toggle
stream << (quint8)0x06; // element #4
stream << (quint8)0x01; // InputAndOptions: 0x01 = second physical input
stream << (quint8)0x03; // transition: any state -> released
stream << (quint8)0x04; // Endpoint #4 (hosts secondary on/off client cluster on S2)
stream << (quint16)0x0006;
stream << (quint8)0x02; // ZCL command template: Toggle
}
else if (item->toString().startsWith("custom_"))
{
stream << (quint16)0x0001; // attribute id InputActions
stream << (quint8)0x48; // attribute type array
// Example S2 momentary: "custom_41020006000D0306000206010D04060002"
// Example S2 rocker: "custom_41040006000D0306000206010D040600020600030306000206010304060002"
QString subStr("custom");
QByteArray command = item->toString().toUtf8();
command.remove(0, subStr.size() + 1); // remove custom_
QByteArray payload = QByteArray::fromHex(command);
stream.writeRawData(payload.data(), payload.size());
item->setValue(subStr); // setValue "custom"
}
else
{
return false; // do nothing
}
return true;
}
/*! Configures ubisys switch on ubisys S1, ubisys S2, ubisys J1
*
* Using the device setup cluster 0xFC00 on endpoint 0xE8.
*
* ubisys S1: you can configure endpont 0x02
* either to be used with a push-button (momentary switch, one stable position)
* or a rocker switch (two stable positions)
* or any custom configuration.
*
* http://www.ubisys.de/downloads/ubisys-s1-technical-reference.pdf
* 7.7.5.1. InputConfigurations Attribute, Page 28
*
* ubisys S2: you can configure endpoints 0x03 and 0x04
* either to be used with a push-button (momentary switch, one stable position)
* or a rocker switch (two stable positions)
* or any custom configuration.
*
* http://www.ubisys.de/downloads/ubisys-s2-technical-reference.pdf
* 7.7.5.2. InputActions Attribute, Page 30
*
* ubisys J1: you can configure endpoint 0x02
* either to be used with two push-buttons (momentary switches, one stable position)
* or two rocker switches (two stable positions)
* or any custom configuration.
*
* dual push-button operation (momentary, one stable position) default configuration:
* - A short press will move up/down and stop when released,
* - while a long press will move up/down without stopping
*
* two rocker switches (two stable positions):
* - The blind moves as long as either switch is turned on.
* - As soon as it is turned off, motion stops.
*
* http://www.ubisys.de/downloads/ubisys-j1-technical-reference.pdf
* 7.5.5.2. InputActions Attribute, Page 30
\param task - the task item
\return true - on success
false - on error
*/
bool DeRestPluginPrivate::addTaskUbisysConfigureSwitch(TaskItem &task)
{
int device = 0;
Sensor *sensor = getSensorNodeForAddressAndEndpoint(task.req.dstAddress(), 0x02); // Endpoint 0x02
if (sensor)
{
if (sensor->modelId().startsWith(QLatin1String("J1")))
{
device = UBISYS_J1;
}
else if (sensor->modelId().startsWith(QLatin1String("S1")))
{
device = UBISYS_S1;
}
else
{
return false;
}
}
if (device == 0)
{
sensor = getSensorNodeForAddressAndEndpoint(task.req.dstAddress(), 0x03); // Endpoint 0x03
if (sensor && sensor->modelId().startsWith(QLatin1String("S2")))
{
device = UBISYS_S2;
}
}
if (device == 0)
{
return false;
}
ResourceItem *item = 0;
item = sensor->item(RConfigMode);
if (!item)
{
return false;
}
task.taskType = TaskWindowCovering;
task.req.setProfileId(HA_PROFILE_ID);
task.zclFrame.payload().clear();
task.req.setClusterId(UBISYS_DEVICE_SETUP_CLUSTER_ID);
task.req.setDstEndpoint(0xE8);
task.zclFrame.setSequenceNumber(zclSeq++);
task.zclFrame.setCommandId(deCONZ::ZclWriteAttributesId);
task.zclFrame.setFrameControl(deCONZ::ZclFCProfileCommand |
deCONZ::ZclFCDirectionClientToServer |
deCONZ::ZclFCDisableDefaultResponse);
bool addPayloadResult = false;
switch(device)
{
case UBISYS_J1:
addPayloadResult = addPayloadJ1(task, item);
break;
case UBISYS_S1:
addPayloadResult = addPayloadS1(task, item);
break;
case UBISYS_S2:
addPayloadResult = addPayloadS2(task, item);
break;
default:
return false;
}
if (!addPayloadResult)
{
return false;
}
{ // 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);
}
if (!addTask(task))
{
return false;
}
return true;
}