From 0e09b6f46ca097318c2dd7143f37bfb09489cfa8 Mon Sep 17 00:00:00 2001 From: Carl Philipp Klemm Date: Wed, 1 Apr 2026 18:06:11 +0200 Subject: [PATCH 01/21] Add group field to items --- src/items/item.cpp | 19 ++++++++++++++++--- src/items/item.h | 6 +++++- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/items/item.cpp b/src/items/item.cpp index 7f350a2..98e2a1b 100644 --- a/src/items/item.cpp +++ b/src/items/item.cpp @@ -13,8 +13,8 @@ #include -ItemData::ItemData(uint32_t itemIdIn, QString name, uint8_t value, bool loaded, bool hidden, item_value_type_t type): - name_(name), value_(value), itemId_(itemIdIn), loaded_(loaded), hidden_(hidden), type_(type) +ItemData::ItemData(uint32_t itemIdIn, QString name, uint8_t value, bool loaded, bool hidden, item_value_type_t type, QString groupName): + name_(name), value_(value), itemId_(itemIdIn), loaded_(loaded), hidden_(hidden), type_(type), groupName_(groupName) { } @@ -49,6 +49,7 @@ void ItemData::store(QJsonObject &json) json["Name"] = name_; json["ItemId"] = static_cast(itemId_); json["Value"] = static_cast(value_); + json["GroupName"] = groupName_; } void ItemData::load(const QJsonObject &json, const bool preserve) @@ -58,6 +59,7 @@ void ItemData::load(const QJsonObject &json, const bool preserve) name_ = json["Name"].toString(name_); itemId_ = static_cast(json["ItemId"].toDouble(0)); value_ = json["Value"].toInt(); + groupName_ = json["GroupName"].toString("All"); } } @@ -99,10 +101,20 @@ item_value_type_t ItemData::getValueType() return type_; } +QString ItemData::getGroupName() const +{ + return groupName_; +} + +void ItemData::setGroupName(QString groupName) +{ + groupName_ = groupName; +} + //item Item::Item(uint32_t itemIdIn, QString name, uint8_t value, QObject *parent): QObject(parent), ItemData (itemIdIn, name, - value) + value, false, false, ITEM_VALUE_BOOL, "All") { } @@ -156,6 +168,7 @@ Item& Item::operator=(const ItemData& other) value_ = other.getValue(); itemId_ = other.id(); hidden_ = other.isHidden(); + groupName_ = other.getGroupName(); return *this; } diff --git a/src/items/item.h b/src/items/item.h index d461006..1156933 100644 --- a/src/items/item.h +++ b/src/items/item.h @@ -32,6 +32,7 @@ protected: bool loaded_; bool hidden_; item_value_type_t type_; + QString groupName_; public: ItemData(uint32_t itemIdIn = QRandomGenerator::global()->generate(), @@ -39,7 +40,8 @@ public: uint8_t value = 0, bool loaded = false, bool hidden = false, - item_value_type_t type = ITEM_VALUE_BOOL); + item_value_type_t type = ITEM_VALUE_BOOL, + QString groupName = "All"); inline bool operator==(const ItemData& in) const { @@ -61,6 +63,8 @@ public: bool isHidden() const; void setHidden(bool hidden); item_value_type_t getValueType(); + QString getGroupName() const; + void setGroupName(QString groupName); virtual QString getName() const; virtual void store(QJsonObject& json); virtual void load(const QJsonObject& json, const bool preserve = false); From 4a956d34849a4863f3fc0b6b449b6af37776d9d0 Mon Sep 17 00:00:00 2001 From: Carl Philipp Klemm Date: Wed, 1 Apr 2026 21:00:11 +0200 Subject: [PATCH 02/21] Add remote website --- remote.html | 525 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 525 insertions(+) create mode 100644 remote.html diff --git a/remote.html b/remote.html new file mode 100644 index 0000000..8c4c77f --- /dev/null +++ b/remote.html @@ -0,0 +1,525 @@ + + + + + + SHInterface Control + + + + +
+
Disconnected
+ +
+
+
Items
+
Sensors
+
+ +
+
+
+ +
+
+
+
+ + +
+ + + + From f3fe35e778de442a5936ca822007ace47439d82d Mon Sep 17 00:00:00 2001 From: Carl Philipp Klemm Date: Wed, 1 Apr 2026 21:02:55 +0200 Subject: [PATCH 03/21] Only obay value on item update --- remote.html | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/remote.html b/remote.html index 8c4c77f..4765978 100644 --- a/remote.html +++ b/remote.html @@ -281,11 +281,15 @@ if (fullList) { items = {}; // Clear existing items + json.Data.forEach(item => { + items[item.ItemId] = item; + }); + } + else { + json.Data.forEach(item => { + items[item.ItemId].Value = item.Value; + }); } - - json.Data.forEach(item => { - items[item.ItemId] = item; - }); renderItems(); } else if (json.MessageType === 'SensorUpdate') { From 074c2bd448e85e40b3aafa30e274b29bce4334a4 Mon Sep 17 00:00:00 2001 From: Carl Philipp Klemm Date: Sun, 5 Apr 2026 00:24:02 +0200 Subject: [PATCH 04/21] Inital support for ENUM type items --- src/items/item.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/items/item.h b/src/items/item.h index 1156933..8889716 100644 --- a/src/items/item.h +++ b/src/items/item.h @@ -11,7 +11,8 @@ class Actor; typedef enum { ITEM_VALUE_BOOL = 0, ITEM_VALUE_UINT, - ITEM_VALUE_NO_VALUE + ITEM_VALUE_NO_VALUE, + ITEM_VALUE_ENUM, } item_value_type_t; typedef enum { @@ -33,6 +34,7 @@ protected: bool hidden_; item_value_type_t type_; QString groupName_; + std::vector valueNames_; public: ItemData(uint32_t itemIdIn = QRandomGenerator::global()->generate(), From a17cd23a4eb5563db06a9fdcf96282f6f58c5b9c Mon Sep 17 00:00:00 2001 From: Carl Philipp Klemm Date: Wed, 1 Apr 2026 19:40:47 +0200 Subject: [PATCH 05/21] Support item tabs based on item group string --- CMakeLists.txt | 2 +- src/actors/actor.cpp | 10 +- src/actors/alarmtime.h | 2 +- src/actors/timeractor.cpp | 8 +- src/actors/timeractor.h | 8 +- src/items/fixeditemsource.cpp | 16 +++- src/items/item.cpp | 96 ++++++++++++++----- src/items/item.h | 70 +++++++++++--- src/items/itemloadersource.cpp | 10 +- src/items/itemsource.h | 2 +- src/items/itemstore.cpp | 36 ++++--- src/items/itemstore.h | 4 +- src/main.cpp | 11 +-- src/microcontroller.cpp | 19 +++- src/service/server.cpp | 13 ++- src/service/service.cpp | 2 - src/service/tcpclient.cpp | 28 +++++- src/ui/actorsettingsdialog.cpp | 5 +- src/ui/itemscrollbox.cpp | 169 ++++++++++++++++++++++++++++++--- src/ui/itemscrollbox.h | 23 ++++- src/ui/itemsettingsdialog.cpp | 66 ++++++++++--- src/ui/itemsettingsdialog.h | 4 +- src/ui/itemsettingsdialog.ui | 14 +++ src/ui/itemwidget.cpp | 24 +++-- src/ui/itemwidget.h | 4 +- src/ui/mainwindow.cpp | 11 ++- src/ui/mainwindow.h | 2 +- src/ui/mainwindow.ui | 2 +- src/ui/relayscrollbox.ui | 47 +-------- 29 files changed, 527 insertions(+), 181 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ea99bbe..728bae0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 4.0) project(SHinterface VERSION 1.0 LANGUAGES CXX) # Set C++ standard -set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) # Enable all warnings diff --git a/src/actors/actor.cpp b/src/actors/actor.cpp index 51c3c84..15a9919 100644 --- a/src/actors/actor.cpp +++ b/src/actors/actor.cpp @@ -24,6 +24,7 @@ void Actor::performValueAction(uint8_t value) ItemUpdateRequest request; request.type = ITEM_UPDATE_ACTOR; request.payload = ItemData(QRandomGenerator::global()->generate(), "Item", value); + request.changes.value = true; sigItemUpdate(request); } } @@ -47,9 +48,12 @@ void Actor::makeInactive() QString Actor::actionName() { QString string; - if(triggerValue == 0 ) string = "off"; - else if(triggerValue == 1 ) string = "on"; - else string = "value to " + QString::number(triggerValue); + if(triggerValue == 0 ) + string = "off"; + else if(triggerValue == 1 ) + string = "on"; + else + string = "value to " + QString::number(triggerValue); return string; } diff --git a/src/actors/alarmtime.h b/src/actors/alarmtime.h index 0d65d84..99c0b7e 100644 --- a/src/actors/alarmtime.h +++ b/src/actors/alarmtime.h @@ -39,13 +39,13 @@ public: virtual void load(const QJsonObject& json, const bool preserve = false); uint8_t getRepeat(); + virtual QString getName() const; public slots: void run(); virtual void makeActive(); virtual void makeInactive(); - virtual QString getName() const; void doTick(); void changeTime(const QDateTime& time); void setRepeat(const uint8_t repeat); diff --git a/src/actors/timeractor.cpp b/src/actors/timeractor.cpp index a0a6b1e..d916045 100644 --- a/src/actors/timeractor.cpp +++ b/src/actors/timeractor.cpp @@ -7,11 +7,13 @@ TimerActor::TimerActor(const int timeoutSec, QObject *parent): Actor(parent), ti timer.setSingleShot(true); } -void TimerActor::onValueChanged(uint8_t state) +void TimerActor::onItemUpdated(ItemUpdateRequest update) { - if((state && !triggerValue) || (!state && triggerValue)) + if(update.changes.value && ((update.payload.getValue() && !triggerValue) || (!update.payload.getValue() && triggerValue))) { - if(timer.isActive()) timer.stop(); + qDebug()<<"Timer started"; + if(timer.isActive()) + timer.stop(); timer.setInterval(timeoutMsec_); timer.start(); } diff --git a/src/actors/timeractor.h b/src/actors/timeractor.h index 36290d9..f30ff7c 100644 --- a/src/actors/timeractor.h +++ b/src/actors/timeractor.h @@ -16,15 +16,15 @@ private slots: public slots: - virtual void onValueChanged(uint8_t state); + virtual void onItemUpdated(ItemUpdateRequest update) override; void setTimeout(const int timeoutSec); public: explicit TimerActor(const int timeoutSec = 60, QObject *parent = nullptr); - virtual QString getName() const; + virtual QString getName() const override; int getTimeout(); - virtual void store(QJsonObject& json); - virtual void load(const QJsonObject& json, bool preserve); + virtual void store(QJsonObject& json) override; + virtual void load(const QJsonObject& json, bool preserve) override; }; diff --git a/src/items/fixeditemsource.cpp b/src/items/fixeditemsource.cpp index b4459ff..b166831 100644 --- a/src/items/fixeditemsource.cpp +++ b/src/items/fixeditemsource.cpp @@ -10,5 +10,19 @@ FixedItemSource::FixedItemSource(Microcontroller* micro, QObject *parent): void FixedItemSource::refresh() { - gotItems({powerItem, rgbItem, auxItem}, ITEM_UPDATE_BACKEND); + std::vector requests; + + ItemAddRequest request; + request.type = ITEM_UPDATE_BACKEND; + + request.payload = powerItem; + requests.push_back(request); + + request.payload = rgbItem; + requests.push_back(request); + + request.payload = auxItem; + requests.push_back(request); + + gotItems(requests); } diff --git a/src/items/item.cpp b/src/items/item.cpp index 98e2a1b..6f391c5 100644 --- a/src/items/item.cpp +++ b/src/items/item.cpp @@ -46,21 +46,49 @@ uint32_t ItemData::id() const void ItemData::store(QJsonObject &json) { - json["Name"] = name_; + storeWithChanges(json, ItemFieldChanges(true)); +} + +void ItemData::storeWithChanges(QJsonObject& json, const ItemFieldChanges& changes) +{ json["ItemId"] = static_cast(itemId_); - json["Value"] = static_cast(value_); - json["GroupName"] = groupName_; + json["ValueType"] = type_; + if(changes.name) + json["Name"] = name_; + if(changes.value) + json["Value"] = static_cast(value_); + if(changes.groupName) + json["GroupName"] = groupName_; } void ItemData::load(const QJsonObject &json, const bool preserve) { + loadWithChanges(json, preserve); +} + +ItemFieldChanges ItemData::loadWithChanges(const QJsonObject& json, const bool preserve) +{ + ItemFieldChanges changes; if(!preserve) { - name_ = json["Name"].toString(name_); + if(json.contains("Name")) + { + name_ = json["Name"].toString(); + changes.name = true; + } + if(json.contains("Value")) + { + value_ = json["Value"].toInt(); + changes.value = true; + } + if(json.contains("GroupName")) + { + groupName_ = json["GroupName"].toString(); + changes.groupName = true; + } itemId_ = static_cast(json["ItemId"].toDouble(0)); - value_ = json["Value"].toInt(); - groupName_ = json["GroupName"].toString("All"); } + return changes; } bool ItemData::getLoaded() const @@ -73,15 +101,23 @@ void ItemData::setLoaded(bool loaded) loaded_ = loaded; } -bool ItemData::hasChanged(const ItemData& other) +bool ItemData::hasChanged(const ItemData& other) const { - if(other != *this) - return false; - if(other.getName() != getName()) + ItemFieldChanges changes(true); + return hasChanged(other, changes); +} + +bool ItemData::hasChanged(const ItemData& other, const ItemFieldChanges& changes) const +{ + if(changes.name && other.getName() != getName()) return true; - if(other.getValue() != getValue()) + if(changes.value && other.getValue() != getValue()) return true; - if(other.getLoaded() != getLoaded()) + if(changes.hidden && other.isHidden() != isHidden()) + return true; + if(changes.groupName && other.getGroupName() != getGroupName()) + return true; + if(changes.actors) return true; return false; } @@ -143,7 +179,6 @@ void Item::store(QJsonObject &json) } } json["Actors"] = actorsArray; - json["ValueType"] = type_; } void Item::load(const QJsonObject &json, const bool preserve) @@ -175,7 +210,14 @@ Item& Item::operator=(const ItemData& other) void Item::requestUpdate(ItemUpdateRequest update) { assert(update.type != ITEM_UPDATE_INVALID); - if(update.type != ITEM_UPDATE_LOADED && value_ == update.payload.getValue()) + assert(!update.changes.isNone()); + + if(update.type == ITEM_UPDATE_LOADED) + { + qDebug()<<__func__<& actor : update.newActors) addActor(actor); @@ -302,14 +348,12 @@ std::shared_ptr Item::loadItem(const QJsonObject& json) return newItem; } -ItemUpdateRequest Item::createValueUpdateRequest(uint8_t value, - item_update_type_t type, +ItemUpdateRequest Item::createValueUpdateRequest(item_update_type_t type, bool withActors) { ItemUpdateRequest update; update.type = type; update.payload = *this; - update.payload.setValueData(value); if(withActors) update.newActors = actors_; return update; diff --git a/src/items/item.h b/src/items/item.h index 1156933..8cf9c4d 100644 --- a/src/items/item.h +++ b/src/items/item.h @@ -23,6 +23,9 @@ typedef enum { ITEM_UPDATE_INVALID } item_update_type_t; +struct ItemFieldChanges; +struct ItemUpdateRequest; + class ItemData { protected: @@ -41,7 +44,7 @@ public: bool loaded = false, bool hidden = false, item_value_type_t type = ITEM_VALUE_BOOL, - QString groupName = "All"); + QString groupName = ""); inline bool operator==(const ItemData& in) const { @@ -54,7 +57,8 @@ public: uint32_t id() const; - bool hasChanged(const ItemData& other); + bool hasChanged(const ItemData& other) const; + bool hasChanged(const ItemData& other, const ItemFieldChanges& changes) const; void setName(QString name); uint8_t getValue() const; void setValueData(uint8_t value); @@ -65,19 +69,13 @@ public: item_value_type_t getValueType(); QString getGroupName() const; void setGroupName(QString groupName); + void storeWithChanges(QJsonObject& json, const ItemFieldChanges& changes); + ItemFieldChanges loadWithChanges(const QJsonObject& json, const bool preserve = false); virtual QString getName() const; virtual void store(QJsonObject& json); virtual void load(const QJsonObject& json, const bool preserve = false); }; -struct ItemUpdateRequest -{ - item_update_type_t type = ITEM_UPDATE_INVALID; - ItemData payload; - std::vector > newActors; -}; - - class Item: public QObject, public ItemData { Q_OBJECT @@ -108,8 +106,7 @@ public: void setActorsActive(bool in); void setOverride(const bool in); bool getOverride(); - ItemUpdateRequest createValueUpdateRequest(uint8_t value, - item_update_type_t type, + ItemUpdateRequest createValueUpdateRequest(item_update_type_t type, bool withActors = false); virtual void store(QJsonObject& json); @@ -122,3 +119,52 @@ protected: }; + +struct ItemFieldChanges +{ + bool name :1; + bool value :1; + bool hidden :1; + bool type :1; + bool groupName :1; + bool actors :1; + ItemFieldChanges(bool defaultVal = false) + { + name = defaultVal; + value = defaultVal; + hidden = defaultVal; + type = defaultVal; + groupName = defaultVal; + actors = false; + } + inline bool isNone() const + { + return !name && !value && !hidden && !type && !groupName && !actors; + } +}; + +struct ItemUpdateRequest +{ + item_update_type_t type = ITEM_UPDATE_INVALID; + ItemData payload; + ItemFieldChanges changes; + std::vector > newActors; +}; + +struct ItemAddRequest +{ + item_update_type_t type = ITEM_UPDATE_INVALID; + std::shared_ptr payload; + ItemFieldChanges changes; + inline ItemUpdateRequest updateRequest() const + { + ItemUpdateRequest update; + update.payload = *payload; + update.type = type; + update.changes = changes; + if(changes.actors) + update.newActors = payload->getActors(); + return update; + } +}; + diff --git a/src/items/itemloadersource.cpp b/src/items/itemloadersource.cpp index a24e5a5..db5478a 100644 --- a/src/items/itemloadersource.cpp +++ b/src/items/itemloadersource.cpp @@ -10,7 +10,7 @@ ItemLoaderSource::ItemLoaderSource(const QJsonObject& json, QObject *parent): void ItemLoaderSource::refresh() { - std::vector> items; + std::vector itemAddRequests; const QJsonArray itemsArray(json["Items"].toArray()); for(int i = 0; i < itemsArray.size(); ++i) { @@ -21,11 +21,15 @@ void ItemLoaderSource::refresh() std::shared_ptr newItem = Item::loadItem(itemObject); if(newItem) { - items.push_back(newItem); qDebug()<<"Loaded item"<getName(); + ItemAddRequest request; + request.type = ITEM_UPDATE_LOADED; + request.payload = newItem; + request.changes = ItemFieldChanges(true); + itemAddRequests.push_back(request); } } - gotItems(items, ITEM_UPDATE_LOADED); + gotItems(itemAddRequests); } void ItemLoaderSource::updateJson(const QJsonObject& json) diff --git a/src/items/itemsource.h b/src/items/itemsource.h index 5c2bba2..1753f75 100644 --- a/src/items/itemsource.h +++ b/src/items/itemsource.h @@ -17,7 +17,7 @@ public slots: virtual void refresh() = 0; signals: - void gotItems(std::vector> items, item_update_type_t updateType); + void gotItems(std::vector items); void requestReplaceItems(std::vector> items); void updateItems(std::vector updates); }; diff --git a/src/items/itemstore.cpp b/src/items/itemstore.cpp index 232d319..1b52bf9 100644 --- a/src/items/itemstore.cpp +++ b/src/items/itemstore.cpp @@ -6,39 +6,38 @@ ItemStore::ItemStore(QObject *parent): QObject(parent) { } -void ItemStore::addItem(const std::shared_ptr& item, item_update_type_t updateType) +void ItemStore::addItem(const ItemAddRequest& item) { + qDebug()<<"Item add request for"<getName()<id(); std::shared_ptr matched = nullptr; for(unsigned i = 0; i < items_.size(); i++ ) { - if(*items_[i] == *item) + if(*items_[i] == *item.payload) { matched = items_[i]; + assert(matched->id() == items_[i]->id()); break; } } if(!matched) { - items_.push_back(std::shared_ptr(item)); - connect(item.get(), &Item::updated, this, &ItemStore::itemUpdateSlot); - qDebug()<<"Item"<getName()<<"added"<<(item->getLoaded() ? "from loaded" : ""); + items_.push_back(item.payload); + connect(item.payload.get(), &Item::updated, this, &ItemStore::itemUpdateSlot); + qDebug()<<"Item"<getName()<<"added"<<(item.payload->getLoaded() ? "from loaded" : ""); itemAdded(std::weak_ptr(items_.back())); } - else + else if(!item.changes.isNone()) { - ItemUpdateRequest request = item->createValueUpdateRequest(item->getValue(), - updateType, - updateType == ITEM_UPDATE_LOADED); - request.newActors = item->getActors(); + qDebug()<<"Item"<getName()<<"was matched with"<getName()<<"and has changes"; + ItemUpdateRequest request = item.updateRequest(); updateItem(request); } } -void ItemStore::addItems(const std::vector>& itemIn, - item_update_type_t updateType) +void ItemStore::addItems(const std::vector& itemIn) { for(unsigned j = 0; j < itemIn.size(); j++) - addItem(itemIn[j], updateType); + addItem(itemIn[j]); } void ItemStore::removeItem(const ItemData& item) @@ -57,8 +56,15 @@ void ItemStore::removeItem(const ItemData& item) void ItemStore::replaceItems(const std::vector>& items) { - qDebug()<<__func__; - addItems(items, ITEM_UPDATE_LOADED); + for(const std::shared_ptr& item : items) + { + ItemAddRequest request; + request.changes = ItemFieldChanges(true); + request.changes.actors = true; + request.type = ITEM_UPDATE_LOADED; + request.payload = item; + addItem(request); + } std::vector deletedItems; for(std::shared_ptr item : items_) { diff --git a/src/items/itemstore.h b/src/items/itemstore.h index b4bdeaa..93d1b9f 100644 --- a/src/items/itemstore.h +++ b/src/items/itemstore.h @@ -39,8 +39,8 @@ signals: public slots: void removeItem(const ItemData& item); - void addItem(const std::shared_ptr& item, item_update_type_t updateType); - void addItems(const std::vector>& itemsIn, item_update_type_t updateType); + void addItem(const ItemAddRequest& item); + void addItems(const std::vector& itemsIn); void replaceItems(const std::vector>& items); void updateItems(const std::vector& updates); void updateItem(const ItemUpdateRequest& update); diff --git a/src/main.cpp b/src/main.cpp index e3a6e03..722f6ed 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -115,12 +115,7 @@ int main(int argc, char *argv[]) QObject::connect(&mainObject.micro, SIGNAL(textRecived(QString)), w, SLOT(changeHeaderLableText(QString))); QObject::connect(w, &MainWindow::sigSetRgb, &mainObject.micro, &Microcontroller::changeRgbColor); QObject::connect(w, &MainWindow::sigSave, &mainObject, [&mainObject, settingsPath](){mainObject.storeToDisk(settingsPath);}); - QObject::connect(w, - &MainWindow::createdItem, - &globalItems, - [](std::shared_ptr item) { - globalItems.addItem(item, ITEM_UPDATE_USER); - }); + QObject::connect(w, &MainWindow::createdItem, &globalItems, &ItemStore::addItem); w->show(); } retVal = a.exec(); @@ -132,9 +127,7 @@ int main(int argc, char *argv[]) { SecondaryMainObject mainObject(parser.value(hostOption), parser.value(portOption).toInt()); MainWindow w(&mainObject); - QObject::connect(&w, &MainWindow::createdItem, &globalItems, [](std::shared_ptr item) { - globalItems.addItem(item, ITEM_UPDATE_USER); - }); + QObject::connect(&w, &MainWindow::createdItem, &globalItems, &ItemStore::addItem); QObject::connect(&w, &MainWindow::sigSave, mainObject.tcpClient, &TcpClient::sendItems); w.show(); diff --git a/src/microcontroller.cpp b/src/microcontroller.cpp index 0bf8690..8005278 100644 --- a/src/microcontroller.cpp +++ b/src/microcontroller.cpp @@ -157,16 +157,31 @@ void Microcontroller::processList(const QString& buffer) else if(buffer.contains("EOL")) { listMode = false; - gotItems(relayList, ITEM_UPDATE_BACKEND); + std::vector requests; + for(const std::shared_ptr& item : relayList) + { + ItemAddRequest request; + request.changes.name = true; + request.changes.value = true; + request.payload = item; + request.type = ITEM_UPDATE_BACKEND; + requests.push_back(request); + } + gotItems(requests); relayList.clear(); } - else listMode = false; + else + { + listMode = false; + } } void Microcontroller::processRelayState(const QString& buffer) { ItemUpdateRequest update; update.type = ITEM_UPDATE_BACKEND; + update.changes.name = true; + update.changes.value = true; update.payload = static_cast(*processRelayLine(buffer)); updateItems({update}); } diff --git a/src/service/server.cpp b/src/service/server.cpp index 8d900a2..13fc7f3 100644 --- a/src/service/server.cpp +++ b/src/service/server.cpp @@ -25,6 +25,7 @@ void Server::processIncomeingJson(const QByteArray& jsonbytes) QJsonArray data = json["Data"].toArray(); bool FullList = json["FullList"].toBool(false); std::vector> items; + std::vector fieldChanges; for(QJsonValueRef itemjson : data) { QJsonObject jsonobject = itemjson.toObject(); @@ -33,6 +34,7 @@ void Server::processIncomeingJson(const QByteArray& jsonbytes) { qDebug()<<"Server got item"<getName(); item->setLoaded(FullList); + fieldChanges.push_back(item->loadWithChanges(jsonobject)); items.push_back(item); } } @@ -43,7 +45,16 @@ void Server::processIncomeingJson(const QByteArray& jsonbytes) } else if(!items.empty()) { - gotItems(items, ITEM_UPDATE_REMOTE); + std::vector updates; + for(size_t i = 0; i < items.size(); i++) + { + ItemUpdateRequest request; + request.payload = *items[i]; + request.changes = fieldChanges[i]; + request.type = ITEM_UPDATE_REMOTE; + updates.push_back(request); + } + updateItems(updates); } } else diff --git a/src/service/service.cpp b/src/service/service.cpp index ae75b7e..a43a313 100644 --- a/src/service/service.cpp +++ b/src/service/service.cpp @@ -63,7 +63,6 @@ void Service::sendItems() sendJson(json); } - void Service::processIncomeingJson(const QByteArray& jsonbytes) { QJsonDocument doc = QJsonDocument::fromJson(jsonbytes); @@ -82,7 +81,6 @@ void Service::processIncomeingJson(const QByteArray& jsonbytes) else if(type == "SensorUpdate") { QJsonArray data = json["Data"].toArray(); - qWarning()<<"Got sensor update with no/empty sensors array"; for(QJsonValueRef sensorjson : data) { QJsonObject jsonobject = sensorjson.toObject(); diff --git a/src/service/tcpclient.cpp b/src/service/tcpclient.cpp index 63729d1..afcb473 100644 --- a/src/service/tcpclient.cpp +++ b/src/service/tcpclient.cpp @@ -34,20 +34,40 @@ void TcpClient::processIncomeingJson(const QByteArray& jsonbytes) if(type == "ItemUpdate") { QJsonArray data = json["Data"].toArray(); + bool FullList = json["FullList"].toBool(false); std::vector> items; + std::vector fieldChanges; for(QJsonValueRef itemjson : data) { QJsonObject jsonobject = itemjson.toObject(); std::shared_ptr item = Item::loadItem(jsonobject); if(item) { - qDebug()<<"Client got item"<getName(); - item->setLoaded(false); + item->setLoaded(FullList); + fieldChanges.push_back(item->loadWithChanges(jsonobject)); items.push_back(item); } } - if(!items.empty()) - gotItems(items, ITEM_UPDATE_REMOTE); + if(FullList && !items.empty()) + { + qDebug()<<"Client replaceing items"; + requestReplaceItems(items); + } + else if(!items.empty()) + { + std::vector itemAddRequests; + qDebug()<<"Client updateing items"; + for(size_t i = 0; i < items.size(); i++) + { + ItemAddRequest request; + request.type = ITEM_UPDATE_REMOTE; + request.payload = items[i]; + request.changes = fieldChanges[i]; + itemAddRequests.push_back(request); + qDebug()<<"Payload"<id(); + } + gotItems(itemAddRequests); + } } else { diff --git a/src/ui/actorsettingsdialog.cpp b/src/ui/actorsettingsdialog.cpp index f89a1a0..ccd17e5 100644 --- a/src/ui/actorsettingsdialog.cpp +++ b/src/ui/actorsettingsdialog.cpp @@ -106,8 +106,11 @@ ActorSettingsDialog::~ActorSettingsDialog() void ActorSettingsDialog::editAsItem() { - ItemSettingsDialog itemSettingsDiag(actor_, this); + setModal(false); + ItemSettingsDialog itemSettingsDiag(actor_, false, this); + itemSettingsDiag.setModal(false); itemSettingsDiag.exec(); + setModal(true); } void ActorSettingsDialog::setEnabled() diff --git a/src/ui/itemscrollbox.cpp b/src/ui/itemscrollbox.cpp index 170a557..55ba900 100644 --- a/src/ui/itemscrollbox.cpp +++ b/src/ui/itemscrollbox.cpp @@ -1,15 +1,16 @@ #include "itemscrollbox.h" #include "ui_relayscrollbox.h" -#include "../items/auxitem.h" -#include "../items/messageitem.h" +#include +#include ItemScrollBox::ItemScrollBox(QWidget *parent) : QWidget(parent), ui(new Ui::RelayScrollBox) { ui->setupUi(this); - QScroller::grabGesture(ui->scrollArea, QScroller::TouchGesture); - QScroller::grabGesture(ui->scrollArea, QScroller::LeftMouseButtonGesture); + + ensureTabExists("All"); + ui->tabWidget->setCurrentIndex(0); } ItemScrollBox::~ItemScrollBox() @@ -23,24 +24,166 @@ void ItemScrollBox::addItem(std::weak_ptr item) { if(workItem->isHidden()) return; - widgets_.push_back(new ItemWidget(item)); - ui->relayWidgetVbox->addWidget(widgets_.back()); - connect(widgets_.back(), &ItemWidget::deleteRequest, this, &ItemScrollBox::deleteRequest); - connect(widgets_.back(), &ItemWidget::deleteRequest, this, &ItemScrollBox::removeItem); + + // Add to "All" tab + widgets_["All"].push_back(new ItemWidget(item, false)); + QWidget* allScrollContent = tabs_["All"].content; + QLayout* layout = allScrollContent->layout(); + layout->removeItem(tabs_["All"].spacer); + layout->addWidget(widgets_["All"].back()); + layout->addItem(tabs_["All"].spacer); + connect(widgets_["All"].back(), &ItemWidget::deleteRequest, this, &ItemScrollBox::deleteRequest); + connect(widgets_["All"].back(), &ItemWidget::deleteRequest, this, &ItemScrollBox::removeItem); + + addItemToTabs(item); + } +} + +void ItemScrollBox::addItemToTabs(std::weak_ptr item) +{ + if(auto workItem = item.lock()) + { + if(workItem->isHidden()) + return; + + QString groupName = workItem->getGroupName(); + if(groupName.isEmpty() || groupName != "All") + { + ensureTabExists(groupName); + ItemWidget* groupWidget = new ItemWidget(item, true); + widgets_[groupName].push_back(groupWidget); + + QWidget* scrollContent = tabs_[groupName].content; + QLayout* groupLayout = scrollContent->layout(); + groupLayout->removeItem(tabs_[groupName].spacer); + groupLayout->addWidget(groupWidget); + groupLayout->addItem(tabs_[groupName].spacer); + + connect(groupWidget, &ItemWidget::deleteRequest, this, &ItemScrollBox::deleteRequest); + connect(groupWidget, &ItemWidget::deleteRequest, this, &ItemScrollBox::removeItem); + + connect(widgets_[groupName].back(), &ItemWidget::deleteRequest, this, &ItemScrollBox::deleteRequest); + connect(widgets_[groupName].back(), &ItemWidget::deleteRequest, this, &ItemScrollBox::removeItem); + } } } void ItemScrollBox::removeItem(const ItemData& item) { - for(unsigned i = 0; i < widgets_.size(); i++) + QString key = "All"; + std::vector& widgets = widgets_[key]; + for(unsigned i = 0; i < widgets.size(); i++) { - if(widgets_[i]->controles(item)) + if(widgets[i]->controles(item)) { - ui->relayWidgetVbox->removeWidget(widgets_[i]); - delete widgets_[i]; - widgets_.erase(widgets_.begin()+i); + QWidget* tabContent = tabs_[key].content; + if(tabContent) + { + QLayout* layout = tabContent->layout(); + if(layout) + layout->removeWidget(widgets[i]); + } + delete widgets[i]; + widgets.erase(widgets.begin()+i); + break; + } + } + + removeItemFromTabs(item); + cleanupEmptyTabs(); +} + +void ItemScrollBox::removeItemFromTabs(const ItemData& item) +{ + for(const QString& key : widgets_.keys()) + { + if(key == "All") + continue; + std::vector& widgets = widgets_[key]; + for(unsigned i = 0; i < widgets.size(); i++) + { + if(widgets[i]->controles(item)) + { + QWidget* tabContent = tabs_[key].content; + if(tabContent) + { + QLayout* layout = tabContent->layout(); + if(layout) + layout->removeWidget(widgets[i]); + } + delete widgets[i]; + widgets.erase(widgets.begin()+i); + break; + } + } + } + + cleanupEmptyTabs(); +} + + +void ItemScrollBox::onItemUpdate(const ItemUpdateRequest& update) +{ + if(!update.changes.groupName) + return; + + for(ItemWidget* widget : widgets_["All"]) + { + if(widget->controles(update.payload)) + { + qDebug()<<"ItemUpdate with group change"; + std::weak_ptr item = widget->getItem(); + removeItemFromTabs(update.payload); + addItemToTabs(item); } } } +void ItemScrollBox::ensureTabExists(const QString& groupName) +{ + if(!tabs_.contains(groupName)) + { + Tab tab; + tab.scroller = new QScrollArea(ui->tabWidget); + tab.scroller->setWidgetResizable(true); + tab.scroller->setFrameShape(QFrame::NoFrame); + tab.scroller->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + tab.content = new QWidget(tab.scroller); + QVBoxLayout* scrollLayout = new QVBoxLayout(tab.content); + scrollLayout->setContentsMargins(0, 0, 0, 0); + tab.content->setLayout(scrollLayout); + tab.content->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum); + tab.scroller->setWidget(tab.content); + + tab.spacer = new QSpacerItem(0, 0, QSizePolicy::Fixed, QSizePolicy::Expanding); + scrollLayout->addSpacerItem(tab.spacer); + + ui->tabWidget->addTab(tab.scroller, groupName); + tabs_[groupName] = tab; + } +} + +void ItemScrollBox::cleanupEmptyTabs() +{ + for(auto it = tabs_.begin(); it != tabs_.end(); ++it) + { + QString groupName = it.key(); + if(groupName == "All") + continue; + + qDebug()<<__func__<layout()->count(); + + if(it.value().content->layout()->count() <= 1) + { + int index = ui->tabWidget->indexOf(tabs_[groupName].scroller); + if(index >= 0) + ui->tabWidget->removeTab(index); + Tab tab = tabs_.take(groupName); + delete tab.content; + delete tab.scroller; + cleanupEmptyTabs(); + break; + } + } +} diff --git a/src/ui/itemscrollbox.h b/src/ui/itemscrollbox.h index 54d5fe8..4292aa0 100644 --- a/src/ui/itemscrollbox.h +++ b/src/ui/itemscrollbox.h @@ -4,9 +4,9 @@ #include #include #include -#include +#include +#include #include "itemwidget.h" -#include "../items/relay.h" #include "../items/item.h" #include "../items/itemstore.h" @@ -20,7 +20,16 @@ class ItemScrollBox : public QWidget { Q_OBJECT private: - std::vector< ItemWidget* > widgets_; + struct Tab + { + QScrollArea* scroller; + QWidget* content; + QSpacerItem* spacer; + }; + + QMap tabs_; + QMap> widgets_; + Ui::RelayScrollBox *ui; signals: void deleteRequest(const ItemData& item); @@ -36,9 +45,15 @@ public slots: void addItem(std::weak_ptr item); void removeItem(const ItemData& item); + void onItemUpdate(const ItemUpdateRequest& update); private: - Ui::RelayScrollBox *ui; + void ensureTabExists(const QString& groupName); + void cleanupEmptyTabs(); + +private: + void addItemToTabs(std::weak_ptr item); + void removeItemFromTabs(const ItemData& item); }; #endif // RELAYSCROLLBOX_H diff --git a/src/ui/itemsettingsdialog.cpp b/src/ui/itemsettingsdialog.cpp index 08417d3..aa60f01 100644 --- a/src/ui/itemsettingsdialog.cpp +++ b/src/ui/itemsettingsdialog.cpp @@ -1,5 +1,6 @@ #include "itemsettingsdialog.h" #include "ui_itemsettingsdialog.h" +#include "../items/itemstore.h" #include "actorsettingsdialog.h" #include "../actors/alarmtime.h" #include "../actors/sensoractor.h" @@ -13,7 +14,7 @@ #include "itemsettingswidgets/relayitemsettingswidget.h" #include -ItemSettingsDialog::ItemSettingsDialog(std::shared_ptr item, QWidget *parent) : +ItemSettingsDialog::ItemSettingsDialog(std::shared_ptr item, bool noGroup, QWidget *parent) : QDialog(parent), item_(item), ui(new Ui::ItemSettingsDialog) @@ -22,9 +23,17 @@ ItemSettingsDialog::ItemSettingsDialog(std::shared_ptr item, QWidget *pare setModal(false); + if(noGroup) + ui->comboBox_Group->setEnabled(false); + ui->label_name->setText(item_->getName()); ui->checkBox_Override->setChecked(item_->getOverride()); - + + // Setup group combobox with editable mode for creating new groups + ui->comboBox_Group->setEditable(true); + ui->comboBox_Group->addItem("All"); + ui->comboBox_Group->addItems(getExistingGroups()); + ui->comboBox_Group->setCurrentText(item_->getGroupName()); if(std::shared_ptr relay = std::dynamic_pointer_cast(item_)) { @@ -48,6 +57,7 @@ ItemSettingsDialog::ItemSettingsDialog(std::shared_ptr item, QWidget *pare connect(ui->pushButton_remove, &QPushButton::clicked, this, &ItemSettingsDialog::removeActor); connect(ui->pushButton_edit, &QPushButton::clicked, this, &ItemSettingsDialog::editActor); connect(ui->checkBox_Override, &QPushButton::clicked, this, &ItemSettingsDialog::changeOverride); + connect(ui->comboBox_Group, &QComboBox::currentTextChanged, this, &ItemSettingsDialog::changeGroup); ui->tableWidget->setHorizontalHeaderItem(0, new QTableWidgetItem("Actor")); @@ -61,10 +71,23 @@ ItemSettingsDialog::ItemSettingsDialog(std::shared_ptr item, QWidget *pare ItemSettingsDialog::~ItemSettingsDialog() { - if(itemSpecificWidget_) delete itemSpecificWidget_; + if(itemSpecificWidget_) + delete itemSpecificWidget_; delete ui; } +void ItemSettingsDialog::changeGroup() +{ + QString newGroup = ui->comboBox_Group->currentText(); + if(newGroup != item_->getGroupName()) + { + ItemUpdateRequest update = item_->createValueUpdateRequest(ITEM_UPDATE_USER); + update.payload.setGroupName(newGroup); + update.changes.groupName = true; + item_->requestUpdate(update); + } +} + void ItemSettingsDialog::changeOverride() { item_->setOverride(ui->checkBox_Override->isChecked()); @@ -166,14 +189,21 @@ void ItemSettingsDialog::editActor() ActorSettingsDialog* dialog; - if(alarmTime) dialog = new ActorSettingsDialog(alarmTime, this); - else if(regulator) dialog = new ActorSettingsDialog(regulator, this); - else if(sensorActor) dialog = new ActorSettingsDialog(sensorActor, this); - else if(timerActor) dialog = new ActorSettingsDialog(timerActor, this); - else if(polynomalActor) dialog = new ActorSettingsDialog(polynomalActor, this); - else if(factorActor) dialog = new ActorSettingsDialog(factorActor, this); - else dialog = new ActorSettingsDialog(actor, this); - dialog->setParent(this); + if(alarmTime) + dialog = new ActorSettingsDialog(alarmTime, this); + else if(regulator) + dialog = new ActorSettingsDialog(regulator, this); + else if(sensorActor) + dialog = new ActorSettingsDialog(sensorActor, this); + else if(timerActor) + dialog = new ActorSettingsDialog(timerActor, this); + else if(polynomalActor) + dialog = new ActorSettingsDialog(polynomalActor, this); + else if(factorActor) + dialog = new ActorSettingsDialog(factorActor, this); + else + dialog = new ActorSettingsDialog(actor, this); + dialog->show(); dialog->exec(); @@ -183,5 +213,19 @@ void ItemSettingsDialog::editActor() ui->tableWidget->item(i, 1)->setText(item_->getActors()[i]->actionName()); ui->tableWidget->item(i, 2)->setText(item_->getActors()[i]->isActive() ? "Y" : "N"); } + delete dialog; } } + +QStringList ItemSettingsDialog::getExistingGroups() +{ + QSet uniqueGroups; + for(const auto& item : *globalItems.getItems()) + { + if(!item->getGroupName().isEmpty() && item->getGroupName() != "All") + { + uniqueGroups.insert(item->getGroupName()); + } + } + return uniqueGroups.values(); +} diff --git a/src/ui/itemsettingsdialog.h b/src/ui/itemsettingsdialog.h index dc2a5af..606261c 100644 --- a/src/ui/itemsettingsdialog.h +++ b/src/ui/itemsettingsdialog.h @@ -19,9 +19,10 @@ class ItemSettingsDialog : public QDialog private: void loadActorList(); + QStringList getExistingGroups(); public: - explicit ItemSettingsDialog(std::shared_ptr item, QWidget *parent = nullptr); + explicit ItemSettingsDialog(std::shared_ptr item, bool noGroup = false, QWidget *parent = nullptr); ~ItemSettingsDialog(); private slots: @@ -30,6 +31,7 @@ private slots: void addActor(); void editActor(); void changeOverride(); + void changeGroup(); private: Ui::ItemSettingsDialog *ui; diff --git a/src/ui/itemsettingsdialog.ui b/src/ui/itemsettingsdialog.ui index 7faeede..c7f4113 100644 --- a/src/ui/itemsettingsdialog.ui +++ b/src/ui/itemsettingsdialog.ui @@ -63,6 +63,20 @@ + + + + + + Group: + + + + + + + + diff --git a/src/ui/itemwidget.cpp b/src/ui/itemwidget.cpp index c91c5e8..de7544e 100644 --- a/src/ui/itemwidget.cpp +++ b/src/ui/itemwidget.cpp @@ -4,10 +4,12 @@ #include #include #include +#include "itemsettingsdialog.h" -ItemWidget::ItemWidget(std::weak_ptr item, QWidget *parent) : +ItemWidget::ItemWidget(std::weak_ptr item, bool noGroupEdit, QWidget *parent) : QWidget(parent), item_(item), + noGroupEdit_(noGroupEdit), ui(new Ui::ItemWidget) { ui->setupUi(this); @@ -40,12 +42,13 @@ ItemWidget::ItemWidget(std::weak_ptr item, QWidget *parent) : connect(ui->pushButton, &QPushButton::clicked, this, &ItemWidget::showSettingsDialog); connect(workingItem.get(), &Item::updated, this, &ItemWidget::onItemUpdated); connect(ui->pushButton_Remove, &QPushButton::clicked, this, &ItemWidget::deleteItem); - } else { disable(); } + + setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); } void ItemWidget::deleteItem() @@ -60,7 +63,9 @@ void ItemWidget::moveToValue(int value) { if(auto workingItem = item_.lock()) { - ItemUpdateRequest request = workingItem->createValueUpdateRequest(value, ITEM_UPDATE_USER); + ItemUpdateRequest request = workingItem->createValueUpdateRequest(ITEM_UPDATE_USER); + request.payload.setValueData(value); + request.changes.value = true; workingItem->requestUpdate(request); } else @@ -71,15 +76,7 @@ void ItemWidget::moveToValue(int value) void ItemWidget::moveToState(bool state) { - if(auto workingItem = item_.lock()) - { - ItemUpdateRequest request = workingItem->createValueUpdateRequest(state, ITEM_UPDATE_USER); - workingItem->requestUpdate(request); - } - else - { - disable(); - } + moveToValue(state); } void ItemWidget::disable() @@ -104,8 +101,9 @@ void ItemWidget::showSettingsDialog() { if(auto workingItem = item_.lock()) { - ItemSettingsDialog dialog(workingItem, this); + ItemSettingsDialog dialog(workingItem, noGroupEdit_, this); dialog.exec(); + } else disable(); } diff --git a/src/ui/itemwidget.h b/src/ui/itemwidget.h index 90d5a97..51c642f 100644 --- a/src/ui/itemwidget.h +++ b/src/ui/itemwidget.h @@ -3,7 +3,6 @@ #include #include -#include "itemsettingsdialog.h" #include "../items/item.h" namespace Ui @@ -16,6 +15,7 @@ class ItemWidget : public QWidget Q_OBJECT private: std::weak_ptr item_; + bool noGroupEdit_; void disable(); @@ -30,7 +30,7 @@ private slots: void deleteItem(); public: - explicit ItemWidget(std::weak_ptr item, QWidget *parent = nullptr); + explicit ItemWidget(std::weak_ptr item, bool noGroupEdit = false, QWidget *parent = nullptr); std::weak_ptr getItem(); bool controles(const ItemData& relay); ~ItemWidget(); diff --git a/src/ui/mainwindow.cpp b/src/ui/mainwindow.cpp index 79a6b3e..369d3e9 100644 --- a/src/ui/mainwindow.cpp +++ b/src/ui/mainwindow.cpp @@ -24,6 +24,7 @@ MainWindow::MainWindow(MainObject * const mainObject, QWidget *parent) : connect(ui->pushButton_refesh, &QPushButton::clicked, mainObject, &MainObject::refresh); connect(&globalItems, &ItemStore::itemAdded, ui->relayList, &ItemScrollBox::addItem); connect(&globalItems, &ItemStore::itemDeleted, ui->relayList, &ItemScrollBox::removeItem); + connect(&globalItems, &ItemStore::itemUpdated, ui->relayList, &ItemScrollBox::onItemUpdate); for(size_t i = 0; i < globalItems.getItems()->size(); ++i) ui->relayList->addItem(globalItems.getItems()->at(i)); @@ -65,7 +66,7 @@ void MainWindow::showPowerItemDialog() } if(powerItem) { - ItemSettingsDialog diag(std::shared_ptr(powerItem), this); + ItemSettingsDialog diag(std::shared_ptr(powerItem), false, this); diag.show(); diag.exec(); } @@ -79,7 +80,13 @@ void MainWindow::showItemCreationDialog() ItemCreationDialog diag(this); diag.show(); if(diag.exec()) - createdItem(diag.item); + { + ItemAddRequest request; + request.type = ITEM_UPDATE_USER; + request.changes = ItemFieldChanges(true); + request.payload = diag.item; + createdItem(request); + } } void MainWindow::changeHeaderLableText(QString string) diff --git a/src/ui/mainwindow.h b/src/ui/mainwindow.h index 5b04b64..efd9709 100644 --- a/src/ui/mainwindow.h +++ b/src/ui/mainwindow.h @@ -32,7 +32,7 @@ private: signals: void sigSave(); - void createdItem(std::shared_ptr item); + void createdItem(ItemAddRequest request); void sigSetRgb(const QColor color); private slots: diff --git a/src/ui/mainwindow.ui b/src/ui/mainwindow.ui index baa31c8..422862d 100644 --- a/src/ui/mainwindow.ui +++ b/src/ui/mainwindow.ui @@ -94,7 +94,7 @@ - 400 + 350 0 diff --git a/src/ui/relayscrollbox.ui b/src/ui/relayscrollbox.ui index cb0fae2..7ef7dc2 100644 --- a/src/ui/relayscrollbox.ui +++ b/src/ui/relayscrollbox.ui @@ -13,52 +13,15 @@ Form - + - - - QFrame::NoFrame + + + false - - 0 - - - Qt::ScrollBarAlwaysOff - - - Qt::ScrollBarAlwaysOff - - + true - - - - 0 - 0 - 388 - 288 - - - - - - - - - - Qt::Vertical - - - - 20 - 40 - - - - - - From a3a5277e5d4bb535f2e48ece64561cab463d9c8b Mon Sep 17 00:00:00 2001 From: Carl Philipp Klemm Date: Thu, 9 Apr 2026 11:55:08 +0200 Subject: [PATCH 06/21] Add README and AGENTS --- AGENTS.md | 129 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 90 +++++++++++++++++++++++++++++++++++++ 2 files changed, 219 insertions(+) create mode 100644 AGENTS.md create mode 100644 README.md diff --git a/AGENTS.md b/AGENTS.md new file mode 100644 index 0000000..81908fc --- /dev/null +++ b/AGENTS.md @@ -0,0 +1,129 @@ +# SHinterface - Smart Home Interface + +## Overview +SHinterface is a Qt6-based smart home control application that interfaces with microcontrollers and various sensors to manage home automation devices. It supports both primary (master) and secondary (client) modes, allowing for distributed control across multiple devices. + +## Architecture + +### Core Components + +1. **Main Application** (`main.cpp`) + - Entry point with command-line argument parsing + - Supports three modes: + - `PROGRAM_MODE_UI_ONLY`: Secondary client mode + - `PROGRAM_MODE_PRIMARY`: Master mode with GUI + - `PROGRAM_MODE_HEADLESS_PRIMARY`: Master mode without GUI (server only) + +2. **Main Object** (`mainobject.h`, `mainobject.cpp`) + - Base class: `MainObject` + - Primary mode: `PrimaryMainObject` - Manages microcontroller, sensors, and item sources + - Secondary mode: `SecondaryMainObject` - Connects to primary via TCP + +3. **Microcontroller Interface** (`microcontroller.h`, `microcontroller.cpp`) + - Communicates with embedded devices via serial or TCP + - Handles relay control, sensor data, RGB lighting, and PWM outputs + - Implements a write queue to prevent buffer overflows + +### Key Systems + +#### Items System +- **Item** (`items/item.h`, `items/item.cpp`): Base class for all controllable items + - Supports different value types: boolean, unsigned integer, no value + - Tracks update sources (user, actor, remote, loaded, backend) + +- Item Types: + - **Relay** (`items/relay.h`, `items/relay.cpp`): Switchable outputs + - **PowerItem**: Power measurement items + - **MessageItem**: Display messages + - **SystemItem**: System-related controls + - **AuxItem**: Auxiliary PWM outputs + - **RGBItem**: RGB LED control + +- **ItemStore** (`items/itemstore.h`, `items/itemstore.cpp`): Manages collection of items +- **ItemSource** (`items/itemsource.h`): Interface for item providers + - **FixedItemSource**: Static predefined items + - **ItemLoaderSource**: Loads items from configuration + +#### Actors System +Actors trigger actions based on sensor conditions or timers: + +- **Actor** (`actors/actor.h`, `actors/actor.cpp`): Base actor class + - Can be active/inactive and exhausted (preventing repeated triggers) + +- Actor Types: + - **FactorActor**: Triggers when a factor crosses a threshold + - **PolynomalActor**: Uses polynomial calculations for triggering + - **SensorActor**: Reacts to specific sensor states + - **TimerActor**: Time-based triggering + - **AlarmTime**: Alarm/clock functionality + - **Regulator**: PID-like regulation control + +#### Sensors System +- **Sensor** (`sensors/sensor.h`, `sensors/sensor.cpp`): Represents physical sensors + - Sensor types: door, temperature, humidity, pressure, brightness, button, ADC, CO2, PM2.5, VOC, etc. + +- Sensor Sources: + - **SunSensorSource** (`sensors/sunsensor.h`, `sensors/sunsensor.cpp`): Solar position calculations + - **MqttSensorSource** (`sensors/mqttsensorsource.h`, `sensors/mqttsensorsource.cpp`): MQTT-based sensor data + +#### Networking Services +- **TcpServer** (`service/tcpserver.h`, `service/tcpserver.cpp`): TCP server for remote control +- **WebSocketServer** (`service/websocketserver.h`, `service/websocketserver.cpp`): WebSocket interface +- **TcpClient** (`service/tcpclient.h`, `service/tcpclient.cpp`): Client for secondary instances + +### UI Components +- **MainWindow** (`ui/mainwindow.ui`, `ui/mainwindow.cpp`): Main application window +- **ItemWidget**: Visual representation of items +- **ItemScrollBox**: Scrollable container for items +- **SensorListWidget**: Displays sensor information +- **ItemCreationDialog**: Create new items +- **ItemSettingsDialog**: Configure item properties +- **ActorSettingsDialog**: Configure actor behavior + +## Data Flow + +### Primary Mode (Master) +``` +Microcontroller ↔ Items ↔ Actors ↔ Sensors + ↑ ↑ ↑ ↑ + TCP/WebSocket │ │ │ + └──────┬──────┘ + ↓ + UI (MainWindow) +``` + +### Secondary Mode (Client) +``` +Secondary Client ↔ TCP Server (Primary) → Items → Microcontroller +``` + +## Configuration +- Settings stored in JSON format +- Default location: `~/.config/shinterface.json` +- Command-line options: + - `-m`, `--master`: Run in master mode + - `-H`, `--host`: Set server host IP + - `-p`, `--port`: Set server port (default: 38940) + - `-c`, `--config`: Specify config file path + - `-e`, `--headless`: Run without GUI (master mode only) + +## Build Requirements +- CMake 4.0+ +- Qt6 (Core, Gui, Widgets, Network, Multimedia, SerialPort, Mqtt, WebSockets) +- libpipewire-0.3 +- libnl-3.0 + +## Communication Protocols +- **Microcontroller**: Text-based protocol over serial or TCP +- **Networking**: JSON messages over TCP/WebSocket +- **Sensors**: MQTT for remote sensors, calculated values for sun position + +## Key Features +1. Distributed control with primary/secondary architecture +2. Actor-based automation system +3. Multi-protocol sensor support (MQTT, serial, calculated) +4. Relay and PWM control +5. RGB lighting control +6. WebSocket API for remote access +7. Configurable via JSON +8. Cross-platform Qt application diff --git a/README.md b/README.md new file mode 100644 index 0000000..962d87f --- /dev/null +++ b/README.md @@ -0,0 +1,90 @@ +# SHinterface - Smart Home Control Interface + +A Qt6-based smart home control application that interfaces with microcontrollers and sensors to manage home automation devices. + +## Quick Start + +### Building +```bash +mkdir build +cd build +cmake .. +make +``` + +### Running + +**Primary (Master) Mode:** +```bash +./SHinterface -m +``` + +**Secondary (Client) Mode:** +```bash +./SHinterface -H 192.168.1.100 -p 38940 +``` + +**Headless Server Mode:** +```bash +./SHinterface -m -e +``` + +## Features + +- ✅ Control relays and PWM outputs +- ✅ Monitor various sensors (temperature, humidity, doors, etc.) +- ✅ MQTT sensor integration +- ✅ Actor-based automation system +- ✅ RGB lighting control +- ✅ WebSocket API for remote access +- ✅ Primary/secondary architecture for distributed control + +## Configuration + +Settings are stored in JSON format. Default location: +``` +~/.config/shinterface.json +``` + +You can specify a custom config file with: +```bash +./SHinterface -c /path/to/config.json +``` + +## Usage + +### Primary Mode (Master) +The primary instance connects to your microcontroller and manages all devices. It can run with or without a GUI. + +### Secondary Mode (Client) +Secondary instances connect to the primary via TCP and provide additional control points without needing their own microcontroller connection. + +## Command Line Options + +``` +-m, --master Use in master mode +-H, --host
Set server host IP address (default: 0.0.0.0) +-p, --port Set server port (default: 38940) +-c, --config Set config file path +-e, --headless Don't start the GUI (master mode only) +--help Show help +--version Show version +``` + +## Architecture + +- **Items**: Represent controllable devices (relays, lights, etc.) +- **Actors**: Automate actions based on sensors or time +- **Sensors**: Provide data from physical sensors and calculated sources +- **Services**: TCP and WebSocket interfaces for remote control + +## Dependencies + +- CMake 4.0+ +- Qt6 (Core, Gui, Widgets, Network, Multimedia, SerialPort, Mqtt, WebSockets) +- libpipewire-0.3 +- libnl-3.0 + +## License + +See LICENSE file for details. From 3fd27905ee14f0a554b9baaca4ccd7da026a70c5 Mon Sep 17 00:00:00 2001 From: Carl Philipp Klemm Date: Thu, 9 Apr 2026 16:18:17 +0200 Subject: [PATCH 07/21] Add tests --- CMakeLists.txt | 6 + tests/CMakeLists.txt | 140 +++++++++ tests/unit/actors/test_actor.cpp | 333 +++++++++++++++++++++ tests/unit/items/test_item.cpp | 225 ++++++++++++++ tests/unit/items/test_itemloadersource.cpp | 240 +++++++++++++++ tests/unit/items/test_itemstore.cpp | 255 ++++++++++++++++ tests/unit/sensors/test_sensor.cpp | 230 ++++++++++++++ 7 files changed, 1429 insertions(+) create mode 100644 tests/CMakeLists.txt create mode 100644 tests/unit/actors/test_actor.cpp create mode 100644 tests/unit/items/test_item.cpp create mode 100644 tests/unit/items/test_itemloadersource.cpp create mode 100644 tests/unit/items/test_itemstore.cpp create mode 100644 tests/unit/sensors/test_sensor.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 728bae0..39c1e2e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,6 +24,12 @@ set(CMAKE_AUTOUIC ON) # Add src to include path for relative includes include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src) +# Enable testing framework +enable_testing() + +# Add subdirectory for tests +add_subdirectory(tests) + # Create executable add_executable(SHinterface src/sensors/mqttsensorsource.h src/sensors/mqttsensorsource.cpp) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..6beb2eb --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,140 @@ +cmake_minimum_required(VERSION 4.0) + +# Enable testing +enable_testing() + +# Find Qt packages for tests +find_package(Qt6 COMPONENTS Core Gui Widgets Multimedia Test REQUIRED) + +# Define common sources needed by all tests - include only what's actually needed for basic testing +set(COMMON_TEST_SOURCES + ../src/items/item.h + ../src/items/item.cpp + ../src/sensors/sensor.h + ../src/sensors/sensor.cpp + ../src/programmode.h + ../src/programmode.cpp + ../src/microcontroller.h + ../src/microcontroller.cpp + ../src/actors/actor.h + ../src/actors/actor.cpp + ../src/actors/factoractor.h + ../src/actors/factoractor.cpp + ../src/actors/polynomalactor.h + ../src/actors/polynomalactor.cpp + ../src/actors/sensoractor.h + ../src/actors/sensoractor.cpp + ../src/actors/timeractor.h + ../src/actors/timeractor.cpp + ../src/items/relay.h + ../src/items/relay.cpp + ../src/items/messageitem.h + ../src/items/messageitem.cpp + ../src/items/systemitem.h + ../src/items/systemitem.cpp + ../src/items/auxitem.h + ../src/items/auxitem.cpp + ../src/items/poweritem.h + ../src/items/poweritem.cpp + ../src/items/rgbitem.h + ../src/items/rgbitem.cpp + ../src/actors/alarmtime.h + ../src/actors/alarmtime.cpp + ../src/actors/regulator.h + ../src/actors/regulator.cpp + ../src/items/itemsource.h + ../src/items/itemsource.cpp + ../src/items/itemstore.h + ../src/items/itemstore.cpp + ../src/items/itemloadersource.h + ../src/items/itemloadersource.cpp +) + +# Add test executables - compile all needed sources into each test +add_executable(test_item unit/items/test_item.cpp ${COMMON_TEST_SOURCES}) +add_executable(test_sensor unit/sensors/test_sensor.cpp ${COMMON_TEST_SOURCES}) +add_executable(test_actor unit/actors/test_actor.cpp ${COMMON_TEST_SOURCES}) +add_executable(test_itemstore unit/items/test_itemstore.cpp ${COMMON_TEST_SOURCES}) +add_executable(test_itemloadersource unit/items/test_itemloadersource.cpp ${COMMON_TEST_SOURCES}) + +# Link libraries for test_item +target_link_libraries(test_item + Qt6::Core + Qt6::Gui + Qt6::Widgets + Qt6::Multimedia + Qt6::Test +) + +# Include paths for source files +target_include_directories(test_item PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/../src + ${Qt6Gui_PRIVATE_INCLUDE_DIRS} +) + +# Link libraries for test_sensor +target_link_libraries(test_sensor + Qt6::Core + Qt6::Gui + Qt6::Widgets + Qt6::Multimedia + Qt6::Test +) + +# Include paths for source files +target_include_directories(test_sensor PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/../src + ${Qt6Gui_PRIVATE_INCLUDE_DIRS} +) + +# Link libraries for test_actor +target_link_libraries(test_actor + Qt6::Core + Qt6::Gui + Qt6::Widgets + Qt6::Multimedia + Qt6::Test +) + +# Include paths for source files +target_include_directories(test_actor PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/../src + ${Qt6Gui_PRIVATE_INCLUDE_DIRS} +) + +# Link libraries for test_itemstore +target_link_libraries(test_itemstore + Qt6::Core + Qt6::Gui + Qt6::Widgets + Qt6::Multimedia + Qt6::Test +) + +# Include paths for source files +target_include_directories(test_itemstore PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/../src + ${Qt6Gui_PRIVATE_INCLUDE_DIRS} +) + +# Link libraries for test_itemloadersource +target_link_libraries(test_itemloadersource + Qt6::Core + Qt6::Gui + Qt6::Widgets + Qt6::Multimedia + Qt6::Test +) + +# Include paths for source files +target_include_directories(test_itemloadersource PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/../src + ${Qt6Gui_PRIVATE_INCLUDE_DIRS} +) + +# Add tests to CTest +add_test(NAME test_item COMMAND test_item) +add_test(NAME test_sensor COMMAND test_sensor) +add_test(NAME test_actor COMMAND test_actor) +add_test(NAME test_itemstore COMMAND test_itemstore) +add_test(NAME test_itemloadersource COMMAND test_itemloadersource) diff --git a/tests/unit/actors/test_actor.cpp b/tests/unit/actors/test_actor.cpp new file mode 100644 index 0000000..cf42d32 --- /dev/null +++ b/tests/unit/actors/test_actor.cpp @@ -0,0 +1,333 @@ +#include +#include "actors/actor.h" +#include "actors/timeractor.h" +#include "actors/sensoractor.h" + +class TestActor : public QObject +{ + Q_OBJECT + +private slots: + void initTestCase() + { + // Setup for all tests + } + + void testActorCreation() + { + Actor actor; + + // Actor should be active by default + QVERIFY(actor.isActive()); + + // Actor should not be exhausted by default + QVERIFY(!actor.isExausted()); + } + + void testActorActivation() + { + Actor actor; + + // Test makeActive/makeInactive + actor.makeActive(); + QVERIFY(actor.isActive()); + + actor.makeInactive(); + QVERIFY(!actor.isActive()); + } + + void testActorSetActive() + { + Actor actor; + + // Test setActive + actor.setActive(1); + QVERIFY(actor.isActive()); + + actor.setActive(0); + QVERIFY(!actor.isActive()); + } + + void testActorTriggerValue() + { + Actor actor; + + // Default trigger value should be 0 + QVERIFY(actor.getTriggerValue() == 0); + + // Set trigger value + actor.setTriggerValue(1); + QVERIFY(actor.getTriggerValue() == 1); + + actor.setTriggerValue(255); + QVERIFY(actor.getTriggerValue() == 255); + } + + void testActorActionName() + { + Actor actor; + + // Default trigger value is 0, so action name should be "off" + QVERIFY(actor.actionName() == "off"); + + // Set trigger value to 1 + actor.setTriggerValue(1); + QVERIFY(actor.actionName() == "on"); + + // Set trigger value to something else + actor.setTriggerValue(5); + QVERIFY(actor.actionName() == "value to 5"); + } + + void testActorJsonSerialization() + { + Actor actor; + actor.setTriggerValue(1); + actor.makeInactive(); + + QJsonObject json; + actor.store(json); + + // Verify JSON contents + QVERIFY(json.contains("Active")); + QVERIFY(json.contains("Exausted")); + QVERIFY(json.contains("TriggerValue")); + + QVERIFY(json["Active"].toBool() == false); + QVERIFY(json["Exausted"].toBool() == false); + QVERIFY(json["TriggerValue"].toInt() == 1); + } + + void testActorJsonDeserialization() + { + QJsonObject json; + json["Active"] = false; + json["Exausted"] = true; + json["TriggerValue"] = 5; + + Actor actor; + actor.load(json); + + QVERIFY(actor.isActive() == false); + QVERIFY(actor.isExausted() == true); + QVERIFY(actor.getTriggerValue() == 5); + } + + void testActorCreateActor() + { + // Test creating different actor types + std::shared_ptr alarmActor = Actor::createActor("Alarm"); + QVERIFY(alarmActor != nullptr); + + std::shared_ptr sensorActor = Actor::createActor("Sensor"); + QVERIFY(sensorActor != nullptr); + + std::shared_ptr timerActor = Actor::createActor("Timer"); + QVERIFY(timerActor != nullptr); + + std::shared_ptr regulatorActor = Actor::createActor("Regulator"); + QVERIFY(regulatorActor != nullptr); + + std::shared_ptr polynomalActor = Actor::createActor("Polynomal"); + QVERIFY(polynomalActor != nullptr); + + std::shared_ptr factorActor = Actor::createActor("MultiFactor"); + QVERIFY(factorActor != nullptr); + + std::shared_ptr genericActor = Actor::createActor("Actor"); + QVERIFY(genericActor != nullptr); + + // Test unknown type returns nullptr + std::shared_ptr unknownActor = Actor::createActor("UnknownType"); + QVERIFY(unknownActor == nullptr); + } + + void testActorLoadActor() + { + QJsonObject json; + json["Type"] = "Timer"; + json["Active"] = true; + json["Exausted"] = false; + json["TriggerValue"] = 1; + json["Timeout"] = 5000; + + std::shared_ptr actor = Actor::loadActor(json); + QVERIFY(actor != nullptr); + + // Verify the actor was loaded with correct values + QVERIFY(actor->isActive()); + QVERIFY(!actor->isExausted()); + QVERIFY(actor->getTriggerValue() == 1); + } + + void testTimerActorCreation() + { + TimerActor actor(60); + + // Default timeout should be 60 seconds + QVERIFY(actor.getTimeout() == 60); + } + + void testTimerActorSetTimeout() + { + TimerActor actor(60); + + actor.setTimeout(120); + QVERIFY(actor.getTimeout() == 120); + + actor.setTimeout(5); + QVERIFY(actor.getTimeout() == 5); + } + + void testTimerActorJsonSerialization() + { + TimerActor actor(120); + + QJsonObject json; + actor.store(json); + + // Verify JSON contents + QVERIFY(json.contains("Type")); + QVERIFY(json.contains("Timeout")); + + QVERIFY(json["Type"].toString() == "Timer"); + QVERIFY(json["Timeout"].toInt() == 120000); // Converted to milliseconds + } + + void testTimerActorJsonDeserialization() + { + QJsonObject json; + json["Type"] = "Timer"; + json["Timeout"] = 30000; // 30 seconds in milliseconds + json["Active"] = true; + json["Exausted"] = false; + json["TriggerValue"] = 1; + + TimerActor actor; + actor.load(json, false); + + // Timeout should be 30 seconds + QVERIFY(actor.getTimeout() == 30); + } + + void testTimerActorGetName() + { + TimerActor actor(60); + + QString name = actor.getName(); + QVERIFY(name.contains("60")); + } + + void testSensorActorCreation() + { + Sensor sensor(Sensor::TYPE_TEMPERATURE, 1, 25.0, "temp_sensor"); + SensorActor actor(sensor); + + // Verify sensor was set correctly + QVERIFY(actor.getSensor().type == Sensor::TYPE_TEMPERATURE); + QVERIFY(actor.getSensor().id == 1); + } + + void testSensorActorDefaultCreation() + { + SensorActor actor; + + // Default sensor should be dummy + QVERIFY(actor.getSensor().type == Sensor::TYPE_DUMMY); + } + + void testSensorActorSetSensor() + { + SensorActor actor; + + Sensor newSensor(Sensor::TYPE_HUMIDITY, 2, 60.0, "humidity_sensor"); + actor.setSensor(newSensor); + + QVERIFY(actor.getSensor().type == Sensor::TYPE_HUMIDITY); + QVERIFY(actor.getSensor().id == 2); + } + + void testSensorActorSetThreshold() + { + SensorActor actor; + + actor.setThreshold(25.0); + QVERIFY(actor.getThreshold() == 25.0); + } + + void testSensorActorSetSlope() + { + SensorActor actor; + + actor.setSloap(SensorActor::SLOPE_UP); + QVERIFY(actor.getSloap() == SensorActor::SLOPE_UP); + + actor.setSloap(SensorActor::SLOPE_DOWN); + QVERIFY(actor.getSloap() == SensorActor::SLOPE_DOWN); + + actor.setSloap(SensorActor::SLOPE_BOTH); + QVERIFY(actor.getSloap() == SensorActor::SLOPE_BOTH); + } + + void testSensorActorJsonSerialization() + { + Sensor sensor(Sensor::TYPE_TEMPERATURE, 1, 25.0, "temp_sensor"); + SensorActor actor(sensor); + actor.setThreshold(30.0); + actor.setSloap(SensorActor::SLOPE_UP); + + QJsonObject json; + actor.store(json); + + // Verify JSON contents + QVERIFY(json.contains("Type")); + QVERIFY(json.contains("Threshold")); + QVERIFY(json.contains("Sloap")); + QVERIFY(json.contains("SensorType")); + + QVERIFY(json["Type"].toString() == "Sensor"); + QVERIFY(json["Threshold"].toDouble() == 30.0); + QVERIFY(json["Sloap"].toInt() == SensorActor::SLOPE_UP); + } + + void testSensorActorJsonDeserialization() + { + QJsonObject json; + json["Type"] = "Sensor"; + json["Threshold"] = 25.5; + json["Sloap"] = SensorActor::SLOPE_DOWN; + json["SensorType"] = Sensor::TYPE_HUMIDITY; + json["SensorId"] = 3; + json["SensorField"] = 65.0; + json["SensorName"] = "humidity"; + json["Active"] = true; + json["Exausted"] = false; + json["TriggerValue"] = 1; + + SensorActor actor; + actor.load(json, false); + + QVERIFY(actor.getThreshold() == 25.5); + QVERIFY(actor.getSloap() == SensorActor::SLOPE_DOWN); + } + + void testSensorActorGetName() + { + Sensor sensor(Sensor::TYPE_TEMPERATURE, 1, 25.0, "temp_sensor"); + SensorActor actor(sensor); + actor.setThreshold(30.0); + + QString name = actor.getName(); + QVERIFY(name.contains("temp_sensor")); + QVERIFY(name.contains("30")); + } + + void cleanupTestCase() + { + // Cleanup after all tests + } +}; + +QTEST_APPLESS_MAIN(TestActor) + +#include "test_actor.moc" \ No newline at end of file diff --git a/tests/unit/items/test_item.cpp b/tests/unit/items/test_item.cpp new file mode 100644 index 0000000..b4ad369 --- /dev/null +++ b/tests/unit/items/test_item.cpp @@ -0,0 +1,225 @@ +#include +#include "items/item.h" +#include "items/itemstore.h" +#include "items/itemsource.h" +#include "items/itemloadersource.h" + +class TestItem : public QObject +{ + Q_OBJECT + +private slots: + void initTestCase() + { + // Setup for all tests + } + + void testItemCreation() + { + Item item(0, "test_item", 0); + QCOMPARE(item.getName(), QString("test_item")); + } + + void testOverrideFunctionality() + { + Item item(0, "test_item", 0); + + // Test override on/off + item.setOverride(true); + QVERIFY(item.getOverride()); + + item.setOverride(false); + QVERIFY(!item.getOverride()); + } + + void testItemValueTypes() + { + // Test default value type is BOOL + Item item(1, "test_item", 0); + QCOMPARE(item.getValueType(), ITEM_VALUE_BOOL); + + // Test setting and getting value + item.setValueData(1); + QCOMPARE(item.getValue(), static_cast(1)); + + item.setValueData(0); + QCOMPARE(item.getValue(), static_cast(0)); + } + + void testItemId() + { + Item item(42, "test_item", 0); + QCOMPARE(item.id(), static_cast(42)); + } + + void testItemGroupName() + { + Item item(1, "test_item", 0); + QCOMPARE(item.getGroupName(), QString("All")); // Default group + + item.setGroupName("Living Room"); + QCOMPARE(item.getGroupName(), QString("Living Room")); + } + + void testItemHidden() + { + Item item(1, "test_item", 0); + QVERIFY(!item.isHidden()); + + item.setHidden(true); + QVERIFY(item.isHidden()); + + item.setHidden(false); + QVERIFY(!item.isHidden()); + } + + void testItemLoaded() + { + Item item(1, "test_item", 0); + QVERIFY(!item.getLoaded()); + + item.setLoaded(true); + QVERIFY(item.getLoaded()); + } + + void testItemJsonSerialization() + { + Item item(42, "test_item", 1); + item.setGroupName("TestGroup"); + + QJsonObject json; + item.store(json); + + // Verify JSON contents + QCOMPARE(json["ItemId"].toDouble(), 42.0); + // Note: Name is only stored if changes.name is true + // Value is only stored if changes.value is true + } + + void testItemJsonDeserialization() + { + QJsonObject json; + json["ItemId"] = 100; + json["Name"] = "loaded_item"; + json["Value"] = 1; + json["GroupName"] = "Bedroom"; + + Item item; + item.load(json); + + // Note: load() uses loadWithChanges which requires preserve=false to apply changes + // The id should be set + // The name should be set (since preserve defaults to false) + } + + void testItemUpdateRequest() + { + Item item(1, "test_item", 0); + + // Test creating update request + ItemUpdateRequest request = item.createValueUpdateRequest(ITEM_UPDATE_USER, false); + + QVERIFY(request.type == ITEM_UPDATE_USER); + // Note: changes is not automatically set by createValueUpdateRequest + // The caller is expected to set the changes they want + QVERIFY(request.newActors.empty()); + } + + void testItemUpdateRequestWithActors() + { + Item item(1, "test_item", 0); + + // Test creating update request with actors + ItemUpdateRequest request = item.createValueUpdateRequest(ITEM_UPDATE_USER, true); + + QVERIFY(request.type == ITEM_UPDATE_USER); + // With actors=true, newActors should be populated + // Note: changes.actors is set by the caller, not by createValueUpdateRequest + } + + void testItemHasActors() + { + Item item(1, "test_item", 0); + + QVERIFY(!item.hasActors()); + + // Note: Adding actors requires a valid Actor pointer + } + + void testItemRemoveAllActors() + { + Item item(1, "test_item", 0); + + item.removeAllActors(); + QVERIFY(!item.hasActors()); + } + + void testItemSetActorsActive() + { + Item item(1, "test_item", 0); + + // Should not crash when no actors + item.setActorsActive(true); + item.setActorsActive(false); + } + + void testItemDataChangeDetection() + { + ItemData data1(1, "item1", 0); + ItemData data2(1, "item1", 1); + + // Test value change detection + ItemFieldChanges changes(true); + QVERIFY(data1.hasChanged(data2, changes)); + + // Test no change + changes = ItemFieldChanges(false); + QVERIFY(!data1.hasChanged(data2, changes)); + } + + void testItemDataNameChange() + { + ItemData data1(1, "item1", 0); + ItemData data2(1, "item2", 0); + + ItemFieldChanges changes; + changes.name = true; + + QVERIFY(data1.hasChanged(data2, changes)); + } + + void testItemDataGroupNameChange() + { + ItemData data1(1, "item1", 0); + data1.setGroupName("Group1"); + + ItemData data2(1, "item1", 0); + data2.setGroupName("Group2"); + + ItemFieldChanges changes; + changes.groupName = true; + + QVERIFY(data1.hasChanged(data2, changes)); + } + + void testItemFieldChanges() + { + ItemFieldChanges changes(false); + QVERIFY(changes.isNone()); + + changes.value = true; + QVERIFY(!changes.isNone()); + + ItemFieldChanges allChanges(true); + QVERIFY(!allChanges.isNone()); + } + + void cleanupTestCase() + { + // Cleanup after all tests + } +}; + +QTEST_APPLESS_MAIN(TestItem) + +#include "test_item.moc" diff --git a/tests/unit/items/test_itemloadersource.cpp b/tests/unit/items/test_itemloadersource.cpp new file mode 100644 index 0000000..156edc4 --- /dev/null +++ b/tests/unit/items/test_itemloadersource.cpp @@ -0,0 +1,240 @@ +#include +#include "items/itemloadersource.h" +#include "items/item.h" + +class TestItemLoaderSource : public QObject +{ + Q_OBJECT + +private slots: + void initTestCase() + { + // Setup for all tests + } + + void testItemLoaderSourceCreation() + { + ItemLoaderSource source; + + // Should be created with empty JSON + QVERIFY(true); // No crash + } + + void testItemLoaderSourceCreationWithJson() + { + QJsonObject json; + json["Items"] = QJsonArray(); + + ItemLoaderSource source(json); + + // Should be created with JSON + QVERIFY(true); // No crash + } + + void testItemLoaderSourceRefreshEmpty() + { + ItemLoaderSource source; + + // Should not crash with empty JSON + source.refresh(); + + // No items should be emitted + QVERIFY(true); + } + + void testItemLoaderSourceRefreshWithItems() + { + QJsonObject json; + QJsonArray itemsArray; + + QJsonObject item1; + item1["Type"] = "Item"; + item1["ItemId"] = 1; + item1["Name"] = "test_item1"; + item1["Value"] = 0; + itemsArray.append(item1); + + QJsonObject item2; + item2["Type"] = "Item"; + item2["ItemId"] = 2; + item2["Name"] = "test_item2"; + item2["Value"] = 1; + itemsArray.append(item2); + + json["Items"] = itemsArray; + + ItemLoaderSource source(json); + + // Capture the gotItems signal + std::vector capturedItems; + connect(&source, &ItemLoaderSource::gotItems, + [&capturedItems](std::vector items) { + capturedItems = items; + }); + + source.refresh(); + + // Should have 2 items + QVERIFY(capturedItems.size() == 2); + } + + void testItemLoaderSourceRefreshWithRelay() + { + QJsonObject json; + QJsonArray itemsArray; + + QJsonObject relay; + relay["Type"] = "Relay"; + relay["ItemId"] = 100; + relay["Name"] = "test_relay"; + relay["Value"] = 0; + relay["Id"] = 1; + relay["Address"] = 0x2000; + itemsArray.append(relay); + + json["Items"] = itemsArray; + + ItemLoaderSource source(json); + + // Capture the gotItems signal + std::vector capturedItems; + connect(&source, &ItemLoaderSource::gotItems, + [&capturedItems](std::vector items) { + capturedItems = items; + }); + + source.refresh(); + + // Should have 1 item + QVERIFY(capturedItems.size() == 1); + } + + void testItemLoaderSourceUpdateJson() + { + ItemLoaderSource source; + + // Initial JSON + QJsonObject json1; + QJsonArray items1; + QJsonObject item1; + item1["Type"] = "Item"; + item1["ItemId"] = 1; + item1["Name"] = "item1"; + items1.append(item1); + json1["Items"] = items1; + + source.updateJson(json1); + + // Update JSON + QJsonObject json2; + QJsonArray items2; + QJsonObject item2; + item2["Type"] = "Item"; + item2["ItemId"] = 2; + item2["Name"] = "item2"; + items2.append(item2); + json2["Items"] = items2; + + source.updateJson(json2); + + // Should not crash + QVERIFY(true); + } + + void testItemLoaderSourceRefreshWithInvalidItems() + { + QJsonObject json; + QJsonArray itemsArray; + + // Add invalid item (no Type) + QJsonObject item1; + item1["ItemId"] = 1; + itemsArray.append(item1); + + json["Items"] = itemsArray; + + ItemLoaderSource source(json); + + // Capture the gotItems signal + std::vector capturedItems; + connect(&source, &ItemLoaderSource::gotItems, + [&capturedItems](std::vector items) { + capturedItems = items; + }); + + source.refresh(); + + // Should handle invalid item gracefully (may return 0 items) + QVERIFY(capturedItems.size() == 0 || capturedItems.size() == 1); + } + + void testItemLoaderSourceRefreshWithMessageItem() + { + QJsonObject json; + QJsonArray itemsArray; + + QJsonObject messageItem; + messageItem["Type"] = "Message"; + messageItem["ItemId"] = 200; + messageItem["Name"] = "alert_item"; + messageItem["Value"] = 0; + messageItem["Message"] = "Test message"; + itemsArray.append(messageItem); + + json["Items"] = itemsArray; + + ItemLoaderSource source(json); + + // Capture the gotItems signal + std::vector capturedItems; + connect(&source, &ItemLoaderSource::gotItems, + [&capturedItems](std::vector items) { + capturedItems = items; + }); + + source.refresh(); + + // Should have 1 item + QVERIFY(capturedItems.size() == 1); + } + + void testItemLoaderSourceRefreshWithSystemItem() + { + QJsonObject json; + QJsonArray itemsArray; + + QJsonObject systemItem; + systemItem["Type"] = "System"; + systemItem["ItemId"] = 300; + systemItem["Name"] = "system_item"; + systemItem["Value"] = 0; + systemItem["OnCommand"] = "echo on"; + systemItem["OffCommand"] = "echo off"; + itemsArray.append(systemItem); + + json["Items"] = itemsArray; + + ItemLoaderSource source(json); + + // Capture the gotItems signal + std::vector capturedItems; + connect(&source, &ItemLoaderSource::gotItems, + [&capturedItems](std::vector items) { + capturedItems = items; + }); + + source.refresh(); + + // Should have 1 item + QVERIFY(capturedItems.size() == 1); + } + + void cleanupTestCase() + { + // Cleanup after all tests + } +}; + +QTEST_APPLESS_MAIN(TestItemLoaderSource) + +#include "test_itemloadersource.moc" \ No newline at end of file diff --git a/tests/unit/items/test_itemstore.cpp b/tests/unit/items/test_itemstore.cpp new file mode 100644 index 0000000..79d16c4 --- /dev/null +++ b/tests/unit/items/test_itemstore.cpp @@ -0,0 +1,255 @@ +#include +#include "items/itemstore.h" +#include "items/item.h" + +class TestItemStore : public QObject +{ + Q_OBJECT + +private slots: + void initTestCase() + { + // Setup for all tests + } + + void testItemStoreCreation() + { + ItemStore store; + + // Should start empty + QVERIFY(store.getItems()->empty()); + } + + void testItemStoreAddItem() + { + ItemStore store; + + std::shared_ptr item(new Item(1, "test_item", 0)); + + ItemAddRequest request; + request.type = ITEM_UPDATE_USER; + request.payload = item; + request.changes = ItemFieldChanges(true); + + store.addItem(request); + + // Item should be added + QVERIFY(store.getItems()->size() == 1); + } + + void testItemStoreAddMultipleItems() + { + ItemStore store; + + std::shared_ptr item1(new Item(1, "item1", 0)); + std::shared_ptr item2(new Item(2, "item2", 1)); + + std::vector requests; + + ItemAddRequest request1; + request1.type = ITEM_UPDATE_USER; + request1.payload = item1; + request1.changes = ItemFieldChanges(true); + requests.push_back(request1); + + ItemAddRequest request2; + request2.type = ITEM_UPDATE_USER; + request2.payload = item2; + request2.changes = ItemFieldChanges(true); + requests.push_back(request2); + + store.addItems(requests); + + // Both items should be added + QVERIFY(store.getItems()->size() == 2); + } + + void testItemStoreGetItem() + { + ItemStore store; + + std::shared_ptr item(new Item(42, "test_item", 0)); + + ItemAddRequest request; + request.type = ITEM_UPDATE_USER; + request.payload = item; + request.changes = ItemFieldChanges(true); + + store.addItem(request); + + // Get item by id + std::shared_ptr found = store.getItem(42); + QVERIFY(found != nullptr); + QVERIFY(found->getName() == "test_item"); + + // Get non-existent item + std::shared_ptr notFound = store.getItem(999); + QVERIFY(notFound == nullptr); + } + + void testItemStoreRemoveItem() + { + ItemStore store; + + std::shared_ptr item(new Item(1, "test_item", 0)); + + ItemAddRequest request; + request.type = ITEM_UPDATE_USER; + request.payload = item; + request.changes = ItemFieldChanges(true); + + store.addItem(request); + QVERIFY(store.getItems()->size() == 1); + + // Remove item + ItemData itemData(1, "test_item", 0); + store.removeItem(itemData); + + QVERIFY(store.getItems()->empty()); + } + + void testItemStoreClear() + { + ItemStore store; + + // Add multiple items + for (int i = 0; i < 5; i++) { + std::shared_ptr item(new Item(i, "item" + QString::number(i), 0)); + ItemAddRequest request; + request.type = ITEM_UPDATE_USER; + request.payload = item; + request.changes = ItemFieldChanges(true); + store.addItem(request); + } + + QVERIFY(store.getItems()->size() == 5); + + // Clear all items + store.clear(); + + QVERIFY(store.getItems()->empty()); + } + + void testItemStoreUpdateItem() + { + ItemStore store; + + std::shared_ptr item(new Item(1, "test_item", 0)); + + ItemAddRequest request; + request.type = ITEM_UPDATE_USER; + request.payload = item; + request.changes = ItemFieldChanges(true); + + store.addItem(request); + + // Update item value + ItemUpdateRequest update; + update.type = ITEM_UPDATE_USER; + ItemData updatedData(1, "test_item", 1); // value = 1 + update.payload = updatedData; + update.changes.value = true; + + store.updateItem(update); + + // Verify value was updated + std::shared_ptr found = store.getItem(1); + QVERIFY(found->getValue() == 1); + } + + void testItemStoreUpdateMultipleItems() + { + ItemStore store; + + // Add items + for (int i = 0; i < 3; i++) { + std::shared_ptr item(new Item(i, "item" + QString::number(i), 0)); + ItemAddRequest request; + request.type = ITEM_UPDATE_USER; + request.payload = item; + request.changes = ItemFieldChanges(true); + store.addItem(request); + } + + // Update multiple items + std::vector updates; + + for (int i = 0; i < 3; i++) { + ItemUpdateRequest update; + update.type = ITEM_UPDATE_USER; + ItemData data(i, "item" + QString::number(i), 1); + update.payload = data; + update.changes.value = true; + updates.push_back(update); + } + + store.updateItems(updates); + + // Verify all values were updated + for (int i = 0; i < 3; i++) { + std::shared_ptr found = store.getItem(i); + QVERIFY(found->getValue() == 1); + } + } + + void testItemStoreJsonSerialization() + { + ItemStore store; + + // Add items + for (int i = 0; i < 2; i++) { + std::shared_ptr item(new Item(i, "item" + QString::number(i), i)); + item->setGroupName("TestGroup"); + ItemAddRequest request; + request.type = ITEM_UPDATE_USER; + request.payload = item; + request.changes = ItemFieldChanges(true); + store.addItem(request); + } + + // Serialize to JSON + QJsonObject json; + store.store(json); + + // Verify JSON contains items array + QVERIFY(json.contains("Items")); + QVERIFY(json["Items"].toArray().size() == 2); + } + + void testItemStoreReplaceItems() + { + ItemStore store; + + // Add initial items + std::shared_ptr item1(new Item(1, "item1", 0)); + ItemAddRequest request1; + request1.type = ITEM_UPDATE_USER; + request1.payload = item1; + request1.changes = ItemFieldChanges(true); + store.addItem(request1); + + QVERIFY(store.getItems()->size() == 1); + + // Replace with new items + std::vector> newItems; + newItems.push_back(std::shared_ptr(new Item(2, "new_item1", 0))); + newItems.push_back(std::shared_ptr(new Item(3, "new_item2", 1))); + + store.replaceItems(newItems); + + // Should have new items + QVERIFY(store.getItems()->size() == 2); + + // Old item should be removed + QVERIFY(store.getItem(1) == nullptr); + } + + void cleanupTestCase() + { + // Cleanup after all tests + } +}; + +QTEST_APPLESS_MAIN(TestItemStore) + +#include "test_itemstore.moc" \ No newline at end of file diff --git a/tests/unit/sensors/test_sensor.cpp b/tests/unit/sensors/test_sensor.cpp new file mode 100644 index 0000000..7ff4505 --- /dev/null +++ b/tests/unit/sensors/test_sensor.cpp @@ -0,0 +1,230 @@ +#include +#include "sensors/sensor.h" + +class TestSensor : public QObject +{ + Q_OBJECT + +private slots: + void initTestCase() + { + // Setup for all tests + } + + void testSensorCreation() + { + Sensor sensor(Sensor::TYPE_TEMPERATURE, 1, 0.0, "test_sensor"); + + QCOMPARE(sensor.type, Sensor::TYPE_TEMPERATURE); + QCOMPARE(sensor.name, QString("test_sensor")); + } + + void testSensorData() + { + Sensor sensor(Sensor::TYPE_TEMPERATURE, 1, 0.0, "temp_sensor"); + + // Test setting and getting values + sensor.field = 25.0; + QCOMPARE(sensor.field, 25.0); + } + + void testSensorTypes() + { + // Test different sensor types + Sensor doorSensor(Sensor::TYPE_DOOR, 1); + Sensor tempSensor(Sensor::TYPE_TEMPERATURE, 2); + Sensor humiditySensor(Sensor::TYPE_HUMIDITY, 3); + + QCOMPARE(doorSensor.type, Sensor::TYPE_DOOR); + QCOMPARE(tempSensor.type, Sensor::TYPE_TEMPERATURE); + QCOMPARE(humiditySensor.type, Sensor::TYPE_HUMIDITY); + } + + void testSensorStringParsing() + { + // Test parsing sensor from string (format: "SENSOR TYPE: X ID: Y FIELD: Z TIME: T") + // This tests the sensorFromString function + QString sensorStr = "SENSOR TYPE: 1 ID: 5 FIELD: 250 TIME: 1234567890"; + Sensor sensor = Sensor::sensorFromString(sensorStr); + + // The function should parse the type, id, and field + // Note: temperature and humidity values are divided by 10 + QVERIFY(sensor.type != Sensor::TYPE_DUMMY || sensor.field != 0); + } + + void testSensorStringParsingInvalid() + { + // Test parsing invalid string returns dummy sensor + QString invalidStr = "invalid data"; + Sensor sensor = Sensor::sensorFromString(invalidStr); + + // Should return a dummy sensor with hidden=true + QVERIFY(sensor.hidden); + } + + void testSensorToString() + { + Sensor sensor(Sensor::TYPE_TEMPERATURE, 1, 25.5, "test_sensor"); + QString str = sensor.toString(); + + // Should contain type, id, and field info + QVERIFY(str.contains("SENSOR TYPE")); + QVERIFY(str.contains("ID:")); + QVERIFY(str.contains("FIELD:")); + } + + void testSensorJsonSerialization() + { + Sensor sensor(Sensor::TYPE_TEMPERATURE, 1, 25.0, "test_sensor"); + + QJsonObject json; + sensor.store(json); + + // Verify JSON contents + QVERIFY(json.contains("Type")); + QVERIFY(json.contains("SensorType")); + QVERIFY(json.contains("Id")); + QVERIFY(json.contains("Field")); + QVERIFY(json.contains("Name")); + QVERIFY(json.contains("Unit")); + + // Check values + QVERIFY(json["Type"].toString() == "Sensor"); + QVERIFY(json["SensorType"].toInt() == Sensor::TYPE_TEMPERATURE); + QVERIFY(json["Id"].toInt() == 1); + QVERIFY(json["Field"].toDouble() == 25.0); + QVERIFY(json["Name"].toString() == "test_sensor"); + } + + void testSensorJsonDeserialization() + { + QJsonObject json; + json["SensorType"] = Sensor::TYPE_HUMIDITY; + json["Id"] = 5; + json["Field"] = 60.5; + json["Name"] = "humidity_sensor"; + json["Hidden"] = false; + + Sensor sensor(json); + + QVERIFY(sensor.type == Sensor::TYPE_HUMIDITY); + QVERIFY(sensor.id == 5); + QVERIFY(sensor.field == 60.5); + QVERIFY(sensor.name == "humidity_sensor"); + QVERIFY(!sensor.hidden); + } + + void testSensorEquality() + { + Sensor sensor1(Sensor::TYPE_TEMPERATURE, 1, 25.0); + Sensor sensor2(Sensor::TYPE_TEMPERATURE, 1, 30.0); + Sensor sensor3(Sensor::TYPE_TEMPERATURE, 2, 25.0); + + // Same type and id means equal + QVERIFY(sensor1 == sensor2); + + // Different id means not equal + QVERIFY(sensor1 != sensor3); + } + + void testSensorUpdateSeen() + { + Sensor sensor(Sensor::TYPE_TEMPERATURE, 1, 25.0); + QDateTime before = sensor.lastSeen; + + // Wait a tiny bit and update + sensor.updateSeen(); + + QVERIFY(sensor.lastSeen >= before); + } + + void testSensorGenerateName() + { + // Test auto-generated names for different sensor types + Sensor tempSensor(Sensor::TYPE_TEMPERATURE, 1); + QVERIFY(tempSensor.name.contains("Temperature")); + + Sensor doorSensor(Sensor::TYPE_DOOR, 2); + QVERIFY(doorSensor.name.contains("Door")); + + Sensor buttonSensor(Sensor::TYPE_BUTTON, 3); + QVERIFY(buttonSensor.name.contains("Button")); + + Sensor humiditySensor(Sensor::TYPE_HUMIDITY, 4); + QVERIFY(humiditySensor.name.contains("Humidity")); + } + + void testSensorGetUnit() + { + Sensor tempSensor(Sensor::TYPE_TEMPERATURE, 1); + QVERIFY(tempSensor.getUnit() == "°C"); + + Sensor humiditySensor(Sensor::TYPE_HUMIDITY, 1); + QVERIFY(humiditySensor.getUnit() == "%"); + + Sensor pressureSensor(Sensor::TYPE_PRESSURE, 1); + QVERIFY(pressureSensor.getUnit() == "hPa"); + + Sensor brightnessSensor(Sensor::TYPE_BRIGHTNESS, 1); + QVERIFY(brightnessSensor.getUnit() == "lx"); + + Sensor co2Sensor(Sensor::TYPE_CO2, 1); + QVERIFY(co2Sensor.getUnit() == "ppm"); + + Sensor vocSensor(Sensor::TYPE_TOTAL_VOC, 1); + QVERIFY(vocSensor.getUnit() == "ppb"); + } + + void testSensorHidden() + { + Sensor sensor(Sensor::TYPE_TEMPERATURE, 1, 25.0, "test", false); + QVERIFY(!sensor.hidden); + + Sensor hiddenSensor(Sensor::TYPE_TEMPERATURE, 2, 25.0, "test", true); + QVERIFY(hiddenSensor.hidden); + } + + void testSensorAllTypes() + { + // Test all sensor types can be created + Sensor door(Sensor::TYPE_DOOR, 1); + Sensor temp(Sensor::TYPE_TEMPERATURE, 1); + Sensor humidity(Sensor::TYPE_HUMIDITY, 1); + Sensor pressure(Sensor::TYPE_PRESSURE, 1); + Sensor brightness(Sensor::TYPE_BRIGHTNESS, 1); + Sensor button(Sensor::TYPE_BUTTON, 1); + Sensor adc(Sensor::TYPE_ADC, 1); + Sensor co2(Sensor::TYPE_CO2, 1); + Sensor pm25(Sensor::TYPE_PM25, 1); + Sensor voc(Sensor::TYPE_TOTAL_VOC, 1); + Sensor lowBattery(Sensor::TYPE_LOWBATTERY, 1); + Sensor occupancy(Sensor::TYPE_OCUPANCY, 1); + Sensor sunAlt(Sensor::TYPE_SUN_ALTITUDE, 1); + Sensor audio(Sensor::TYPE_AUDIO_OUTPUT, 1); + + // All should be valid + QVERIFY(door.type == Sensor::TYPE_DOOR); + QVERIFY(temp.type == Sensor::TYPE_TEMPERATURE); + QVERIFY(humidity.type == Sensor::TYPE_HUMIDITY); + QVERIFY(pressure.type == Sensor::TYPE_PRESSURE); + QVERIFY(brightness.type == Sensor::TYPE_BRIGHTNESS); + QVERIFY(button.type == Sensor::TYPE_BUTTON); + QVERIFY(adc.type == Sensor::TYPE_ADC); + QVERIFY(co2.type == Sensor::TYPE_CO2); + QVERIFY(pm25.type == Sensor::TYPE_PM25); + QVERIFY(voc.type == Sensor::TYPE_TOTAL_VOC); + QVERIFY(lowBattery.type == Sensor::TYPE_LOWBATTERY); + QVERIFY(occupancy.type == Sensor::TYPE_OCUPANCY); + QVERIFY(sunAlt.type == Sensor::TYPE_SUN_ALTITUDE); + QVERIFY(audio.type == Sensor::TYPE_AUDIO_OUTPUT); + } + + void cleanupTestCase() + { + // Cleanup after all tests + } +}; + +QTEST_APPLESS_MAIN(TestSensor) + +#include "test_sensor.moc" From 0fd50eb227f93306cd0bf9769919797bce0fbba1 Mon Sep 17 00:00:00 2001 From: Carl Philipp Klemm Date: Thu, 9 Apr 2026 16:52:58 +0200 Subject: [PATCH 08/21] Add tests for the tcp server/client --- src/service/tcpserver.h | 2 + tests/CMakeLists.txt | 28 +++ tests/unit/service/test_tcp.cpp | 343 ++++++++++++++++++++++++++++++++ 3 files changed, 373 insertions(+) create mode 100644 tests/unit/service/test_tcp.cpp diff --git a/src/service/tcpserver.h b/src/service/tcpserver.h index f6c6056..069bb35 100644 --- a/src/service/tcpserver.h +++ b/src/service/tcpserver.h @@ -16,6 +16,8 @@ public: TcpServer(QObject* parent = nullptr); virtual bool launch(const QHostAddress &address = QHostAddress::Any, quint16 port = 0) override; virtual void sendJson(const QJsonObject& json) override; + quint16 getServerPort() const { return server.serverPort(); } + bool isListening() const { return server.isListening(); } signals: void sigRequestSave(); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 6beb2eb..2b02715 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -56,6 +56,16 @@ add_executable(test_sensor unit/sensors/test_sensor.cpp ${COMMON_TEST_SOURCES}) add_executable(test_actor unit/actors/test_actor.cpp ${COMMON_TEST_SOURCES}) add_executable(test_itemstore unit/items/test_itemstore.cpp ${COMMON_TEST_SOURCES}) add_executable(test_itemloadersource unit/items/test_itemloadersource.cpp ${COMMON_TEST_SOURCES}) +add_executable(test_tcp unit/service/test_tcp.cpp ${COMMON_TEST_SOURCES} + ../src/service/service.h + ../src/service/service.cpp + ../src/service/server.h + ../src/service/server.cpp + ../src/service/tcpserver.h + ../src/service/tcpserver.cpp + ../src/service/tcpclient.h + ../src/service/tcpclient.cpp +) # Link libraries for test_item target_link_libraries(test_item @@ -132,9 +142,27 @@ target_include_directories(test_itemloadersource PRIVATE ${Qt6Gui_PRIVATE_INCLUDE_DIRS} ) +# Link libraries for test_tcp +target_link_libraries(test_tcp + Qt6::Core + Qt6::Gui + Qt6::Widgets + Qt6::Multimedia + Qt6::Network + Qt6::WebSockets + Qt6::Test +) + +# Include paths for source files +target_include_directories(test_tcp PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/../src + ${Qt6Gui_PRIVATE_INCLUDE_DIRS} +) + # Add tests to CTest add_test(NAME test_item COMMAND test_item) add_test(NAME test_sensor COMMAND test_sensor) add_test(NAME test_actor COMMAND test_actor) add_test(NAME test_itemstore COMMAND test_itemstore) add_test(NAME test_itemloadersource COMMAND test_itemloadersource) +add_test(NAME test_tcp COMMAND test_tcp) diff --git a/tests/unit/service/test_tcp.cpp b/tests/unit/service/test_tcp.cpp new file mode 100644 index 0000000..b1390c7 --- /dev/null +++ b/tests/unit/service/test_tcp.cpp @@ -0,0 +1,343 @@ +#include +#include +#include +#include +#include +#include + +#include "service/tcpserver.h" +#include "service/tcpclient.h" +#include "service/server.h" +#include "service/service.h" +#include "items/item.h" + +class TestTcp : public QObject +{ + Q_OBJECT + +private slots: + void initTestCase() + { + // Setup for all tests + } + + void testTcpServerCreation() + { + TcpServer server; + + // Server should be created successfully + QVERIFY(true); + } + + void testTcpServerLaunch() + { + TcpServer server; + + // Launch server on any address, port 0 means dynamic port + bool result = server.launch(QHostAddress::Any, 0); + + QVERIFY(result); + + // Server should be listening after launch + QVERIFY(server.isListening()); + } + + void testTcpServerLaunchSpecificPort() + { + TcpServer server; + + // Try to launch on a specific port + bool result = server.launch(QHostAddress::LocalHost, 0); + + QVERIFY(result); + + // Server should be listening + QVERIFY(server.isListening()); + } + + void testTcpClientCreation() + { + TcpClient client; + + // Client should be created successfully + QVERIFY(true); + } + + void testTcpProtocolMessageFormat() + { + // Test the protocol message format: "MSG JSON LEN \n" + QJsonObject json; + json["MessageType"] = "TestMessage"; + json["Data"] = QJsonArray(); + + QByteArray jsonData = QJsonDocument(json).toJson(); + QString message = QString("MSG JSON LEN %1\n").arg(jsonData.size()) + QString::fromUtf8(jsonData); + + // Verify message format + QVERIFY(message.startsWith("MSG JSON LEN ")); + QVERIFY(message.contains("\n")); + + // Parse the message size + QStringList parts = message.split("\n"); + QVERIFY(parts.size() >= 1); + + QString sizeStr = parts[0].mid(13); // Skip "MSG JSON LEN " + bool ok; + quint64 size = sizeStr.toUInt(&ok); + QVERIFY(ok); + QVERIFY(size == jsonData.size()); + } + + void testTcpProtocolParseCommand() + { + // Test parsing a command from the protocol + QByteArray command = "MSG JSON LEN 123\n"; + + QVERIFY(command.startsWith("MSG JSON LEN ")); + + QByteArray sizeStr = command.mid(13); + sizeStr.chop(1); // Remove newline + + bool ok; + quint64 size = sizeStr.toLongLong(&ok); + QVERIFY(ok); + QVERIFY(size == 123); + } + + void testTcpServerSendJson() + { + // Start server + TcpServer server; + bool serverResult = server.launch(QHostAddress::LocalHost, 0); + QVERIFY(serverResult); + + // Connect a client to trigger client list + QTcpSocket clientSocket; + clientSocket.connectToHost(QHostAddress::LocalHost, server.getServerPort()); + QVERIFY(clientSocket.waitForConnected(1000)); + + // Give server time to accept connection + QTest::qWait(200); + + // Send JSON from server + QJsonObject json; + json["MessageType"] = "TestMessage"; + json["Data"] = QJsonArray(); + + server.sendJson(json); + + // Give more time for data to be sent + QTest::qWait(200); + + // Client should receive data (or at least the connection should work) + // Note: This may fail due to timing in test environment + QVERIFY(clientSocket.state() == QTcpSocket::ConnectedState); + + // Cleanup + clientSocket.close(); + } + + void testTcpServerProcessIncomingJson() + { + // Start server + TcpServer server; + bool serverResult = server.launch(QHostAddress::LocalHost, 0); + QVERIFY(serverResult); + + // Connect client + QTcpSocket clientSocket; + clientSocket.connectToHost(QHostAddress::LocalHost, server.getServerPort()); + QVERIFY(clientSocket.waitForConnected(1000)); + + // Give server time to accept connection + QTest::qWait(100); + + // Send a message that the server can process + QJsonObject json; + json["MessageType"] = "GetItems"; + json["Data"] = QJsonArray(); + + QByteArray jsonData = QJsonDocument(json).toJson(); + QString message = QString("MSG JSON LEN %1\n").arg(jsonData.size()) + QString::fromUtf8(jsonData); + + clientSocket.write(message.toUtf8()); + clientSocket.flush(); + + // Give server time to process + QTest::qWait(100); + + // Cleanup + clientSocket.close(); + } + + void testTcpServerMultipleClients() + { + // Start server + TcpServer server; + bool serverResult = server.launch(QHostAddress::LocalHost, 0); + QVERIFY(serverResult); + + // Connect multiple clients + QTcpSocket client1; + client1.connectToHost(QHostAddress::LocalHost, server.getServerPort()); + QVERIFY(client1.waitForConnected(1000)); + + QTcpSocket client2; + client2.connectToHost(QHostAddress::LocalHost, server.getServerPort()); + QVERIFY(client2.waitForConnected(1000)); + + // Give server time to accept connections + QTest::qWait(200); + + // Send message to all clients + QJsonObject json; + json["MessageType"] = "TestMessage"; + json["Data"] = QJsonArray(); + + server.sendJson(json); + + // Give time for data to be sent + QTest::qWait(200); + + // Both clients should be connected (timing may affect actual data receipt) + QVERIFY(client1.state() == QTcpSocket::ConnectedState); + QVERIFY(client2.state() == QTcpSocket::ConnectedState); + + // Cleanup + client1.close(); + client2.close(); + } + + void testItemUpdateMessageFormat() + { + // Test creating an item update message format + Item item(1, "test_item", 1); + + QJsonObject itemJson; + item.store(itemJson); + + QJsonArray items; + items.append(itemJson); + + // Manually create the message (since createMessage is protected) + QJsonObject message; + message["MessageType"] = "ItemUpdate"; + message["Data"] = items; + message["FullList"] = false; + + QVERIFY(message["MessageType"].toString() == "ItemUpdate"); + QVERIFY(message["Data"].toArray().size() == 1); + QVERIFY(message["FullList"].toBool() == false); + } + + void testSensorUpdateMessageFormat() + { + // Test creating a sensor update message format + Sensor sensor(Sensor::TYPE_TEMPERATURE, 1, 25.0, "temp_sensor"); + + QJsonObject sensorJson; + sensor.store(sensorJson); + + QJsonArray sensors; + sensors.append(sensorJson); + + // Manually create the message (since createMessage is protected) + QJsonObject message; + message["MessageType"] = "SensorUpdate"; + message["Data"] = sensors; + + QVERIFY(message["MessageType"].toString() == "SensorUpdate"); + QVERIFY(message["Data"].toArray().size() == 1); + } + + void testTcpClientSocketState() + { + // Test that TcpClient creates a socket + TcpClient client; + + // The client should have a valid socket (internal implementation detail) + // We can verify the client was created successfully + QVERIFY(true); + } + + void testTcpServerClientConnection() + { + // Start server + TcpServer server; + bool serverResult = server.launch(QHostAddress::LocalHost, 0); + QVERIFY(serverResult); + + // Connect a client + QTcpSocket clientSocket; + clientSocket.connectToHost(QHostAddress::LocalHost, server.getServerPort()); + + bool waitResult = clientSocket.waitForConnected(1000); + QVERIFY(waitResult); + + // Verify connection state + QVERIFY(clientSocket.state() == QTcpSocket::ConnectedState); + + // Cleanup + clientSocket.close(); + } + + void testTcpServerDisconnection() + { + // Start server + TcpServer server; + bool serverResult = server.launch(QHostAddress::LocalHost, 0); + QVERIFY(serverResult); + + // Connect a client + QTcpSocket* client = new QTcpSocket(); + client->connectToHost(QHostAddress::LocalHost, server.getServerPort()); + QVERIFY(client->waitForConnected(1000)); + + // Give server time to accept connection + QTest::qWait(100); + + // Disconnect client + client->disconnectFromHost(); + QTest::qWait(100); + + // Cleanup + delete client; + } + + void testTcpProtocolLargeMessage() + { + // Test the protocol with a larger message + QJsonObject json; + json["MessageType"] = "TestMessage"; + + // Create a large data array + QJsonArray data; + for (int i = 0; i < 100; i++) { + data.append(QJsonObject{{"id", i}, {"value", QString("item%1").arg(i)}}); + } + json["Data"] = data; + + QByteArray jsonData = QJsonDocument(json).toJson(); + QString message = QString("MSG JSON LEN %1\n").arg(jsonData.size()) + QString::fromUtf8(jsonData); + + // Verify message format + QVERIFY(message.startsWith("MSG JSON LEN ")); + + // Parse the message size + QStringList parts = message.split("\n"); + QString sizeStr = parts[0].mid(13); + bool ok; + quint64 size = sizeStr.toUInt(&ok); + QVERIFY(ok); + QVERIFY(size == jsonData.size()); + } + + void cleanupTestCase() + { + // Cleanup after all tests + } +}; + +QTEST_APPLESS_MAIN(TestTcp) + +#include "test_tcp.moc" \ No newline at end of file From eb60f856049621a903d525acbfb5fb46942073d5 Mon Sep 17 00:00:00 2001 From: Carl Philipp Klemm Date: Sun, 12 Apr 2026 18:06:19 +0200 Subject: [PATCH 09/21] Add Mqtt item --- CMakeLists.txt | 9 +- src/items/item.cpp | 3 + src/items/mqttitem.cpp | 164 ++++++++++++++ src/items/mqttitem.h | 53 +++++ src/items/mqttitemsource.cpp | 205 ++++++++++++++++++ src/items/mqttitemsource.h | 34 +++ src/mainobject.cpp | 9 +- src/mainobject.h | 1 + src/mqttclient.cpp | 110 ++++++++++ src/mqttclient.h | 40 ++++ src/sensors/mqttsensorsource.cpp | 61 ++---- src/sensors/mqttsensorsource.h | 10 +- src/ui/itemcreationdialog.cpp | 9 + src/ui/itemcreationdialog.ui | 24 +- src/ui/itemsettingsdialog.cpp | 6 + .../mqttitemsettingswidget.cpp | 62 ++++++ .../mqttitemsettingswidget.h | 32 +++ .../mqttitemsettingswidget.ui | 108 +++++++++ tests/CMakeLists.txt | 9 + 19 files changed, 901 insertions(+), 48 deletions(-) create mode 100644 src/items/mqttitem.cpp create mode 100644 src/items/mqttitem.h create mode 100644 src/items/mqttitemsource.cpp create mode 100644 src/items/mqttitemsource.h create mode 100644 src/mqttclient.cpp create mode 100644 src/mqttclient.h create mode 100644 src/ui/itemsettingswidgets/mqttitemsettingswidget.cpp create mode 100644 src/ui/itemsettingswidgets/mqttitemsettingswidget.h create mode 100644 src/ui/itemsettingswidgets/mqttitemsettingswidget.ui diff --git a/CMakeLists.txt b/CMakeLists.txt index 39c1e2e..ca60d65 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,7 +32,11 @@ add_subdirectory(tests) # Create executable add_executable(SHinterface - src/sensors/mqttsensorsource.h src/sensors/mqttsensorsource.cpp) + src/sensors/mqttsensorsource.h src/sensors/mqttsensorsource.cpp + src/items/mqttitem.h src/items/mqttitem.cpp + src/mqttclient.h src/mqttclient.cpp + +) # Add sources to executable target_sources(SHinterface @@ -139,6 +143,8 @@ target_sources(SHinterface src/ui/itemsettingswidgets/relayitemsettingswidget.cpp src/ui/itemsettingswidgets/systemitemsettingswidget.h src/ui/itemsettingswidgets/systemitemsettingswidget.cpp + src/ui/itemsettingswidgets/mqttitemsettingswidget.h + src/ui/itemsettingswidgets/mqttitemsettingswidget.cpp ) # Add UI files @@ -159,6 +165,7 @@ target_sources(SHinterface src/ui/itemsettingswidgets/messageitemsettingswidget.ui src/ui/itemsettingswidgets/relayitemsettingswidget.ui src/ui/itemsettingswidgets/systemitemsettingswidget.ui + src/ui/itemsettingswidgets/mqttitemsettingswidget.ui ) # Add resource file diff --git a/src/items/item.cpp b/src/items/item.cpp index 6f391c5..b902c32 100644 --- a/src/items/item.cpp +++ b/src/items/item.cpp @@ -10,6 +10,7 @@ #include "auxitem.h" #include "poweritem.h" #include "rgbitem.h" +#include "mqttitem.h" #include @@ -338,6 +339,8 @@ std::shared_ptr Item::loadItem(const QJsonObject& json) newItem = std::shared_ptr(new PowerItem); else if(json["Type"].toString("") == "Rgb") newItem = std::shared_ptr(new RgbItem); + else if(json["Type"].toString("") == "Mqtt") + newItem = std::shared_ptr(new MqttItem); else qWarning()<<"Unable to load unkown item type: "< +#include +#include + +#include "mqttclient.h" + +MqttItem::MqttItem(QString name, uint8_t value, QObject *parent) + : Item(0, name, value, parent), + topic_(""), + valueKey_("state"), + valueOn_("ON"), + valueOff_("OFF") +{ + hashId(); + std::shared_ptr workClient = client.lock(); + assert(workClient); + + connect(workClient->getClient().get(), &QMqttClient::stateChanged, this, &MqttItem::onClientStateChanged); +} + +MqttItem::~MqttItem() +{ + qDebug()<<__func__; + std::shared_ptr workClient = client.lock(); + if(!workClient || topic_.isEmpty() || !subscription) + return; + + workClient->unsubscribe(subscription); +} + +void MqttItem::onClientStateChanged(QMqttClient::ClientState state) +{ + if(state == QMqttClient::Connected) + refreshSubscription(); +} + +void MqttItem::refreshSubscription() +{ + std::shared_ptr workClient = client.lock(); + if(!workClient || topic_.isEmpty()) + return; + + if(workClient->getClient()->state() != QMqttClient::Connected) + return; + + if(subscription) + { + disconnect(subscription->subscription, &QMqttSubscription::messageReceived, this, &MqttItem::onMessageReceived); + workClient->unsubscribe(subscription); + } + + subscription = workClient->subscribe(workClient->getBaseTopic() + "/" + getTopic()); + connect(subscription->subscription, &QMqttSubscription::messageReceived, this, &MqttItem::onMessageReceived); +} + +void MqttItem::onMessageReceived(const QMqttMessage& message) +{ + QJsonDocument doc = QJsonDocument::fromJson(message.payload()); + if(doc.isObject()) + { + QJsonObject obj = doc.object(); + if(obj.contains(getValueKey())) + { + QString value = obj[getValueKey()].toString(); + ItemUpdateRequest req = createValueUpdateRequest(ITEM_UPDATE_BACKEND); + req.changes.value = true; + if(value == getValueOn()) + req.payload.setValueData(true); + else + req.payload.setValueData(false); + requestUpdate(req); + } + } +} + +void MqttItem::hashId() +{ + QString hashString = topic_ + "/" + valueKey_; + itemId_ = qHash(hashString.toLatin1()); +} + +void MqttItem::setTopic(const QString& topic) +{ + topic_ = topic; + hashId(); + refreshSubscription(); +} + +void MqttItem::setValueKey(const QString& valueKey) +{ + valueKey_ = valueKey; + hashId(); +} + +void MqttItem::setValueOn(const QString& valueOn) +{ + valueOn_ = valueOn; +} + +void MqttItem::setValueOff(const QString& valueOff) +{ + valueOff_ = valueOff; +} + +QString MqttItem::getTopic() const +{ + return topic_; +} + +QString MqttItem::getValueKey() const +{ + return valueKey_; +} + +QString MqttItem::getValueOn() const +{ + return valueOn_; +} + +QString MqttItem::getValueOff() const +{ + return valueOff_; +} + +void MqttItem::store(QJsonObject& json) +{ + Item::store(json); + json["Type"] = "Mqtt"; + json["Topic"] = topic_; + json["ValueKey"] = valueKey_; + json["ValueOn"] = valueOn_; + json["ValueOff"] = valueOff_; +} + +void MqttItem::load(const QJsonObject& json, const bool preserve) +{ + Item::load(json, preserve); + topic_ = json["Topic"].toString(); + valueKey_ = json["ValueKey"].toString("state"); + valueOn_ = json["ValueOn"].toString("ON"); + valueOff_ = json["ValueOff"].toString("OFF"); + hashId(); + refreshSubscription(); +} + +void MqttItem::enactValue(uint8_t value) +{ + std::shared_ptr workClient = client.lock(); + if(!workClient || topic_.isEmpty()) + return; + + QString fullTopic = workClient->getBaseTopic() + "/" + topic_ + "/set"; + QJsonObject payload; + + payload[valueKey_] = value ? valueOn_ : valueOff_; + + QJsonDocument doc(payload); + QByteArray data = doc.toJson(QJsonDocument::Compact); + + qDebug() << "MqttItem publishing to" << fullTopic << ":" << data; + workClient->getClient()->publish(fullTopic, data); +} diff --git a/src/items/mqttitem.h b/src/items/mqttitem.h new file mode 100644 index 0000000..c092444 --- /dev/null +++ b/src/items/mqttitem.h @@ -0,0 +1,53 @@ +#ifndef MQTTITEM_H +#define MQTTITEM_H + +#include "item.h" +#include "mqttclient.h" + +class QString; + +class MqttItem : public Item +{ + Q_OBJECT +public: + inline static std::weak_ptr client; + +private: + QString topic_; + QString valueKey_; + QString valueOn_; + QString valueOff_; + + MqttClient::Subscription* subscription = nullptr; + + void hashId(); + void refreshSubscription(); + void onMessageReceived(const QMqttMessage& message); + void onClientStateChanged(QMqttClient::ClientState state); + +public: + explicit MqttItem(QString name = "MqttItem", + uint8_t value = 0, + QObject *parent = nullptr); + virtual ~MqttItem() override; + + void setTopic(const QString& topic); + void setValueKey(const QString& valueKey); + void setBaseTopic(const QString& baseTopic); + void setValueOn(const QString& valueOn); + void setValueOff(const QString& valueOff); + + QString getTopic() const; + QString getValueKey() const; + QString getBaseTopic() const; + QString getValueOn() const; + QString getValueOff() const; + + virtual void store(QJsonObject& json) override; + virtual void load(const QJsonObject& json, const bool preserve = false) override; + +protected: + virtual void enactValue(uint8_t value) override; +}; + +#endif // MQTTITEM_H \ No newline at end of file diff --git a/src/items/mqttitemsource.cpp b/src/items/mqttitemsource.cpp new file mode 100644 index 0000000..eb3c11c --- /dev/null +++ b/src/items/mqttitemsource.cpp @@ -0,0 +1,205 @@ +#include "mqttitemsource.h" + +#include +#include +#include + +MqttItemSource::MqttItemSource(QObject *parent) + : ItemSource(parent) +{ +} + +MqttItemSource::~MqttItemSource() +{ +} + +void MqttItemSource::start(const QJsonObject& settings) +{ + baseTopicName_ = settings["BaseTopic"].toString("zigbee2mqtt"); + + connect(&client_, &QMqttClient::stateChanged, this, &MqttItemSource::onClientStateChanged); + connect(&client_, &QMqttClient::errorChanged, this, &MqttItemSource::onClientError); + + client_.setHostname(settings["Host"].toString("127.0.0.1")); + client_.setPort(settings["Port"].toInt(1883)); + if(settings.contains("User")) + client_.setUsername(settings["User"].toString()); + if(settings.contains("Password")) + client_.setPassword(settings["Password"].toString()); + client_.setProtocolVersion(QMqttClient::MQTT_5_0); + + client_.connectToHost(); + + QJsonArray itemsArray = settings["Items"].toArray(); + + for(QJsonValueRef itemRef : itemsArray) + { + QJsonObject itemObject = itemRef.toObject(); + if(!itemObject.contains("Topic")) + continue; + + MqttItemConfig config; + config.topic = itemObject["Topic"].toString(); + config.name = itemObject.contains("Name") ? itemObject["Name"].toString() : config.topic; + config.valueKey = itemObject["ValueKey"].toString("state"); + config.valueOn = itemObject["ValueOn"].toString("ON"); + config.valueOff = itemObject["ValueOff"].toString("OFF"); + + // Determine value type + QString valueTypeStr = itemObject["ValueType"].toString("bool"); + if(valueTypeStr == "uint") + config.valueType = ITEM_VALUE_UINT; + else + config.valueType = ITEM_VALUE_BOOL; + + config.id = qHash(baseTopicName_ + "/" + config.topic); + + // Create the item + config.item = std::make_shared(config.id, config.name, 0); + config.item->setTopic(config.topic); + config.item->setValueKey(config.valueKey); + config.item->setBaseTopic(baseTopicName_); + config.item->setValueOn(config.valueOn); + config.item->setValueOff(config.valueOff); + config.item->setMqttClient(&client_); + + items_.push_back(config); + } +} + +MqttItemSource::MqttItemConfig* MqttItemSource::findConfig(const QString& topic) +{ + for(MqttItemConfig& config : items_) + { + if(baseTopicName_ + "/" + config.topic == topic) + return &config; + } + return nullptr; +} + +void MqttItemSource::onClientError(QMqttClient::ClientError error) +{ + qWarning() << "MqttItemSource Client error:" << error; +} + +void MqttItemSource::onClientStateChanged(QMqttClient::ClientState state) +{ + if(state == QMqttClient::ClientState::Connected) + { + qInfo() << "MqttItemSource connected to MQTT broker at " << client_.hostname() << client_.port(); + for(MqttItemConfig& config : items_) + { + // Subscribe to state topic to receive updates from devices + QString stateTopic = baseTopicName_ + "/" + config.topic; + qDebug() << "MqttItemSource subscribing to" << stateTopic; + QMqttSubscription* subscription = client_.subscribe(stateTopic); + if(subscription) + { + connect(subscription, &QMqttSubscription::messageReceived, this, &MqttItemSource::onMessageReceived); + } + } + } + else if(state == QMqttClient::ClientState::Disconnected) + { + qWarning() << "MqttItemSource lost connection to MQTT broker"; + } + else if(state == QMqttClient::ClientState::Connecting) + { + qInfo() << "MqttItemSource connecting to MQTT broker at " << client_.hostname() << client_.port(); + } +} + +void MqttItemSource::onMessageReceived(const QMqttMessage& message) +{ + MqttItemConfig* config = findConfig(message.topic().name()); + if(!config) + return; + + QJsonDocument doc = QJsonDocument::fromJson(message.payload()); + if(doc.isObject()) + { + QJsonObject obj = doc.object(); + QString valueKey = config->valueKey; + + if(obj.contains(valueKey)) + { + uint8_t newValue = 0; + + // Handle different value types + if(config->valueType == ITEM_VALUE_UINT) + { + // Numeric value (brightness, etc.) + newValue = obj[valueKey].toInt(0); + } + else + { + // Binary value (state on/off, etc.) + QString value = obj[valueKey].toString(); + if(value == config->valueOn || value == "ON" || value == "true") + newValue = 1; + else if(value == config->valueOff || value == "OFF" || value == "false") + newValue = 0; + } + + // Only update if value changed + if(config->item->getValue() != newValue) + { + ItemUpdateRequest update; + update.type = ITEM_UPDATE_BACKEND; + update.payload = *config->item; + update.payload.setValueData(newValue); + update.changes.value = true; + config->item->requestUpdate(update); + } + } + } +} + +void MqttItemSource::refresh() +{ + std::vector requests; + + for(MqttItemConfig& config : items_) + { + ItemAddRequest request; + request.type = ITEM_UPDATE_BACKEND; + request.payload = config.item; + request.changes = ItemFieldChanges(true); + requests.push_back(request); + } + + gotItems(requests); +} + +void MqttItemSource::store(QJsonObject& json) +{ + json["Host"] = client_.hostname(); + json["Port"] = client_.port(); + json["BaseTopic"] = baseTopicName_; + if(client_.username() != "") + json["User"] = client_.username(); + if(client_.password() != "") + json["Password"] = client_.password(); + + QJsonArray itemsArray; + for(const MqttItemConfig& config : items_) + { + QJsonObject itemObject; + itemObject["Name"] = config.name; + itemObject["Topic"] = config.topic; + itemObject["ValueKey"] = config.valueKey; + itemObject["ValueOn"] = config.valueOn; + itemObject["ValueOff"] = config.valueOff; + if(config.valueType == ITEM_VALUE_UINT) + itemObject["ValueType"] = "uint"; + else + itemObject["ValueType"] = "bool"; + itemsArray.append(itemObject); + } + json["Items"] = itemsArray; +} + +QMqttClient* MqttItemSource::getClient() +{ + return &client_; +} \ No newline at end of file diff --git a/src/items/mqttitemsource.h b/src/items/mqttitemsource.h new file mode 100644 index 0000000..080e8a8 --- /dev/null +++ b/src/items/mqttitemsource.h @@ -0,0 +1,34 @@ +#ifndef MQTTITEMSOURCE_H +#define MQTTITEMSOURCE_H + +#include +#include +#include +#include +#include + +#include "itemsource.h" +#include "mqttitem.h" + +class MqttItemSource : public ItemSource +{ + Q_OBJECT + + QString baseTopicName_; + QMqttClient client_; + +private slots: + void onClientStateChanged(QMqttClient::ClientState state); + void onMessageReceived(const QMqttMessage& message); + void onClientError(QMqttClient::ClientError error); + +public: + explicit MqttItemSource(QObject *parent = nullptr); + virtual ~MqttItemSource() override; + virtual void refresh() override; + void start(const QJsonObject& settings); + void store(QJsonObject& json); + QMqttClient* getClient(); +}; + +#endif // MQTTITEMSOURCE_H \ No newline at end of file diff --git a/src/mainobject.cpp b/src/mainobject.cpp index eed22d7..aeb90de 100644 --- a/src/mainobject.cpp +++ b/src/mainobject.cpp @@ -4,6 +4,8 @@ #include #include +#include "mqttclient.h" +#include "items/mqttitem.h" #include "items/itemstore.h" MainObject::MainObject(QObject *parent) : @@ -74,9 +76,12 @@ PrimaryMainObject::PrimaryMainObject(QIODevice* microDevice, const QString& sett micro(microDevice), tcpServer(new TcpServer(this)), webServer(new WebSocketServer("shinterface", this)), + mqttClient(new MqttClient), sunSensorSource(49.824972, 8.702194), fixedItems(µ) { + MqttItem::client = mqttClient; + //connect sensors subsystem connect(&globalSensors, &SensorStore::sensorChangedState, tcpServer, &TcpServer::sensorEvent); connect(tcpServer, &TcpServer::gotSensor, &globalSensors, &SensorStore::sensorGotState); @@ -98,7 +103,8 @@ PrimaryMainObject::PrimaryMainObject(QIODevice* microDevice, const QString& sett loadFromDisk(settingsPath); QJsonObject mqttJson = settings["Mqtt"].toObject(); - mqttSensorSource.start(mqttJson); + mqttClient->start(mqttJson); + mqttSensorSource.start(mqttClient, mqttJson); tcpServer->launch(QHostAddress(host), port); webServer->launch(QHostAddress(host), port+1); @@ -115,6 +121,7 @@ void PrimaryMainObject::store(QJsonObject &json) { globalItems.store(json); QJsonObject mqttJson = json["Mqtt"].toObject(); + mqttClient->store(mqttJson); mqttSensorSource.store(mqttJson); json["Mqtt"] = mqttJson; } diff --git a/src/mainobject.h b/src/mainobject.h index 48835dc..5552c6f 100644 --- a/src/mainobject.h +++ b/src/mainobject.h @@ -46,6 +46,7 @@ public: Microcontroller micro; TcpServer* tcpServer; WebSocketServer* webServer; + std::shared_ptr mqttClient; //sensors SunSensorSource sunSensorSource; diff --git a/src/mqttclient.cpp b/src/mqttclient.cpp new file mode 100644 index 0000000..fcc4692 --- /dev/null +++ b/src/mqttclient.cpp @@ -0,0 +1,110 @@ +#include "mqttclient.h" + +MqttClient::MqttClient(): + client(new QMqttClient) +{ + +} + +void MqttClient::start(const QJsonObject& settings) +{ + baseTopicName = settings["BaseTopic"].toString("zigbee2mqtt"); + + QMqttClient* cl = client.get(); + connect(cl, &QMqttClient::stateChanged, this, &MqttClient::onClientStateChanged); + connect(cl, &QMqttClient::errorChanged, this, &MqttClient::onClientError); + + client->setHostname(settings["Host"].toString("127.0.0.1")); + client->setPort(settings["Port"].toInt(1883)); + if(settings.contains("User")) + client->setUsername(settings["User"].toString()); + if(settings.contains("Password")) + client->setPassword(settings["Password"].toString()); + client->setProtocolVersion(QMqttClient::MQTT_5_0); + + client->connectToHost(); +} + +void MqttClient::onClientError(QMqttClient::ClientError error) +{ + qWarning()<<"MQTT Client error:"<hostname()<port(); + else if (state == QMqttClient::ClientState::Disconnected) + qWarning()<<"Lost connection to MQTT broker"; + else if(state == QMqttClient::ClientState::Connecting) + qInfo()<<"Connecting to MQTT broker at "<hostname()<port(); +} + +void MqttClient::store(QJsonObject& json) +{ + json["Host"] = client->hostname(); + json["Port"] = client->port(); + json["BaseTopic"] = baseTopicName; + if(client->username() != "") + json["User"] = client->username(); + if(client->password() != "") + json["Password"] = client->password(); +} + +std::shared_ptr MqttClient::getClient() +{ + return client; +} + +MqttClient::Subscription* MqttClient::subscribe(QString topic) +{ + if(subscriptions.contains(topic)) + { + MqttClient::Subscription* sub = subscriptions[topic]; + ++sub->ref; + return sub; + } + else + { + qDebug()<<"MqttClient: subscibeing to"<subscription = client->subscribe(topic); + sub->ref = 1; + subscriptions.insert({topic, sub}); + return sub; + } +} + +void MqttClient::unsubscribe(MqttClient::Subscription* subscription) +{ + QString topic = subscription->subscription->topic().filter(); + unsubscribe(topic); +} + +void MqttClient::unsubscribe(QString topic) +{ + assert(!subscriptions.contains(topic)); + MqttClient::Subscription* sub = subscriptions[topic]; + + if(--sub->ref > 0) + return; + + qDebug()<<"MqttClient: unsubscibeing"<subscription->topic(); + client->unsubscribe(sub->subscription->topic()); + subscriptions.erase(topic); + delete sub; +} + +QString MqttClient::getBaseTopic() +{ + return baseTopicName; +} + +MqttClient::~MqttClient() +{ + for(const std::pair sub : subscriptions) + { + qWarning()<unsubscribe(sub.second->subscription->topic()); + } +} \ No newline at end of file diff --git a/src/mqttclient.h b/src/mqttclient.h new file mode 100644 index 0000000..1199855 --- /dev/null +++ b/src/mqttclient.h @@ -0,0 +1,40 @@ +#ifndef MQTTCLIENT_H +#define MQTTCLIENT_H + +#include +#include +#include +#include + +class MqttClient: public QObject +{ + Q_OBJECT +public: + struct Subscription + { + int ref; + QMqttSubscription* subscription; + }; + +private: + QString baseTopicName; + std::shared_ptr client; + std::map subscriptions; + +private slots: + void onClientStateChanged(QMqttClient::ClientState state); + void onClientError(QMqttClient::ClientError error); + +public: + explicit MqttClient(); + ~MqttClient(); + void start(const QJsonObject& settings); + void store(QJsonObject& json); + std::shared_ptr getClient(); + Subscription* subscribe(QString topic); + void unsubscribe(Subscription* subscription); + void unsubscribe(QString topic); + QString getBaseTopic(); +}; + +#endif // MQTTCLIENT_H diff --git a/src/sensors/mqttsensorsource.cpp b/src/sensors/mqttsensorsource.cpp index 4202ab8..63b5c02 100644 --- a/src/sensors/mqttsensorsource.cpp +++ b/src/sensors/mqttsensorsource.cpp @@ -7,22 +7,11 @@ MqttSensorSource::MqttSensorSource(QObject *parent) { } -void MqttSensorSource::start(const QJsonObject& settings) +void MqttSensorSource::start(std::shared_ptr client, const QJsonObject& settings) { - baseTopicName = settings["BaseTopic"].toString("zigbee2mqtt"); + this->client = client; - connect(&client, &QMqttClient::stateChanged, this, &MqttSensorSource::onClientStateChanged); - connect(&client, &QMqttClient::errorChanged, this, &MqttSensorSource::onClientError); - - client.setHostname(settings["Host"].toString("127.0.0.1")); - client.setPort(settings["Port"].toInt(1883)); - if(settings.contains("User")) - client.setUsername(settings["User"].toString()); - if(settings.contains("Password")) - client.setPassword(settings["Password"].toString()); - client.setProtocolVersion(QMqttClient::MQTT_5_0); - - client.connectToHost(); + connect(client->getClient().get(), &QMqttClient::stateChanged, this, &MqttSensorSource::onClientStateChanged); QJsonArray sensorsArray = settings["Sensors"].toArray(); @@ -37,21 +26,16 @@ void MqttSensorSource::start(const QJsonObject& settings) sensor.name = sensor.topic; else sensor.name = sensorObject["Name"].toString(); - sensor.id = qHash(baseTopicName + "/" + sensor.topic); + sensor.id = qHash(client->getBaseTopic() + "/" + sensor.topic); sensors.push_back(sensor); } } -void MqttSensorSource::onClientError(QMqttClient::ClientError error) -{ - qWarning()<<"MQTT Client error:"<getBaseTopic() + "/" + sensor.topic == topic) return sensor; } assert(false); @@ -61,30 +45,24 @@ void MqttSensorSource::onClientStateChanged(QMqttClient::ClientState state) { if(state == QMqttClient::ClientState::Connected) { - qInfo()<<"Connected to MQTT broker at "<getBaseTopic() + "/" + sensor.topic; + sensor.subscription = client->subscribe(client->getBaseTopic() + "/" + sensor.topic); + connect(sensor.subscription->subscription, &QMqttSubscription::messageReceived, this, &MqttSensorSource::onMessageReceived); } } else if (state == QMqttClient::ClientState::Disconnected) { - qWarning()<<"Lost connection to MQTT broker"; for(SensorSubscription& sensor : sensors) { if(sensor.subscription) { - client.unsubscribe(sensor.topic); + client->unsubscribe(sensor.topic); sensor.subscription = nullptr; } } } - else if(state == QMqttClient::ClientState::Connecting) - { - qInfo()<<"Connecting to MQTT broker at "<unsubscribe(sub.topic); +} + diff --git a/src/sensors/mqttsensorsource.h b/src/sensors/mqttsensorsource.h index ca601c1..c726d70 100644 --- a/src/sensors/mqttsensorsource.h +++ b/src/sensors/mqttsensorsource.h @@ -7,6 +7,7 @@ #include #include "sensor.h" +#include "mqttclient.h" class MqttSensorSource : public QObject { @@ -17,12 +18,11 @@ class MqttSensorSource : public QObject uint64_t id; QString topic; QString name; - QMqttSubscription* subscription = nullptr; + MqttClient::Subscription* subscription = nullptr; }; - QString baseTopicName; std::vector sensors; - QMqttClient client; + std::shared_ptr client; private: SensorSubscription& findSubscription(const QString& topic); @@ -30,11 +30,11 @@ private: private slots: void onClientStateChanged(QMqttClient::ClientState state); void onMessageReceived(const QMqttMessage& message); - void onClientError(QMqttClient::ClientError error); public: explicit MqttSensorSource(QObject *parent = nullptr); - void start(const QJsonObject& settings); + ~MqttSensorSource(); + void start(std::shared_ptr client, const QJsonObject& settings); void store(QJsonObject& json); signals: diff --git a/src/ui/itemcreationdialog.cpp b/src/ui/itemcreationdialog.cpp index 57e804f..1133d08 100644 --- a/src/ui/itemcreationdialog.cpp +++ b/src/ui/itemcreationdialog.cpp @@ -3,6 +3,8 @@ #include "itemsettingswidgets/messageitemsettingswidget.h" #include "itemsettingswidgets/systemitemsettingswidget.h" +#include "itemsettingswidgets/mqttitemsettingswidget.h" +#include "items/mqttitem.h" ItemCreationDialog::ItemCreationDialog(QWidget *parent) : QDialog(parent), @@ -41,6 +43,13 @@ void ItemCreationDialog::itemTypeChanged(const QString& type) widget = new SystemItemSettingsWidget(systemItem, this); ui->verticalLayout->addWidget(widget); } + if(type == "Mqtt") + { + std::shared_ptr mqttItem(new MqttItem); + item = mqttItem; + widget = new MqttItemSettingsWidget(mqttItem, this); + ui->verticalLayout->addWidget(widget); + } } void ItemCreationDialog::itemNameChanged(const QString& name) diff --git a/src/ui/itemcreationdialog.ui b/src/ui/itemcreationdialog.ui index 4b55d6d..cc76289 100644 --- a/src/ui/itemcreationdialog.ui +++ b/src/ui/itemcreationdialog.ui @@ -7,7 +7,7 @@ 0 0 400 - 140 + 274 @@ -39,6 +39,11 @@ System + + + Mqtt + + @@ -64,13 +69,26 @@ + + + + Qt::Orientation::Vertical + + + + 20 + 40 + + + + - Qt::Horizontal + Qt::Orientation::Horizontal - QDialogButtonBox::Cancel|QDialogButtonBox::Ok + QDialogButtonBox::StandardButton::Cancel|QDialogButtonBox::StandardButton::Ok diff --git a/src/ui/itemsettingsdialog.cpp b/src/ui/itemsettingsdialog.cpp index aa60f01..d517312 100644 --- a/src/ui/itemsettingsdialog.cpp +++ b/src/ui/itemsettingsdialog.cpp @@ -12,6 +12,8 @@ #include "itemsettingswidgets/messageitemsettingswidget.h" #include "itemsettingswidgets/systemitemsettingswidget.h" #include "itemsettingswidgets/relayitemsettingswidget.h" +#include "itemsettingswidgets/mqttitemsettingswidget.h" +#include "../items/mqttitem.h" #include ItemSettingsDialog::ItemSettingsDialog(std::shared_ptr item, bool noGroup, QWidget *parent) : @@ -47,6 +49,10 @@ ItemSettingsDialog::ItemSettingsDialog(std::shared_ptr item, bool noGroup, { itemSpecificWidget_ = new SystemItemSettingsWidget(sysItem); } + else if(std::shared_ptr mqttItem = std::dynamic_pointer_cast(item_)) + { + itemSpecificWidget_ = new MqttItemSettingsWidget(mqttItem); + } if(itemSpecificWidget_) { diff --git a/src/ui/itemsettingswidgets/mqttitemsettingswidget.cpp b/src/ui/itemsettingswidgets/mqttitemsettingswidget.cpp new file mode 100644 index 0000000..43799c4 --- /dev/null +++ b/src/ui/itemsettingswidgets/mqttitemsettingswidget.cpp @@ -0,0 +1,62 @@ +#include "mqttitemsettingswidget.h" +#include "ui_mqttitemsettingswidget.h" + +#include + +MqttItemSettingsWidget::MqttItemSettingsWidget(std::weak_ptr item, QWidget *parent) : + QWidget(parent), + item_(item), + ui(new Ui::MqttItemSettingsWidget) +{ + ui->setupUi(this); + + if(auto workingItem = item_.lock()) + { + ui->lineEdit_topic->setText(workingItem->getTopic()); + ui->lineEdit_valueKey->setText(workingItem->getValueKey()); + ui->lineEdit_valueOn->setText(workingItem->getValueOn()); + ui->lineEdit_valueOff->setText(workingItem->getValueOff()); + } + + connect(ui->lineEdit_topic, &QLineEdit::textChanged, this, &MqttItemSettingsWidget::setTopic); + connect(ui->lineEdit_valueKey, &QLineEdit::textChanged, this, &MqttItemSettingsWidget::setValueKey); + connect(ui->lineEdit_valueOn, &QLineEdit::textChanged, this, &MqttItemSettingsWidget::setValueOn); + connect(ui->lineEdit_valueOff, &QLineEdit::textChanged, this, &MqttItemSettingsWidget::setValueOff); +} + +void MqttItemSettingsWidget::setTopic(const QString& topic) +{ + if(auto workingItem = item_.lock()) + { + workingItem->setTopic(topic); + } +} + +void MqttItemSettingsWidget::setValueKey(const QString& valueKey) +{ + if(auto workingItem = item_.lock()) + { + workingItem->setValueKey(valueKey); + } +} + +void MqttItemSettingsWidget::setValueOn(const QString& valueOn) +{ + if(auto workingItem = item_.lock()) + { + workingItem->setValueOn(valueOn); + } +} + +void MqttItemSettingsWidget::setValueOff(const QString& valueOff) +{ + if(auto workingItem = item_.lock()) + { + workingItem->setValueOff(valueOff); + } +} + +MqttItemSettingsWidget::~MqttItemSettingsWidget() +{ + delete ui; +} \ No newline at end of file diff --git a/src/ui/itemsettingswidgets/mqttitemsettingswidget.h b/src/ui/itemsettingswidgets/mqttitemsettingswidget.h new file mode 100644 index 0000000..b848b14 --- /dev/null +++ b/src/ui/itemsettingswidgets/mqttitemsettingswidget.h @@ -0,0 +1,32 @@ +#ifndef MQTTITEMSETTINGSWIDGET_H +#define MQTTITEMSETTINGSWIDGET_H + +#include +#include +#include "../../items/mqttitem.h" + +namespace Ui +{ +class MqttItemSettingsWidget; +} + +class MqttItemSettingsWidget : public QWidget +{ + Q_OBJECT + std::weak_ptr item_; + +private slots: + void setTopic(const QString& topic); + void setValueKey(const QString& valueKey); + void setValueOn(const QString& valueOn); + void setValueOff(const QString& valueOff); + +public: + explicit MqttItemSettingsWidget(std::weak_ptr item, QWidget *parent = nullptr); + ~MqttItemSettingsWidget(); + +private: + Ui::MqttItemSettingsWidget *ui; +}; + +#endif // MQTTITEMSETTINGSWIDGET_H \ No newline at end of file diff --git a/src/ui/itemsettingswidgets/mqttitemsettingswidget.ui b/src/ui/itemsettingswidgets/mqttitemsettingswidget.ui new file mode 100644 index 0000000..827b510 --- /dev/null +++ b/src/ui/itemsettingswidgets/mqttitemsettingswidget.ui @@ -0,0 +1,108 @@ + + + MqttItemSettingsWidget + + + + 0 + 0 + 400 + 216 + + + + Form + + + + + + 0 + + + + + Topic: + + + + + + + e.g., kitchen/light + + + + + + + + + 0 + + + + + Value Key: + + + + + + + state + + + e.g., state, brightness + + + + + + + + + 0 + + + + + Value On: + + + + + + + ON + + + + + + + + + 0 + + + + + Value Off: + + + + + + + OFF + + + + + + + + + + diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 2b02715..28c8792 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -48,6 +48,9 @@ set(COMMON_TEST_SOURCES ../src/items/itemstore.cpp ../src/items/itemloadersource.h ../src/items/itemloadersource.cpp + ../src/items/mqttitem.h + ../src/items/mqttitem.cpp + ../src/mqttclient.cpp ) # Add test executables - compile all needed sources into each test @@ -73,6 +76,7 @@ target_link_libraries(test_item Qt6::Gui Qt6::Widgets Qt6::Multimedia + Qt6::Mqtt Qt6::Test ) @@ -88,6 +92,7 @@ target_link_libraries(test_sensor Qt6::Gui Qt6::Widgets Qt6::Multimedia + Qt6::Mqtt Qt6::Test ) @@ -103,6 +108,7 @@ target_link_libraries(test_actor Qt6::Gui Qt6::Widgets Qt6::Multimedia + Qt6::Mqtt Qt6::Test ) @@ -118,6 +124,7 @@ target_link_libraries(test_itemstore Qt6::Gui Qt6::Widgets Qt6::Multimedia + Qt6::Mqtt Qt6::Test ) @@ -133,6 +140,7 @@ target_link_libraries(test_itemloadersource Qt6::Gui Qt6::Widgets Qt6::Multimedia + Qt6::Mqtt Qt6::Test ) @@ -150,6 +158,7 @@ target_link_libraries(test_tcp Qt6::Multimedia Qt6::Network Qt6::WebSockets + Qt6::Mqtt Qt6::Test ) From cd64cbe08e1a2ec34670a9bff072da9e4059dc7c Mon Sep 17 00:00:00 2001 From: Carl Philipp Klemm Date: Mon, 13 Apr 2026 14:08:24 +0200 Subject: [PATCH 10/21] UI: fix finger scolling in itemscrollbox tabs --- src/ui/itemscrollbox.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/ui/itemscrollbox.cpp b/src/ui/itemscrollbox.cpp index 55ba900..f704996 100644 --- a/src/ui/itemscrollbox.cpp +++ b/src/ui/itemscrollbox.cpp @@ -1,6 +1,7 @@ #include "itemscrollbox.h" #include "ui_relayscrollbox.h" #include +#include #include ItemScrollBox::ItemScrollBox(QWidget *parent) : @@ -131,7 +132,7 @@ void ItemScrollBox::onItemUpdate(const ItemUpdateRequest& update) { if(widget->controles(update.payload)) { - qDebug()<<"ItemUpdate with group change"; + qDebug()<<"ItemUpdate with group change for item"< item = widget->getItem(); removeItemFromTabs(update.payload); addItemToTabs(item); @@ -148,6 +149,7 @@ void ItemScrollBox::ensureTabExists(const QString& groupName) tab.scroller->setWidgetResizable(true); tab.scroller->setFrameShape(QFrame::NoFrame); tab.scroller->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + QScroller::grabGesture(tab.scroller->viewport(), QScroller::LeftMouseButtonGesture); tab.content = new QWidget(tab.scroller); QVBoxLayout* scrollLayout = new QVBoxLayout(tab.content); From b0792d32dbe9a99bc1147605acb3e97820c67b7b Mon Sep 17 00:00:00 2001 From: Carl Philipp Klemm Date: Mon, 13 Apr 2026 14:09:34 +0200 Subject: [PATCH 11/21] Service: Only sent item changes in item updates --- src/service/server.cpp | 2 +- src/service/tcpclient.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/service/server.cpp b/src/service/server.cpp index 13fc7f3..b979413 100644 --- a/src/service/server.cpp +++ b/src/service/server.cpp @@ -107,7 +107,7 @@ void Server::itemUpdated(ItemUpdateRequest update) { QJsonArray items; QJsonObject itemjson; - update.payload.store(itemjson); + update.payload.storeWithChanges(itemjson, update.changes); items.append(itemjson); QJsonObject json = createMessage("ItemUpdate", items); json["FullList"] = false; diff --git a/src/service/tcpclient.cpp b/src/service/tcpclient.cpp index afcb473..6e00e2d 100644 --- a/src/service/tcpclient.cpp +++ b/src/service/tcpclient.cpp @@ -120,7 +120,7 @@ void TcpClient::itemUpdated(ItemUpdateRequest update) { QJsonArray items; QJsonObject itemjson; - update.payload.store(itemjson); + update.payload.storeWithChanges(itemjson, update.changes); items.append(itemjson); QJsonObject json = createMessage("ItemUpdate", items); json["FullList"] = false; From d69bfb58b9bef2c5bb0065529b3b3161497abaf6 Mon Sep 17 00:00:00 2001 From: Carl Philipp Klemm Date: Mon, 13 Apr 2026 14:09:58 +0200 Subject: [PATCH 12/21] Fix mqtt items asserting on client instances --- src/items/itemloadersource.cpp | 1 + src/items/mqttitem.cpp | 6 ++++-- src/mqttclient.cpp | 8 ++++++-- src/sensors/mqttsensorsource.cpp | 5 ++--- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/items/itemloadersource.cpp b/src/items/itemloadersource.cpp index db5478a..e2d1e68 100644 --- a/src/items/itemloadersource.cpp +++ b/src/items/itemloadersource.cpp @@ -26,6 +26,7 @@ void ItemLoaderSource::refresh() request.type = ITEM_UPDATE_LOADED; request.payload = newItem; request.changes = ItemFieldChanges(true); + request.changes.value = false; itemAddRequests.push_back(request); } } diff --git a/src/items/mqttitem.cpp b/src/items/mqttitem.cpp index a6b30e5..76c5264 100644 --- a/src/items/mqttitem.cpp +++ b/src/items/mqttitem.cpp @@ -5,6 +5,7 @@ #include #include "mqttclient.h" +#include "programmode.h" MqttItem::MqttItem(QString name, uint8_t value, QObject *parent) : Item(0, name, value, parent), @@ -15,9 +16,10 @@ MqttItem::MqttItem(QString name, uint8_t value, QObject *parent) { hashId(); std::shared_ptr workClient = client.lock(); - assert(workClient); + assert(workClient || programMode == PROGRAM_MODE_UI_ONLY); - connect(workClient->getClient().get(), &QMqttClient::stateChanged, this, &MqttItem::onClientStateChanged); + if(workClient) + connect(workClient->getClient().get(), &QMqttClient::stateChanged, this, &MqttItem::onClientStateChanged); } MqttItem::~MqttItem() diff --git a/src/mqttclient.cpp b/src/mqttclient.cpp index fcc4692..ab61375 100644 --- a/src/mqttclient.cpp +++ b/src/mqttclient.cpp @@ -83,13 +83,17 @@ void MqttClient::unsubscribe(MqttClient::Subscription* subscription) void MqttClient::unsubscribe(QString topic) { - assert(!subscriptions.contains(topic)); MqttClient::Subscription* sub = subscriptions[topic]; + if(!sub) + { + qWarning()<<"MqttClient: Trying to unsubscribe from unkown topic:"<ref > 0) return; - qDebug()<<"MqttClient: unsubscibeing"<subscription->topic(); + qDebug()<<"MqttClient: unsubscibeing"<subscription->topic().filter(); client->unsubscribe(sub->subscription->topic()); subscriptions.erase(topic); delete sub; diff --git a/src/sensors/mqttsensorsource.cpp b/src/sensors/mqttsensorsource.cpp index 63b5c02..0853abf 100644 --- a/src/sensors/mqttsensorsource.cpp +++ b/src/sensors/mqttsensorsource.cpp @@ -47,7 +47,6 @@ void MqttSensorSource::onClientStateChanged(QMqttClient::ClientState state) { for(SensorSubscription& sensor : sensors) { - qDebug()<<"MQTT subscribeing to"<getBaseTopic() + "/" + sensor.topic; sensor.subscription = client->subscribe(client->getBaseTopic() + "/" + sensor.topic); connect(sensor.subscription->subscription, &QMqttSubscription::messageReceived, this, &MqttSensorSource::onMessageReceived); } @@ -58,7 +57,7 @@ void MqttSensorSource::onClientStateChanged(QMqttClient::ClientState state) { if(sensor.subscription) { - client->unsubscribe(sensor.topic); + client->unsubscribe(client->getBaseTopic() + "/" + sensor.topic); sensor.subscription = nullptr; } } @@ -167,6 +166,6 @@ void MqttSensorSource::store(QJsonObject& json) MqttSensorSource::~MqttSensorSource() { for(SensorSubscription& sub : sensors) - client->unsubscribe(sub.topic); + client->unsubscribe(client->getBaseTopic() + "/" + sub.topic); } From 6c6fc11439f1c17a39cb810a6be40f4bfb27e4d3 Mon Sep 17 00:00:00 2001 From: Carl Philipp Klemm Date: Mon, 13 Apr 2026 14:20:11 +0200 Subject: [PATCH 13/21] Change the name of the project to smartvos and use a static libary for the core --- CMakeLists.txt | 189 ++++++++++++++++++++++++++----------------- tests/CMakeLists.txt | 189 +++++++------------------------------------ 2 files changed, 142 insertions(+), 236 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ca60d65..6508688 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 4.0) -project(SHinterface VERSION 1.0 LANGUAGES CXX) +project(smartvos VERSION 1.0 LANGUAGES CXX) # Set C++ standard set(CMAKE_CXX_STANDARD 20) @@ -27,88 +27,113 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src) # Enable testing framework enable_testing() +# Define shared sources for static library (core sources used by both main and tests) +set(SHINTERFACE_CORE_SOURCES + src/sensors/mqttsensorsource.h + src/sensors/mqttsensorsource.cpp + src/items/mqttitem.h + src/items/mqttitem.cpp + src/mqttclient.h + src/mqttclient.cpp + src/microcontroller.h + src/microcontroller.cpp + src/sun.h + src/sun.cpp + src/programmode.h + src/programmode.cpp + + src/service/service.h + src/service/service.cpp + src/service/tcpclient.h + src/service/tcpclient.cpp + src/service/server.h + src/service/server.cpp + src/service/tcpserver.h + src/service/tcpserver.cpp + src/service/websocketserver.h + src/service/websocketserver.cpp + + src/actors/actor.h + src/actors/actor.cpp + src/actors/factoractor.h + src/actors/factoractor.cpp + src/actors/polynomalactor.h + src/actors/polynomalactor.cpp + src/actors/sensoractor.h + src/actors/sensoractor.cpp + src/actors/alarmtime.h + src/actors/alarmtime.cpp + src/actors/regulator.h + src/actors/regulator.cpp + src/actors/timeractor.h + src/actors/timeractor.cpp + + src/sensors/sensor.h + src/sensors/sensor.cpp + src/sensors/sunsensor.h + src/sensors/sunsensor.cpp + + src/items/item.h + src/items/item.cpp + src/items/relay.h + src/items/relay.cpp + src/items/poweritem.h + src/items/poweritem.cpp + src/items/messageitem.h + src/items/messageitem.cpp + src/items/systemitem.h + src/items/systemitem.cpp + src/items/auxitem.h + src/items/auxitem.cpp + src/items/rgbitem.h + src/items/rgbitem.cpp + src/items/itemsource.h + src/items/itemsource.cpp + src/items/itemloadersource.h + src/items/itemloadersource.cpp + src/items/fixeditemsource.h + src/items/fixeditemsource.cpp + src/items/itemstore.h + src/items/itemstore.cpp +) + +# Create static library +add_library(smartvos_core STATIC ${SHINTERFACE_CORE_SOURCES}) + +# Link Qt and system libraries to static library +target_link_libraries(smartvos_core + Qt6::Core + Qt6::Gui + Qt6::Widgets + Qt6::Network + Qt6::Multimedia + Qt6::SerialPort + Qt6::Mqtt + Qt6::WebSockets + ${PIPEWIRE_LIBRARIES} + ${LIBNL3_LIBRARIES} +) + +# Add include paths to static library +target_include_directories(smartvos_core PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/src + ${PIPEWIRE_INCLUDE_DIRS} + ${LIBNL3_INCLUDE_DIRS} +) + # Add subdirectory for tests add_subdirectory(tests) # Create executable -add_executable(SHinterface - src/sensors/mqttsensorsource.h src/sensors/mqttsensorsource.cpp - src/items/mqttitem.h src/items/mqttitem.cpp - src/mqttclient.h src/mqttclient.cpp - -) - -# Add sources to executable -target_sources(SHinterface - PRIVATE +add_executable(smartvos src/main.cpp src/mainobject.h src/mainobject.cpp src/apgetconnected.h src/apgetconnected.cpp - src/microcontroller.h - src/microcontroller.cpp - src/sun.h - src/sun.cpp - src/programmode.h - src/programmode.cpp src/pipewire.h src/pipewire.cpp - src/service/service.h - src/service/service.cpp - src/service/tcpclient.h - src/service/tcpclient.cpp - src/service/server.h - src/service/server.cpp - src/service/tcpserver.h - src/service/tcpserver.cpp - src/service/websocketserver.h - src/service/websocketserver.cpp - - src/actors/actor.h - src/actors/actor.cpp - src/actors/factoractor.h - src/actors/factoractor.cpp - src/actors/polynomalactor.h - src/actors/polynomalactor.cpp - src/actors/sensoractor.h - src/actors/sensoractor.cpp - src/actors/alarmtime.h - src/actors/alarmtime.cpp - src/actors/regulator.h - src/actors/regulator.cpp - src/actors/timeractor.h - src/actors/timeractor.cpp - - src/sensors/sensor.h - src/sensors/sensor.cpp - src/sensors/sunsensor.h - src/sensors/sunsensor.cpp - - src/items/item.h - src/items/item.cpp - src/items/relay.h - src/items/relay.cpp - src/items/poweritem.h - src/items/poweritem.cpp - src/items/messageitem.h - src/items/messageitem.cpp - src/items/systemitem.h - src/items/systemitem.cpp - src/items/auxitem.h - src/items/auxitem.cpp - src/items/rgbitem.h - src/items/rgbitem.cpp - src/items/itemsource.h - src/items/itemsource.cpp - src/items/itemloadersource.h - src/items/itemloadersource.cpp - src/items/fixeditemsource.h - src/items/fixeditemsource.cpp - src/items/itemstore.h - src/items/itemstore.cpp - src/ui/mainwindow.h src/ui/mainwindow.cpp src/ui/itemwidget.h @@ -148,7 +173,7 @@ target_sources(SHinterface ) # Add UI files -target_sources(SHinterface +target_sources(smartvos PRIVATE src/ui/mainwindow.ui src/ui/itemwidget.ui @@ -169,13 +194,14 @@ target_sources(SHinterface ) # Add resource file -target_sources(SHinterface +target_sources(smartvos PRIVATE resources.qrc ) -# Link libraries -target_link_libraries(SHinterface +# Link libraries - link to static library plus UI-specific dependencies +target_link_libraries(smartvos + smartvos_core Qt6::Core Qt6::Gui Qt6::Widgets @@ -190,3 +216,16 @@ target_link_libraries(SHinterface # Add include paths include_directories(${PIPEWIRE_INCLUDE_DIRS} ${LIBNL3_INCLUDE_DIRS}) + +# Installation +install(TARGETS smartvos DESTINATION bin) +install(TARGETS smartvos_core DESTINATION lib) + +# Install icon +install(FILES UVOSicon.bmp DESTINATION share/icons/hicolor/48x48/apps RENAME smartvos.png) + +# Install .desktop file +install(FILES smartvos.desktop DESTINATION share/applications) + +# Update icon cache (optional, for icon themes) +install(CODE "execute_process(COMMAND gtk-update-icon-cache -f -t ${CMAKE_INSTALL_PREFIX}/share/icons/hicolor WORKING_DIRECTORY ${CMAKE_INSTALL_PREFIX})") diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 28c8792..dabb4fd 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -6,167 +6,34 @@ enable_testing() # Find Qt packages for tests find_package(Qt6 COMPONENTS Core Gui Widgets Multimedia Test REQUIRED) -# Define common sources needed by all tests - include only what's actually needed for basic testing -set(COMMON_TEST_SOURCES - ../src/items/item.h - ../src/items/item.cpp - ../src/sensors/sensor.h - ../src/sensors/sensor.cpp - ../src/programmode.h - ../src/programmode.cpp - ../src/microcontroller.h - ../src/microcontroller.cpp - ../src/actors/actor.h - ../src/actors/actor.cpp - ../src/actors/factoractor.h - ../src/actors/factoractor.cpp - ../src/actors/polynomalactor.h - ../src/actors/polynomalactor.cpp - ../src/actors/sensoractor.h - ../src/actors/sensoractor.cpp - ../src/actors/timeractor.h - ../src/actors/timeractor.cpp - ../src/items/relay.h - ../src/items/relay.cpp - ../src/items/messageitem.h - ../src/items/messageitem.cpp - ../src/items/systemitem.h - ../src/items/systemitem.cpp - ../src/items/auxitem.h - ../src/items/auxitem.cpp - ../src/items/poweritem.h - ../src/items/poweritem.cpp - ../src/items/rgbitem.h - ../src/items/rgbitem.cpp - ../src/actors/alarmtime.h - ../src/actors/alarmtime.cpp - ../src/actors/regulator.h - ../src/actors/regulator.cpp - ../src/items/itemsource.h - ../src/items/itemsource.cpp - ../src/items/itemstore.h - ../src/items/itemstore.cpp - ../src/items/itemloadersource.h - ../src/items/itemloadersource.cpp - ../src/items/mqttitem.h - ../src/items/mqttitem.cpp - ../src/mqttclient.cpp -) +# Add test executables - link to static library instead of compiling sources +add_executable(test_item unit/items/test_item.cpp) +add_executable(test_sensor unit/sensors/test_sensor.cpp) +add_executable(test_actor unit/actors/test_actor.cpp) +add_executable(test_itemstore unit/items/test_itemstore.cpp) +add_executable(test_itemloadersource unit/items/test_itemloadersource.cpp) +add_executable(test_tcp unit/service/test_tcp.cpp) -# Add test executables - compile all needed sources into each test -add_executable(test_item unit/items/test_item.cpp ${COMMON_TEST_SOURCES}) -add_executable(test_sensor unit/sensors/test_sensor.cpp ${COMMON_TEST_SOURCES}) -add_executable(test_actor unit/actors/test_actor.cpp ${COMMON_TEST_SOURCES}) -add_executable(test_itemstore unit/items/test_itemstore.cpp ${COMMON_TEST_SOURCES}) -add_executable(test_itemloadersource unit/items/test_itemloadersource.cpp ${COMMON_TEST_SOURCES}) -add_executable(test_tcp unit/service/test_tcp.cpp ${COMMON_TEST_SOURCES} - ../src/service/service.h - ../src/service/service.cpp - ../src/service/server.h - ../src/service/server.cpp - ../src/service/tcpserver.h - ../src/service/tcpserver.cpp - ../src/service/tcpclient.h - ../src/service/tcpclient.cpp -) - -# Link libraries for test_item -target_link_libraries(test_item - Qt6::Core - Qt6::Gui - Qt6::Widgets - Qt6::Multimedia - Qt6::Mqtt - Qt6::Test -) - -# Include paths for source files -target_include_directories(test_item PRIVATE - ${CMAKE_CURRENT_SOURCE_DIR}/../src - ${Qt6Gui_PRIVATE_INCLUDE_DIRS} -) - -# Link libraries for test_sensor -target_link_libraries(test_sensor - Qt6::Core - Qt6::Gui - Qt6::Widgets - Qt6::Multimedia - Qt6::Mqtt - Qt6::Test -) - -# Include paths for source files -target_include_directories(test_sensor PRIVATE - ${CMAKE_CURRENT_SOURCE_DIR}/../src - ${Qt6Gui_PRIVATE_INCLUDE_DIRS} -) - -# Link libraries for test_actor -target_link_libraries(test_actor - Qt6::Core - Qt6::Gui - Qt6::Widgets - Qt6::Multimedia - Qt6::Mqtt - Qt6::Test -) - -# Include paths for source files -target_include_directories(test_actor PRIVATE - ${CMAKE_CURRENT_SOURCE_DIR}/../src - ${Qt6Gui_PRIVATE_INCLUDE_DIRS} -) - -# Link libraries for test_itemstore -target_link_libraries(test_itemstore - Qt6::Core - Qt6::Gui - Qt6::Widgets - Qt6::Multimedia - Qt6::Mqtt - Qt6::Test -) - -# Include paths for source files -target_include_directories(test_itemstore PRIVATE - ${CMAKE_CURRENT_SOURCE_DIR}/../src - ${Qt6Gui_PRIVATE_INCLUDE_DIRS} -) - -# Link libraries for test_itemloadersource -target_link_libraries(test_itemloadersource - Qt6::Core - Qt6::Gui - Qt6::Widgets - Qt6::Multimedia - Qt6::Mqtt - Qt6::Test -) - -# Include paths for source files -target_include_directories(test_itemloadersource PRIVATE - ${CMAKE_CURRENT_SOURCE_DIR}/../src - ${Qt6Gui_PRIVATE_INCLUDE_DIRS} -) - -# Link libraries for test_tcp -target_link_libraries(test_tcp - Qt6::Core - Qt6::Gui - Qt6::Widgets - Qt6::Multimedia - Qt6::Network - Qt6::WebSockets - Qt6::Mqtt - Qt6::Test -) - -# Include paths for source files -target_include_directories(test_tcp PRIVATE - ${CMAKE_CURRENT_SOURCE_DIR}/../src - ${Qt6Gui_PRIVATE_INCLUDE_DIRS} -) +# Link all tests to static library +foreach(test test_item test_sensor test_actor test_itemstore test_itemloadersource test_tcp) + target_link_libraries(${test} + smartvos_core + Qt6::Core + Qt6::Gui + Qt6::Widgets + Qt6::Multimedia + Qt6::Network + Qt6::WebSockets + Qt6::Mqtt + Qt6::Test + ) + + # Include paths - the static library already has the correct include paths + target_include_directories(${test} PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/../src + ${Qt6Gui_PRIVATE_INCLUDE_DIRS} + ) +endforeach() # Add tests to CTest add_test(NAME test_item COMMAND test_item) @@ -174,4 +41,4 @@ add_test(NAME test_sensor COMMAND test_sensor) add_test(NAME test_actor COMMAND test_actor) add_test(NAME test_itemstore COMMAND test_itemstore) add_test(NAME test_itemloadersource COMMAND test_itemloadersource) -add_test(NAME test_tcp COMMAND test_tcp) +add_test(NAME test_tcp COMMAND test_tcp) \ No newline at end of file From e881472e7c6b0cd9c241dd74fe8558bd5936faf4 Mon Sep 17 00:00:00 2001 From: Carl Philipp Klemm Date: Mon, 13 Apr 2026 14:20:45 +0200 Subject: [PATCH 14/21] Add .desktop file --- smartvos.desktop | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 smartvos.desktop diff --git a/smartvos.desktop b/smartvos.desktop new file mode 100644 index 0000000..adc785e --- /dev/null +++ b/smartvos.desktop @@ -0,0 +1,10 @@ +[Desktop Entry] +Name=SmartVOS +Comment=Smart Home Interface +Exec=smartvos +Icon=smartvos +Terminal=false +Type=Application +Categories=Utility;HomeAutomation; +Keywords=smart;home;automation;iot; +StartupNotify=true \ No newline at end of file From 3794e0031b03ea34545cb02b07dc88b0d5ac638c Mon Sep 17 00:00:00 2001 From: Carl Philipp Klemm Date: Mon, 13 Apr 2026 15:13:47 +0200 Subject: [PATCH 15/21] Finis support for ENUM item types --- src/items/item.cpp | 52 +++++++++++++++++++++++++++++++++++++++++++ src/items/item.h | 8 ++++++- src/ui/itemwidget.cpp | 27 ++++++++++++++++++++++ src/ui/itemwidget.ui | 10 +++++++++ 4 files changed, 96 insertions(+), 1 deletion(-) diff --git a/src/items/item.cpp b/src/items/item.cpp index b902c32..14d31b3 100644 --- a/src/items/item.cpp +++ b/src/items/item.cpp @@ -60,6 +60,13 @@ void ItemData::storeWithChanges(QJsonObject& json, const ItemFieldChanges& chang json["Value"] = static_cast(value_); if(changes.groupName) json["GroupName"] = groupName_; + if(changes.valueNames) + { + QJsonArray valueNamesArray; + for(const QString& name : valueNames_) + valueNamesArray.append(name); + json["ValueNames"] = valueNamesArray; + } } void ItemData::load(const QJsonObject &json, const bool preserve) @@ -87,6 +94,19 @@ ItemFieldChanges ItemData::loadWithChanges(const QJsonObject& json, const bool p groupName_ = json["GroupName"].toString(); changes.groupName = true; } + if(json.contains("ValueType")) + { + type_ = static_cast(json["ValueType"].toInt()); + changes.type = true; + } + if(json.contains("ValueNames")) + { + valueNames_.clear(); + QJsonArray valueNamesArray = json["ValueNames"].toArray(); + for(int i = 0; i < valueNamesArray.size(); ++i) + valueNames_.push_back(valueNamesArray[i].toString()); + changes.valueNames = true; + } itemId_ = static_cast(json["ItemId"].toDouble(0)); } return changes; @@ -120,6 +140,8 @@ bool ItemData::hasChanged(const ItemData& other, const ItemFieldChanges& changes return true; if(changes.actors) return true; + if(changes.valueNames && other.getValueNames() != getValueNames()) + return true; return false; } @@ -148,6 +170,33 @@ void ItemData::setGroupName(QString groupName) groupName_ = groupName; } +std::vector ItemData::getValueNames() const +{ + return valueNames_; +} + +void ItemData::setValueNames(std::vector valueNames) +{ + valueNames_ = std::move(valueNames); +} + +int ItemData::valueNameToIndex(const QString& name) const +{ + for(size_t i = 0; i < valueNames_.size(); ++i) + { + if(valueNames_[i] == name) + return static_cast(i); + } + return -1; +} + +QString ItemData::indexToValueName(int index) const +{ + if(index >= 0 && static_cast(index) < valueNames_.size()) + return valueNames_[index]; + return QString(); +} + //item Item::Item(uint32_t itemIdIn, QString name, uint8_t value, QObject *parent): QObject(parent), ItemData (itemIdIn, name, @@ -205,6 +254,7 @@ Item& Item::operator=(const ItemData& other) itemId_ = other.id(); hidden_ = other.isHidden(); groupName_ = other.getGroupName(); + valueNames_ = other.getValueNames(); return *this; } @@ -248,6 +298,8 @@ void Item::requestUpdate(ItemUpdateRequest update) for(std::shared_ptr& actor : update.newActors) addActor(actor); } + if(update.changes.valueNames) + valueNames_ = update.payload.getValueNames(); update.payload = *this; updated(update); } diff --git a/src/items/item.h b/src/items/item.h index b12575c..4a70fae 100644 --- a/src/items/item.h +++ b/src/items/item.h @@ -71,6 +71,10 @@ public: item_value_type_t getValueType(); QString getGroupName() const; void setGroupName(QString groupName); + std::vector getValueNames() const; + void setValueNames(std::vector valueNames); + int valueNameToIndex(const QString& name) const; + QString indexToValueName(int index) const; void storeWithChanges(QJsonObject& json, const ItemFieldChanges& changes); ItemFieldChanges loadWithChanges(const QJsonObject& json, const bool preserve = false); virtual QString getName() const; @@ -130,6 +134,7 @@ struct ItemFieldChanges bool type :1; bool groupName :1; bool actors :1; + bool valueNames :1; ItemFieldChanges(bool defaultVal = false) { name = defaultVal; @@ -138,10 +143,11 @@ struct ItemFieldChanges type = defaultVal; groupName = defaultVal; actors = false; + valueNames = defaultVal; } inline bool isNone() const { - return !name && !value && !hidden && !type && !groupName && !actors; + return !name && !value && !hidden && !type && !groupName && !actors && !valueNames; } }; diff --git a/src/ui/itemwidget.cpp b/src/ui/itemwidget.cpp index de7544e..3846911 100644 --- a/src/ui/itemwidget.cpp +++ b/src/ui/itemwidget.cpp @@ -2,6 +2,7 @@ #include "ui_itemwidget.h" #include +#include #include #include #include "itemsettingsdialog.h" @@ -26,6 +27,16 @@ ItemWidget::ItemWidget(std::weak_ptr item, bool noGroupEdit, QWidget *pare ui->checkBox->hide(); ui->slider->hide(); } + else if(workingItem->getValueType() == ITEM_VALUE_ENUM) + { + ui->slider->hide(); + ui->checkBox->hide(); + QStringList list; + for(const QString& name : workingItem->getValueNames()) + list.append(name); + ui->comboBox->addItems(list); + ui->comboBox->setCurrentIndex(workingItem->getValue()); + } else { ui->slider->hide(); @@ -37,6 +48,8 @@ ItemWidget::ItemWidget(std::weak_ptr item, bool noGroupEdit, QWidget *pare if(workingItem->getValueType() == ITEM_VALUE_UINT) connect(ui->slider, &QSlider::valueChanged, this, &ItemWidget::moveToValue); + else if(workingItem->getValueType() == ITEM_VALUE_ENUM) + connect(ui->comboBox, QOverload::of(&QComboBox::currentIndexChanged), this, &ItemWidget::moveToValue); else connect(ui->checkBox, &QCheckBox::toggled, this, &ItemWidget::moveToState); connect(ui->pushButton, &QPushButton::clicked, this, &ItemWidget::showSettingsDialog); @@ -84,6 +97,7 @@ void ItemWidget::disable() ui->checkBox->setEnabled(false); ui->label->setEnabled(false); ui->slider->setEnabled(false); + ui->comboBox->setEnabled(false); ui->pushButton_Remove->setEnabled(false); } @@ -115,6 +129,16 @@ std::weak_ptr ItemWidget::getItem() void ItemWidget::onItemUpdated(ItemUpdateRequest update) { + if(update.changes.valueNames) + { + ui->comboBox->blockSignals(true); + ui->comboBox->clear(); + QStringList list; + for(const QString& name : update.payload.getValueNames()) + list.append(name); + ui->comboBox->addItems(list); + ui->comboBox->blockSignals(false); + } stateChanged(update.payload.getValue()); } @@ -126,6 +150,9 @@ void ItemWidget::stateChanged(int state) ui->checkBox->blockSignals(true); ui->checkBox->setChecked(state); ui->checkBox->blockSignals(false); + ui->comboBox->blockSignals(true); + ui->comboBox->setCurrentIndex(state); + ui->comboBox->blockSignals(false); } ItemWidget::~ItemWidget() diff --git a/src/ui/itemwidget.ui b/src/ui/itemwidget.ui index 96b625e..a1e4bf0 100644 --- a/src/ui/itemwidget.ui +++ b/src/ui/itemwidget.ui @@ -59,6 +59,16 @@ + + + + + 0 + 0 + + + + From be303aa85178da5fffb883590dfa851bbf5a933f Mon Sep 17 00:00:00 2001 From: Carl Philipp Klemm Date: Fri, 17 Apr 2026 18:30:43 +0200 Subject: [PATCH 16/21] Support power, engery and voltage sensors in sensors and mqtt --- src/sensors/mqttsensorsource.cpp | 24 ++++++++++++++++++++++++ src/sensors/sensor.h | 9 +++++++++ 2 files changed, 33 insertions(+) diff --git a/src/sensors/mqttsensorsource.cpp b/src/sensors/mqttsensorsource.cpp index 0853abf..be4ec20 100644 --- a/src/sensors/mqttsensorsource.cpp +++ b/src/sensors/mqttsensorsource.cpp @@ -147,6 +147,30 @@ void MqttSensorSource::onMessageReceived(const QMqttMessage& message) sensor.field = obj["voc"].toDouble(0); stateChanged(sensor); } + + if(obj.contains("power")) + { + sensor.name = baseName + " Power"; + sensor.type = Sensor::TYPE_POWER; + sensor.field = obj["Power"].toDouble(0); + stateChanged(sensor); + } + + if(obj.contains("energy")) + { + sensor.name = baseName + " Energy"; + sensor.type = Sensor::TYPE_ENERGY_USE; + sensor.field = obj["energy"].toDouble(0); + stateChanged(sensor); + } + + if(obj.contains("voltage")) + { + sensor.name = baseName + " Voltage"; + sensor.type = Sensor::TYPE_VOLTAGE; + sensor.field = obj["voltage"].toDouble(0); + stateChanged(sensor); + } } } diff --git a/src/sensors/sensor.h b/src/sensors/sensor.h index 86fa5cb..d24090a 100644 --- a/src/sensors/sensor.h +++ b/src/sensors/sensor.h @@ -22,6 +22,9 @@ public: TYPE_FORMALDEHYD, TYPE_PM25, TYPE_TOTAL_VOC, + TYPE_ENERGY_USE, + TYPE_POWER, + TYPE_VOLTAGE, TYPE_LOWBATTERY = 128, TYPE_SHUTDOWN_IMMINENT = 251, TYPE_OCUPANCY, @@ -145,6 +148,12 @@ public: return "ppb"; case TYPE_SUN_ALTITUDE: return "°"; + case TYPE_POWER: + return "W"; + case TYPE_ENERGY_USE: + return "kWh"; + case TYPE_VOLTAGE: + return "V"; default: return ""; } From cb05c2237e18af87d3f0425d23a06ac2f054d810 Mon Sep 17 00:00:00 2001 From: Carl Philipp Klemm Date: Fri, 17 Apr 2026 18:31:42 +0200 Subject: [PATCH 17/21] Dont show combo box on non-enum items --- src/ui/itemwidget.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/ui/itemwidget.cpp b/src/ui/itemwidget.cpp index 3846911..336ffb2 100644 --- a/src/ui/itemwidget.cpp +++ b/src/ui/itemwidget.cpp @@ -21,11 +21,13 @@ ItemWidget::ItemWidget(std::weak_ptr item, bool noGroupEdit, QWidget *pare { ui->horizontalSpacer->changeSize(0,0); ui->checkBox->hide(); + ui->comboBox->hide(); } else if(workingItem->getValueType() == ITEM_VALUE_NO_VALUE) { ui->checkBox->hide(); ui->slider->hide(); + ui->comboBox->hide(); } else if(workingItem->getValueType() == ITEM_VALUE_ENUM) { @@ -40,6 +42,7 @@ ItemWidget::ItemWidget(std::weak_ptr item, bool noGroupEdit, QWidget *pare else { ui->slider->hide(); + ui->comboBox->hide(); } ui->checkBox->setChecked(workingItem->getValue()); From 869067c2f9d82d85c25b7d68ebe97d82f4648300 Mon Sep 17 00:00:00 2001 From: Carl Philipp Klemm Date: Fri, 17 Apr 2026 18:32:41 +0200 Subject: [PATCH 18/21] Test: Add mqttitem unit tests --- tests/CMakeLists.txt | 4 +- tests/unit/items/test_mqttitem.cpp | 306 +++++++++++++++++++++++++++++ 2 files changed, 309 insertions(+), 1 deletion(-) create mode 100644 tests/unit/items/test_mqttitem.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index dabb4fd..bddc2f4 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -12,10 +12,11 @@ add_executable(test_sensor unit/sensors/test_sensor.cpp) add_executable(test_actor unit/actors/test_actor.cpp) add_executable(test_itemstore unit/items/test_itemstore.cpp) add_executable(test_itemloadersource unit/items/test_itemloadersource.cpp) +add_executable(test_mqttitem unit/items/test_mqttitem.cpp) add_executable(test_tcp unit/service/test_tcp.cpp) # Link all tests to static library -foreach(test test_item test_sensor test_actor test_itemstore test_itemloadersource test_tcp) +foreach(test test_item test_sensor test_actor test_itemstore test_itemloadersource test_mqttitem test_tcp) target_link_libraries(${test} smartvos_core Qt6::Core @@ -41,4 +42,5 @@ add_test(NAME test_sensor COMMAND test_sensor) add_test(NAME test_actor COMMAND test_actor) add_test(NAME test_itemstore COMMAND test_itemstore) add_test(NAME test_itemloadersource COMMAND test_itemloadersource) +add_test(NAME test_mqttitem COMMAND test_mqttitem) add_test(NAME test_tcp COMMAND test_tcp) \ No newline at end of file diff --git a/tests/unit/items/test_mqttitem.cpp b/tests/unit/items/test_mqttitem.cpp new file mode 100644 index 0000000..27a8940 --- /dev/null +++ b/tests/unit/items/test_mqttitem.cpp @@ -0,0 +1,306 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "items/mqttitem.h" +#include "mqttclient.h" +#include "programmode.h" + +class TestMqttItem : public QObject +{ + Q_OBJECT + +private slots: + void initTestCase() + { + // Setup for all tests + // Try to load config and connect to MQTT broker if configured + QString settingsPath = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + "/shinterface.json"; + + QJsonObject json; + if (QFile::exists(settingsPath)) { + QFile file(settingsPath); + if (file.open(QIODevice::ReadOnly)) { + QByteArray data = file.readAll(); + file.close(); + QJsonParseError error; + QJsonDocument doc = QJsonDocument::fromJson(data, &error); + if (error.error == QJsonParseError::NoError) { + json = doc.object(); + } + } + } + + QJsonObject mqttJson = json["Mqtt"].toObject(); + QString host = mqttJson["Host"].toString(); + int port = mqttJson["Port"].toInt(1883); + + // If MQTT is configured with a host, try to connect + static std::shared_ptr mqttClient; + if (!host.isEmpty()) { + qDebug() << "MQTT configured:" << host << port; + mqttClient = std::make_shared(); + mqttClient->start(mqttJson); + // Give it a moment to connect + QTest::qWait(1000); + + // Check if connected or connecting + auto qClient = mqttClient->getClient(); + if (qClient && (qClient->state() == QMqttClient::Connected || + qClient->state() == QMqttClient::Connecting)) { + qDebug() << "MQTT connected/connecting, using client"; + MqttItem::client = mqttClient; + } else { + qDebug() << "MQTT connection failed, using UI_ONLY mode"; + programMode = PROGRAM_MODE_UI_ONLY; + } + } else { + qDebug() << "No MQTT host configured, using UI_ONLY mode"; + programMode = PROGRAM_MODE_UI_ONLY; + } + } + + void testMqttItemCreation() + { + MqttItem item("test_mqtt", 0); + QCOMPARE(item.getName(), QString("test_mqtt")); + QVERIFY(item.getTopic().isEmpty()); + QVERIFY(item.getValueKey() == "state"); + } + + void testMqttItemSetTopic() + { + MqttItem item("test_mqtt", 0); + item.setTopic("my_device"); + item.setValueKey("state"); + + QVERIFY(item.getTopic() == "my_device"); + QVERIFY(item.getValueKey() == "state"); + } + + void testMqttItemSetValueType() + { + MqttItem item("test_mqtt", 0); + + // Default should be BOOL + QVERIFY(item.getValueType() == ITEM_VALUE_BOOL); + + // Set to UINT + item.setValueType(ITEM_VALUE_UINT); + QVERIFY(item.getValueType() == ITEM_VALUE_UINT); + + // Set to ENUM + item.setValueType(ITEM_VALUE_ENUM); + QVERIFY(item.getValueType() == ITEM_VALUE_ENUM); + } + + void testMqttItemValueNames() + { + MqttItem item("test_mqtt", 0); + + // Initially empty + QVERIFY(item.getValueNames().empty()); + + // Set value names + std::vector names = {"off", "heat", "cool"}; + item.setValueNames(names); + + auto storedNames = item.getValueNames(); + QVERIFY(storedNames.size() == 3); + QVERIFY(storedNames[0] == "off"); + QVERIFY(storedNames[1] == "heat"); + QVERIFY(storedNames[2] == "cool"); + } + + void testValueNameConversion() + { + MqttItem item("test_mqtt", 0); + + // Set value names for enum + std::vector names = {"off", "heat", "cool", "auto"}; + item.setValueNames(names); + item.setValueType(ITEM_VALUE_ENUM); + + // Test name to index + QVERIFY(item.valueNameToIndex("heat") == 1); + QVERIFY(item.valueNameToIndex("cool") == 2); + QVERIFY(item.valueNameToIndex("unknown") == -1); + + // Test index to name + QVERIFY(item.indexToValueName(0) == "off"); + QVERIFY(item.indexToValueName(3) == "auto"); + QVERIFY(item.indexToValueName(99).isEmpty()); // Out of bounds + } + + void testSetFromExposeBinary() + { + MqttItem item("test_mqtt", 0); + + QJsonObject expose; + expose["type"] = "binary"; + expose["property"] = "state"; + expose["value_on"] = "ON"; + expose["value_off"] = "OFF"; + + item.setFromExpose(expose); + + QVERIFY(item.getValueType() == ITEM_VALUE_BOOL); + QVERIFY(item.getValueKey() == "state"); + QVERIFY(item.getValueOn() == "ON"); + QVERIFY(item.getValueOff() == "OFF"); + } + + void testSetFromExposeNumeric() + { + MqttItem item("test_mqtt", 0); + + QJsonObject expose; + expose["type"] = "numeric"; + expose["property"] = "brightness"; + expose["value_min"] = 0; + expose["value_max"] = 254; + expose["value_step"] = 1; + + item.setFromExpose(expose); + + QVERIFY(item.getValueType() == ITEM_VALUE_UINT); + QVERIFY(item.getValueKey() == "brightness"); + QVERIFY(item.getValueMin() == 0); + QVERIFY(item.getValueMax() == 254); + QVERIFY(item.getValueStep() == 1); + } + + void testSetFromExposeEnum() + { + MqttItem item("test_mqtt", 0); + + QJsonObject expose; + expose["type"] = "enum"; + expose["property"] = "system_mode"; + expose["values"] = QJsonArray{"off", "heat", "cool", "auto"}; + + item.setFromExpose(expose); + + QVERIFY(item.getValueType() == ITEM_VALUE_ENUM); + QVERIFY(item.getValueKey() == "system_mode"); + + auto names = item.getValueNames(); + QVERIFY(names.size() == 4); + QVERIFY(names[0] == "off"); + QVERIFY(names[1] == "heat"); + QVERIFY(names[2] == "cool"); + QVERIFY(names[3] == "auto"); + } + + void testJsonSerialization() + { + MqttItem item("test_mqtt", 1); + item.setTopic("my_device"); + item.setValueKey("state"); + item.setValueOn("ON"); + item.setValueOff("OFF"); + + QJsonObject json; + item.store(json); + + QVERIFY(json["Type"] == "Mqtt"); + QVERIFY(json["Topic"] == "my_device"); + QVERIFY(json["ValueKey"] == "state"); + QVERIFY(json["ValueOn"] == "ON"); + QVERIFY(json["ValueOff"] == "OFF"); + } + + void testJsonDeserialization() + { + QJsonObject json; + json["Type"] = "Mqtt"; + json["ItemId"] = 100; + json["Name"] = "loaded_mqtt"; + json["Topic"] = "test_device"; + json["ValueKey"] = "state"; + json["ValueOn"] = "ON"; + json["ValueOff"] = "OFF"; + json["Value"] = 1; + + MqttItem item; + item.load(json); + + QVERIFY(item.getTopic() == "test_device"); + QVERIFY(item.getValueKey() == "state"); + QVERIFY(item.getValueOn() == "ON"); + QVERIFY(item.getValueOff() == "OFF"); + } + + void testLoadExposeFromDevice() + { + // Create item with specific topic and valueKey + MqttItem item("test", 0); + item.setTopic("0xa4c138ef510950e3"); + item.setValueKey("system_mode"); + + // Simulate device data from zigbee2mqtt/bridge/devices + QJsonObject device; + device["friendly_name"] = "0xa4c138ef510950e3"; + device["ieee_address"] = "0xa4c138ef510950e3"; + + QJsonObject definition; + definition["model"] = "TS0601_thermostat"; + definition["vendor"] = "Tuya"; + definition["description"] = "Thermostat"; + + QJsonArray exposes; + + // Binary expose + QJsonObject stateExpose; + stateExpose["type"] = "binary"; + stateExpose["property"] = "state"; + stateExpose["value_on"] = "ON"; + stateExpose["value_off"] = "OFF"; + exposes.append(stateExpose); + + // Enum expose - the one we're looking for + QJsonObject systemModeExpose; + systemModeExpose["type"] = "enum"; + systemModeExpose["property"] = "system_mode"; + systemModeExpose["values"] = QJsonArray{"off", "heat", "cool", "auto"}; + exposes.append(systemModeExpose); + + // Numeric expose + QJsonObject tempExpose; + tempExpose["type"] = "numeric"; + tempExpose["property"] = "current_temperature"; + tempExpose["value_min"] = 0; + tempExpose["value_max"] = 100; + exposes.append(tempExpose); + + definition["exposes"] = exposes; + device["definition"] = definition; + + // Call the private method via public API - we need to test the logic + // Since loadExposeFromDevice is private, we test via setFromExpose + QJsonObject enumExpose; + enumExpose["type"] = "enum"; + enumExpose["property"] = "system_mode"; + enumExpose["values"] = QJsonArray{"off", "heat", "cool", "auto"}; + + item.setFromExpose(enumExpose); + + QVERIFY(item.getValueType() == ITEM_VALUE_ENUM); + QVERIFY(item.getValueNames().size() == 4); + } + + void cleanupTestCase() + { + // Cleanup after all tests + } +}; + +QTEST_APPLESS_MAIN(TestMqttItem) + +#include "test_mqttitem.moc" \ No newline at end of file From 25dd99d8b6b427c7e70f918e34d37a5b91888805 Mon Sep 17 00:00:00 2001 From: Carl Philipp Klemm Date: Fri, 17 Apr 2026 18:33:39 +0200 Subject: [PATCH 19/21] Remove unused mqttitemsource files --- src/items/mqttitemsource.cpp | 205 ----------------------------------- src/items/mqttitemsource.h | 34 ------ 2 files changed, 239 deletions(-) delete mode 100644 src/items/mqttitemsource.cpp delete mode 100644 src/items/mqttitemsource.h diff --git a/src/items/mqttitemsource.cpp b/src/items/mqttitemsource.cpp deleted file mode 100644 index eb3c11c..0000000 --- a/src/items/mqttitemsource.cpp +++ /dev/null @@ -1,205 +0,0 @@ -#include "mqttitemsource.h" - -#include -#include -#include - -MqttItemSource::MqttItemSource(QObject *parent) - : ItemSource(parent) -{ -} - -MqttItemSource::~MqttItemSource() -{ -} - -void MqttItemSource::start(const QJsonObject& settings) -{ - baseTopicName_ = settings["BaseTopic"].toString("zigbee2mqtt"); - - connect(&client_, &QMqttClient::stateChanged, this, &MqttItemSource::onClientStateChanged); - connect(&client_, &QMqttClient::errorChanged, this, &MqttItemSource::onClientError); - - client_.setHostname(settings["Host"].toString("127.0.0.1")); - client_.setPort(settings["Port"].toInt(1883)); - if(settings.contains("User")) - client_.setUsername(settings["User"].toString()); - if(settings.contains("Password")) - client_.setPassword(settings["Password"].toString()); - client_.setProtocolVersion(QMqttClient::MQTT_5_0); - - client_.connectToHost(); - - QJsonArray itemsArray = settings["Items"].toArray(); - - for(QJsonValueRef itemRef : itemsArray) - { - QJsonObject itemObject = itemRef.toObject(); - if(!itemObject.contains("Topic")) - continue; - - MqttItemConfig config; - config.topic = itemObject["Topic"].toString(); - config.name = itemObject.contains("Name") ? itemObject["Name"].toString() : config.topic; - config.valueKey = itemObject["ValueKey"].toString("state"); - config.valueOn = itemObject["ValueOn"].toString("ON"); - config.valueOff = itemObject["ValueOff"].toString("OFF"); - - // Determine value type - QString valueTypeStr = itemObject["ValueType"].toString("bool"); - if(valueTypeStr == "uint") - config.valueType = ITEM_VALUE_UINT; - else - config.valueType = ITEM_VALUE_BOOL; - - config.id = qHash(baseTopicName_ + "/" + config.topic); - - // Create the item - config.item = std::make_shared(config.id, config.name, 0); - config.item->setTopic(config.topic); - config.item->setValueKey(config.valueKey); - config.item->setBaseTopic(baseTopicName_); - config.item->setValueOn(config.valueOn); - config.item->setValueOff(config.valueOff); - config.item->setMqttClient(&client_); - - items_.push_back(config); - } -} - -MqttItemSource::MqttItemConfig* MqttItemSource::findConfig(const QString& topic) -{ - for(MqttItemConfig& config : items_) - { - if(baseTopicName_ + "/" + config.topic == topic) - return &config; - } - return nullptr; -} - -void MqttItemSource::onClientError(QMqttClient::ClientError error) -{ - qWarning() << "MqttItemSource Client error:" << error; -} - -void MqttItemSource::onClientStateChanged(QMqttClient::ClientState state) -{ - if(state == QMqttClient::ClientState::Connected) - { - qInfo() << "MqttItemSource connected to MQTT broker at " << client_.hostname() << client_.port(); - for(MqttItemConfig& config : items_) - { - // Subscribe to state topic to receive updates from devices - QString stateTopic = baseTopicName_ + "/" + config.topic; - qDebug() << "MqttItemSource subscribing to" << stateTopic; - QMqttSubscription* subscription = client_.subscribe(stateTopic); - if(subscription) - { - connect(subscription, &QMqttSubscription::messageReceived, this, &MqttItemSource::onMessageReceived); - } - } - } - else if(state == QMqttClient::ClientState::Disconnected) - { - qWarning() << "MqttItemSource lost connection to MQTT broker"; - } - else if(state == QMqttClient::ClientState::Connecting) - { - qInfo() << "MqttItemSource connecting to MQTT broker at " << client_.hostname() << client_.port(); - } -} - -void MqttItemSource::onMessageReceived(const QMqttMessage& message) -{ - MqttItemConfig* config = findConfig(message.topic().name()); - if(!config) - return; - - QJsonDocument doc = QJsonDocument::fromJson(message.payload()); - if(doc.isObject()) - { - QJsonObject obj = doc.object(); - QString valueKey = config->valueKey; - - if(obj.contains(valueKey)) - { - uint8_t newValue = 0; - - // Handle different value types - if(config->valueType == ITEM_VALUE_UINT) - { - // Numeric value (brightness, etc.) - newValue = obj[valueKey].toInt(0); - } - else - { - // Binary value (state on/off, etc.) - QString value = obj[valueKey].toString(); - if(value == config->valueOn || value == "ON" || value == "true") - newValue = 1; - else if(value == config->valueOff || value == "OFF" || value == "false") - newValue = 0; - } - - // Only update if value changed - if(config->item->getValue() != newValue) - { - ItemUpdateRequest update; - update.type = ITEM_UPDATE_BACKEND; - update.payload = *config->item; - update.payload.setValueData(newValue); - update.changes.value = true; - config->item->requestUpdate(update); - } - } - } -} - -void MqttItemSource::refresh() -{ - std::vector requests; - - for(MqttItemConfig& config : items_) - { - ItemAddRequest request; - request.type = ITEM_UPDATE_BACKEND; - request.payload = config.item; - request.changes = ItemFieldChanges(true); - requests.push_back(request); - } - - gotItems(requests); -} - -void MqttItemSource::store(QJsonObject& json) -{ - json["Host"] = client_.hostname(); - json["Port"] = client_.port(); - json["BaseTopic"] = baseTopicName_; - if(client_.username() != "") - json["User"] = client_.username(); - if(client_.password() != "") - json["Password"] = client_.password(); - - QJsonArray itemsArray; - for(const MqttItemConfig& config : items_) - { - QJsonObject itemObject; - itemObject["Name"] = config.name; - itemObject["Topic"] = config.topic; - itemObject["ValueKey"] = config.valueKey; - itemObject["ValueOn"] = config.valueOn; - itemObject["ValueOff"] = config.valueOff; - if(config.valueType == ITEM_VALUE_UINT) - itemObject["ValueType"] = "uint"; - else - itemObject["ValueType"] = "bool"; - itemsArray.append(itemObject); - } - json["Items"] = itemsArray; -} - -QMqttClient* MqttItemSource::getClient() -{ - return &client_; -} \ No newline at end of file diff --git a/src/items/mqttitemsource.h b/src/items/mqttitemsource.h deleted file mode 100644 index 080e8a8..0000000 --- a/src/items/mqttitemsource.h +++ /dev/null @@ -1,34 +0,0 @@ -#ifndef MQTTITEMSOURCE_H -#define MQTTITEMSOURCE_H - -#include -#include -#include -#include -#include - -#include "itemsource.h" -#include "mqttitem.h" - -class MqttItemSource : public ItemSource -{ - Q_OBJECT - - QString baseTopicName_; - QMqttClient client_; - -private slots: - void onClientStateChanged(QMqttClient::ClientState state); - void onMessageReceived(const QMqttMessage& message); - void onClientError(QMqttClient::ClientError error); - -public: - explicit MqttItemSource(QObject *parent = nullptr); - virtual ~MqttItemSource() override; - virtual void refresh() override; - void start(const QJsonObject& settings); - void store(QJsonObject& json); - QMqttClient* getClient(); -}; - -#endif // MQTTITEMSOURCE_H \ No newline at end of file From 05e4fb1cc180d690989a7f695b4948e7af7053f4 Mon Sep 17 00:00:00 2001 From: Carl Philipp Klemm Date: Fri, 17 Apr 2026 18:35:16 +0200 Subject: [PATCH 20/21] Auto mqttitem attempt --- src/items/mqttitem.cpp | 239 +++++++++++++++++++++++++++++++++++++++-- src/items/mqttitem.h | 23 +++- src/mqttclient.cpp | 3 +- 3 files changed, 254 insertions(+), 11 deletions(-) diff --git a/src/items/mqttitem.cpp b/src/items/mqttitem.cpp index 76c5264..9c2ea78 100644 --- a/src/items/mqttitem.cpp +++ b/src/items/mqttitem.cpp @@ -1,5 +1,6 @@ #include "mqttitem.h" +#include #include #include #include @@ -26,16 +27,35 @@ MqttItem::~MqttItem() { qDebug()<<__func__; std::shared_ptr workClient = client.lock(); - if(!workClient || topic_.isEmpty() || !subscription) + if(!workClient) return; - workClient->unsubscribe(subscription); + if(subscription) + { + disconnect(subscription->subscription, &QMqttSubscription::messageReceived, this, &MqttItem::onMessageReceived); + workClient->unsubscribe(subscription); + } + + if(devicesSubscription) + { + disconnect(devicesSubscription->subscription, &QMqttSubscription::messageReceived, this, &MqttItem::onDevicesMessageReceived); + workClient->unsubscribe(devicesSubscription); + } } void MqttItem::onClientStateChanged(QMqttClient::ClientState state) { if(state == QMqttClient::Connected) + { refreshSubscription(); + // Subscribe to bridge/devices to get exposes + std::shared_ptr workClient = client.lock(); + if(workClient && !exposeLoaded_) + { + devicesSubscription = workClient->subscribe(workClient->getBaseTopic() + "/bridge/devices"); + connect(devicesSubscription->subscription, &QMqttSubscription::messageReceived, this, &MqttItem::onDevicesMessageReceived); + } + } } void MqttItem::refreshSubscription() @@ -57,6 +77,101 @@ void MqttItem::refreshSubscription() connect(subscription->subscription, &QMqttSubscription::messageReceived, this, &MqttItem::onMessageReceived); } +void MqttItem::onDevicesMessageReceived(const QMqttMessage& message) +{ + if(exposeLoaded_) + return; + + QJsonDocument doc = QJsonDocument::fromJson(message.payload()); + if(!doc.isArray()) + return; + + QJsonArray devices = doc.array(); + for(const QJsonValue& deviceValue : devices) + { + if(!deviceValue.isObject()) + continue; + + QJsonObject device = deviceValue.toObject(); + QString ieeeAddr = device["ieee_address"].toString(); + + // Check if this device matches our topic (friendly_name) + QString friendlyName = device["friendly_name"].toString(); + if(friendlyName == topic_) + { + loadExposeFromDevice(device); + exposeLoaded_ = true; + + // Unsubscribe from devices topic since we found our device + std::shared_ptr workClient = client.lock(); + if(workClient && devicesSubscription) + { + disconnect(devicesSubscription->subscription, &QMqttSubscription::messageReceived, this, &MqttItem::onDevicesMessageReceived); + workClient->unsubscribe(devicesSubscription); + devicesSubscription = nullptr; + } + break; + } + } +} + +void MqttItem::loadExposeFromDevice(const QJsonObject& device) +{ + // Get definition - may be null for unsupported devices + QJsonObject definition = device["definition"].toObject(); + if(definition.isEmpty()) + { + qWarning() << "MqttItem" << topic_ << "device has no definition (unsupported)"; + return; + } + + // Get exposes from definition + QJsonArray exposes = definition["exposes"].toArray(); + if(exposes.isEmpty()) + { + qWarning() << "MqttItem" << topic_ << "device has no exposes"; + return; + } + + for(const QJsonValue& exposeValue : exposes) + { + if(!exposeValue.isObject()) + continue; + + QJsonObject expose = exposeValue.toObject(); + QString property = expose["property"].toString(); + + // Check if this expose matches our valueKey + if(property == valueKey_) + { + setFromExpose(expose); + qDebug() << "MqttItem" << topic_ << "detected type" << expose["type"].toString() << "for property" << valueKey_; + return; + } + + // Check if it's a composite type with features + if(expose["type"].toString() == "composite" || expose["type"].toString() == "light") + { + QJsonArray features = expose["features"].toArray(); + for(const QJsonValue& featureValue : features) + { + if(!featureValue.isObject()) + continue; + + QJsonObject feature = featureValue.toObject(); + if(feature["property"].toString() == valueKey_) + { + setFromExpose(feature); + qDebug() << "MqttItem" << topic_ << "detected type" << feature["type"].toString() << "for property" << valueKey_; + return; + } + } + } + } + + qWarning() << "MqttItem" << topic_ << "could not find expose for property" << valueKey_; +} + void MqttItem::onMessageReceived(const QMqttMessage& message) { QJsonDocument doc = QJsonDocument::fromJson(message.payload()); @@ -65,13 +180,32 @@ void MqttItem::onMessageReceived(const QMqttMessage& message) QJsonObject obj = doc.object(); if(obj.contains(getValueKey())) { - QString value = obj[getValueKey()].toString(); + QJsonValue value = obj[getValueKey()]; ItemUpdateRequest req = createValueUpdateRequest(ITEM_UPDATE_BACKEND); req.changes.value = true; - if(value == getValueOn()) - req.payload.setValueData(true); + + if(getValueType() == ITEM_VALUE_UINT) + { + // Numeric value + req.payload.setValueData(value.toInt(0)); + } + else if(getValueType() == ITEM_VALUE_ENUM) + { + // Enum value - find index + QString strValue = value.toString(); + int index = valueNameToIndex(strValue); + if(index >= 0) + req.payload.setValueData(index); + } else - req.payload.setValueData(false); + { + // Binary value + QString strValue = value.toString(); + if(strValue == getValueOn() || strValue == "ON" || strValue == "true") + req.payload.setValueData(true); + else + req.payload.setValueData(false); + } requestUpdate(req); } } @@ -106,6 +240,59 @@ void MqttItem::setValueOff(const QString& valueOff) valueOff_ = valueOff; } +void MqttItem::setValueMin(int min) +{ + valueMin_ = min; +} + +void MqttItem::setValueMax(int max) +{ + valueMax_ = max; +} + +void MqttItem::setValueStep(int step) +{ + valueStep_ = step; +} + +void MqttItem::setValueType(item_value_type_t type) +{ + type_ = type; +} + +void MqttItem::setFromExpose(const QJsonObject& expose) +{ + QString type = expose["type"].toString(); + QString property = expose["property"].toString(); + + setValueKey(property); + + if(type == "binary") + { + type_ = ITEM_VALUE_BOOL; + setValueOn(expose["value_on"].toString("ON")); + setValueOff(expose["value_off"].toString("OFF")); + } + else if(type == "numeric") + { + type_ = ITEM_VALUE_UINT; + setValueMin(expose["value_min"].toInt(0)); + setValueMax(expose["value_max"].toInt(255)); + setValueStep(expose["value_step"].toInt(1)); + } + else if(type == "enum") + { + type_ = ITEM_VALUE_ENUM; + QJsonArray values = expose["values"].toArray(); + std::vector valueNames; + for(const QJsonValue& v : values) + valueNames.push_back(v.toString()); + setValueNames(valueNames); + } + + hashId(); +} + QString MqttItem::getTopic() const { return topic_; @@ -126,6 +313,26 @@ QString MqttItem::getValueOff() const return valueOff_; } +int MqttItem::getValueMin() const +{ + return valueMin_; +} + +int MqttItem::getValueMax() const +{ + return valueMax_; +} + +int MqttItem::getValueStep() const +{ + return valueStep_; +} + +bool MqttItem::getExposeLoaded() const +{ + return exposeLoaded_; +} + void MqttItem::store(QJsonObject& json) { Item::store(json); @@ -134,6 +341,9 @@ void MqttItem::store(QJsonObject& json) json["ValueKey"] = valueKey_; json["ValueOn"] = valueOn_; json["ValueOff"] = valueOff_; + json["ValueMin"] = valueMin_; + json["ValueMax"] = valueMax_; + json["ValueStep"] = valueStep_; } void MqttItem::load(const QJsonObject& json, const bool preserve) @@ -143,6 +353,10 @@ void MqttItem::load(const QJsonObject& json, const bool preserve) valueKey_ = json["ValueKey"].toString("state"); valueOn_ = json["ValueOn"].toString("ON"); valueOff_ = json["ValueOff"].toString("OFF"); + valueMin_ = json["ValueMin"].toInt(0); + valueMax_ = json["ValueMax"].toInt(255); + valueStep_ = json["ValueStep"].toInt(1); + exposeLoaded_ = json["ExposeLoaded"].toBool(false); hashId(); refreshSubscription(); } @@ -156,7 +370,18 @@ void MqttItem::enactValue(uint8_t value) QString fullTopic = workClient->getBaseTopic() + "/" + topic_ + "/set"; QJsonObject payload; - payload[valueKey_] = value ? valueOn_ : valueOff_; + if(getValueType() == ITEM_VALUE_UINT) + { + payload[valueKey_] = static_cast(value); + } + else if(getValueType() == ITEM_VALUE_ENUM) + { + payload[valueKey_] = indexToValueName(value); + } + else + { + payload[valueKey_] = value ? valueOn_ : valueOff_; + } QJsonDocument doc(payload); QByteArray data = doc.toJson(QJsonDocument::Compact); diff --git a/src/items/mqttitem.h b/src/items/mqttitem.h index c092444..f31203c 100644 --- a/src/items/mqttitem.h +++ b/src/items/mqttitem.h @@ -15,15 +15,22 @@ public: private: QString topic_; QString valueKey_; - QString valueOn_; - QString valueOff_; + QString valueOn_ = "ON"; + QString valueOff_ = "OFF"; + int valueMin_ = 0; + int valueMax_ = 255; + int valueStep_ = 1; + bool exposeLoaded_ = false; MqttClient::Subscription* subscription = nullptr; + MqttClient::Subscription* devicesSubscription = nullptr; void hashId(); void refreshSubscription(); void onMessageReceived(const QMqttMessage& message); void onClientStateChanged(QMqttClient::ClientState state); + void onDevicesMessageReceived(const QMqttMessage& message); + void loadExposeFromDevice(const QJsonObject& device); public: explicit MqttItem(QString name = "MqttItem", @@ -36,12 +43,22 @@ public: void setBaseTopic(const QString& baseTopic); void setValueOn(const QString& valueOn); void setValueOff(const QString& valueOff); + void setValueMin(int min); + void setValueMax(int max); + void setValueStep(int step); + void setValueType(item_value_type_t type); + + // Configure from Zigbee2MQTT expose info + void setFromExpose(const QJsonObject& expose); QString getTopic() const; QString getValueKey() const; - QString getBaseTopic() const; QString getValueOn() const; QString getValueOff() const; + int getValueMin() const; + int getValueMax() const; + int getValueStep() const; + bool getExposeLoaded() const; virtual void store(QJsonObject& json) override; virtual void load(const QJsonObject& json, const bool preserve = false) override; diff --git a/src/mqttclient.cpp b/src/mqttclient.cpp index ab61375..eb504a6 100644 --- a/src/mqttclient.cpp +++ b/src/mqttclient.cpp @@ -16,11 +16,12 @@ void MqttClient::start(const QJsonObject& settings) client->setHostname(settings["Host"].toString("127.0.0.1")); client->setPort(settings["Port"].toInt(1883)); + client->setClientId(settings["ClientId"].toString("smartvos")); if(settings.contains("User")) client->setUsername(settings["User"].toString()); if(settings.contains("Password")) client->setPassword(settings["Password"].toString()); - client->setProtocolVersion(QMqttClient::MQTT_5_0); + client->setProtocolVersion(QMqttClient::MQTT_3_1); client->connectToHost(); } From 7c96b87a11193b7737f8e2ebb7acba8da5d326be Mon Sep 17 00:00:00 2001 From: Carl Philipp Klemm Date: Sun, 19 Apr 2026 19:22:49 +0200 Subject: [PATCH 21/21] Rename icons and .desktop files --- CMakeLists.txt | 4 ++-- UVOSicon.bmp | Bin 114798 -> 0 bytes resources.qrc | 2 +- src/sensors/mqttsensorsource.cpp | 2 +- src/ui/mainwindow.ui | 2 +- xyz.uvos.icon.bmp | Bin 0 -> 3210 bytes xyz.uvos.icon.png | Bin 0 -> 2949 bytes smartvos.desktop => xyz.uvos.smartvos.desktop | 6 +++--- 8 files changed, 8 insertions(+), 8 deletions(-) delete mode 100644 UVOSicon.bmp create mode 100644 xyz.uvos.icon.bmp create mode 100644 xyz.uvos.icon.png rename smartvos.desktop => xyz.uvos.smartvos.desktop (71%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6508688..fdf32b7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -222,10 +222,10 @@ install(TARGETS smartvos DESTINATION bin) install(TARGETS smartvos_core DESTINATION lib) # Install icon -install(FILES UVOSicon.bmp DESTINATION share/icons/hicolor/48x48/apps RENAME smartvos.png) +install(FILES xyz.uvos.icon.png DESTINATION share/icons/hicolor/128x128/apps) # Install .desktop file -install(FILES smartvos.desktop DESTINATION share/applications) +install(FILES xyz.uvos.smartvos.desktop DESTINATION share/applications) # Update icon cache (optional, for icon themes) install(CODE "execute_process(COMMAND gtk-update-icon-cache -f -t ${CMAKE_INSTALL_PREFIX}/share/icons/hicolor WORKING_DIRECTORY ${CMAKE_INSTALL_PREFIX})") diff --git a/UVOSicon.bmp b/UVOSicon.bmp deleted file mode 100644 index fd0d9a26a67d4f67f2fcec39e6e641809f3b4d85..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 114798 zcmeI5dz&0Z6~;H*Ap}rVLbwFNC4^guTn#D;Dk2gB$|XPmQM@1*1^v%Y;Fr?p@vHbf z^f!CbhqG1PJvF=4J6${Pd6J#lnckZ2x8HfssZ*zFo`2=@e;?Xf*gyII5B~q>(*HwS zM`>+s{r6vowr)PWwf%qliR~Z%+uE{!+rK^k;_F|1{aRV;va-W;ln@XCLO=)z0U;m+ zgn$qb0zyCt2mv7=1cZPP5CTF#2nYcoAOwVf5D)@FKnMr{As_^VfDjM@LO=)z0U;m+ zgn$qb0zyCt2mv7=1cZPP5CTF#2nYcoAOwVf5D)@FKnMr{As_^VfDjM@LO=)z0U;m+ zgn$qb0zyCt>=Obv-E`A4&ph+J%+6-^Y-ZozX+PL$KODB_cG{1E{dlK6AMAyl_LE>g z4feA>dvT}zyt9{r{UX@Q!G776%1 zGu*&MGr5O5@7{~u-MjP{?%_+}9xj^UHon~5#>HcICoeso3-)TT^TA#V_Ij{ib@uCE zZv=a@v$ul1-5Doym8LAkItq*r8hb%y&0(VR!60`FI>2QG#xr*TaJoDUn6kj$PwHTblRgX&4f$v zYAKz1xdf@4PR=D7PABIQ4c_9~Yp?wpM76dN!1X`+=%c;Tl=wJ)U9X!JREUp`^vLk~Ta56#9yAgEFvQI8DJ z63@jrZOD0*#6l&S6M15?gYz;MhV#Ps^_^hvcJ^Lp7q^Y4B~3YtPMo;?_S+fOQ%H08 z@L~S&^2;yZ;|b``TStIo*KN1m_T-aKl8Pr^N>bEnRIG%+iVD1(fK(@KGMrvfU@!3} z{2{eb@a~Q~?topb!=m#qM}XUX{P^*wpMIKjI(&c(G>k?5a~IVq%<0=q3cXSEckjLT zlFUT4%1=y3HiQ5yLWQWNvnjkdHRCL@#xA%NCuMxjj%ga|KuKJy45=ZWjGrp85CM-( z?HQ4*Jw&I-CED)kXtYCDpNIfX zgz|Dm`j{z_|BTXB+^o8S{i{Hg&MD~EnUZ!l6I#@fM7-%za z%S*~VShB`Q$P}fCHdG{-q0a{%cz|py1-b`(MG?z-zP(x=eLNkzlW-CUF=7==1@2cI zi8OI@_cN;lqu@M_55gn^x*;wRieorRRj}+EorqLdhV>%G-Y>mef>b%3Or7>qMb`_?8sGiQnKLVGYB7@$ zH;HayHW#Q7X@X0r69>!LO--|2E}>MtQ4`P4XTt}ihOo^9%DFToO)!e==+t0tc>kng za$V}>62l@^n#>9=QU_$$;Ys9Dq!TZ>v1TtQ?5(6iG4@{>;hgc zAx7YlON39(B|IHQhG7?Pp+6}>fGM+HF6Gk1GqcV9kt&BXu1>uXI0wKb)Jc_@CB)dp zF?Zj6w{dByG?7WJHBqvQ^IZBT%ojtUoKCJw#wF-PE`7Eiilvu1=L49HE#wkur8#qn z$soM2(`iJS*kw+E(-fR!njYuX>*kV>Y0f^*kRgLgX0k+@pc84&De&CnK9L$m)+N%! zIBxz4WXh#VqzO+)eT!-dsxzOoz>qZYysHyjV(owRcdy=I44isB9Yn&ACXTHB?i=3W zt+(C^VU|i0^(Re$PYx5!F;CaaB@e=36vfFKe)sEMK7*Pf*WKy^*o>kO@-Jj^-f~4UTLA zD{9C+8Q$yZ5G6CxO@#&OON1cmkDN;* z(nLP6m3S3tLU~HKL{_RUm>b?3lPP?Voleom(RMdS$iz3dwt^KOkdmm0{D8iTr{X=e z9ZrV06zRkGI?NTtb>!xu_=2lUS&POGM!8%-qBkF`z3< zgA+9ot~YT}O`IeFhI5HV5`jl)f^~B)L5XOyZly44YGRToUfP`7EDebBDLI`yYJyku zFB>qZDJ{<39N|;$=}2#?Os*n7ZL11br$m~ZOC$$4vf}$r+7-zG%Ug~Q==cKGhAwJS z#Hw_TG&zxSU5aiFI<>Os3~3s7b7++12P92ZSmd4f;uIs*h%~V`m;cqA8#bkq(lZQ0Px4vDdAF-CbmazB?2e2T^Kbrl<}x|(+Oy7QAysCkm>B%v+F(8@^{B~k4NAZHIcVOZ_B&Yk>dy; zO_Z0oo6CdnIG}5#a)2a8p)?VJw=(BAejjJ@>ZwzwMz|ECrn&7iFa#hU5LWrQG{yWt z9)z>u`A|Q4epLkc6h}8VB2C<&Rwmh*z_p?#3!LDUo72gX18h9LDq=c*1OYZ+FB#A= zJ;$cXBmK{1kQ%!vpVypPlgmW)u(vFvvmxkNirC!-U2 z%hRV%PkJU5J150DM5h=vAx*7hJSu`ST~~aa1fg}LQ!kfb6o<`cbJTG_n81x(O4KRR ziFu;&uJmd-0*qy1?QX71P|H1CL$$kPkRqL8)D%4((xlqma<(izl4S+Buo7uvQg~@^ zGdUnGD=<<;I=M74pyR_blXE^8r;$igu1+RRc!X9K2QjR$?}6%7C+89k=+w%bV-sW} z2^@84h)XbvadaywK29|fiFU6vK__-RYvMLFagq#;$CCpdH9@mhB5*e4fIKC1qH>^> z4@eVqqOKmoM0MiVXy)dKtQs0eQ(9BPC05V1vR_aWTtb>6k*poLMB4{+twJXIuWvgZJF>g5vB)XJU%(1VP9!lj|WNy&kVTsnI6DCCLilyJ$ViE&@W z_novWd^bXr(5Y9N8Y)Ph6tOCu6MyneyOB$|G#Q;p4zx0kMw;yVe3#ORsHv4X$11mb z>Q30@yF{9Bb8M+P^~u!gpCLa)TFNC7O$}XCEpVpmhYhroWd*r3Sp*K{TKN={9H6MF zgihzqozpk2R%928gG!_cMzvCbkMHJ+2)Pq!%7bvENwvEbxs>d?YvB|3A~CqcORc2% zn3X2KT_R1|E43n*=!)3~qmchhh{UgOi3pr2)VgmyU2mMNaVb%!WK2ipKr7`C(_vMy zXO@0Di@ecGC)U8Wvf_!VzolG)SICzZ_EzK)N} zsi)&G)$Ufj5yaIN5xFkWNVvqWBnQ~#uj2bo+7&~7%Uh1q2{+eJLGq-CRp}hhl~fM& za>=45zI3w6n@-&c%L?4h(LjIPlBkL2l^m#eJCZ#Idfl8!Q$v*lB=!sG)X-|>iZ^|t zu2|TcEGy`x6VgO_vy}*(fhU^NOQ$GJwE37!1ndTOOYSIH*xSn`lcrW`cUi4W2C0`z za4gb^sHv5$%$gu)UOCW9C#0#NaWu(+A!&+qA~~Q93G1?yN{o^qoB>@cdk(Na@Q5@; zI?+CI~lUHRuy|@_5nASd%A>6_G>FCKE7!PE|t)Uig8AN74h8Uu1F5BsZq`))G4PE_oxZ1 zSW3gf-h@lNffIVw%A8{p$Wyxub4uvcP?_tQGiTsQLZ|5IXcIUQSrMx@ahsYrNu-I{ zhY~IkHIXoH;-ZkIg_Apf-ox*qz=?XxRtl1vK%O;mCDKH4fOU3FToEV9-ial2LYf-N zc+4MsN_tG1mZ}p6v{E_1!d~N2qE5M|;~RSwfmdXyE$m&&B?9tR7WU%1t#&t8r-V+p zxnswU?HbILc(@5g!URHuL%qW{Tw62>40a9sKL26ivI4lYmoF2Cv?NH`Hf{XsIDo0B zR<>&xzm}!1;_2LQWJ5;P*-D?prSCiG0i10#@@cto08c`RNzX)`3jrY@1cZPP5CTF# z2nYcoAOwVf5D)@FKnMr{As_^Vz`77%H3!>XQP(tNyt)fJe*8Fdd8-;LC<~3H5N0gP zEjP+^M~uA}5~EhR&`u5;TaG8jLWTpmWREEl>gv75o~jHtrmoyrvFJwabTqy|+3iBG zcY?he?Bd0X?{)V6`|tBBM#JrcU>^qi=%bH*)7fvgjc0iL-L}#AIN0xl{h_lz2K!TI zpLF);V4rsOS!bUI`=YbIboSR^Uv~CYXMgML@15}qpW<~1$aL?^$HXVoom5tc=)9ug zyrSW}vPLhp0<9L!z^r!yv)=7!^`4Q+embvc1ZI5@nDt>ttHmRvGBb|ZPvaOr1I0de zigo;2JUYcXek~q5ihbe~>-hC)$FI)sZ*xKW4Su5kxpnD!CU!9zLg^h1<{;dE{#Z2;_OzX zQ{)nDcXqp0r=@f%S0`l3{ax!6`pa2IIZU%A8R{bc%6!l%`sNQ^_c=Hx7@^&g1YsN)sBD zhvL1l(>R?Dwlon3@>M&mAai4DlxWY68k_C7vEgcNY!nua2+fUk+__O691Yp*H7bqW z@)e^z<0Z$GCA(1^o*N5$%_!pSH5PTcy_^}Fu0$i#g5kvy - UVOSicon.bmp + xyz.uvos.icon.bmp diff --git a/src/sensors/mqttsensorsource.cpp b/src/sensors/mqttsensorsource.cpp index be4ec20..eaa23e1 100644 --- a/src/sensors/mqttsensorsource.cpp +++ b/src/sensors/mqttsensorsource.cpp @@ -152,7 +152,7 @@ void MqttSensorSource::onMessageReceived(const QMqttMessage& message) { sensor.name = baseName + " Power"; sensor.type = Sensor::TYPE_POWER; - sensor.field = obj["Power"].toDouble(0); + sensor.field = obj["power"].toDouble(0); stateChanged(sensor); } diff --git a/src/ui/mainwindow.ui b/src/ui/mainwindow.ui index 422862d..693bb9f 100644 --- a/src/ui/mainwindow.ui +++ b/src/ui/mainwindow.ui @@ -27,7 +27,7 @@ - :/images/UVOSicon.bmp:/images/UVOSicon.bmp + :/images/xyz.uvos.icon.bmp:/images/xyz.uvos.icon.bmp diff --git a/xyz.uvos.icon.bmp b/xyz.uvos.icon.bmp new file mode 100644 index 0000000000000000000000000000000000000000..846c190c8b1f60183cd48acf38898f4fdb8ad3d5 GIT binary patch literal 3210 zcmeH|*=kx*6owN;qEtjJLUFjq!u=<{}Tgzw&r~KDRd?_(C{;|4uwY@#FpN*WX3e=q|qeIlJyla~^wULRu~LKLKMc z6bcoKMcnChI=F*(xm?cU@jOAQ`2#7N5;ULBNdXs7Z6Xi|2!5~E`&rh$>5M)t827}>6Ja+K{6WruQ zfU;7r*AoX!`2BtvvQQ|%KO7D<8a30B@WP+Z=Vkn6vzc7e2sEM$qo607qS35Ys|;4D zRB(YdlmV3W6hAI77GyBD+bw#T4;e;5Pk+HrTj_K<8H^gng=)1b11RXBWhh~S$tQ!+ z9k@U(nM@`bMnTWyguhfO$uL^2meGd`>UzOqv4}!dh%n5!fE7lT>>3Js3Y9`qxE^<4 zZexPQp}-?Ma=BdaBk;&D6e|48SQ$WBPZF7IcDr2$V;<6p$nQ!Mu~OICGApX}3}>43 z@uC!qKn!X|#CpBnY&P5NcDLK@_xr=)aDRV)JRVP{)A@WRwy&CI+tSVyYPnn{+BDST feNE(PG_kK))(kz$i93jWtvWBS-s%nTk3`@*&}^1| literal 0 HcmV?d00001 diff --git a/xyz.uvos.icon.png b/xyz.uvos.icon.png new file mode 100644 index 0000000000000000000000000000000000000000..d4d9368ac06c60e5b0d2c03313b610a81eb0b84a GIT binary patch literal 2949 zcmb7Gc{J2rAHQS9C`*LKk_X9SB!tGAZA9pa8p#$S`;sL~JofFe4x*?mV@V^+3{5Cv zl&2(FCgh>Ukk=j~YmDLj^`7(o`=0au&bhyP?)RSWz2|$spYQkcNxx!g%Ev9v4FKRX zH#4?^=J;RZ5EA-#>AUtq6D-8a)Ce}#qZkJ*5MEddEC7u;JbSL3&|2Ef%*q0QKjk2& zWB_&{Q~W#tVJZMDx&WZ_005Do;#Qn4v~f7d%svDF9-&_Y1|AiQLPoAoa|;u$MTC%m z6vz5Cpa}r?7jt8*b;S5`iM_4(WubSqT%sCdM~wtn>wP*apZ5gIOfj!;~KOAz>wE#n>u8>Hi6;B`y_wjs-LkR#`j(B5`dk6@^3}t{B z0;B`!qyNh&nNl2nDOBbzAg%{~5_5Y}I@ES~`4KIuiFAD@uI@?ChwTg7Bfjb^*yQ?- zi=Q8lu4K+ij^y4xiBPXk+^QSfUX1bD)SjVCuuCktwzjsua~aCz!>9~?iEet-l;yWb z3bWbjpM$lmwU;#S&KxO&@0`Gm7llH}Isd%lgR4XBV@~AC9@dZ9MU+@}J6~|ol}vR} z@otqG_N%E!?Z%)w9xA(8r5M<;Zh0XB0szi&%aN$+5Fkl0JCdsHHkIJgkHAI+s< zFaAz6DM%|C-OD<`Y+0LN?JC(!j4W-IIVvBtA9=2kD}nv*<9zzqgX%Ios~>4!fGt?| z8c^^-1I+X0X5_<57eu(R0xp4p{MTCH3E=z6N@DNKr{BnP>yba#W-_bptn2dY6ZjW5 zTNlH~qdby1Tn_c9&^9L)N2h3#yV}!nZr**?#)h!dxQK{I!d&Etk*#coOf-9UWUb8v zhrkJ|lNl2eMs0Lt=^P)-@D^NAQ4tKRx2BxQkQw$>51s5E7+6B-cL!gQ7nb|H*ZYh& zQTm@2o*wSfB)bV!Ip&Y`kw%h5}up8>?`CQS-oI zKQmfC2i+&?7l#p7=PjtSsck)9?&BR3<3!myO(oI;x@Km6hm}=%W5)S^4%e{h0U}%5 z+qkNtxyWm&Bi_5G?{i~{EI16w{OUFA`xiO?Y?suT4is=yA0R091_cGh*M50(?}4!l zto+LRhAjetP)WJ2_V7~NaK*9k@ZjKtexDR1c=w=uI#m^^si_$h7M7wQl=NC!Hs3tg z!+vNTZ7wJwB~=L*;RCT$soB}tP(K4V-iuX&t@czrp6}}9O||_&*y_J}gy=sgdFg00 zx>`*|m>a7rDFqJ~p0IlA{z21#0^}{K9gqryKOjHeVgf-5ZrjHU4Eu2B39xf;uxctZ z#Zxa#g4045Pae1nUxm`=vNRmTH=@%wNaPeqIU- zt&Mp$<_`hr8p{M{thb~Y23|RzN$zr`kLS>Sx@AGYR{NS}FMzoIB9@5k93L0gk~l(- z@@*2=VcM&RT01T39pZ4~?TBF^!-tdp6NfPOf_U6B#q;$x=S8&hd)(iBFQ5g~Ig`IU z+lf;FP`|^t&2r%l{IOC;QsK|WzT3Unu2#6uJ)TQ`3ZD$p`pc93+*)3Emo<8mR%=Vf z_)#k?iAlbVXdUoos=@R9sLOKqtbUl51RIy>%($Ukh^ybE)TE!`zhQ5$`icm(*3C`z znk@4wD>vmOTE_vYNVo424}`7PbL&1Dgp41y^)|H4@-xn_21_>XdWdnSaOec#U;^pJ z*67Q~1EpRy9qIv88UinT7Ybz$L-l#xetdjcqg0=^ko@+o$)uKL z3zNZMn6{h~Qvmnik{ekD+0%n!ae<5ub;rjJNdSm{i*z1|({FXvBtGr4PSfH0-%bMqxx@!{unpd*ppxF0T_LTzu?6JDs*SP?Nf zsvo=e)BDkWI@|@bP7H;8SX=awziZan-`}6xWy)uV6P}_T>{CpN4QgD=aC;J+1T9$m zwPv9mngmyEZEXXE0#7hBypvuC^`GcyUW2&(n{z1Mk1`h#1pOEUP%$0LZ2;P~I|Mzlu7i*h&qOjM(UntF8?eJ4;-;X_& z9;=&w>S<|fW80c~+nYYAlqa2xEKQ};o&G?FoIV$i%6uCY+^7(>O{%X?U@)c5b(qv{ zZ9^VfS`D}E@&8gW%`Gh_P?-w>R9}AhmY%``fyB7?-Pm^n-{8di@lm*WtY{{Q2V5FwyI?d{B5n zFCA-^5G#}|{*{r@fR?HGd2i3UsCz&j>7*s`m)AfkR!c(i(NWCR>AvR(ZoADte}fot z7fTGs)8niG?}xc{tXzJAnJkAhStO#A8MW~7jdZVCXd8V$xTfBQ2V#Z~S4|Fq5*w`c z>OV8LH~CG&+vw-p(vF~>%V$7pqwLGF5pgIXyp69yeq7%yL=Glx(YW?YeLWT{2pN2@ z=Fk~A7dv~9uWPeHXm+B7K+Na9GMdI(k4 z9HMirtIBTZ?qIibj-q1zp3BK`jhuB8ejXi$OU~cX(H4)dBi0akC)55fDfwT~x%K$~ Yd>Wrlb~j4B4M7!{n^+o?j9e1`4J}|_%m4rY literal 0 HcmV?d00001 diff --git a/smartvos.desktop b/xyz.uvos.smartvos.desktop similarity index 71% rename from smartvos.desktop rename to xyz.uvos.smartvos.desktop index adc785e..49dc5df 100644 --- a/smartvos.desktop +++ b/xyz.uvos.smartvos.desktop @@ -1,10 +1,10 @@ [Desktop Entry] Name=SmartVOS Comment=Smart Home Interface -Exec=smartvos -Icon=smartvos +Exec=smartvos -H 10.0.0.1 +Icon=xyz.uvos.icon Terminal=false Type=Application Categories=Utility;HomeAutomation; Keywords=smart;home;automation;iot; -StartupNotify=true \ No newline at end of file +StartupNotify=true