forked from dresden-elektronik/deconz-rest-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
device_compat.cpp
185 lines (154 loc) · 5.6 KB
/
device_compat.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
/*
* Copyright (c) 2021 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 <memory>
#include "database.h"
#include "device.h"
#include "device_compat.h"
#include "resource.h"
#include "sensor.h"
#include "light_node.h"
int getFreeSensorId();
int getFreeLightId();
/*! Overloads to add specific resources in higher layer.
Since Device class doesn't know anything about web plugin or testing code,
this is a free standing function which needs to be implemented in the higher layer.
*/
Resource *DEV_AddResource(const Sensor &sensor);
Resource *DEV_AddResource(const LightNode &lightNode);
/*! V1 compatibility function to create SensorNodes based on sub-device description.
*/
static Resource *DEV_InitSensorNodeFromDescription(Device *device, const DeviceDescription::SubDevice &sub, const QString &uniqueId)
{
Sensor sensor;
sensor.fingerPrint() = sub.fingerPrint;
sensor.address().setExt(device->item(RAttrExtAddress)->toNumber());
sensor.address().setNwk(device->item(RAttrNwkAddress)->toNumber());
sensor.setModelId(device->item(RAttrModelId)->toCString());
sensor.setManufacturer(device->item(RAttrManufacturerName)->toCString());
sensor.setType(DeviceDescriptions::instance()->constantToString(sub.type));
sensor.setUniqueId(uniqueId);
sensor.setNode(const_cast<deCONZ::Node*>(device->node()));
R_SetValue(&sensor, RConfigOn, true, ResourceItem::SourceApi);
auto dbItem = std::make_unique<DB_LegacyItem>();
dbItem->uniqueId = sensor.item(RAttrUniqueId)->toCString();
{
dbItem->column = "sid";
if (DB_LoadLegacySensorValue(dbItem.get()))
{
sensor.setId(toLatin1String(dbItem->value));
}
else
{
sensor.setId(QString::number(getFreeSensorId()));
}
}
{
dbItem->column = "name";
if (DB_LoadLegacySensorValue(dbItem.get()))
{
sensor.setName(dbItem->value.c_str());
}
else
{
QString friendlyName = sensor.type();
if (friendlyName.startsWith("ZHA") || friendlyName.startsWith("ZLL"))
{
friendlyName = friendlyName.mid(3);
}
sensor.setName(QString("%1 %2").arg(friendlyName, sensor.id()));
}
}
sensor.setNeedSaveDatabase(true);
sensor.rx();
auto *r = DEV_AddResource(sensor);
Q_ASSERT(r);
device->addSubDevice(r);
return r;
}
/*! V1 compatibility function to create LightsNode based on sub-device description.
*/
static Resource *DEV_InitLightNodeFromDescription(Device *device, const DeviceDescription::SubDevice &sub, const QString &uniqueId)
{
LightNode lightNode;
{
const auto ls = uniqueId.split('-', SKIP_EMPTY_PARTS);
if (ls.size() >= 2 && device->node())
{
bool ok;
const uint ep = ls[1].toUInt(&ok, 16);
deCONZ::SimpleDescriptor sd;
if (device->node()->copySimpleDescriptor(ep, &sd) == 0)
{
lightNode.setHaEndpoint(sd);
}
}
}
lightNode.address().setExt(device->item(RAttrExtAddress)->toNumber());
lightNode.address().setNwk(device->item(RAttrNwkAddress)->toNumber());
lightNode.setModelId(device->item(RAttrModelId)->toCString());
lightNode.setManufacturerName(device->item(RAttrManufacturerName)->toCString());
lightNode.setManufacturerCode(device->node()->nodeDescriptor().manufacturerCode());
lightNode.setNode(const_cast<deCONZ::Node*>(device->node())); // TODO this is evil
lightNode.item(RAttrType)->setValue(DeviceDescriptions::instance()->constantToString(sub.type));
lightNode.setUniqueId(uniqueId);
lightNode.setNode(const_cast<deCONZ::Node*>(device->node()));
auto dbItem = std::make_unique<DB_LegacyItem>();
dbItem->uniqueId = lightNode.item(RAttrUniqueId)->toCString();
{
dbItem->column = "id";
if (DB_LoadLegacyLightValue(dbItem.get()))
{
lightNode.setId(toLatin1String(dbItem->value));
}
else
{
lightNode.setId(QString::number(getFreeLightId()));
}
}
{
dbItem->column = "name";
if (DB_LoadLegacyLightValue(dbItem.get()))
{
lightNode.setName(dbItem->value.c_str());
}
else
{
lightNode.setName(QString("%1 %2").arg(lightNode.type(), lightNode.id()));
}
}
// remove some items which need to be specified via DDF
lightNode.removeItem(RStateOn);
lightNode.removeItem(RStateBri);
lightNode.removeItem(RStateHue);
lightNode.removeItem(RStateSat);
lightNode.removeItem(RStateAlert);
lightNode.setNeedSaveDatabase(true);
lightNode.rx();
auto *r = DEV_AddResource(lightNode);
Q_ASSERT(r);
device->addSubDevice(r);
return r;
}
/*! Creates Sensor and LightNode based on sub-device description.
The purpose of this function is to hide Sensor and LightNode classes from Device code.
\returns Resource pointer of the related node.
*/
Resource *DEV_InitCompatNodeFromDescription(Device *device, const DeviceDescription::SubDevice &sub, const QString &uniqueId)
{
if (sub.restApi == QLatin1String("/sensors"))
{
return DEV_InitSensorNodeFromDescription(device, sub, uniqueId);
}
else if (sub.restApi == QLatin1String("/lights"))
{
return DEV_InitLightNodeFromDescription(device, sub, uniqueId);
}
return nullptr;
}