Skip to content

Commit 24ec596

Browse files
committedMay 27, 2021
Refactor button map data types
* Separate from Sensor class; * Add button map related struct types in seperate files; * Use std::vector instead QMap; * Use lightweight index based references to button maps in Sensor; * Don't copy "large" button maps around; * Parse "buttons" meta information for introspection; This is preparation for separate REST API introspection PR. Fix shadow iterator variable in loadButtonMapJson() Correct intendation in BM_ButtonMapRefForHash
1 parent c12a79d commit 24ec596

11 files changed

+305
-90
lines changed
 

‎button_maps.cpp

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright (c) 2021 dresden elektronik ingenieurtechnik gmbh.
3+
* All rights reserved.
4+
*
5+
* The software in this package is published under the terms of the BSD
6+
* style license a copy of which has been included with this distribution in
7+
* the LICENSE.txt file.
8+
*
9+
*/
10+
11+
#include "button_maps.h"
12+
13+
ButtonMapRef BM_ButtonMapRefForHash(uint32_t buttonMapNameHash, const std::vector<ButtonMap> &buttonMaps)
14+
{
15+
const auto bm = std::find_if(buttonMaps.cbegin(), buttonMaps.cend(),
16+
[buttonMapNameHash](const auto &bm) { return bm.buttonMapRef.hash == buttonMapNameHash; });
17+
if (bm != buttonMaps.cend())
18+
{
19+
return bm->buttonMapRef;
20+
}
21+
22+
return {};
23+
}
24+
25+
const ButtonMap *BM_ButtonMapForRef(ButtonMapRef ref, const std::vector<ButtonMap> &buttonMaps)
26+
{
27+
if (isValid(ref) && ref.index < buttonMaps.size())
28+
{
29+
const ButtonMap &bm = buttonMaps[ref.index];
30+
if (bm.buttonMapRef.hash == ref.hash)
31+
{
32+
return &bm;
33+
}
34+
}
35+
36+
return nullptr;
37+
}
38+
39+
const ButtonMap *BM_ButtonMapForProduct(ProductIdHash productHash, const std::vector<ButtonMap> &buttonMaps,
40+
const std::vector<ButtonProduct> &buttonProductMap)
41+
{
42+
ButtonMapRef buttonMapHash{};
43+
{
44+
const auto mapping = std::find_if(buttonProductMap.cbegin(), buttonProductMap.cend(),
45+
[productHash](const auto &i) { return i.productHash == productHash; });
46+
47+
if (mapping != buttonProductMap.cend())
48+
{
49+
buttonMapHash = mapping->buttonMapRef;
50+
}
51+
}
52+
53+
if (isValid(buttonMapHash))
54+
{
55+
return BM_ButtonMapForRef(buttonMapHash, buttonMaps);
56+
}
57+
58+
return nullptr;
59+
}

