forked from dresden-elektronik/deconz-rest-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
electrical_measurement.cpp
279 lines (247 loc) · 14.7 KB
/
electrical_measurement.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
#include <math.h>
#include "de_web_plugin.h"
#include "de_web_plugin_private.h"
#include "device.h"
#include "device_descriptions.h"
#define ACTIVE_POWER 0x050B
#define RMS_VOLTAGE 0x0505
#define RMS_CURRENT 0x0508
#define APPARENT_POWER 0x050F
/*! Handle packets related to the ZCL electrical measurement cluster.
\param ind the APS level data indication containing the ZCL packet
\param zclFrame the actual ZCL frame which holds the electrical measurement cluster command or attribute
*/
void DeRestPluginPrivate::handleElectricalMeasurementClusterIndication(const deCONZ::ApsDataIndication &ind, const deCONZ::ZclFrame &zclFrame)
{
if (zclFrame.isDefaultResponse())
{
return;
}
Sensor *sensor = getSensorNodeForAddressAndEndpoint(ind.srcAddress(), ind.srcEndpoint(), QLatin1String("ZHAPower"));
if (!sensor)
{
DBG_Printf(DBG_INFO, "No power 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;
bool isReporting = false;
if (zclFrame.isProfileWideCommand() && zclFrame.commandId() == deCONZ::ZclReadAttributesResponseId)
{
isReadAttr = true;
}
if (zclFrame.isProfileWideCommand() && zclFrame.commandId() == deCONZ::ZclReportAttributesId)
{
isReporting = true;
}
// Read ZCL reporting and ZCL Read Attributes Response
if (isReadAttr || isReporting)
{
const auto modelId = sensor->modelId();
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 ACTIVE_POWER:
{
qint16 power = attr.numericValue().s16;
item = sensor->item(RStatePower);
if (item && power != -32768)
{
if (modelId == QLatin1String("SmartPlug") || // Heiman
modelId.startsWith(QLatin1String("SKHMP30")) || // GS smart plug
modelId.startsWith(QLatin1String("ROB_200")) || // ROBB Smarrt micro dimmer
modelId.startsWith(QLatin1String("Micro Smart Dimmer")) || // Sunricher Micro Smart Dimmer
modelId == QLatin1String("RICI01") || // LifeControl Smart Plug
modelId.startsWith(QLatin1String("outlet")) || // Samsung SmartThings IM6001-OTP/IM6001-OTP01
modelId == QLatin1String("3200-Sgb") || // Samsung/Centralite smart outlet
modelId == QLatin1String("3200-de") || // Samsung/Centralite smart outlet
modelId.startsWith(QLatin1String("lumi.switch.n0agl1")) || // Xiaomi Aqara Single Switch Module T1 (With Neutral)
modelId.startsWith(QLatin1String("lumi.switch.b1naus01"))) // Xiaomi ZB3.0 Smart Wall Switch
{
power = static_cast<qint16>(round((double)power / 10.0)); // 0.1W -> W
DDF_AnnoteZclParse(sensor, item, ind.srcEndpoint(), ind.clusterId(), attrId, "if (Attr.val != -32768) { Item.val = Math.round(Attr.val / 10); } ");
}
else if (modelId.startsWith(QLatin1String("Plug")) && sensor->manufacturer() == QLatin1String("OSRAM")) // OSRAM
{
power = power == 28000 ? 0 : power / 10;
DDF_AnnoteZclParse(sensor, item, ind.srcEndpoint(), ind.clusterId(), attrId, "if (Attr.val != -32768) { Item.val = Attr.val == 28000 ? 0: Attr.val / 10; } ");
}
else if (modelId.startsWith(QLatin1String("SZ-ESW01"))) // Sercomm / Telstra smart plug
{
power = static_cast<qint16>(round(((double)power * 128) / 1000.0));
DDF_AnnoteZclParse(sensor, item, ind.srcEndpoint(), ind.clusterId(), attrId, "if (Attr.val != -32768) { Item.val = Math.round(Attr.val * 128 / 1000); } ");
}
else if (modelId.startsWith(QLatin1String("lumi.relay.c2acn"))) // Xiaomi relay
{
continue; // Device seems to always report -1 via this cluster/attribute
}
else
{
DDF_AnnoteZclParse(sensor, item, ind.srcEndpoint(), ind.clusterId(), attrId, "if (Attr.val != -32768) { Item.val = Attr.val; } ");
}
if (!item->lastSet().isValid() || item->toNumber() != power)
{
item->setValue(power); // in W
enqueueEvent(Event(RSensors, RStatePower, sensor->id(), item));
}
}
sensor->setZclValue(updateType, ind.srcEndpoint(), ELECTRICAL_MEASUREMENT_CLUSTER_ID, attrId, attr.numericValue());
stateUpdated = true;
}
break;
case RMS_VOLTAGE:
{
quint16 voltage = attr.numericValue().u16;
item = sensor->item(RStateVoltage);
if (item && voltage != 65535)
{
if (modelId == QLatin1String("SmartPlug") || // Heiman
modelId.startsWith(QLatin1String("SMRZB-3")) || // Develco smart relay
modelId.startsWith(QLatin1String("SMRZB-1")) || // Develco smart cable
modelId.startsWith(QLatin1String("SKHMP30")) || // GS smart plug
modelId == QLatin1String("Smart16ARelay51AU") || // Aurora (Develco) smart plug
modelId == QLatin1String("PoP")) // Apex Smart Plug
{
voltage = static_cast<quint16>(round((double)voltage / 100.0)); // 0.01V -> V
DDF_AnnoteZclParse(sensor, item, ind.srcEndpoint(), ind.clusterId(), attrId, "if (Attr.val != 65535) { Item.val = Math.round(Attr.val / 100); } ");
}
else if (modelId == QLatin1String("RICI01") || // LifeControl Smart Plug
modelId.startsWith(QLatin1String("outlet")) || // Samsung SmartThings IM6001-OTP/IM6001-OTP01
modelId.startsWith(QLatin1String("ROB_200")) || // ROBB Smarrt micro dimmer
modelId.startsWith(QLatin1String("Micro Smart Dimmer"))) // Sunricher Micro Smart Dimmer
{
voltage = static_cast<quint16>(round((double)voltage / 10.0)); // 0.1V -> V
DDF_AnnoteZclParse(sensor, item, ind.srcEndpoint(), ind.clusterId(), attrId, "if (Attr.val != 65535) { Item.val = Math.round(Attr.val / 10); } ");
}
else if (modelId.startsWith(QLatin1String("SZ-ESW01"))) // Sercomm / Telstra smart plug
{
voltage = static_cast<quint16>(round((double)voltage / 125.0)); // -> V
DDF_AnnoteZclParse(sensor, item, ind.srcEndpoint(), ind.clusterId(), attrId, "if (Attr.val != 65535) { Item.val = Math.round(Attr.val / 125); } ");
}
else
{
DDF_AnnoteZclParse(sensor, item, ind.srcEndpoint(), ind.clusterId(), attrId, "if (Attr.val != 65535) { Item.val = Attr.val; } ");
}
if (!item->lastSet().isValid() || item->toNumber() != voltage)
{
item->setValue(voltage); // in V
enqueueEvent(Event(RSensors, RStateVoltage, sensor->id(), item));
}
}
sensor->setZclValue(updateType, ind.srcEndpoint(), ELECTRICAL_MEASUREMENT_CLUSTER_ID, attrId, attr.numericValue());
stateUpdated = true;
}
break;
case RMS_CURRENT:
{
quint16 current = attr.numericValue().u16;
item = sensor->item(RStateCurrent);
if (item && current != 65535)
{
if (modelId.startsWith(QLatin1String("outlet")) || // Samsung SmartThings IM6001-OTP/IM6001-OTP01
modelId == QLatin1String("DoubleSocket50AU") || // Aurora
modelId == QLatin1String("Smart16ARelay51AU") || // Aurora (Develco) smart plug
modelId == QLatin1String("RICI01") || // LifeControl Smart Plug
modelId.startsWith(QLatin1String("SZ-ESW01")) || // Sercomm / Telstra smart plug
modelId == QLatin1String("TS0121") || // Tuya smart plug
modelId.startsWith(QLatin1String("ROB_200")) || // ROBB Smarrt micro dimmer
modelId.startsWith(QLatin1String("Micro Smart Dimmer")) || // Sunricher Micro Smart Dimmer
modelId == QLatin1String("SMRZB-1") || // Develco smart cable
modelId == QLatin1String("PoP") || // Apex Smart Plug
modelId == QLatin1String("TS011F")) // Tuya plugs
{
// already in mA
DDF_AnnoteZclParse(sensor, item, ind.srcEndpoint(), ind.clusterId(), attrId, "if (Attr.val != 65535) { Item.val = Attr.val; } ");
}
else if (modelId == QLatin1String("SmartPlug") || // Heiman
modelId.startsWith(QLatin1String("EMIZB-1")) || // Develco EMI
modelId.startsWith(QLatin1String("SKHMP30")) || // GS smart plug
modelId == QLatin1String("3200-Sgb") || // Samsung smart outlet
modelId == QLatin1String("3200-de") || // Samsung smart outlet
modelId.startsWith(QLatin1String("SPW35Z")) || // RT-RK OBLO SPW35ZD0 smart plug
modelId == QLatin1String("TH1300ZB")) // Sinope thermostat
{
current *= 10; // 0.01A -> mA
DDF_AnnoteZclParse(sensor, item, ind.srcEndpoint(), ind.clusterId(), attrId, "if (Attr.val != 65535) { Item.val = Attr.val * 10; } ");
}
else
{
current *= 1000; // A -> mA
DDF_AnnoteZclParse(sensor, item, ind.srcEndpoint(), ind.clusterId(), attrId, "if (Attr.val != 65535) { Item.val = Attr.val * 1000; } ");
}
if (!item->lastSet().isValid() || item->toNumber() != current)
{
item->setValue(current); // in mA
enqueueEvent(Event(RSensors, RStateCurrent, sensor->id(), item));
}
}
sensor->setZclValue(updateType, ind.srcEndpoint(), ELECTRICAL_MEASUREMENT_CLUSTER_ID, attrId, attr.numericValue());
stateUpdated = true;
}
break;
case APPARENT_POWER:
{
quint16 power = attr.numericValue().u16;
item = sensor->item(RStatePower);
if (item && power != 65535)
{
if (modelId == QLatin1String("TH1300ZB")) // Sinope thermostat
{
power = static_cast<quint16>(round((double)power / 1000.0)); // -> W
DDF_AnnoteZclParse(sensor, item, ind.srcEndpoint(), ind.clusterId(), attrId, "if (Attr.val != 65535) { Item.val = Math.round(Attr.val / 1000); } ");
}
else
{
DDF_AnnoteZclParse(sensor, item, ind.srcEndpoint(), ind.clusterId(), attrId, "if (Attr.val != 65535) { Item.val = Attr.val; } ");
}
if (!item->lastSet().isValid() || item->toNumber() != power)
{
item->setValue(power); // in W
enqueueEvent(Event(RSensors, RStatePower, sensor->id(), item));
}
}
sensor->setZclValue(updateType, ind.srcEndpoint(), ELECTRICAL_MEASUREMENT_CLUSTER_ID, attrId, attr.numericValue());
stateUpdated = true;
}
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);
}
}
}