forked from dresden-elektronik/deconz-rest-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alarm_system_device_table.cpp
196 lines (158 loc) · 5.11 KB
/
alarm_system_device_table.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
/*
* 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 "deconz/dbg_trace.h"
#include "deconz/timeref.h"
#include "alarm_system_device_table.h"
#include "database.h"
#include "utils/utils.h"
//! getIterator() is a helper to simplyfy code
static std::vector<AS_DeviceEntry>::iterator getIterator(std::vector<AS_DeviceEntry> &table, quint64 extAddress)
{
return std::find_if(table.begin(), table.end(), [&](const AS_DeviceEntry &entry) { return entry.extAddress == extAddress; });
}
static std::vector<AS_DeviceEntry>::const_iterator getIterator(const std::vector<AS_DeviceEntry> &table, quint64 extAddress)
{
return std::find_if(table.cbegin(), table.cend(), [&](const AS_DeviceEntry &entry) { return entry.extAddress == extAddress; });
}
AS_DeviceTable::AS_DeviceTable()
{
static_assert(sizeof(AS_DeviceEntry) == 64, "expected size of AS_DeviceEntry == 64 bytes");
static_assert (AS_MAX_UNIQUEID_LENGTH == DB_MAX_UNIQUEID_SIZE, "DB/AS max uniqueid size mismatch");
}
const AS_DeviceEntry &AS_DeviceTable::get(const QString &uniqueId) const
{
const quint64 extAddress = extAddressFromUniqueId(uniqueId);
const auto i = getIterator(m_table, extAddress);
if (i != m_table.cend())
{
return *i;
}
return m_invalidEntry;
}
const AS_DeviceEntry &AS_DeviceTable::get(quint64 extAddress) const
{
const auto i = getIterator(m_table, extAddress);
if (i != m_table.cend())
{
return *i;
}
return m_invalidEntry;
}
const AS_DeviceEntry &AS_DeviceTable::at(size_t index) const
{
if (index < m_table.size())
{
return m_table[index];
}
return m_invalidEntry;
}
static void entryInitArmMask(AS_DeviceEntry &entry)
{
memset(entry.armMask, 0, sizeof(entry.armMask));
char *p = entry.armMask;
if (entry.flags & AS_ENTRY_FLAG_ARMED_AWAY) { *p++ = 'A'; }
if (entry.flags & AS_ENTRY_FLAG_ARMED_STAY) { *p++ = 'S'; }
if (entry.flags & AS_ENTRY_FLAG_ARMED_NIGHT) { *p++ = 'N'; }
}
static bool storeDeviceEntry(const AS_DeviceEntry &entry)
{
DB_AlarmSystemDevice dbDevice;
copyString(dbDevice.uniqueid, sizeof(dbDevice.uniqueid), entry.uniqueId);
DBG_Assert(!isEmptyString(dbDevice.uniqueid));
if (isEmptyString(dbDevice.uniqueid))
{
return false;
}
dbDevice.alarmSystemId = entry.alarmSystemId;
dbDevice.flags = entry.flags;
dbDevice.timestamp = deCONZ::systemTimeRef().ref;
return DB_StoreAlarmSystemDevice(dbDevice);
}
bool AS_DeviceTable::put(const QString &uniqueId, quint32 flags, quint8 alarmSystemId)
{
const quint64 extAddress = extAddressFromUniqueId(uniqueId);
if (extAddress == 0)
{
return false;
}
// check existing
auto i = getIterator(m_table, extAddress);
if (i != m_table.end())
{
if (i->flags != flags || i->alarmSystemId != alarmSystemId)
{
i->flags = flags;
i->alarmSystemId = alarmSystemId;
entryInitArmMask(*i);
storeDeviceEntry(*i);
}
return true;
}
// not existing, create new
m_table.push_back(AS_DeviceEntry());
AS_DeviceEntry &entry = m_table.back();
if (size_t(uniqueId.size()) >= sizeof(entry.uniqueId))
{
m_table.pop_back();
return false;
}
entry.uniqueIdSize = quint8(uniqueId.size());
memcpy(entry.uniqueId, qPrintable(uniqueId), entry.uniqueIdSize);
entry.uniqueId[entry.uniqueIdSize] = '\0';
entry.extAddress = extAddress;
entry.alarmSystemId = alarmSystemId;
entry.flags = flags;
entryInitArmMask(entry);
storeDeviceEntry(entry);
return true;
}
bool AS_DeviceTable::erase(const QLatin1String &uniqueId)
{
quint64 extAddress = extAddressFromUniqueId(uniqueId);
auto i = getIterator(m_table, extAddress);
if (i != m_table.end() && DB_DeleteAlarmSystemDevice(i->uniqueId))
{
*i = m_table.back();
m_table.pop_back();
return true;
}
return false;
}
void AS_DeviceTable::reset(std::vector<AS_DeviceEntry> &&table)
{
m_table = table;
}
void DB_LoadAlarmSystemDevices(AS_DeviceTable *devTable)
{
const auto dbDevices = DB_LoadAlarmSystemDevices();
if (dbDevices.empty())
{
return;
}
std::vector<AS_DeviceEntry> table;
table.reserve(dbDevices.size());
for (const auto &dbDev : dbDevices)
{
if (strlen(dbDev.uniqueid) > AS_MAX_UNIQUEID_LENGTH)
{
continue;
}
table.push_back(AS_DeviceEntry());
AS_DeviceEntry &entry = table.back();
entry.extAddress = extAddressFromUniqueId(QLatin1String(dbDev.uniqueid));
entry.alarmSystemId = dbDev.alarmSystemId;
entry.uniqueIdSize = quint8(strlen(dbDev.uniqueid));
memcpy(entry.uniqueId, dbDev.uniqueid, entry.uniqueIdSize);
entry.uniqueId[entry.uniqueIdSize] = '\0';
entry.flags = dbDev.flags;
entryInitArmMask(entry);
}
devTable->reset(std::move(table));
}