‎button_maps.h

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright (c) 2021 dresden elektronik ingenieurtechnik gmbh.
3+
* All rights reserved.
4+
*
5+
* The software in this package is published under the terms of the BSD
6+
* style license a copy of which has been included with this distribution in
7+
* the LICENSE.txt file.
8+
*
9+
*/
10+
11+
#ifndef BUTTON_MAPS_H
12+
#define BUTTON_MAPS_H
13+
14+
#include <QString>
15+
#include <vector>
16+
17+
using ProductIdHash = std::size_t;
18+
19+
/*! Reference to a button map for quick lookup and leightweight embedding. */
20+
struct ButtonMapRef
21+
{
22+
uint32_t hash = 0; //! hash of button map name
23+
uint32_t index = UINT32_MAX; //! index into ButtonMap vector
24+
};
25+
26+
/*! Meta data about buttons, not used by the plugin but exposed via REST API. */
27+
struct ButtonMeta
28+
{
29+
struct Button
30+
{
31+
QString name;
32+
int button;
33+
};
34+
35+
std::vector<ButtonMeta::Button> buttons;
36+
ButtonMapRef buttonMapRef{};
37+
};
38+
39+
/*! Mapping between a product and a buttom map.
40+
*/
41+
struct ButtonProduct
42+
{
43+
ButtonMapRef buttonMapRef{};
44+
ProductIdHash productHash = 0; //! hash of modelid or manufacturer name generated by \c productHash(Resource*)
45+
};
46+
47+
struct ButtonMap
48+
{
49+
struct Item
50+
{
51+
quint8 mode;
52+
quint8 endpoint;
53+
quint16 clusterId;
54+
quint8 zclCommandId;
55+
quint16 zclParam0;
56+
int button;
57+
QString name;
58+
};
59+
std::vector<ButtonMap::Item> buttons;
60+
ButtonMapRef buttonMapRef{};
61+
};
62+
63+
ButtonMapRef BM_ButtonMapRefForHash(uint32_t buttonMapNameHash, const std::vector<ButtonMap> &buttonMaps);
64+
const ButtonMap *BM_ButtonMapForRef(ButtonMapRef ref, const std::vector<ButtonMap> &buttonMaps);
65+
const ButtonMap *BM_ButtonMapForProduct(ProductIdHash productHash, const std::vector<ButtonMap> &buttonMaps, const std::vector<ButtonProduct> &buttonProductMap);
66+
67+
inline bool isValid(ButtonMapRef a) { return a.hash != 0 && a.index != UINT32_MAX; }
68+
inline bool operator==(ButtonMapRef a, ButtonMapRef b) { return a.hash == b.hash && a.index == b.index; }
69+
inline bool operator!=(ButtonMapRef a, ButtonMapRef b) { return !(a == b); }
70+
71+
#endif // BUTTON_MAPS_H

‎de_web.pro

+2
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ DEFINES += GW_DEFAULT_NAME=\\\"Phoscon-GW\\\"
9999

100100
HEADERS = bindings.h \
101101
backup.h \
102+
button_maps.h \
102103
connectivity.h \
103104
colorspace.h \
104105
daylight.h \
@@ -132,6 +133,7 @@ SOURCES = air_quality.cpp \
132133
authorisation.cpp \
133134
backup.cpp \
134135
bindings.cpp \
136+
button_maps.cpp \
135137
change_channel.cpp \
136138
connectivity.cpp \
137139
colorspace.cpp \

‎de_web_plugin.cpp

+25-9
Original file line numberDiff line numberDiff line change
@@ -868,14 +868,15 @@ DeRestPluginPrivate::DeRestPluginPrivate(QObject *parent) :
868868
}
869869

870870
QStringList requiredJsonObjects = {"buttons", "buttonActions", "clusters", "commands", "maps"};
871-
QJsonDocument buttonMaps = readButtonMapJson(path);
871+
QJsonDocument buttonMapsDoc = readButtonMapJson(path);
872872

