forked from dresden-elektronik/deconz-rest-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
thermostat.cpp
602 lines (526 loc) · 20.6 KB
/
thermostat.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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
/*
* thermostat.cpp
*
* Add Support for Thermostat Cluster 0x0201 and Bitron Thermostat 902010/32
*
* A sensor type ZHAThermostat is created with the following options in state and config:
*
* option | read/write | attribute | description
* -------------------|------------|-----------|---------------------
* state.on | read only | 0x0029 | running state on/off
* state.temperature | read only | 0x0000 | measured temperature
* config.heatsetpoint| read write | 0x0012 | heating setpoint
* config.scheduleron | read write | 0x0025 | scheduler on/off
* config.offset | read write | 0x0010 | temperature offset
* config.scheduler | read write | (command) | scheduled setpoints
*
*
* Example sensor:
*
* /api/<apikey>/sensors/<id>/
* {
* config: {
* "heatsetpoint": 2200,
* "offset": 0,
* "scheduler": "Monday,Tuesday,Wednesday,Thursday,Friday 05:00 2200 19:00 1800;Saturday,Sunday 06:00 2100 19:00 1800;"
* "scheduleron": true
* },
* state: {
* "on": true,
* "temperature": 2150
* },
* "ep": 1,
* "manufacturername": "Bitron Home",
* "modelid": "902010/32",
* "type": "ZHAThermostat",
* ...
* }
*
* Rest API example commands:
* -X PUT /api/<apikey>/sensors/<id>/config -d '{ "heatsetpoint": 1800 }'
* -X PUT /api/<apikey>/sensors/<id>/config -d '{ "scheduleron": true }'
* -X PUT /api/<apikey>/sensors/<id>/config -d '{ "offset": 0 }'
* -X PUT /api/<apikey>/sensors/<id>/config -d '{ "scheduler": "Monday 05:00 2200 19:00 1800;" }'
* -d '{ "scheduler": "" }' (send get scheduler command)
*
*
* Attributes (only a subset):
* ID Type Type Description Default
* 0x0000 0x29 int16s Local Temperature --
* 0x0010 0x28 int8s Local Temperature Calibration --
* 0x0012 0x29 int16s Occupied Heating Setpoint 2000 (20 °C)
* 0x0025 0x18 bit8 Programming Operation Mode 0
* Bit#0 of this attribute controls
* the enabling of the Thermostat Scheduler.
* 0x0029 0x19 bit16 Thermostat Running State 0
* Bit0=Heat State On
* Bit1=Cool State On
*
* Commands Received (Client to Server):
* Command-ID Name
* 0x00 Setpoint Raise/Lower
* 0x01 Set Weekly Schedule
* 0x02 Get Weekly Schedule
* 0x03 Clear Weekly Schedule
*
* Commands Generated (Server to Client):
* Command-ID Name
* 0x00 Current Weekly Schedule
*
* Weekly Schedule format
* Octets 1 1 1 2 2/0 2/0 ... 2 2/0 2/0
* Type enum8 bit8 bit8 int16u int16s int16s
* Name Number Day of Mode Transition Heat Cool
* Transitions Week (1,2) Time 1 Setpoint 1 Setpoint1
*
* Day of Week: bitmap8 [Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Away]
* bit 0 1 2 3 4 5 6 7
* Example:
* Monday, Tuesday, Thursday, Friday = 0011 0110 = 0x36
* | | | ^-------^| ||
* | | --------------- ||
* | -----------------------------|
* ---------------------------------------
*
*/
#include <bitset>
#include <QJsonDocument>
#include "de_web_plugin.h"
#include "de_web_plugin_private.h"
static const QStringList weekday({"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Away"});
static TaskItem taskScheduleTimer;
static int dayofweekTimer = 0;
/*! Handle packets related to the ZCL Thermostat cluster.
\param ind the APS level data indication containing the ZCL packet
\param zclFrame the actual ZCL frame which holds the Thermostat cluster command or attribute
*/
void DeRestPluginPrivate::handleThermostatClusterIndication(const deCONZ::ApsDataIndication &ind, deCONZ::ZclFrame &zclFrame)
{
Sensor *sensor = getSensorNodeForAddressAndEndpoint(ind.srcAddress(), 0x01);
QDataStream stream(zclFrame.payload());
stream.setByteOrder(QDataStream::LittleEndian);
bool isReadAttr = false;
bool isReporting = false;
bool isClusterCmd = false;
if (zclFrame.isProfileWideCommand() && zclFrame.commandId() == deCONZ::ZclReadAttributesResponseId)
{
isReadAttr = true;
}
if (zclFrame.isProfileWideCommand() && zclFrame.commandId() == deCONZ::ZclReportAttributesId)
{
isReporting = true;
}
if ((zclFrame.frameControl() & 0x09) == (deCONZ::ZclFCDirectionServerToClient | deCONZ::ZclFCClusterCommand))
{
isClusterCmd = true;
}
// Read ZCL reporting and ZCL Read Attributes Response
if (isReadAttr || isReporting)
{
const NodeValue::UpdateType updateType = isReadAttr ? NodeValue::UpdateByZclRead : NodeValue::UpdateByZclReport;
while (!stream.atEnd())
{
quint16 attrId;
quint8 attrTypeId;
stream >> attrId;
if (isReadAttr)
{
quint8 status;
stream >> status; // Read Attribute Response status
if (status != deCONZ::ZclSuccessStatus)
{
continue;
}
}
stream >> attrTypeId;
deCONZ::ZclAttribute attr(attrId, attrTypeId, QLatin1String(""), deCONZ::ZclRead, false);
if (!attr.readFromStream(stream))
{
continue;
}
if (!sensor)
{
continue;
}
ResourceItem *item = nullptr;
switch (attrId)
{
case 0x0000: // Local Temperature
item = sensor->item(RStateTemperature);
if (item)
{
item->setValue(attr.numericValue().s16);
sensor->updateStateTimestamp();
Event e(RSensors, RStateTemperature, sensor->id(), item);
enqueueEvent(e);
sensor->setZclValue(updateType, THERMOSTAT_CLUSTER_ID, attrId, attr.numericValue());
}
break;
case 0x0010: // Local Temperature Calibration (offset in 0.1 °C steps, from -2,5 °C to +2,5 °C)
item = sensor->item(RConfigOffset);
if (item)
{
item->setValue(attr.numericValue().s8);
Event e(RSensors, RConfigOffset, sensor->id(), item);
enqueueEvent(e);
}
break;
case 0x0012: // Occupied Heating Setpoint
item = sensor->item(RConfigHeating);
if (item)
{
item->setValue(attr.numericValue().s16);
Event e(RSensors, RConfigHeating, sensor->id(), item);
enqueueEvent(e);
}
break;
case 0x0025: // Thermostat Programming Operation Mode, default 0 (bit#0 = disable/enable Scheduler)
item = sensor->item(RConfigSchedulerOn);
if (item)
{
bool onoff = attr.bitmap() & 0x01 ? true : false;
item->setValue(onoff);
Event e(RSensors, RConfigSchedulerOn, sensor->id(), item);
enqueueEvent(e);
}
break;
case 0x0029: // Thermostat Running State (bit0=Heat State On/Off, bit1=Cool State On/Off)
item = sensor->item(RStateOn);
if (item)
{
item->setValue(attr.bitmap() > 0);
Event e(RSensors, RStateOn, sensor->id(), item);
enqueueEvent(e);
sensor->setZclValue(updateType, THERMOSTAT_CLUSTER_ID, 0x0029, attr.numericValue());
}
break;
// manufacturerspecific reported by Eurotronic SPZB0001
case 0x4002: // U8 (0x20): value 0x00
case 0x4003: // S16 (0x29): 2300
case 0x4008: // U24 (0x22): 0x000001
{
if (zclFrame.manufacturerCode() == VENDOR_JENNIC)
{
}
}
break;
default:
break;
}
sensor->setNeedSaveDatabase(true);
queSaveDb(DB_SENSORS, DB_SHORT_SAVE_DELAY);
}
}
// Read ZCL Cluster Command Response
if (isClusterCmd && zclFrame.commandId() == 0x00) // get schedule command response
{
QVariantMap map;
QVariantMap mapsorted;
QStringList schedList;
QString sched;
ResourceItem *item = nullptr;
item = sensor->item(RConfigScheduler);
QString val;
QString valstored;
if (item)
{
valstored = item->toString();
}
if (item && !valstored.isEmpty())
{
schedList = valstored.split(";", QString::SkipEmptyParts);
for (const QString &dayEntry : schedList)
{
QString schedEntry;
QStringList checkdayList;
QStringList checkSchedule = dayEntry.split(" ");
checkdayList = checkSchedule.at(0).split(",");
if (checkSchedule.length() > 1)
{
checkSchedule.removeFirst();
schedEntry = checkSchedule.join(" ");
}
for (const QString &checkday : checkdayList)
{
if (weekday.contains(checkday))
{
map[checkday] = schedEntry;
}
}
}
}
// ZCL Cluster Command Response variables
int count = 0;
int daycount = 0;
while (!stream.atEnd())
{
quint8 nrTrans = 0;
quint8 dayOfWeek = 0;
quint8 modeSeq = 0;
if (count == 0)
{
stream >> nrTrans;
stream >> dayOfWeek;
stream >> modeSeq;
for (daycount = 0; daycount < 8; daycount++)
{
if ((dayOfWeek >> daycount) & 1U)
{
break;
}
}
}
if (count < nrTrans)
{
quint16 transTime;
qint16 heatSetPoint;
qint16 coolSetPoint;
stream >> transTime;
QTime midnight(0, 0, 0);
QTime heatTime = midnight.addSecs(transTime * 60);
val = val + " " + qPrintable(heatTime.toString("HH:mm"));
if (modeSeq & 0x01) // bit-0 heat set point
{
stream >> heatSetPoint;
val = val + QString(" %1").arg(heatSetPoint);
}
if (modeSeq & 0x02) // bit-1 cool set point
{
stream >> coolSetPoint;
}
count++;
}
}
if (stream.status() == QDataStream::ReadPastEnd)
{
return;
}
map[weekday.at(daycount)] = val.trimmed();
QStringList weekdayList({"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"});
for (const QString &key : weekdayList)
{
QString schedtime = map[key].toString();
if (mapsorted.contains(schedtime))
{
mapsorted[schedtime] = mapsorted[schedtime].toString() + "," + key; // e.g. Monday,Tuesday,...
}
else if (!schedtime.isEmpty())
{
mapsorted[schedtime] = key; // e.g. Monday
}
}
for (const QString &key : mapsorted.keys())
{
sched = sched + mapsorted[key].toString() + " " + key + ";"; // e.g "Monday,Tuesday 06:00 2100 22:00 1700"
}
// Example: scheduler =
// "Monday,Tuesday,Wednesday,Thursday,Friday 05:00 2200 06:00 1700 16:30 2200 17:00 2000 18:00 2200 19:00 1800;Saturday,Sunday 06:00 2100 16:30 2200 17:00 2000 18:00 2200 19:00 1800;"
//
// 22.0 °C ----------- ----------- ------------ ----------- -----------
// | | | | | | ------------- | | |
// 20.0 °C | | | ------------ | | ------------ |
// | | | | | |
// 18.0 °C ---- | | -------- ----- ----------
// 17.0 °C --------------
DBG_Printf(DBG_INFO, "Thermostat 0x%04X scheduler = %s\n", ind.srcAddress().nwk(), qPrintable(sched));
if (item)
{
item->setValue(sched);
}
}
}
static QByteArray setSchedule(const QString &sched)
{
QByteArray payload;
QDataStream stream(&payload, QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
quint8 dayofweek = 0;
QStringList schedList = sched.simplified().split(";", QString::SkipEmptyParts);
for (const QString &dayEntry : schedList)
{
QStringList checkdayList;
QStringList checkSchedule = dayEntry.split(" ", QString::SkipEmptyParts);
checkdayList = checkSchedule.at(0).split(","); // e.g. Monday,Tuesday,Wednesday
for (const QString &checkday : checkdayList)
{
if (weekday.contains(checkday))
{
int n = weekday.indexOf(checkday);
dayofweek |= (0x01 << n);
}
}
if (checkSchedule.length() > 1)
{
checkSchedule.removeFirst();
stream << (quint8) (checkSchedule.length() / 2); // number of transitions
stream << dayofweek;
stream << (quint8) 0x01; // mode heat
// e.g. 06:00 2100 22:00 1700
for (int i = 0; i < checkSchedule.length(); i++)
{
const QString &entry = checkSchedule[i];
if (i & 1) // odd 1,3... setpoint = 2100 1700
{
stream << (qint16) entry.toInt(); // setpoint
}
else // even 0,2,... time = 06:00 22:00
{
QTime midnight(0, 0, 0);
QTime heatTime = QTime::fromString(entry, "hh:mm");
int heatTimeMinutes = midnight.secsTo(heatTime) / 60;
stream << (quint16) heatTimeMinutes; // transition time
}
}
}
}
return payload;
}
/*! Adds a thermostat command task to the queue.
\param task - the task item
\param cmdId - 0x00 setpoint raise/lower
0x01 set schedule
0x02 get schedule
0x03 clear schedule
\param setpoint - raise/lower value
\param schedule - set schedule e.g. "Monday,Tuesday 06:00 2000 22:00 1700; Saturday,Sunday 06:00 2100 22:00 1700"
\param days - days to return schedule
\return true - on success
false - on error
*/
bool DeRestPluginPrivate::addTaskThermostatCmd(TaskItem &task, uint8_t cmd, int8_t setpoint, const QString &schedule, uint8_t daysToReturn)
{
task.taskType = TaskThermostat;
task.req.setClusterId(THERMOSTAT_CLUSTER_ID);
task.req.setProfileId(HA_PROFILE_ID);
task.zclFrame.payload().clear();
task.zclFrame.setSequenceNumber(zclSeq++);
task.zclFrame.setCommandId(cmd);
task.zclFrame.setFrameControl(deCONZ::ZclFCClusterCommand |
deCONZ::ZclFCDirectionClientToServer);
// payload
QDataStream stream(&task.zclFrame.payload(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
if (cmd == 0x00)
{
stream << (qint8) setpoint; // 8-bit raise/lower
}
else if (cmd == 0x01) // set schedule
{
QByteArray payload = setSchedule(schedule);
stream.writeRawData(payload.data(), payload.size());
}
else if (cmd == 0x02) // get schedule
{
stream << (quint8) daysToReturn;
stream << (quint8) 0x01; // mode heat
}
else if (cmd == 0x03) // clear schedule
{
// no payload
}
else
{
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);
}
return addTask(task);
}
/*! Helper to generate a new task with new task and req id based on a reference */
static void copyTaskReq(TaskItem &a, TaskItem &b)
{
b.req.dstAddress() = a.req.dstAddress();
b.req.setDstAddressMode(a.req.dstAddressMode());
b.req.setSrcEndpoint(a.req.srcEndpoint());
b.req.setDstEndpoint(a.req.dstEndpoint());
b.req.setRadius(a.req.radius());
b.req.setTxOptions(a.req.txOptions());
b.req.setSendDelay(a.req.sendDelay());
b.zclFrame.payload().clear();
}
/*! Set Scheduler on thermostat cluster.
* Iterate over every weekday and get schedule.
\param task - the task item
\return true - on success
false - on error
*/
bool DeRestPluginPrivate::addTaskThermostatSetAndGetSchedule(TaskItem &task, const QString &sched)
{
copyTaskReq(task, taskScheduleTimer);
if (!sched.isEmpty() && !addTaskThermostatCmd(task, 0x01, 0, sched, 0)) // set schedule
{
return false;
}
dayofweekTimer = 0;
for (int i = 0; i < 7; i++)
{
// use QTimer to send a command once every second to battery Endpoints
QTimer::singleShot(1000 * (i + 2), this, SLOT(addTaskThermostatGetScheduleTimer()));
}
return true;
}
void DeRestPluginPrivate::addTaskThermostatGetScheduleTimer()
{
TaskItem task;
copyTaskReq(taskScheduleTimer, task);
uint8_t dayofweek = (1 << dayofweekTimer);
dayofweekTimer++;
addTaskThermostatCmd(task, 0x02, 0, nullptr, dayofweek); // get schedule
}
/*! Write Attribute on thermostat cluster.
* Iterate over every day and get schedule for each day.
\param task - the task item
\param attrId
\param attrType
\param attrValue
\return true - on success
false - on error
*/
bool DeRestPluginPrivate::addTaskThermostatReadWriteAttribute(TaskItem &task, uint8_t readOrWriteCmd, uint16_t attrId, uint8_t attrType, uint16_t attrValue)
{
if (readOrWriteCmd != deCONZ::ZclReadAttributesId && readOrWriteCmd != deCONZ::ZclWriteAttributesId)
{
DBG_Printf(DBG_INFO, "Thermostat invalid parameter readOrWriteCmd %d\n", readOrWriteCmd);
return false;
}
task.taskType = TaskThermostat;
task.req.setClusterId(THERMOSTAT_CLUSTER_ID);
task.req.setProfileId(HA_PROFILE_ID);
task.zclFrame.payload().clear();
task.zclFrame.setSequenceNumber(zclSeq++);
task.zclFrame.setCommandId(readOrWriteCmd);
task.zclFrame.setFrameControl(deCONZ::ZclFCProfileCommand |
deCONZ::ZclFCDirectionClientToServer |
deCONZ::ZclFCDisableDefaultResponse);
// payload
QDataStream stream(&task.zclFrame.payload(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
stream << (quint16) attrId;
if (readOrWriteCmd == deCONZ::ZclWriteAttributesId)
{
stream << (quint8) attrType;
if (attrType == deCONZ::Zcl8BitEnum || attrType == deCONZ::Zcl8BitInt || attrType == deCONZ::Zcl8BitBitMap)
{
stream << (quint8) attrValue;
}
else if (attrType == deCONZ::Zcl16BitInt || attrType == deCONZ::Zcl16BitBitMap)
{
stream << (quint16) attrValue;
}
else
{
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);
}
return addTask(task);
}