forked from dresden-elektronik/deconz-rest-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoll_manager.cpp
343 lines (300 loc) · 8.91 KB
/
poll_manager.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
/*
* Copyright (c) 2017 dresden elektronik ingenieurtechnik gmbh.
* All rights reserved.
*
* The software in this package is published under the terms of the BSD
* style license a copy of which has been included with this distribution in
* the LICENSE.txt file.
*
*/
#include "poll_manager.h"
#include "de_web_plugin_private.h"
/*! Constructor.
*/
PollManager::PollManager(QObject *parent) :
QObject(parent)
{
pollState = StateIdle;
timer = new QTimer(this);
timer->setSingleShot(true);
connect(timer, SIGNAL(timeout()), this, SLOT(pollTimerFired()));
plugin = qobject_cast<DeRestPluginPrivate*>(parent);
}
/*! Queues polling of the node.
\param restNode - the node to poll
*/
void PollManager::poll(RestNodeBase *restNode, const QDateTime &tStart)
{
Resource *r = dynamic_cast<Resource*>(restNode);
DBG_Assert(r != 0);
if (!r)
{
return;
}
PollItem pitem;
if (r->prefix() == RLights)
{
LightNode *lightNode = static_cast<LightNode*>(restNode);
DBG_Assert(lightNode != 0);
pitem.endpoint = lightNode->haEndpoint().endpoint();
}
else
{
return;
}
pitem.id = restNode->id();
pitem.prefix = r->prefix();
pitem.address = restNode->address();
pitem.tStart = tStart;
for (int i = 0; i < r->itemCount(); i++)
{
const ResourceItem *item = r->itemForIndex(i);
const char *suffix = item ? item->descriptor().suffix : 0;
if (suffix == RStateOn ||
suffix == RStateBri ||
suffix == RStateColorMode)
{
pitem.items.push_back(suffix);
}
}
for (PollItem &i : items)
{
if (i.prefix == r->prefix() && i.id == restNode->id())
{
i.items = pitem.items; // update
if (tStart.isValid())
{
i.tStart = tStart;
}
return;
}
}
items.push_back(pitem);
if (!timer->isActive())
{
timer->start(1);
}
}
/*! Delays polling for \p ms milliseconds.
*/
void PollManager::delay(int ms)
{
timer->stop();
timer->start(ms);
}
/*! Handle APS confirm if related to polling.
*/
void PollManager::apsdeDataConfirm(const deCONZ::ApsDataConfirm &conf)
{
if (pollState != StateWait)
{
return;
}
if (apsReqId != conf.id())
{
return;
}
if (dstAddr.hasExt() && conf.dstAddress().hasExt()
&& dstAddr.ext() != conf.dstAddress().ext())
{
}
else if (dstAddr.hasNwk() && conf.dstAddress().hasNwk()
&& dstAddr.nwk() != conf.dstAddress().nwk())
{
}
DBG_Printf(DBG_INFO_L2, "Poll APS confirm %u status: 0x%02X\n", conf.id(), conf.status());
pollState = StateIdle;
timer->stop();
timer->start(1);
}
/*! Timer callback to proceed polling.
*/
void PollManager::pollTimerFired()
{
if (pollState == StateWait)
{
DBG_Printf(DBG_INFO, "timout on poll APS confirm\n");
pollState = StateIdle;
}
DBG_Assert(pollState == StateIdle);
if (items.empty())
{
return;
}
QDateTime now = QDateTime::currentDateTime();
PollItem &pitem = items.front();
Resource *r = plugin->getResource(pitem.prefix, pitem.id);
ResourceItem *item = r ? r->item(RStateReachable) : 0;
RestNodeBase *restNode = 0;
const LightNode *lightNode = 0;
if (r && r->prefix() == RLights)
{
restNode = plugin->getLightNodeForId(pitem.id);
lightNode = static_cast<LightNode*>(restNode);
}
if (pitem.tStart.isValid() && pitem.tStart > now)
{
if (items.size() > 1)
{
PollItem tmp = pitem;
items.front() = items.back();
items.back() = tmp;
}
timer->start(1);
return;
}
if (!r || pitem.items.empty() ||
!restNode ||
!restNode->lastRx().isValid() ||
!item || !item->toBool()) // not reachable
{
items.front() = items.back();
items.pop_back();
timer->start(1);
return;
}
quint16 clusterId = 0;
std::vector<quint16> attributes;
item = r->item(RStateOn);
bool isOn = item ? item->toBool() : false;
const char *&suffix = pitem.items[0];
for (size_t i = 0; pitem.items[0] == 0 && i < pitem.items.size(); i++)
{
if (pitem.items[i] != 0)
{
pitem.items[0] = pitem.items[i]; // move to front
pitem.items[i] = 0; // clear
break;
}
}
if (!suffix)
{
pitem.items.clear(); // all done
}
if (suffix == RStateOn)
{
clusterId = ONOFF_CLUSTER_ID;
attributes.push_back(0x0000); // onOff
}
else if (suffix == RStateBri && isOn)
{
NodeValue &val = restNode->getZclValue(LEVEL_CLUSTER_ID, 0x0000);
if (isOn || !val.timestamp.isValid())
{
clusterId = LEVEL_CLUSTER_ID;
attributes.push_back(0x0000); // current level
}
}
else if (suffix == RStateColorMode && lightNode)
{
clusterId = COLOR_CLUSTER_ID;
item = r->item(RConfigColorCapabilities);
if (!item)
{
attributes.push_back(0x0008); // color mode
attributes.push_back(0x4001); // enhanced color mode
attributes.push_back(0x400a); // color capabilities
attributes.push_back(0x400b); // color temperature min
attributes.push_back(0x400c); // color temperature max
}
else
{
quint16 cap = item->toNumber();
std::vector<quint16> toCheck;
if (cap & 0x0002) // enhanced hue supported
{
toCheck.push_back(0x4001); // enhanced color mode
toCheck.push_back(0x4000); // enhanced hue
toCheck.push_back(0x0001); // saturation
}
else if (cap & 0x0001)
{
toCheck.push_back(0x0000); // hue
toCheck.push_back(0x0001); // saturation
toCheck.push_back(0x0008); // color mode
}
else
{
toCheck.push_back(0x0008); // color mode
}
if (cap & 0x0004)
{
toCheck.push_back(0x4002); // Color loop active
}
if (cap & 0x0008)
{
toCheck.push_back(0x0003); // currentX
toCheck.push_back(0x0004); // currentY
}
if (cap & 0x0010)
{
toCheck.push_back(0x0007); // color temperature
}
for (const deCONZ::ZclCluster &cl : lightNode->haEndpoint().inClusters())
{
if (cl.id() != COLOR_CLUSTER_ID)
{
continue;
}
for (const deCONZ::ZclAttribute &attr : cl.attributes())
{
for (quint16 attrId : toCheck)
{
// discard attributes which are not be available
if (attrId == attr.id() && attr.isAvailable())
{
NodeValue &val = restNode->getZclValue(clusterId, attrId);
if (isOn || !val.timestamp.isValid())
{
attributes.push_back(attrId);
}
}
}
}
break;
}
}
}
size_t fresh = 0;
for (quint16 attrId : attributes)
{
NodeValue &val = restNode->getZclValue(clusterId, attrId);
if (val.timestampLastReport.isValid() && val.timestampLastReport.secsTo(now) < 360)
{
fresh++;
}
}
if (clusterId && fresh > 0 && fresh == attributes.size())
{
DBG_Printf(DBG_INFO, "Poll APS request to 0x%016llX cluster: 0x%04X dropped, values are fresh enough\n", pitem.address.ext(), clusterId);
suffix = 0; // clear
timer->start(100);
}
else if (!attributes.empty() && clusterId &&
plugin->readAttributes(restNode, pitem.endpoint, clusterId, attributes))
{
pollState = StateWait;
// TODO this hack to get aps request id
DBG_Assert(plugin->tasks.back().taskType == TaskReadAttributes);
apsReqId = plugin->tasks.back().req.id();
dstAddr = pitem.address;
timer->start(20 * 1000); // wait for confirm
suffix = 0; // clear
DBG_Printf(DBG_INFO_L2, "Poll APS request %u to 0x%016llX cluster: 0x%04X\n", apsReqId, dstAddr.ext(), clusterId);
}
else if (suffix)
{
suffix = 0; // clear
timer->start(100);
}
else
{
if (clusterId)
{
DBG_Printf(DBG_INFO, "Poll APS request to 0x%016llX cluster: 0x%04X dropped\n", pitem.address.ext(), clusterId);
}
timer->start(100);
items.front() = items.back();
items.pop_back();
}
}