873-
if (checkRootLevelObjectsJson(buttonMaps, requiredJsonObjects))
873+
if (checkRootLevelObjectsJson(buttonMapsDoc, requiredJsonObjects))
874874
{
875-
btnMapClusters = loadButtonMapClustersJson(buttonMaps);
876-
btnMapClusterCommands = loadButtonMapCommadsJson(buttonMaps);
877-
buttonMapForModelId = loadButtonMapModelIdsJson(buttonMaps);
878-
buttonMapData = loadButtonMapsJson(buttonMaps, btnMapClusters, btnMapClusterCommands);
875+
btnMapClusters = loadButtonMapClustersJson(buttonMapsDoc);
876+
btnMapClusterCommands = loadButtonMapCommadsJson(buttonMapsDoc);
877+
buttonMaps = loadButtonMapsJson(buttonMapsDoc, btnMapClusters, btnMapClusterCommands);
878+
buttonMeta = loadButtonMetaJson(buttonMapsDoc, buttonMaps);
879+
buttonProductMap = loadButtonMapModelIdsJson(buttonMapsDoc, buttonMaps);
879880
break; // only load once
880881
}
881882
}
@@ -4014,7 +4015,22 @@ void DeRestPluginPrivate::checkSensorButtonEvent(Sensor *sensor, const deCONZ::A
40144015

40154016
bool checkReporting = false;
40164017
bool checkClientCluster = false;
4017-
const std::vector<Sensor::ButtonMap> buttonMapVec = sensor->buttonMap(buttonMapData, buttonMapForModelId);
4018+
4019+
const ButtonMap *buttonMap = nullptr;
4020+
4021+
if (!isValid(sensor->buttonMapRef())) // TODO sensor.hasButtonMap()
4022+
{
4023+
buttonMap = BM_ButtonMapForProduct(productHash(sensor), buttonMaps, buttonProductMap);
4024+
if (buttonMap)
4025+
{
4026+
sensor->setButtonMapRef(buttonMap->buttonMapRef);
4027+
}
4028+
}
4029+
else
4030+
{
4031+
buttonMap = BM_ButtonMapForRef(sensor->buttonMapRef(), buttonMaps);
4032+
}
4033+
40184034
QString cluster = "0x" + QString("%1").arg(ind.clusterId(), 4, 16, QLatin1Char('0')).toUpper();
40194035
QString cmd = "0x" + QString("%1").arg(zclFrame.commandId(), 2, 16, QLatin1Char('0')).toUpper();
40204036
QString addressMode;
@@ -4034,7 +4050,7 @@ void DeRestPluginPrivate::checkSensorButtonEvent(Sensor *sensor, const deCONZ::A
40344050
if (!temp.empty() && !temp.key(zclFrame.commandId()).isEmpty()) { cmd = temp.key(zclFrame.commandId()) + " (" + cmd + ")"; }
40354051
}
40364052

4037-
if (buttonMapVec.empty())
4053+
if (!buttonMap || buttonMap->buttons.empty())
40384054
{
40394055
DBG_Printf(DBG_INFO, "[INFO] - No button map for: %s%s, endpoint: 0x%02X, cluster: %s, command: %s, payload: %s, zclSeq: %u\n",
40404056
qPrintable(sensor->modelId()), qPrintable(addressMode), ind.srcEndpoint(), qPrintable(cluster), qPrintable(cmd), qPrintable(zclPayload), zclFrame.sequenceNumber());
@@ -4435,7 +4451,7 @@ void DeRestPluginPrivate::checkSensorButtonEvent(Sensor *sensor, const deCONZ::A
44354451
}
44364452

44374453
bool ok = false;
4438-
for (const auto &buttonMap : buttonMapVec)
4454+
for (const auto &buttonMap : buttonMap->buttons)
44394455
{
44404456
if (buttonMap.mode != Sensor::ModeNone && !ok)
44414457
{

‎de_web_plugin_private.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -1684,10 +1684,11 @@ public Q_SLOTS:
16841684
QString emptyString;
16851685

16861686
// JSON support
1687-
QMap<QString, std::vector<Sensor::ButtonMap>> buttonMapData;
1687+
std::vector<ButtonMeta> buttonMeta;
1688+
std::vector<ButtonMap> buttonMaps;
16881689
QMap<QString, quint16> btnMapClusters;
16891690
QMap<QString, QMap<QString, quint16>> btnMapClusterCommands;
1690-
QMap<QString, QString> buttonMapForModelId;
1691+
std::vector<ButtonProduct> buttonProductMap;
16911692

16921693
// gateways
16931694
std::vector<Gateway*> gateways;

‎product_match.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -243,3 +243,21 @@ bool isLidlDevice(const QString &zigbeeModelIdentifier, const QString &manufactu
243243
}
244244
return false;
245245
}
246+
247+
uint productHash(const Resource *r)
248+
{
249+
if (!r || !r->item(RAttrManufacturerName) || !r->item(RAttrModelId))
250+
{
251+
return 0;
252+
}
253+
254+
if (isTuyaManufacturerName(r->item(RAttrManufacturerName)->toString()))
255+
{
256+
// for Tuya devices use manufacturer name as modelid
257+
return qHash(r->item(RAttrManufacturerName)->toString());
258+
}
259+
else
260+
{
261+
return qHash(r->item(RAttrModelId)->toString());
262+
}
263+
}

‎product_match.h

+1
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@ const QString R_GetProductId(Resource *resource);
2626
bool isTuyaManufacturerName(const QString &manufacturer);
2727
bool isLidlDevice(const QString &zigbeeModelIdentifier, const QString &manufacturername);
2828
const lidlDevice *getLidlDevice(const QString &zigbeeManufacturerName);
29+
uint productHash(const Resource *r);
2930

3031
#endif // PRODUCT_MATCH_H

‎read_files.cpp

+115-24
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#include <QFile>
2-
#include <QJsonDocument>
32
#include <QJsonObject>
43
#include <QJsonArray>
54
#include <QJsonValue>
65
#include <QJsonParseError>
7-
#include <sensor.h>
8-
#include <deconz.h>
6+
#include <deconz/dbg_trace.h>
7+
#include "read_files.h"
8+
#include "sensor.h"
99

1010
QJsonDocument readButtonMapJson(const QString &path)
1111
{
@@ -142,46 +142,51 @@ QMap<QString, QMap<QString, quint16>> loadButtonMapCommadsJson(const QJsonDocume
142142

143143
/*! Reads the associated modelIDs from all available button maps in JSON file.
144144
*/
145-
QMap<QString, QString> loadButtonMapModelIdsJson(const QJsonDocument &buttonMaps)
145+
std::vector<ButtonProduct> loadButtonMapModelIdsJson(const QJsonDocument &buttonMapsDoc, const std::vector<ButtonMap> &buttonMaps)
146146
{
147-
// Load ModelIDs
148-
QMap<QString, QString> buttonMapForModelId;
149-
QJsonObject allMapsObj = buttonMaps.object().value(QLatin1String("maps")).toObject(); // Get all button maps
147+
std::vector<ButtonProduct> result;
148+
result.reserve(128);
149+
150+
const QJsonObject allMapsObj = buttonMapsDoc.object().value(QLatin1String("maps")).toObject(); // Get all button maps
150151

151152
for (auto i = allMapsObj.constBegin(); i != allMapsObj.constEnd(); ++i) // Loop through button maps
152153
{
153-
QString buttonMapName = i.key(); // Individual button map name
154+
const QString buttonMapName = i.key(); // Individual button map name
155+
156+
ButtonProduct item;
157+
item.buttonMapRef = BM_ButtonMapRefForHash(qHash(i.key()), buttonMaps);
154158

155-
if (i.value().isObject()) // Check if individual button map is an object
159+
if (isValid(item.buttonMapRef) && i.value().isObject()) // Check if individual button map is an object
156160
{
157-
QJsonObject buttonMapObj = i.value().toObject();
161+
const QJsonObject buttonMapObj = i.value().toObject();
158162
if (buttonMapObj.value(QString("modelids")).isArray())
159163
{
160-
QJsonArray buttonMapModelIds = buttonMapObj.value(QString("modelids")).toArray();
164+
const QJsonArray buttonMapModelIds = buttonMapObj.value(QString("modelids")).toArray();
161165

162166
if (buttonMapModelIds.size() == 0)
163167
{
164168
DBG_Printf(DBG_INFO, "[WARNING] - Button map '%s' in JSON file has no assigned ModelIDs. Skip loading button map...\n", qPrintable(buttonMapName));
165169
continue; // Skip button map
166170
}
167171

168-
for (auto i = buttonMapModelIds.constBegin(); i != buttonMapModelIds.constEnd(); ++i) // Loop through modelIDs
172+
const auto jend = buttonMapModelIds.constEnd();
173+
for (auto j = buttonMapModelIds.constBegin(); j != jend; ++j) // Loop through modelIDs
169174
{
170-
QJsonValue val = *i;
175+
const QString modelId = j->toString();
171176

172-
if (val.isString() && val.toString().size() <= 32)
177+
if (j->isString() && !modelId.isEmpty() && modelId.size() <= 32)
173178
{
174-
buttonMapForModelId.insert(val.toString(), buttonMapName); // Assign button map to modelIDs
179+
item.productHash = qHash(modelId);
180+
result.push_back(item);
175181
}
176-
else if (val.isString() && val.toString().size() > 32)
182+
else if (j->isString() && modelId.size() > 32)
177183
{
178184
DBG_Printf(DBG_INFO, "[ERROR] - Entry of 'modelids', button map '%s' in JSON file is too long. Skipping entry...\n", qPrintable(buttonMapName));
179185
continue;
180186
}
181187
else
182188
{
183189
DBG_Printf(DBG_INFO, "[ERROR] - Expected entry of 'modelids', button map '%s' in JSON file to be a string, but isn't. Skipping entry...\n", qPrintable(buttonMapName));
184-
continue;
185190
}
186191
}
187192
}
@@ -197,19 +202,23 @@ QMap<QString, QString> loadButtonMapModelIdsJson(const QJsonDocument &buttonMaps
197202
continue; // Skip button map
198203
}
199204
}
200-
return buttonMapForModelId;
205+
return result;
201206
}
202207

203208

204209
/*! Reads all available button maps from JSON file.
205210
*/
206-
QMap<QString, std::vector<Sensor::ButtonMap>> loadButtonMapsJson(const QJsonDocument &buttonMaps, const QMap<QString, quint16> &btnMapClusters,
211+
std::vector<ButtonMap> loadButtonMapsJson(const QJsonDocument &buttonMaps, const QMap<QString, quint16> &btnMapClusters,
207212
const QMap<QString, QMap<QString, quint16>> &btnMapClusterCommands)
208213
{
214+
std::vector<ButtonMap> result;
215+
result.reserve(128);
216+
209217
QMap<QString, quint16> buttons;
210218
QMap<QString, quint8> actions;
211219
quint8 counter = 0;
212220

221+
213222
// Load button map buttons
214223
QJsonObject buttonsObj = buttonMaps.object().value(QLatin1String("buttons")).toObject();
215224

@@ -258,7 +267,6 @@ QMap<QString, std::vector<Sensor::ButtonMap>> loadButtonMapsJson(const QJsonDocu
258267
}
259268

260269
// Load button maps
261-
QMap<QString, std::vector<Sensor::ButtonMap>> buttonMapData;
262270
QJsonObject allMapsObj = buttonMaps.object().value(QLatin1String("maps")).toObject(); // Get all button maps
263271

264272
for (auto i = allMapsObj.constBegin(); i != allMapsObj.constEnd(); ++i) // Loop through button maps
@@ -273,7 +281,7 @@ QMap<QString, std::vector<Sensor::ButtonMap>> loadButtonMapsJson(const QJsonDocu
273281

274282
if (buttonMapObj.value(QString("map")).isArray()) // Check if button map is an array of arrays
275283
{
276-
std::vector<Sensor::ButtonMap> btnMapVec;
284+
std::vector<ButtonMap::Item> btnMapVec;
277285
QJsonArray buttonMapArr = buttonMapObj.value(QString("map")).toArray();
278286
//DBG_Printf(DBG_INFO, "[INFO] - Button map size: %d\n", i.value().toArray().size());
279287

@@ -294,7 +302,7 @@ QMap<QString, std::vector<Sensor::ButtonMap>> loadButtonMapsJson(const QJsonDocu
294302
{
295303
bool ok;
296304
quint16 btn = 0;
297-
Sensor::ButtonMap btnMap;
305+
ButtonMap::Item btnMap;
298306

299307
// Initialize with defaults
300308
btnMap.mode = Sensor::ModeNone;
@@ -458,6 +466,7 @@ QMap<QString, std::vector<Sensor::ButtonMap>> loadButtonMapsJson(const QJsonDocu
458466
continue;
459467
}
460468

469+
461470
//DBG_Printf(DBG_INFO, "[INFO] - btnMap item #6: %d\n", btnMap.button);
462471
//DBG_Printf(DBG_INFO, "[INFO] - btnMap item #7: %s\n", qPrintable(btnMap.name));
463472
btnMapVec.push_back(btnMap);
@@ -471,7 +480,21 @@ QMap<QString, std::vector<Sensor::ButtonMap>> loadButtonMapsJson(const QJsonDocu
471480
}
472481
}
473482

474-
buttonMapData.insert(buttonMapName, btnMapVec); // Assign vector of button maps to QMap
483+
ButtonMapRef buttonMapRef;
484+
buttonMapRef.hash = qHash(buttonMapName);
485+
buttonMapRef.index = result.size();
486+
487+
#ifdef QT_DEBUG
488+
{
489+
ButtonMapRef ref = BM_ButtonMapRefForHash(buttonMapRef.hash, result);
490+
if (isValid(ref))
491+
{
492+
DBG_Printf(DBG_INFO, "[ERROR] - Button map duplicated hash for %s\n", qPrintable(buttonMapName));
493+
}
494+
}
495+
#endif
496+
497+
result.emplace_back(ButtonMap{std::move(btnMapVec), buttonMapRef});
475498
}
476499
else
477500
{
@@ -488,5 +511,73 @@ QMap<QString, std::vector<Sensor::ButtonMap>> loadButtonMapsJson(const QJsonDocu
488511

489512
DBG_Printf(DBG_INFO, "[INFO] - Button maps loaded.\n");
490513

491-
return buttonMapData;
514+
return result;
515+
}
516+
517+
std::vector<ButtonMeta> loadButtonMetaJson(const QJsonDocument &buttonMapsDoc, const std::vector<ButtonMap> &buttonMaps)
518+
{
519+
std::vector<ButtonMeta> result;
520+
521+
const QLatin1String buttonPrefix("S_BUTTON_");
522+
const QJsonObject mapsObj = buttonMapsDoc.object().value(QLatin1String("maps")).toObject(); // Get all button maps
523+
524+
for (auto i = mapsObj.constBegin(); i != mapsObj.constEnd(); ++i) // Loop through button maps
525+
{
526+
ButtonMeta meta;
527+
meta.buttons.reserve(4);
528+
meta.buttonMapRef = BM_ButtonMapRefForHash(qHash(i.key()), buttonMaps); // Individual button map name
529+
530+
if (!isValid(meta.buttonMapRef))
531+
{
532+
continue;
533+
}
534+
535+
if (!i.value().isObject())
536+
{
537+
continue;
538+
}
539+
540+
const QJsonObject buttonMapObj = i.value().toObject();
541+
if (!buttonMapObj.value(QLatin1String("buttons")).isArray())
542+
{
543+
continue;
544+
}
545+
546+
const QJsonArray buttons = buttonMapObj.value(QLatin1String("buttons")).toArray();
547+
548+
for (auto j = buttons.constBegin(); j != buttons.constEnd(); ++j)
549+
{
550+
if (!j->isObject())
551+
{
552+
continue;
553+
}
554+
555+
const QJsonObject buttonObj = j->toObject();
556+
const auto keys = buttonObj.keys();
557+
for (const auto &k : keys)
558+
{
559+
if (!k.startsWith(buttonPrefix))
560+
{
561+
continue;
562+
}
563+
564+
bool ok = false;
565+
ButtonMeta::Button b;
566+
b.button = k.midRef(buttonPrefix.size()).toInt(&ok);
567+
568+
if (ok)
569+
{
570+
b.name = buttonObj.value(k).toString();
571+
meta.buttons.push_back(b);
572+
}
573+
}
574+
}
575+
576+
if (!meta.buttons.empty())
577+
{
578+
result.push_back(std::move(meta));
579+
}
580+
}
581+
582+
return result;
492583
}

‎read_files.h

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
#ifndef READ_FILES_H
22
#define READ_FILES_H
33

4-
#include <sensor.h>
4+
#include <QJsonDocument>
5+
#include "button_maps.h"
56

67
QJsonDocument readButtonMapJson(const QString &path);
78
bool checkRootLevelObjectsJson(const QJsonDocument &buttonMaps, const QStringList &requiredObjects);
89
QMap<QString, quint16> loadButtonMapClustersJson(const QJsonDocument &buttonMaps);
910
QMap<QString, QMap<QString, quint16>> loadButtonMapCommadsJson(const QJsonDocument &buttonMaps);
10-
QMap<QString, QString> loadButtonMapModelIdsJson(const QJsonDocument &buttonMaps);
11-
QMap<QString, std::vector<Sensor::ButtonMap>> loadButtonMapsJson(const QJsonDocument &buttonMaps, const QMap<QString, quint16> &btnMapClusters,
12-
const QMap<QString, QMap<QString, quint16>> &btnMapClusterCommands);
11+
std::vector<ButtonProduct> loadButtonMapModelIdsJson(const QJsonDocument &buttonMapsDoc, const std::vector<ButtonMap> &buttonMaps);
12+
std::vector<ButtonMeta> loadButtonMetaJson(const QJsonDocument &buttonMapsDoc, const std::vector<ButtonMap> &buttonMaps);
13+
std::vector<ButtonMap> loadButtonMapsJson(const QJsonDocument &buttonMaps, const QMap<QString, quint16> &btnMapClusters, const QMap<QString, QMap<QString, quint16>> &btnMapClusterCommands);
1314

1415
#endif // READ_FILES_H

‎sensor.cpp

+1-35
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013-2020 dresden elektronik ingenieurtechnik gmbh.
2+
* Copyright (c) 2013-2021 dresden elektronik ingenieurtechnik gmbh.
33
* All rights reserved.
44
*
55
* The software in this package is published under the terms of the BSD
@@ -539,37 +539,3 @@ const SensorFingerprint &Sensor::fingerPrint() const
539539
{
540540
return m_fingerPrint;
541541
}
542-
543-
const std::vector<Sensor::ButtonMap> Sensor::buttonMap(const QMap<QString, std::vector<Sensor::ButtonMap>> &buttonMapData, QMap<QString, QString> &buttonMapForModelId)
544-
{
545-
if (m_buttonMap.empty())
546-
{
547-
QString modelid;
548-
549-
if (isTuyaManufacturerName(item(RAttrManufacturerName)->toString()))
550-
{
551-
// for Tuya devices use manufacturer name as modelid
552-
modelid = item(RAttrManufacturerName)->toString();
553-
}
554-
else
555-
{
556-
modelid = item(RAttrModelId)->toString();
557-
}
558-
559-
for (auto i = buttonMapForModelId.constBegin(); i != buttonMapForModelId.constEnd(); ++i)
560-
{
561-
if (i.key().isEmpty())
562-
{
563-
continue;
564-
}
565-
566-
if (modelid == i.key())
567-
{
568-
m_buttonMap = buttonMapData.value(i.value());
569-
break;
570-
}
571-
}
572-
}
573-
574-
return m_buttonMap;
575-
}

‎sensor.h

+5-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017 dresden elektronik ingenieurtechnik gmbh.
2+
* Copyright (c) 2017-2021 dresden elektronik ingenieurtechnik gmbh.
33
* All rights reserved.
44
*
55
* The software in this package is published under the terms of the BSD
@@ -12,7 +12,7 @@
1212
#define SENSOR_H
1313

1414
#include <QString>
15-
#include <deconz.h>
15+
#include "button_maps.h"
1616
#include "resource.h"
1717
#include "rest_node_base.h"
1818

@@ -101,17 +101,6 @@ class Sensor : public Resource,
101101
StateDeleted
102102
};
103103

104-
struct ButtonMap
105-
{
106-
Sensor::SensorMode mode;
107-
quint8 endpoint;
108-
quint16 clusterId;
109-
quint8 zclCommandId;
110-
quint16 zclParam0;
111-
int button;
112-
QString name;
113-
};
114-
115104
Sensor();
116105

117106
DeletedState deletedState() const;
@@ -144,23 +133,23 @@ class Sensor : public Resource,
144133
void jsonToConfig(const QString &json);
145134
SensorFingerprint &fingerPrint();
146135
const SensorFingerprint &fingerPrint() const;
136+
ButtonMapRef buttonMapRef() const { return m_buttonMapRef; }
137+
void setButtonMapRef(ButtonMapRef ref) { m_buttonMapRef = ref; }
147138

148139
QString etag;
149-
const std::vector<Sensor::ButtonMap> buttonMap(const QMap<QString, std::vector<Sensor::ButtonMap>> &buttonMapData, QMap<QString, QString> &buttonMapForModelId);
150140
uint8_t previousDirection;
151141
quint16 previousCt;
152142
QDateTime durationDue;
153143
uint16_t previousSequenceNumber = 0xffff;
154144
uint8_t previousCommandId;
155-
156145

157146
private:
158147
DeletedState m_deletedstate;
159148
SensorFingerprint m_fingerPrint;
160149
SensorMode m_mode;
161150
uint8_t m_resetRetryCount;
162151
uint8_t m_zdpResetSeq;
163-
std::vector<Sensor::ButtonMap> m_buttonMap;
152+
ButtonMapRef m_buttonMapRef{};
164153
int m_rxCounter;
165154
};
166155

0 commit comments

Comments
 (0)
Please sign in to comment.