From 37c0c5d17bbdee8d2d9997e3ebaddc618af7e6d6 Mon Sep 17 00:00:00 2001 From: Carl Philipp Klemm Date: Sun, 29 Mar 2026 22:11:10 +0200 Subject: [PATCH] Add unit support to sensors --- UVOSicon.bmp | Bin 75386 -> 114798 bytes src/actors/polynomalactor.cpp | 2 +- src/actors/regulator.cpp | 2 +- src/actors/sensoractor.cpp | 2 +- src/items/item.cpp | 19 +++------ src/items/itemstore.cpp | 2 + src/mainobject.cpp | 1 + src/sensors/sensor.cpp | 4 +- src/sensors/sensor.h | 70 ++++++++++++++++++++++++---------- src/service/tcpserver.cpp | 5 ++- src/ui/mainwindow.ui | 2 +- src/ui/sensorlistwidget.cpp | 5 +++ 12 files changed, 73 insertions(+), 41 deletions(-) diff --git a/UVOSicon.bmp b/UVOSicon.bmp index 9565919d7ffd3218978265fa80ed090798f1c4e8..fd0d9a26a67d4f67f2fcec39e6e641809f3b4d85 100644 GIT binary patch delta 119 zcmex$hUHxYySE+r{}~uSXrjLN c<_&)yilPvuJdIULs~FWbFZuHpRYsNz09SA+ZvX%Q delta 65 zcmaFY!2atDi@uX@l^P=h2vh-a4iFy(;szjQWRL)oKme2%=4Jq6hKcsx&5Qh&FY;ql F0|1+n3y=T+ diff --git a/src/actors/polynomalactor.cpp b/src/actors/polynomalactor.cpp index 053dc30..f846c0e 100644 --- a/src/actors/polynomalactor.cpp +++ b/src/actors/polynomalactor.cpp @@ -65,7 +65,7 @@ void PolynomalActor::load(const QJsonObject& json, bool preserve) pow2_ = json["Pow2"].toDouble(0); pow1_ = json["Pow1"].toDouble(1); pow0_ = json["Pow0"].toDouble(0); - sensor_.type = json["SensorType"].toInt(0); + sensor_.type = static_cast(json["SensorType"].toInt(0)); sensor_.id = json["SensorId"].toInt(0); sensor_.field = json["SensorField"].toInt(0); sensor_.name = json["SensorName"].toString("Sensor"); diff --git a/src/actors/regulator.cpp b/src/actors/regulator.cpp index b3ea5f6..2287b7b 100644 --- a/src/actors/regulator.cpp +++ b/src/actors/regulator.cpp @@ -100,7 +100,7 @@ void Regulator::load(const QJsonObject& json, bool preserve) setPoint_ = json["SetPoint"].toDouble(22); safeValue_ = json["SafeValue"].toDouble(0); timeout_ = json["Timeout"].toDouble(1800); - sensor_.type = json["SensorType"].toInt(0); + sensor_.type = static_cast(json["SensorType"].toInt(0)); sensor_.id = json["SensorId"].toInt(0); sensor_.field = json["SensorField"].toInt(0); sensor_.name = json["SensorName"].toString("Sensor"); diff --git a/src/actors/sensoractor.cpp b/src/actors/sensoractor.cpp index 0be3ffa..758b761 100644 --- a/src/actors/sensoractor.cpp +++ b/src/actors/sensoractor.cpp @@ -65,7 +65,7 @@ void SensorActor::load(const QJsonObject& json, bool preserve) Actor::load(json, preserve); sloap_ = json["Sloap"].toInt(0); threshold_ = json["Threshold"].toDouble(0); - sensor_.type = json["SensorType"].toInt(0); + sensor_.type = static_cast(json["SensorType"].toInt(0)); sensor_.id = json["SensorId"].toInt(0); sensor_.field = json["SensorField"].toInt(0); sensor_.name = json["SensorName"].toString("Sensor"); diff --git a/src/items/item.cpp b/src/items/item.cpp index e297094..49f418a 100644 --- a/src/items/item.cpp +++ b/src/items/item.cpp @@ -126,6 +126,7 @@ void Item::store(QJsonObject &json) } } json["Actors"] = actorsArray; + json["ValueType"] = type_; } void Item::load(const QJsonObject &json, const bool preserve) @@ -249,30 +250,22 @@ void Item::mergeLoaded(Item& item) std::shared_ptr Item::loadItem(const QJsonObject& json) { std::shared_ptr newItem = nullptr; - if(json["Type"].toString("") == "Relay") - { + if(json["Type"].toString("Item") == "Item") + newItem = std::shared_ptr(new Item); + else if(json["Type"].toString("") == "Relay") newItem = std::shared_ptr(new Relay); - } else if(json["Type"].toString("") == "Message") - { newItem = std::shared_ptr(new MessageItem); - } else if(json["Type"].toString("") == "System") - { newItem = std::shared_ptr(new SystemItem); - } else if(json["Type"].toString("") == "Aux") - { newItem = std::shared_ptr(new AuxItem); - } else if(json["Type"].toString("") == "Power") - { newItem = std::shared_ptr(new PowerItem); - } else if(json["Type"].toString("") == "Rgb") - { newItem = std::shared_ptr(new RgbItem); - } + else + qWarning()<<"Unable to load unkown item type: "<load(json); diff --git a/src/items/itemstore.cpp b/src/items/itemstore.cpp index 1242afc..37c3889 100644 --- a/src/items/itemstore.cpp +++ b/src/items/itemstore.cpp @@ -9,8 +9,10 @@ ItemStore::ItemStore(QObject *parent): QObject(parent) void ItemStore::addItem(std::shared_ptr item, bool inform) { std::shared_ptr matched = nullptr; + qDebug()<<"referance"<id(); for(unsigned i = 0; i < items_.size(); i++ ) { + qDebug()<<"avail"<id(); if(*items_[i] == *item) { matched = items_[i]; diff --git a/src/mainobject.cpp b/src/mainobject.cpp index c81ea99..eed22d7 100644 --- a/src/mainobject.cpp +++ b/src/mainobject.cpp @@ -89,6 +89,7 @@ PrimaryMainObject::PrimaryMainObject(QIODevice* microDevice, const QString& sett globalItems.registerItemSource(&fixedItems); globalItems.registerItemSource(tcpServer); + globalItems.registerItemSource(webServer); globalItems.registerItemSource(µ); globalItems.registerItemSource(&itemLoader); diff --git a/src/sensors/sensor.cpp b/src/sensors/sensor.cpp index e2a262f..576d1db 100644 --- a/src/sensors/sensor.cpp +++ b/src/sensors/sensor.cpp @@ -6,8 +6,8 @@ SensorStore globalSensors; SensorStore::SensorStore(QObject *parent): QObject(parent) { - sensors_.push_back(Sensor(0,1,0,"Front door")); - sensors_.push_back(Sensor(0,0,0,"Bedroom door")); + sensors_.push_back(Sensor(Sensor::TYPE_DOOR,1,0,"Front door")); + sensors_.push_back(Sensor(Sensor::TYPE_DOOR,0,0,"Bedroom door")); } void SensorStore::sensorGotState(const Sensor& sensor) diff --git a/src/sensors/sensor.h b/src/sensors/sensor.h index bc5e748..10a7316 100644 --- a/src/sensors/sensor.h +++ b/src/sensors/sensor.h @@ -10,32 +10,34 @@ class Sensor { public: - static constexpr uint8_t TYPE_DOOR = 0; - static constexpr uint8_t TYPE_TEMPERATURE = 1; - static constexpr uint8_t TYPE_HUMIDITY = 2; - static constexpr uint8_t TYPE_PRESSURE = 3; - static constexpr uint8_t TYPE_BRIGHTNESS = 4; - static constexpr uint8_t TYPE_BUTTON = 5; - static constexpr uint8_t TYPE_ADC = 6; - static constexpr uint8_t TYPE_CO2 = 7; - static constexpr uint8_t TYPE_FORMALDEHYD= 8; - static constexpr uint8_t TYPE_PM25 = 9; - static constexpr uint8_t TYPE_TOTAL_VOC = 10; - static constexpr uint8_t TYPE_LOWBATTERY = 128; - static constexpr uint8_t TYPE_SHUTDOWN_IMMINENT = 251; - static constexpr uint8_t TYPE_OCUPANCY = 252; - static constexpr uint8_t TYPE_SUN_ALTITUDE = 253; - static constexpr uint8_t TYPE_AUDIO_OUTPUT = 254; - static constexpr uint8_t TYPE_DUMMY = 255; + typedef enum { + TYPE_DOOR = 0, + TYPE_TEMPERATURE, + TYPE_HUMIDITY, + TYPE_PRESSURE, + TYPE_BRIGHTNESS, + TYPE_BUTTON, + TYPE_ADC, + TYPE_CO2, + TYPE_FORMALDEHYD, + TYPE_PM25, + TYPE_TOTAL_VOC, + TYPE_LOWBATTERY = 128, + TYPE_SHUTDOWN_IMMINENT = 251, + TYPE_OCUPANCY, + TYPE_SUN_ALTITUDE, + TYPE_AUDIO_OUTPUT, + TYPE_DUMMY, + } sensor_type_t; - uint8_t type; + sensor_type_t type; uint64_t id; float field; QString name; QDateTime lastSeen; bool hidden; - Sensor(uint64_t typeIn, uint8_t idIn, float fieldIn = 0, QString nameIn = "", bool hiddenIn = false): type(typeIn), + Sensor(sensor_type_t typeIn, uint64_t idIn, float fieldIn = 0, QString nameIn = "", bool hiddenIn = false): type(typeIn), id(idIn), field(fieldIn), name(nameIn), hidden(hiddenIn) { lastSeen = QDateTime::currentDateTime(); @@ -48,7 +50,7 @@ public: } Sensor(const QJsonObject& json) { - type = json["SensorType"].toInt(0); + type = static_cast(json["SensorType"].toInt(0)); id = json["Id"].toInt(0); field = json["Field"].toDouble(0); name = json["Name"].toString("Sensor"); @@ -72,7 +74,7 @@ public: QStringList bufferList = str.split(' '); if(bufferList.size() >= 7) { - Sensor sensor(bufferList[2].toUInt(), bufferList[4].toUInt(), bufferList[6].toUInt()); + Sensor sensor(static_cast(bufferList[2].toUInt()), bufferList[4].toUInt(), bufferList[6].toUInt()); if(sensor.type == Sensor::TYPE_HUMIDITY || sensor.type == Sensor::TYPE_TEMPERATURE) sensor.field = sensor.field/10; @@ -100,6 +102,7 @@ public: json["Name"] = name; json["LastSeen"] = lastSeen.toString(); json["Hidden"] = hidden; + json["Unit"] = getUnit(); } inline void generateName() { @@ -119,6 +122,31 @@ public: name = "Shutdown Imminent"; else name = "Sensor Type " + QString::number(type) + " Id " + QString::number(id); } + QString getUnit() + { + switch(type) + { + case TYPE_TEMPERATURE: + return "°C"; + case TYPE_HUMIDITY: + return "%"; + case TYPE_PRESSURE: + return "hPa"; + case TYPE_BRIGHTNESS: + return "lx"; + case TYPE_CO2: + return "ppm"; + case TYPE_FORMALDEHYD: + case TYPE_PM25: + return "µg/m³"; + case TYPE_TOTAL_VOC: + return "ppb"; + case TYPE_SUN_ALTITUDE: + return "°"; + default: + return ""; + } + } }; class SensorStore: public QObject diff --git a/src/service/tcpserver.cpp b/src/service/tcpserver.cpp index b5b780f..0cdeed8 100644 --- a/src/service/tcpserver.cpp +++ b/src/service/tcpserver.cpp @@ -38,9 +38,11 @@ void TcpServer::processIncomeingJson(const QByteArray& jsonbytes) { QJsonObject jsonobject = itemjson.toObject(); std::shared_ptr item = Item::loadItem(jsonobject); - item->setLoaded(FullList); if(item) + { + item->setLoaded(FullList); items.push_back(item); + } } if(FullList && !items.empty()) { @@ -60,6 +62,7 @@ void TcpServer::processIncomeingJson(const QByteArray& jsonbytes) bool TcpServer::launch(const QHostAddress &address, quint16 port) { + qInfo()<<"Tcp server launched on"< - 300 + 400 0 diff --git a/src/ui/sensorlistwidget.cpp b/src/ui/sensorlistwidget.cpp index 827ab2e..b732362 100644 --- a/src/ui/sensorlistwidget.cpp +++ b/src/ui/sensorlistwidget.cpp @@ -55,6 +55,11 @@ void SensorListWidget::sensorsChanged(std::vector sensors) itemString.append("\"Playing\""); else itemString.append("\"Silent\""); } + else if(!sensors[i].getUnit().isEmpty()) + { + itemString.append(" "); + itemString.append(sensors[i].getUnit()); + } setItem(static_cast(row), 0, new SensorListItem(sensors[i].name + (sensors[i].hidden ? " (H)" : ""), sensors[i])); setItem(static_cast(row), 1, new QTableWidgetItem(itemString));