Item Refactor complete

This commit is contained in:
Carl Philipp Klemm 2026-03-31 13:06:15 +02:00
parent 219fbfb4c7
commit 24c168cf64
17 changed files with 78 additions and 41 deletions

View file

@ -17,17 +17,22 @@ Actor::~Actor()
}
void Actor::performAction()
void Actor::performValueAction(uint8_t value)
{
if(active)
{
ItemUpdateRequest request;
request.type = ITEM_UPDATE_ACTOR;
request.payload = ItemData(QRandomGenerator::global()->generate(), "Item", triggerValue);
request.payload = ItemData(QRandomGenerator::global()->generate(), "Item", value);
sigItemUpdate(request);
}
}
void Actor::performAction()
{
performValueAction(triggerValue);
}
void Actor::makeActive()
{
active = true;

View file

@ -19,6 +19,8 @@ protected:
bool exausted = false;
void performAction();
void performValueAction(uint8_t value);
virtual void enactValue(uint8_t value) override;
signals:

View file

@ -6,18 +6,19 @@ MultiFactorActor::MultiFactorActor(Actor* factorActor, const uint preCancleMin,
preCancleMin_(preCancleMin)
{
activationTime.setMSecsSinceEpoch(0);
if(factorActor) connect(factorActor, &Actor::sigValue, this, &MultiFactorActor::factorActorSlot);
if(factorActor)
connect(factorActor, &Actor::sigItemUpdate, this, &MultiFactorActor::factorActorSlot);
}
void MultiFactorActor::factorActorSlot(uint8_t value)
void MultiFactorActor::factorActorSlot(ItemUpdateRequest update)
{
if(value == factorDirection)
if(update.payload.getValue() == factorDirection)
{
activationTime = QDateTime::currentDateTime();
}
}
void MultiFactorActor::setValue(uint8_t value)
void MultiFactorActor::enactValue(uint8_t value)
{
if(value)
{
@ -46,7 +47,7 @@ QString MultiFactorActor::getName() const
void MultiFactorActor::setFactorActor(std::shared_ptr<Actor> factorActor)
{
factorActor_=factorActor;
connect(factorActor_.get(), &Actor::sigValue, this, &MultiFactorActor::factorActorSlot);
connect(factorActor_.get(), &Actor::sigItemUpdate, this, &MultiFactorActor::factorActorSlot);
}
void MultiFactorActor::store(QJsonObject &json)
@ -73,7 +74,7 @@ void MultiFactorActor::load(const QJsonObject &json, bool preserve)
}
if(factorActor_)
{
connect(factorActor_.get(), &Actor::sigValue, this, &MultiFactorActor::factorActorSlot);
connect(factorActor_.get(), &Actor::sigItemUpdate, this, &MultiFactorActor::factorActorSlot);
}
}

View file

@ -16,17 +16,17 @@ private:
private slots:
void factorActorSlot(uint8_t value);
void factorActorSlot(ItemUpdateRequest update);
public slots:
virtual void setValue(uint8_t value);
virtual void enactValue(uint8_t value) override;
public:
MultiFactorActor(Actor* FactorActor = nullptr, const uint preCancleMin = 10, QObject *parent = nullptr);
virtual QString getName() const;
virtual QString getName() const override;
void setFactorActor(std::shared_ptr<Actor> factorActor);
std::shared_ptr<Actor> getFactorActor()
@ -52,8 +52,8 @@ public:
virtual ~MultiFactorActor() {}
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;
};
#endif // REMINDERACTOR_H

View file

@ -39,7 +39,8 @@ void PolynomalActor::sensorEvent(Sensor sensor)
+pow0_;
if(result < 0) result = 0;
else if(result > 254) result = 255;
if(result != prevValue)sigValue(static_cast<uint8_t>(result));
if(result != prevValue)
performValueAction(static_cast<uint8_t>(result));
prevValue = result;
}
}

View file

@ -28,11 +28,11 @@ void Regulator::sensorEvent(Sensor sensor)
timer.start(timeout_*1000);
if( sensor.field < setPoint_-band_ && (sensor.field < sensor_.field || sensor_.field > setPoint_-band_ || first) )
{
sigValue(triggerValue);
performValueAction(triggerValue);
}
else if( sensor.field > setPoint_+band_ && (sensor.field > sensor_.field || sensor_.field < setPoint_+band_ || first) )
{
sigValue(!triggerValue);
performValueAction(!triggerValue);
}
first = false;
sensor_ = sensor;
@ -42,15 +42,14 @@ void Regulator::sensorEvent(Sensor sensor)
void Regulator::makeInactive()
{
first = true;
if(active)
sigValue(!triggerValue);
performValueAction(!triggerValue);
timer.stop();
Actor::makeInactive();
}
void Regulator::timeout()
{
sigValue(safeValue_);
performValueAction(safeValue_);
}
void Regulator::setPoint(float setPoint)

View file

