|
| 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 "deconz/dbg_trace.h" |
| 12 | +#include "deconz/timeref.h" |
| 13 | +#include "alarm_system_device_table.h" |
| 14 | +#include "database.h" |
| 15 | +#include "utils/utils.h" |
| 16 | + |
| 17 | +//! getIterator() is a helper to simplyfy code |
| 18 | +static std::vector<AS_DeviceEntry>::iterator getIterator(std::vector<AS_DeviceEntry> &table, quint64 extAddress) |
| 19 | +{ |
| 20 | + return std::find_if(table.begin(), table.end(), [&](const AS_DeviceEntry &entry) { return entry.extAddress == extAddress; }); |
| 21 | +} |
| 22 | + |
| 23 | +static std::vector<AS_DeviceEntry>::const_iterator getIterator(const std::vector<AS_DeviceEntry> &table, quint64 extAddress) |
| 24 | +{ |
| 25 | + return std::find_if(table.cbegin(), table.cend(), [&](const AS_DeviceEntry &entry) { return entry.extAddress == extAddress; }); |
| 26 | +} |
| 27 | + |
| 28 | +AS_DeviceTable::AS_DeviceTable() |
| 29 | +{ |
| 30 | + static_assert(sizeof(AS_DeviceEntry) == 64, "expected size of AS_DeviceEntry == 64 bytes"); |
| 31 | + static_assert (AS_MAX_UNIQUEID_LENGTH == DB_MAX_UNIQUEID_SIZE, "DB/AS max uniqueid size mismatch"); |
| 32 | +} |
| 33 | + |
| 34 | +const AS_DeviceEntry &AS_DeviceTable::get(const QString &uniqueId) const |
| 35 | +{ |
| 36 | + const quint64 extAddress = extAddressFromUniqueId(uniqueId); |
| 37 | + const auto i = getIterator(m_table, extAddress); |
| 38 | + |
| 39 | + if (i != m_table.cend()) |
| 40 | + { |
| 41 | + return *i; |
| 42 | + } |
| 43 | + |
| 44 | + return m_invalidEntry; |
| 45 | +} |
| 46 | + |
| 47 | +const AS_DeviceEntry &AS_DeviceTable::get(quint64 extAddress) const |
| 48 | +{ |
| 49 | + const auto i = getIterator(m_table, extAddress); |
| 50 | + |
| 51 | + if (i != m_table.cend()) |
| 52 | + { |
| 53 | + return *i; |
| 54 | + } |
| 55 | + |
| 56 | + return m_invalidEntry; |
| 57 | +} |
| 58 | + |
| 59 | +const AS_DeviceEntry &AS_DeviceTable::at(size_t index) const |
| 60 | +{ |
| 61 | + if (index < m_table.size()) |
| 62 | + { |
| 63 | + return m_table[index]; |
| 64 | + } |
| 65 | + |
| 66 | + return m_invalidEntry; |
| 67 | +} |
| 68 | + |
| 69 | +static void entryInitArmMask(AS_DeviceEntry &entry) |
| 70 | +{ |
| 71 | + memset(entry.armMask, 0, sizeof(entry.armMask)); |
| 72 | + char *p = entry.armMask; |
| 73 | + |
| 74 | + if (entry.flags & AS_ENTRY_FLAG_ARMED_AWAY) { *p++ = 'A'; } |
| 75 | + if (entry.flags & AS_ENTRY_FLAG_ARMED_STAY) { *p++ = 'S'; } |
| 76 | + if (entry.flags & AS_ENTRY_FLAG_ARMED_NIGHT) { *p++ = 'N'; } |
| 77 | +} |
| 78 | + |
| 79 | +static bool storeDeviceEntry(const AS_DeviceEntry &entry) |
| 80 | +{ |
| 81 | + DB_AlarmSystemDevice dbDevice; |
| 82 | + |
| 83 | + copyString(dbDevice.uniqueid, sizeof(dbDevice.uniqueid), entry.uniqueId); |
| 84 | + DBG_Assert(!isEmptyString(dbDevice.uniqueid)); |
| 85 | + if (isEmptyString(dbDevice.uniqueid)) |
| 86 | + { |
| 87 | + return false; |
| 88 | + } |
| 89 | + |
| 90 | + dbDevice.alarmSystemId = entry.alarmSystemId; |
| 91 | + dbDevice.flags = entry.flags; |
| 92 | + dbDevice.timestamp = deCONZ::systemTimeRef().ref; |
| 93 | + |
| 94 | + return DB_StoreAlarmSystemDevice(dbDevice); |
| 95 | +} |
| 96 | + |
| 97 | +bool AS_DeviceTable::put(const QString &uniqueId, quint32 flags, quint8 alarmSystemId) |
| 98 | +{ |
| 99 | + const quint64 extAddress = extAddressFromUniqueId(uniqueId); |
| 100 | + |
| 101 | + if (extAddress == 0) |
| 102 | + { |
| 103 | + return false; |
| 104 | + } |
| 105 | + |
| 106 | + // check existing |
| 107 | + auto i = getIterator(m_table, extAddress); |
| 108 | + |
| 109 | + if (i != m_table.end()) |
| 110 | + { |
| 111 | + if (i->flags != flags || i->alarmSystemId != alarmSystemId) |
| 112 | + { |
| 113 | + i->flags = flags; |
| 114 | + i->alarmSystemId = alarmSystemId; |
| 115 | + entryInitArmMask(*i); |
| 116 | + |
| 117 | + storeDeviceEntry(*i); |
| 118 | + } |
| 119 | + return true; |
| 120 | + } |
| 121 | + |
| 122 | + // not existing, create new |
| 123 | + m_table.push_back(AS_DeviceEntry()); |
| 124 | + AS_DeviceEntry &entry = m_table.back(); |
| 125 | + |
| 126 | + if (size_t(uniqueId.size()) >= sizeof(entry.uniqueId)) |
| 127 | + { |
| 128 | + m_table.pop_back(); |
| 129 | + return false; |
| 130 | + } |
| 131 | + |
| 132 | + entry.uniqueIdSize = quint8(uniqueId.size()); |
| 133 | + memcpy(entry.uniqueId, qPrintable(uniqueId), entry.uniqueIdSize); |
| 134 | + entry.uniqueId[entry.uniqueIdSize] = '\0'; |
| 135 | + entry.extAddress = extAddress; |
| 136 | + entry.alarmSystemId = alarmSystemId; |
| 137 | + entry.flags = flags; |
| 138 | + entryInitArmMask(entry); |
| 139 | + |
| 140 | + storeDeviceEntry(entry); |
| 141 | + return true; |
| 142 | +} |
| 143 | + |
| 144 | +bool AS_DeviceTable::erase(const QLatin1String &uniqueId) |
| 145 | +{ |
| 146 | + quint64 extAddress = extAddressFromUniqueId(uniqueId); |
| 147 | + auto i = getIterator(m_table, extAddress); |
| 148 | + |
| 149 | + if (i != m_table.end() && DB_DeleteAlarmSystemDevice(i->uniqueId)) |
| 150 | + { |
| 151 | + *i = m_table.back(); |
| 152 | + m_table.pop_back(); |
| 153 | + return true; |
| 154 | + } |
| 155 | + |
| 156 | + return false; |
| 157 | +} |
| 158 | + |
| 159 | +void AS_DeviceTable::reset(std::vector<AS_DeviceEntry> &&table) |
| 160 | +{ |
| 161 | + m_table = table; |
| 162 | +} |
| 163 | + |
| 164 | +void DB_LoadAlarmSystemDevices(AS_DeviceTable *devTable) |
| 165 | +{ |
| 166 | + const auto dbDevices = DB_LoadAlarmSystemDevices(); |
| 167 | + |
| 168 | + if (dbDevices.empty()) |
| 169 | + { |
| 170 | + return; |
| 171 | + } |
| 172 | + |
| 173 | + std::vector<AS_DeviceEntry> table; |
| 174 | + table.reserve(dbDevices.size()); |
| 175 | + |
| 176 | + for (const auto &dbDev : dbDevices) |
| 177 | + { |
| 178 | + if (strlen(dbDev.uniqueid) > AS_MAX_UNIQUEID_LENGTH) |
| 179 | + { |
| 180 | + continue; |
| 181 | + } |
| 182 | + |
| 183 | + table.push_back(AS_DeviceEntry()); |
| 184 | + AS_DeviceEntry &entry = table.back(); |
| 185 | + |
| 186 | + entry.extAddress = extAddressFromUniqueId(QLatin1String(dbDev.uniqueid)); |
| 187 | + entry.alarmSystemId = dbDev.alarmSystemId; |
| 188 | + entry.uniqueIdSize = quint8(strlen(dbDev.uniqueid)); |
| 189 | + memcpy(entry.uniqueId, dbDev.uniqueid, entry.uniqueIdSize); |
| 190 | + entry.uniqueId[entry.uniqueIdSize] = '\0'; |
| 191 | + entry.flags = dbDev.flags; |
| 192 | + entryInitArmMask(entry); |
| 193 | + } |
| 194 | + |
| 195 | + devTable->reset(std::move(table)); |
| 196 | +} |
0 commit comments