forked from dresden-elektronik/deconz-rest-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
air_quality.cpp
148 lines (128 loc) · 5.21 KB
/
air_quality.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
#include "de_web_plugin.h"
#include "de_web_plugin_private.h"
/*! Handle packets related to manufacturer specific clusters for air quality (VOC).
\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::handleAirQualityClusterIndication(const deCONZ::ApsDataIndication &ind, deCONZ::ZclFrame &zclFrame)
{
Sensor *sensor = getSensorNodeForAddressAndEndpoint(ind.srcAddress(), ind.srcEndpoint(), QLatin1String("ZHAAirQuality"));
if (!sensor)
{
DBG_Printf(DBG_INFO, "No air quality 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 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;
}
quint32 levelPpb = UINT32_MAX; // invalid value
switch (attrId)
{
case 0x0000: // Measured value
{
if (ind.clusterId() == 0xFC03 && sensor->modelId() == QLatin1String("AQSZB-110")) // Develco air quality sensor
{
levelPpb = attr.numericValue().u16;
}
}
break;
case 0x4004:
{
// Bosch air quality sensor
if (ind.clusterId() == BOSCH_AIR_QUALITY_CLUSTER_ID && sensor->modelId() == QLatin1String("AIR"))
{
levelPpb = attr.numericValue().u16;
}
}
break;
default:
break;
}
if (levelPpb != UINT32_MAX)
{
QString airquality = QLatin1String("none");
if (levelPpb <= 65) { airquality = QLatin1String("excellent"); }
if (levelPpb > 65 && levelPpb <= 220) { airquality = QLatin1String("good"); }
if (levelPpb > 220 && levelPpb <= 660) { airquality = QLatin1String("moderate"); }
if (levelPpb > 660 && levelPpb <= 2200) { airquality = QLatin1String("poor"); }
if (levelPpb > 2200 && levelPpb <= 5500) { airquality = QLatin1String("unhealthy"); }
if (levelPpb > 5500 ) { airquality = QLatin1String("out of scale"); }
ResourceItem *item = sensor->item(RStateAirQuality);
if (item)
{
if (updateType == NodeValue::UpdateByZclReport)
{
stateUpdated = true;
}
if (item->toString() != airquality)
{
item->setValue(airquality);
enqueueEvent(Event(RSensors, RStateAirQuality, sensor->id(), item));
stateUpdated = true;
}
}
item = sensor->item(RStateAirQualityPpb);
if (item)
{
if (updateType == NodeValue::UpdateByZclReport)
{
stateUpdated = true;
}
if (item->toNumber() != levelPpb)
{
item->setValue(levelPpb);
enqueueEvent(Event(RSensors, RStateAirQualityPpb, sensor->id(), item));
stateUpdated = true;
}
}
sensor->setZclValue(updateType, ind.srcEndpoint(), ind.clusterId(), attrId, attr.numericValue());
}
}
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);
}
}
}