forked from dresden-elektronik/deconz-rest-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoccupancy_sensing.cpp
210 lines (178 loc) · 7.65 KB
/
occupancy_sensing.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
#include "de_web_plugin.h"
#include "de_web_plugin_private.h"
#include "device_descriptions.h"
#define OCCUPIED_STATE 0x0000
#define OCCUPIED_TO_UNOCCUPIED_DELAY 0x0010
/*! Handle packets related to the ZCL occupancy sensing cluster.
\param ind the APS level data indication containing the ZCL packet
\param zclFrame the actual ZCL frame which holds the occupancy sensing cluster command or attribute
*/
void DeRestPluginPrivate::handleOccupancySensingClusterIndication(const deCONZ::ApsDataIndication &ind, const deCONZ::ZclFrame &zclFrame)
{
if (zclFrame.isDefaultResponse())
{
return;
}
Sensor *sensor = getSensorNodeForAddressEndpointAndCluster(ind.srcAddress(), ind.srcEndpoint(), OCCUPANCY_SENSING_CLUSTER_ID );
if (!sensor)
{
DBG_Printf(DBG_INFO, "No presence sensor found for 0x%016llX, endpoint: 0x%02X\n", ind.srcAddress().ext(), ind.srcEndpoint());
return;
}
QDataStream stream(zclFrame.payload());
stream.setByteOrder(QDataStream::LittleEndian);
bool isReadAttr = false;
if (zclFrame.isProfileWideCommand() && zclFrame.commandId() == deCONZ::ZclReadAttributesResponseId)
{
isReadAttr = true;
}
else if (zclFrame.isProfileWideCommand() && zclFrame.commandId() == deCONZ::ZclReportAttributesId)
{
}
else
{
return; // neither ZCL Report nor ZCL Read Attributes Response
}
const NodeValue::UpdateType updateType = isReadAttr ? NodeValue::UpdateByZclRead : NodeValue::UpdateByZclReport;
bool configUpdated = false;
bool stateUpdated = false;
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;
}
ResourceItem *item = nullptr;
switch (attrId)
{
case OCCUPIED_STATE:
{
quint8 occupancy = attr.numericValue().u8;
item = sensor->item(RStatePresence);
if (item)
{
item->setValue(occupancy);
enqueueEvent(Event(RSensors, RStatePresence, sensor->id(), item));
stateUpdated = true;
DDF_AnnoteZclParse(sensor, item, ind.srcEndpoint(), ind.clusterId(), attrId, "Item.val = Attr.val != 0");
// The checked sensors support reporting occupancy = false
if (!sensor->modelId().startsWith(QLatin1String("MOSZB-1")) && !sensor->modelId().startsWith(QLatin1String("SML00")))
{
const NodeValue &val = sensor->getZclValue(OCCUPANCY_SENSING_CLUSTER_ID, OCCUPIED_STATE);
// prepare to automatically set presence to false
if (item->toBool())
{
if (val.maxInterval > 0 && updateType == NodeValue::UpdateByZclReport)
{
// prevent setting presence back to false, when report.maxInterval > config.duration
// Add 3 seconds grace time for late reports
sensor->durationDue = item->lastSet().addSecs(val.maxInterval + 3);
}
else
{
ResourceItem *item2 = sensor->item(RConfigDuration);
if (item2 && item2->toNumber() > 0)
{
// If occupied state is not reportable, add duration seconds after a occupied = true to automatically set to false
sensor->durationDue = item->lastSet().addSecs(item2->toNumber());
}
}
}
}
}
sensor->setZclValue(updateType, ind.srcEndpoint(), OCCUPANCY_SENSING_CLUSTER_ID, OCCUPIED_STATE, attr.numericValue());
}
break;
case OCCUPIED_TO_UNOCCUPIED_DELAY:
{
if (sensor->modelId() == QLatin1String("LG IP65 HMS"))
{
// TODO(mpi): this can be removed, I don't think there are any users of this device (large industrial light+sensor)
quint16 duration = attr.numericValue().u16;
item = sensor->item(RConfigDuration);
if (!item) { item = sensor->addItem(DataTypeUInt16, RConfigDuration); }
if (item && item->toNumber() != duration)
{
enqueueEvent(Event(RSensors, RConfigDuration, sensor->id(), item));
if (item->toNumber() <= 0)
{
DBG_Printf(DBG_INFO, "got occupied to unoccupied delay %u\n", duration);
item->setValue(duration);
configUpdated = true;
}
else
{
DBG_Printf(DBG_INFO, "occupied to unoccupied delay is %u should be %u, force rewrite\n", duration, (quint16)item->toNumber());
if (!sensor->mustRead(WRITE_OCCUPANCY_CONFIG))
{
sensor->enableRead(WRITE_OCCUPANCY_CONFIG);
sensor->setNextReadTime(WRITE_OCCUPANCY_CONFIG, queryTime);
queryTime = queryTime.addSecs(1);
}
if (!sensor->mustRead(READ_OCCUPANCY_CONFIG))
{
sensor->enableRead(READ_OCCUPANCY_CONFIG);
sensor->setNextReadTime(READ_OCCUPANCY_CONFIG, queryTime);
queryTime = queryTime.addSecs(5);
}
Q_Q(DeRestPlugin);
q->startZclAttributeTimer(750);
}
}
}
else
{
quint16 delay = attr.numericValue().u16;
item = sensor->item(RConfigDelay);
if (item && item->toNumber() != delay)
{
item->setValue(delay);
enqueueEvent(Event(RSensors, RConfigDelay, sensor->id(), item));
configUpdated = true;
}
if (sensor->mustRead(WRITE_DELAY))
{
ResourceItem *item = sensor->item(RConfigPending);
if (item)
{
quint16 mask = item->toNumber();
mask &= ~R_PENDING_DELAY;
item->setValue(mask);
enqueueEvent(Event(RSensors, RConfigPending, sensor->id(), item));
}
sensor->clearRead(WRITE_DELAY);
}
}
sensor->setZclValue(updateType, ind.srcEndpoint(), OCCUPANCY_SENSING_CLUSTER_ID, OCCUPIED_TO_UNOCCUPIED_DELAY, attr.numericValue());
}
break;
default:
break;
}
}
if (stateUpdated)
{
sensor->updateStateTimestamp();
enqueueEvent(Event(RSensors, RStateLastUpdated, sensor->id()));
}
if (configUpdated || stateUpdated)
{
updateSensorEtag(&*sensor);
sensor->setNeedSaveDatabase(true);
queSaveDb(DB_SENSORS, DB_SHORT_SAVE_DELAY);
}
}