@ -7,7 +7,7 @@ PowerItem::PowerItem(uint32_t itemIdIn, QString name, uint8_t value, QObject* p
Item(itemIdIn, name, value, parent)
{
stateChanged(Sensor(Sensor::TYPE_SHUTDOWN_IMMINENT, 0, 0, "Shutdown Imminent", true));
PowerItem::setValue(true);
value_ = true;
hidden_ = true;
type_ = ITEM_VALUE_NO_VALUE;
}

View file

@ -23,11 +23,6 @@ void Relay::enactValue(uint8_t value)
}
}
void Relay::toggle()
{
value_ ? off() : on();
}
void Relay::store(QJsonObject& json)
{
json["Type"] = "Relay";

View file

@ -115,7 +115,12 @@ 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> item){globalItems.addItem(item, false);});
QObject::connect(w,
&MainWindow::createdItem,
&globalItems,
[](std::shared_ptr<Item> item) {
globalItems.addItem(item, ITEM_UPDATE_USER);
});
w->show();
}
retVal = a.exec();
@ -127,7 +132,9 @@ 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> item){globalItems.addItem(item, false);});
QObject::connect(&w, &MainWindow::createdItem, &globalItems, [](std::shared_ptr<Item> item) {
globalItems.addItem(item, ITEM_UPDATE_USER);
});
QObject::connect(&w, &MainWindow::sigSave, mainObject.tcpClient, &TcpClient::sendItems);
w.show();

View file

@ -43,7 +43,7 @@ void Server::processIncomeingJson(const QByteArray& jsonbytes)
}
else if(!items.empty())
{
gotItems(items, false);
gotItems(items, ITEM_UPDATE_REMOTE);
}
}
else
@ -91,3 +91,14 @@ void Server::removeClient(QWebSocket* socket)
}
}
}
void Server::itemUpdated(ItemUpdateRequest update)
{
QJsonArray items;
QJsonObject itemjson;
update.payload.store(itemjson);
items.append(itemjson);
QJsonObject json = createMessage("ItemUpdate", items);
json["FullList"] = false;
sendJson(json);
}

View file

@ -31,6 +31,9 @@ protected:
std::vector<Client> clients;
public slots:
virtual void itemUpdated(ItemUpdateRequest update) override;
public:
Server(QObject* parent = nullptr);
virtual ~Server();

View file

@ -29,17 +29,7 @@ void Service::sensorEvent(Sensor sensor)
sendJson(json);
}
void Service::itemUpdated(std::weak_ptr<Item> item)
{
qDebug()<<"Service sending item"<<item.lock()->getName();
QJsonArray items;
QJsonObject itemjson;
item.lock()->store(itemjson);
items.append(itemjson);
QJsonObject json = createMessage("ItemUpdate", items);
json["FullList"] = false;
sendJson(json);
}
void Service::itemUpdated(ItemUpdateRequest update) {}
void Service::refresh()
{

View file

@ -24,7 +24,7 @@ signals:
public slots:
void sensorEvent(Sensor sensor);
void itemUpdated(std::weak_ptr<Item> item);
virtual void itemUpdated(ItemUpdateRequest update);
virtual void refresh() override;
public:

View file

@ -94,6 +94,20 @@ void TcpClient::socketReadyRead()
}
}
void TcpClient::itemUpdated(ItemUpdateRequest update)
{
if(update.type == ITEM_UPDATE_USER)
{
QJsonArray items;
QJsonObject itemjson;
update.payload.store(itemjson);
items.append(itemjson);
QJsonObject json = createMessage("ItemUpdate", items);
json["FullList"] = false;
sendJson(json);
}
}
TcpClient::~TcpClient()
{
delete socket;

View file

@ -16,6 +16,9 @@ class TcpClient : public Service
long long recievebytes = 0;
QByteArray buffer;
public slots:
virtual void itemUpdated(ItemUpdateRequest update) override;
public:
TcpClient(QObject* parent = nullptr);
~TcpClient();

View file

@ -38,7 +38,7 @@ ItemWidget::ItemWidget(std::weak_ptr<Item> item, QWidget *parent) :
else
connect(ui->checkBox, &QCheckBox::toggled, this, &ItemWidget::moveToState);
connect(ui->pushButton, &QPushButton::clicked, this, &ItemWidget::showSettingsDialog);
connect(workingItem.get(), &Relay::valueChanged, this, &ItemWidget::stateChanged);
connect(workingItem.get(), &Item::updated, this, &ItemWidget::onItemUpdated);
connect(ui->pushButton_Remove, &QPushButton::clicked, this, &ItemWidget::deleteItem);
}
@ -115,6 +115,11 @@ std::weak_ptr<Item> ItemWidget::getItem()
return item_;
}
void ItemWidget::onItemUpdated(ItemUpdateRequest update)
{
stateChanged(update.payload.getValue());
}
void ItemWidget::stateChanged(int state)
{
ui->slider->blockSignals(true);

View file

@ -38,6 +38,7 @@ public:
public slots:
void stateChanged(int state);
void onItemUpdated(ItemUpdateRequest update);
private:
Ui::ItemWidget *ui;