-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest_node_base.cpp
426 lines (373 loc) · 10.3 KB
/
rest_node_base.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
/*
* Copyright (c) 2016-2020 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 <QTime>
#include "de_web_plugin_private.h"
/*! Constructor.
*/
RestNodeBase::RestNodeBase() :
m_node(0),
m_mgmtBindSupported(true),
m_needSaveDatabase(false),
m_read(0),
m_lastRead(0),
m_lastAttributeReportBind(0)
{
QTime t = QTime::currentTime();
for (int i = 0; i < 16; i++)
{
m_lastRead.push_back(0);
m_nextReadTime.push_back(t);
}
}
/*! Deconstructor.
*/
RestNodeBase::~RestNodeBase()
{
}
/*! Returns the core node object.
*/
deCONZ::Node *RestNodeBase::node()
{
return m_node;
}
/*! Sets the core node object.
\param node the core node
*/
void RestNodeBase::setNode(deCONZ::Node *node)
{
m_node = node;
}
/*! Returns the modifiable address.
*/
deCONZ::Address &RestNodeBase::address()
{
return m_addr;
}
/*! Returns the const address.
*/
const deCONZ::Address &RestNodeBase::address() const
{
return m_addr;
}
/*! Returns true if the node is available.
*/
bool RestNodeBase::isAvailable() const
{
return false;
}
/*! Returns if the data needs to be saved to database.
*/
bool RestNodeBase::needSaveDatabase() const
{
return m_needSaveDatabase;
}
/*! Sets if the data needs to be saved to database.
\param needSave true if needs to be saved
*/
void RestNodeBase::setNeedSaveDatabase(bool needSave)
{
m_needSaveDatabase = needSave;
}
/*! Returns the unique identifier of the node.
*/
const QString &RestNodeBase::id() const
{
const Resource *r = dynamic_cast<const Resource*>(this);
const ResourceItem *item = r ? r->item(RAttrId) : nullptr;
if (item)
{
return item->toString();
}
return m_id;
}
/*! Sets the identifier of the node.
\param id the unique identifier
*/
void RestNodeBase::setId(const QString &id)
{
Resource *r = dynamic_cast<Resource*>(this);
ResourceItem *item = r ? r->item(RAttrId) : nullptr;
if (item)
{
item->setValue(id);
}
m_id = id;
}
/*! Returns the nodes unique Id.
The MAC address of the device with a unique endpoint id in the form: AA:BB:CC:DD:EE:FF:00:11-XX
*/
const QString &RestNodeBase::uniqueId() const
{
const Resource *r = dynamic_cast<const Resource*>(this);
const ResourceItem *item = r ? r->item(RAttrUniqueId) : 0;
if (item)
{
return item->toString();
}
return m_uid;
}
/*! Sets the sensor unique Id.
The MAC address of the device with a unique endpoint id in the form: AA:BB:CC:DD:EE:FF:00:11-XX
\param uid the sensor unique Id
*/
void RestNodeBase::setUniqueId(const QString &uid)
{
Resource *r = dynamic_cast<Resource*>(this);
ResourceItem *item = r ? r->addItem(DataTypeString, RAttrUniqueId) : 0;
if (item)
{
item->setValue(uid);
}
m_uid = uid;
}
/*! Check if some data must be queried from the node.
\param readFlags or combined bitmap of READ_* values
\return true if every flag in readFlags is set
*/
bool RestNodeBase::mustRead(uint32_t readFlags)
{
if ((m_read & readFlags) == readFlags)
{
return true;
}
return false;
}
/*! Enables all flags given in \p readFlags in the read set.
\param readFlags or combined bitmap of READ_* values
*/
void RestNodeBase::enableRead(uint32_t readFlags)
{
m_read |= readFlags;
}
/*! Clears all flags given in \p readFlags in the read set.
\param readFlags or combined bitmap of READ_* values
*/
void RestNodeBase::clearRead(uint32_t readFlags)
{
m_read &= ~readFlags;
}
/*! Returns the time than the next auto reading is queued.
\param item the item to read
*/
const QTime &RestNodeBase::nextReadTime(uint32_t item) const
{
for (size_t i = 0; i < m_nextReadTime.size(); i++)
{
if ((1u << i) == item)
{
return m_nextReadTime[i];
}
}
Q_ASSERT(0 || "m_nextReadTime[] too small");
return m_invalidTime;
}
/*! Sets the time than the next auto reading should be queued.
\param item the item to read
\param time the time for reading
*/
void RestNodeBase::setNextReadTime(uint32_t item, const QTime &time)
{
for (size_t i = 0; i < m_nextReadTime.size(); i++)
{
if ((1u << i) == item)
{
m_nextReadTime[i] = time;
return;
}
}
Q_ASSERT(0 || "m_nextReadTime[] too small");
}
/*! Returns the value of the idleTotalCounter than the last reading happend.
\param item the item to read
*/
int RestNodeBase::lastRead(uint32_t item) const
{
for (size_t i = 0; i < m_lastRead.size(); i++)
{
if ((1u << i) == item)
{
return m_lastRead[i];
}
}
Q_ASSERT(0 || "m_lastRead[] too small");
return 0;
}
/*! Sets the last read counter.
\param item the item to read
\param lastRead copy of idleTotalCounter
*/
void RestNodeBase::setLastRead(uint32_t item, int lastRead)
{
for (size_t i = 0; i < m_lastRead.size(); i++)
{
if ((1u << i) == item)
{
m_lastRead[i] = lastRead;
return;
}
}
Q_ASSERT(0 || "m_lastRead[] too small");
}
/*! Returns the value of the idleTotalCounter than the last attribute report binding was done.
*/
int RestNodeBase::lastAttributeReportBind() const
{
return m_lastAttributeReportBind;
}
/*! Sets idleTotalCounter of last attribute report binding.
\param lastBind copy of idleTotalCounter
*/
void RestNodeBase::setLastAttributeReportBind(int lastBind)
{
m_lastAttributeReportBind = lastBind;
}
/*! Returns true if mgmt bind request/response are supported.
*/
bool RestNodeBase::mgmtBindSupported() const
{
return m_mgmtBindSupported;
}
/*! Sets the query binding table supported flag;
\param supported - query binding table supported flag
*/
void RestNodeBase::setMgmtBindSupported(bool supported)
{
m_mgmtBindSupported = supported;
}
/*! Sets a numeric ZCL attribute value.
A timestamp will begenerated automatically.
\param updateType - specifies if value came by ZCL attribute read or report command
\param clusterId - the cluster id of the value
\param attributeId - the attribute id of the value
\param value - the value data
*/
void RestNodeBase::setZclValue(NodeValue::UpdateType updateType, quint8 endpoint, quint16 clusterId, quint16 attributeId, const deCONZ::NumericUnion &value)
{
QDateTime now = QDateTime::currentDateTime();
std::vector<NodeValue>::iterator i = m_values.begin();
std::vector<NodeValue>::iterator end = m_values.end();
for (; i != end; ++i)
{
if (i->endpoint == endpoint &&
i->clusterId == clusterId &&
i->attributeId == attributeId)
{
i->updateType = updateType;
i->value = value;
i->timestamp = now;
if (updateType == NodeValue::UpdateByZclReport)
{
i->timestampLastReport = now;
}
if (DBG_IsEnabled(DBG_INFO_L2))
{
DBG_Printf(DBG_INFO_L2, "0x%016llX: update ZCL value 0x%02X/0x%04X/0x%04X after %lld s\n", address().ext(), endpoint, clusterId, attributeId, i->timestamp.secsTo(now));
}
return;
}
}
NodeValue val;
val.timestamp = now;
if (updateType == NodeValue::UpdateByZclReport)
{
val.timestampLastReport = now;
}
val.endpoint = endpoint;
val.clusterId = clusterId;
val.attributeId = attributeId;
val.updateType = updateType;
val.value = value;
DBG_Printf(DBG_INFO_L2, "0x%016llX: added ZCL value 0x%02X/0x%04X/0x%04X\n", address().ext(), endpoint, clusterId, attributeId);
m_values.push_back(val);
}
/*! Returns a numeric ZCL attribute value.
If the value couldn't be found the NodeValue::timestamp field holds a invalid QTime.
\param clusterId - the cluster id of the value
\param attributeId - the attribute id of the value
\param endpoint - the endpoint of the value, optional: 0 means no check
*/
const NodeValue &RestNodeBase::getZclValue(quint16 clusterId, quint16 attributeId, quint8 endpoint) const
{
std::vector<NodeValue>::const_iterator i = m_values.begin();
std::vector<NodeValue>::const_iterator end = m_values.end();
for (; i != end; ++i)
{
if (endpoint > 0 && i->endpoint != endpoint)
{
continue;
}
if (i->clusterId == clusterId &&
i->attributeId == attributeId)
{
return *i;
}
}
return m_invalidValue;
}
/*! Returns a numeric ZCL attribute value.
If the value couldn't be found the NodeValue::timestamp field holds a invalid QTime.
\param clusterId - the cluster id of the value
\param attributeId - the attribute id of the value
\param endpoint - the endpoint of the value, optional: 0 means no check
*/
NodeValue &RestNodeBase::getZclValue(quint16 clusterId, quint16 attributeId, quint8 endpoint)
{
std::vector<NodeValue>::iterator i = m_values.begin();
std::vector<NodeValue>::iterator end = m_values.end();
for (; i != end; ++i)
{
if (endpoint > 0 && i->endpoint != endpoint)
{
continue;
}
if (i->clusterId == clusterId &&
i->attributeId == attributeId)
{
return *i;
}
}
return m_invalidValue;
}
/*! Returns ZCL attribute values.
*/
std::vector<NodeValue> &RestNodeBase::zclValues()
{
return m_values;
}
/*! Returns ZCL attribute values.
*/
const std::vector<NodeValue> &RestNodeBase::zclValues() const
{
return m_values;
}
/*! Returns timestamp of last rx. */
const QDateTime &RestNodeBase::lastRx() const
{
return m_lastRx;
}
/*! Mark received command. */
void RestNodeBase::rx()
{
m_lastRx = QDateTime::currentDateTime();
}
const deCONZ::SimpleDescriptor *getSimpleDescriptor(const deCONZ::Node *node, quint8 ep)
{
if (!node)
{
return nullptr;
}
const auto i = std::find_if(node->simpleDescriptors().cbegin(), node->simpleDescriptors().cend(),
[ep](const auto &sd){ return sd.endpoint() == ep; });
if (i != node->simpleDescriptors().cend())
{
return &*i;
}
return nullptr;
}