switched from qsettings to json added editng of actors
This commit is contained in:
24
src/items/auxitem.cpp
Normal file
24
src/items/auxitem.cpp
Normal file
@ -0,0 +1,24 @@
|
||||
#include "auxitem.h"
|
||||
|
||||
AuxItem::AuxItem(SensorStore* sensors, Microcontroller* micro, uint32_t itemIdIn, QString name, uint8_t value, QObject* parent): Item(sensors, itemIdIn, name, value, parent), micro_(micro)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void AuxItem::setValue(uint8_t value)
|
||||
{
|
||||
Item::setValue(value);
|
||||
micro_->setAuxPwm(value);
|
||||
}
|
||||
|
||||
void AuxItem::store(QJsonObject &json)
|
||||
{
|
||||
json["Type"] = "Aux";
|
||||
Item::store(json);
|
||||
}
|
||||
|
||||
void AuxItem::store(QString subsecton, QSettings* settings)
|
||||
{
|
||||
settings->setValue(subsecton + "Type", "Aux");
|
||||
Item::store(subsecton, settings);
|
||||
}
|
21
src/items/auxitem.h
Normal file
21
src/items/auxitem.h
Normal file
@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include "item.h"
|
||||
#include "../microcontroller.h"
|
||||
|
||||
class AuxItem: public Item
|
||||
{
|
||||
Q_OBJECT
|
||||
private:
|
||||
Microcontroller* micro_;
|
||||
|
||||
public slots:
|
||||
|
||||
virtual void setValue(uint8_t value);
|
||||
|
||||
public:
|
||||
AuxItem(SensorStore* sensors, Microcontroller* micro, uint32_t itemIdIn = QRandomGenerator::global()->generate(), QString name = "", uint8_t value = 0, QObject* parent = nullptr);
|
||||
|
||||
virtual void store(QJsonObject& json);
|
||||
virtual void store(QString subsecton, QSettings* settings);
|
||||
};
|
173
src/items/item.cpp
Normal file
173
src/items/item.cpp
Normal file
@ -0,0 +1,173 @@
|
||||
#include "item.h"
|
||||
|
||||
#include "relay.h"
|
||||
#include "../microcontroller.h"
|
||||
#include "../actors/sensoractor.h"
|
||||
#include "../actors/regulator.h"
|
||||
|
||||
#include <QJsonArray>
|
||||
|
||||
ItemData::ItemData(uint32_t itemIdIn, QString name, uint8_t value): name_(name), value_(value), itemId_(itemIdIn)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
QString ItemData::getName() const
|
||||
{
|
||||
return name_;
|
||||
}
|
||||
|
||||
void ItemData::setName(QString name)
|
||||
{
|
||||
name_ = name;
|
||||
}
|
||||
|
||||
uint8_t ItemData::getValue() const
|
||||
{
|
||||
return value_;
|
||||
}
|
||||
|
||||
uint32_t ItemData::id() const
|
||||
{
|
||||
return itemId_;
|
||||
}
|
||||
|
||||
|
||||
//item
|
||||
|
||||
bool Item::secondaryFlag = false;
|
||||
|
||||
Item::Item(SensorStore* sensors, uint32_t itemIdIn, QString name, uint8_t value, QObject *parent): QObject(parent), ItemData (itemIdIn, name, value), sensors_(sensors)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Item::Item(SensorStore* sensors, const ItemData& itemData, QObject *parent): QObject(parent), ItemData(itemData), sensors_(sensors)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
bool Item::actorsActive() const
|
||||
{
|
||||
return actorsActive_;
|
||||
}
|
||||
|
||||
void Item::store(QJsonObject &json)
|
||||
{
|
||||
json["Name"] = name_;
|
||||
json["ItemId"] = static_cast<double>(itemId_);
|
||||
json["ActorsActive"] = actorsActive_;
|
||||
QJsonArray actorsArray;
|
||||
for(size_t i = 0; i < actors_.size(); ++i)
|
||||
{
|
||||
QJsonObject actorObject;
|
||||
actors_[i]->store(actorObject);
|
||||
actorsArray.append(actorObject);
|
||||
}
|
||||
json["Actors"] = actorsArray;
|
||||
}
|
||||
|
||||
void Item::load(const QJsonObject &json, const bool preserve)
|
||||
{
|
||||
if(!preserve)
|
||||
{
|
||||
name_ = json["Name"].toString(name_);
|
||||
itemId_ = static_cast<uint32_t>(json["ItemId"].toDouble(0));
|
||||
}
|
||||
actorsActive_ = json["ActorsActive"].toBool(true);
|
||||
const QJsonArray actorsArray(json["Actors"].toArray(QJsonArray()));
|
||||
for(int i = 0; i < actorsArray.size(); ++i)
|
||||
{
|
||||
if(actorsArray[i].isObject())
|
||||
{
|
||||
Actor* actor = Actor::loadActor(actorsArray[i].toObject());
|
||||
if(actor != nullptr) addActor(actor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Item::store(QString subsecton, QSettings* settings)
|
||||
{
|
||||
settings->setValue(subsecton + "Name", name_);
|
||||
settings->setValue(subsecton + "ItemId", static_cast<unsigned>(itemId_));
|
||||
settings->setValue(subsecton + "ActorsActive", actorsActive_);
|
||||
settings->setValue(subsecton + "Actors", static_cast<unsigned>(actors_.size()));
|
||||
for(size_t i = 0; i < actors_.size(); ++i)
|
||||
{
|
||||
actors_[i]->store(subsecton + "/Actor" + QString::number(i), settings);
|
||||
}
|
||||
}
|
||||
|
||||
void Item::load(QString subsecton, QSettings* settings, bool preserve)
|
||||
{
|
||||
if(!preserve)
|
||||
{
|
||||
name_ = settings->value(subsecton + "Name").toString();
|
||||
itemId_ = settings->value(subsecton + "ItemId").toUInt();
|
||||
}
|
||||
actorsActive_ = settings->value(subsecton + "ActorsActive").toBool();
|
||||
unsigned actorsLen = settings->value(subsecton + "Actors").toUInt();
|
||||
for(unsigned i = 0; i < actorsLen; ++i)
|
||||
{
|
||||
Actor* actor = Actor::loadActor(subsecton + "/Actor" + QString::number(i), settings);
|
||||
if(actor != nullptr) addActor(actor);
|
||||
}
|
||||
}
|
||||
|
||||
void Item::setValue(uint8_t value)
|
||||
{
|
||||
value_ = value;
|
||||
valueChanged(value_);
|
||||
}
|
||||
|
||||
void Item::informValue(uint8_t value)
|
||||
{
|
||||
Item::setValue(value);
|
||||
}
|
||||
|
||||
void Item::addActor(Actor* actor)
|
||||
{
|
||||
actor->setParent(this);
|
||||
actors_.push_back(actor);
|
||||
if(!secondaryFlag)connect(actor, &Actor::sigValue, this, &Item::setValue);
|
||||
connect(this, &Item::valueChanged, actor, &Actor::onValueChanged);
|
||||
|
||||
SensorActor* sensorActor = dynamic_cast<SensorActor*>(actor);
|
||||
if(sensorActor != nullptr && sensors_ != nullptr)connect(sensors_, &SensorStore::sensorChangedState, sensorActor, &SensorActor::sensorEvent);
|
||||
|
||||
Regulator* regulator = dynamic_cast<Regulator*>(actor);
|
||||
if(regulator != nullptr && sensors_ != nullptr)connect(sensors_, &SensorStore::sensorChangedState, regulator, &Regulator::sensorEvent);
|
||||
|
||||
if(actorsActive_) actor->makeActive();
|
||||
else actor->makeInactive();
|
||||
}
|
||||
|
||||
bool Item::removeActor(Actor* actor)
|
||||
{
|
||||
for(unsigned int i = 0; i < actors_.size(); i++)
|
||||
{
|
||||
if(actors_[i] == actor)
|
||||
{
|
||||
delete actor;
|
||||
actors_.erase(actors_.begin()+i);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector< Actor* >& Item::getActors()
|
||||
{
|
||||
return actors_;
|
||||
}
|
||||
|
||||
bool Item::hasActors()
|
||||
{
|
||||
return actors_.size() > 0;
|
||||
}
|
||||
|
||||
void Item::setActorsActive(bool in)
|
||||
{
|
||||
actorsActive_ = in;
|
||||
for(unsigned i = 0; i < actors_.size(); i++) in ? actors_[i]->makeActive() : actors_[i]->makeInactive();
|
||||
}
|
75
src/items/item.h
Normal file
75
src/items/item.h
Normal file
@ -0,0 +1,75 @@
|
||||
#pragma once
|
||||
#include <QObject>
|
||||
#include <vector>
|
||||
#include <QRandomGenerator>
|
||||
#include <QSettings>
|
||||
#include <QJsonObject>
|
||||
|
||||
#include "../actors/actor.h"
|
||||
#include "../sensors/sensor.h"
|
||||
|
||||
class ItemData
|
||||
{
|
||||
protected:
|
||||
QString name_;
|
||||
uint8_t value_;
|
||||
uint32_t itemId_;
|
||||
|
||||
public:
|
||||
|
||||
ItemData(uint32_t itemIdIn = QRandomGenerator::global()->generate(), QString name = "Item", uint8_t value = 0);
|
||||
|
||||
inline bool operator==(const ItemData& in) const{ return itemId_==in.itemId_; }
|
||||
inline bool operator!=(const ItemData& in) const{ return itemId_!=in.itemId_; }
|
||||
|
||||
uint32_t id() const;
|
||||
|
||||
QString getName() const;
|
||||
void setName(QString name);
|
||||
uint8_t getValue() const;
|
||||
};
|
||||
|
||||
|
||||
class Item: public QObject, public ItemData
|
||||
{
|
||||
Q_OBJECT
|
||||
private:
|
||||
std::vector< Actor* > actors_;
|
||||
bool actorsActive_ = true;
|
||||
|
||||
public:
|
||||
|
||||
static bool secondaryFlag;
|
||||
SensorStore* sensors_;
|
||||
|
||||
signals:
|
||||
|
||||
void valueChanged(uint8_t value);
|
||||
|
||||
public slots:
|
||||
|
||||
virtual void setValue(uint8_t value);
|
||||
|
||||
public:
|
||||
|
||||
Item(SensorStore* sensors = nullptr, uint32_t itemIdIn = QRandomGenerator::global()->generate(), QString name = "Item", uint8_t value = 0, QObject *parent = nullptr);
|
||||
Item(SensorStore* sensors, const ItemData& itemData, QObject *parent = nullptr);
|
||||
|
||||
std::vector< Actor* >& getActors();
|
||||
bool hasActors();
|
||||
void addActor(Actor* actor);
|
||||
bool removeActor(Actor* actor);
|
||||
bool actorsActive() const;
|
||||
void setActorsActive(bool in);
|
||||
void informValue(uint8_t value);
|
||||
void setSensorStore(SensorStore* sensors){sensors_ = sensors;}
|
||||
SensorStore* getSensorStore(){return sensors_;}
|
||||
|
||||
virtual void store(QJsonObject& json);
|
||||
virtual void load(const QJsonObject& json, const bool preserve = false);
|
||||
|
||||
virtual void store(QString subsecton, QSettings* settings);
|
||||
virtual void load(QString subsecton, QSettings* settings, bool preserve = false);
|
||||
|
||||
};
|
||||
|
132
src/items/itemstore.cpp
Normal file
132
src/items/itemstore.cpp
Normal file
@ -0,0 +1,132 @@
|
||||
#include "itemstore.h"
|
||||
#include "relay.h"
|
||||
#include <QJsonArray>
|
||||
|
||||
ItemStore::ItemStore(SensorStore* sensors, QObject *parent): QObject(parent), sensors_(sensors)
|
||||
{
|
||||
}
|
||||
|
||||
void ItemStore::addItem(std::shared_ptr<Item> item)
|
||||
{
|
||||
bool mached = false;
|
||||
for(unsigned i = 0; i < items_.size(); i++ ) if(*items_[i] == *item) mached = true;
|
||||
if(!mached)
|
||||
{
|
||||
items_.push_back(std::shared_ptr<Item>(item));
|
||||
items_.back()->setSensorStore(sensors_);
|
||||
itemAdded(std::weak_ptr<Item>(items_.back()));
|
||||
qDebug()<<"item added";
|
||||
}
|
||||
}
|
||||
|
||||
void ItemStore::addItems(const std::vector<std::shared_ptr<Item>>& itemIn)
|
||||
{
|
||||
for(unsigned i = 0; i < items_.size(); i++ )
|
||||
{
|
||||
if(Relay* relay = dynamic_cast<Relay*>(items_[i].get()))
|
||||
{
|
||||
bool mached = false;
|
||||
for(unsigned j = 0; j < itemIn.size(); j++) if(*(items_[i]) == *(itemIn[j]))
|
||||
{
|
||||
mached = true;
|
||||
if(itemIn[j]->getValue() != items_[i]->getValue()) items_[i]->informValue(itemIn[j]->getValue());
|
||||
Relay* relayIn = dynamic_cast<Relay*>(itemIn[j].get());
|
||||
if(relayIn)
|
||||
{
|
||||
if(relay->getId() != relayIn->getId()) relay->setId(relayIn->getId());
|
||||
}
|
||||
|
||||
}
|
||||
if(!mached)
|
||||
{
|
||||
itemDeleted(*items_[i].get());
|
||||
items_.erase(items_.begin()+i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for(unsigned j = 0; j < itemIn.size(); j++)addItem(itemIn[j]);
|
||||
|
||||
}
|
||||
|
||||
void ItemStore::itemStateChanged(const ItemData& item)
|
||||
{
|
||||
for(unsigned i = 0; i < items_.size(); i++ )
|
||||
{
|
||||
if(items_[i]->operator==(item))
|
||||
{
|
||||
qDebug()<<"is item "<<i<<" with ids: "<<item.id()<<" "<<items_[i]->id()<<"\nHas state: "<<items_[i]->getValue()<<" wants state: "<<item.getValue();
|
||||
if(items_[i]->getValue() != item.getValue())items_[i]->informValue(item.getValue());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void ItemStore::store(QJsonObject& json)
|
||||
{
|
||||
QJsonArray itemsArray;
|
||||
for(size_t i = 0; i < items_.size(); ++i)
|
||||
{
|
||||
QJsonObject itemObject;
|
||||
items_[i]->store(itemObject);
|
||||
itemsArray.append(itemObject);
|
||||
}
|
||||
json["Items"] = itemsArray;
|
||||
}
|
||||
|
||||
void ItemStore::load(const QJsonObject& json, Microcontroller * const micro)
|
||||
{
|
||||
const QJsonArray itemsArray(json["Items"].toArray(QJsonArray()));
|
||||
for(int i = 0; i < itemsArray.size(); ++i)
|
||||
{
|
||||
if(itemsArray[i].isObject())
|
||||
{
|
||||
const QJsonObject itemObject = itemsArray[i].toObject();
|
||||
if(!itemObject["Name"].isObject()) qDebug()<<"no name";
|
||||
std::shared_ptr<Relay> newItem;
|
||||
if(itemObject["Type"].toString("") == "Relay")
|
||||
{
|
||||
newItem = std::shared_ptr<Relay>(new Relay(sensors_, micro));
|
||||
}
|
||||
else if(itemObject["Type"].toString("") == "Aux")
|
||||
{
|
||||
}
|
||||
if(newItem)
|
||||
{
|
||||
newItem->load(itemObject);
|
||||
addItem(newItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ItemStore::store(QString subsecton, QSettings* settings)
|
||||
{
|
||||
settings->setValue(subsecton + "/NumberOfItems", static_cast<unsigned>(items_.size()));
|
||||
for(size_t i = 0; i < items_.size(); ++i)
|
||||
{
|
||||
items_[i]->store(subsecton + "/Item" + QString::number(i), settings);
|
||||
}
|
||||
}
|
||||
|
||||
void ItemStore::load(QString subsecton, QSettings* settings, Microcontroller* micro)
|
||||
{
|
||||
unsigned itemLen = settings->value(subsecton + "/NumberOfItems").toUInt();
|
||||
for(size_t i = 0; i < itemLen; ++i)
|
||||
{
|
||||
std::shared_ptr<Relay> newItem;
|
||||
if(settings->value(subsecton + "/Item" + QString::number(i)+ "Type").toString() == "Relay")
|
||||
{
|
||||
newItem = std::shared_ptr<Relay>(new Relay(sensors_, micro));
|
||||
}
|
||||
else if(settings->value(subsecton + "/Item" + QString::number(i)+ "Type").toString() == "Aux")
|
||||
{
|
||||
}
|
||||
if(newItem)
|
||||
{
|
||||
newItem->load(subsecton + "/Item" + QString::number(i), settings);
|
||||
addItem(newItem);
|
||||
}
|
||||
}
|
||||
}
|
41
src/items/itemstore.h
Normal file
41
src/items/itemstore.h
Normal file
@ -0,0 +1,41 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include "item.h"
|
||||
#include "../sensors/sensor.h"
|
||||
#include "../microcontroller.h"
|
||||
|
||||
#include <QJsonObject>
|
||||
|
||||
class ItemStore: public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
private:
|
||||
std::vector< std::shared_ptr<Item> > items_;
|
||||
|
||||
SensorStore* sensors_;
|
||||
|
||||
public:
|
||||
|
||||
ItemStore(SensorStore* sensors_ = nullptr, QObject *parent = nullptr);
|
||||
virtual ~ItemStore(){}
|
||||
|
||||
inline std::vector< std::shared_ptr<Item> >* getItems(){ return &items_; }
|
||||
|
||||
void store(QJsonObject &json);
|
||||
void load(const QJsonObject &json, Microcontroller * const micro);
|
||||
|
||||
void store(QString subsecton, QSettings* settings);
|
||||
void load(QString subsecton, QSettings* settings, Microcontroller* micro);
|
||||
|
||||
signals:
|
||||
|
||||
void itemDeleted(ItemData item);
|
||||
void itemAdded(std::weak_ptr<Item> Item);
|
||||
|
||||
public slots:
|
||||
|
||||
void addItem(std::shared_ptr<Item> item);
|
||||
void addItems(const std::vector<std::shared_ptr<Item>>& itemsIn);
|
||||
void itemStateChanged(const ItemData& item);
|
||||
};
|
37
src/items/poweritem.cpp
Normal file
37
src/items/poweritem.cpp
Normal file
@ -0,0 +1,37 @@
|
||||
#include "poweritem.h"
|
||||
#include <QProcess>
|
||||
#include <QApplication>
|
||||
|
||||
PowerItem::PowerItem(SensorStore* sensors, QApplication* a, uint32_t itemIdIn, QString name, uint8_t value, QObject* parent): Item(sensors, itemIdIn, name, value, parent), a_(a)
|
||||
{
|
||||
stateChanged(Sensor(Sensor::TYPE_SHUTDOWN_IMMINENT, 0, 0));
|
||||
setValue(true);
|
||||
}
|
||||
|
||||
void PowerItem::setValue(uint8_t value)
|
||||
{
|
||||
Item::setValue(value);
|
||||
if(!value)
|
||||
{
|
||||
QTimer::singleShot(5000, this, &PowerItem::timeout);
|
||||
stateChanged(Sensor(Sensor::TYPE_SHUTDOWN_IMMINENT, 0, 1));
|
||||
}
|
||||
}
|
||||
|
||||
void PowerItem::timeout()
|
||||
{
|
||||
QProcess::startDetached("syncoff");
|
||||
a_->exit(0);
|
||||
}
|
||||
|
||||
void PowerItem::store(QJsonObject& json)
|
||||
{
|
||||
json["Type"] = "Power";
|
||||
Item::store(json);
|
||||
}
|
||||
|
||||
void PowerItem::store(QString subsecton, QSettings* settings)
|
||||
{
|
||||
settings->setValue(subsecton + "Type", "Power");
|
||||
Item::store(subsecton, settings);
|
||||
}
|
32
src/items/poweritem.h
Normal file
32
src/items/poweritem.h
Normal file
@ -0,0 +1,32 @@
|
||||
#pragma once
|
||||
|
||||
#include "item.h"
|
||||
#include "../sensors/sensors.h"
|
||||
#include "../microcontroller.h"
|
||||
|
||||
#include <QTimer>
|
||||
|
||||
class PowerItem: public Item
|
||||
{
|
||||
Q_OBJECT
|
||||
private:
|
||||
QApplication* a_;
|
||||
|
||||
signals:
|
||||
|
||||
void stateChanged(Sensor sensor);
|
||||
|
||||
private slots:
|
||||
|
||||
void timeout();
|
||||
|
||||
public slots:
|
||||
|
||||
virtual void setValue(uint8_t value);
|
||||
|
||||
public:
|
||||
PowerItem(SensorStore* sensors, QApplication* a, uint32_t itemIdIn = QRandomGenerator::global()->generate(), QString name = "", uint8_t value = 0, QObject* parent = nullptr);
|
||||
void emmitSensor(){stateChanged(Sensor(Sensor::TYPE_SHUTDOWN_IMMINENT, 0, 0));}
|
||||
virtual void store(QJsonObject& json);
|
||||
virtual void store(QString subsecton, QSettings* settings);
|
||||
};
|
80
src/items/relay.cpp
Normal file
80
src/items/relay.cpp
Normal file
@ -0,0 +1,80 @@
|
||||
#include "relay.h"
|
||||
#include "../microcontroller.h"
|
||||
|
||||
//Relay
|
||||
|
||||
Relay::Relay(SensorStore* sensors, Microcontroller* micro, uint8_t id, QString name, uint16_t address, bool state, QObject* parent): Item(sensors, 0, name, state, parent), micro_(micro), id_(id), address_(address)
|
||||
{
|
||||
itemId_ = address | ((uint32_t)id << 16);
|
||||
}
|
||||
|
||||
void Relay::setValue(uint8_t value)
|
||||
{
|
||||
Item::setValue(value);
|
||||
if(value)micro_->relayOn(id_);
|
||||
else micro_->relayOff(id_);
|
||||
}
|
||||
|
||||
void Relay::on()
|
||||
{
|
||||
setValue(true);
|
||||
}
|
||||
|
||||
void Relay::off()
|
||||
{
|
||||
setValue(false);
|
||||
}
|
||||
|
||||
void Relay::toggle()
|
||||
{
|
||||
value_ ? off() : on();
|
||||
}
|
||||
|
||||
void Relay::store(QJsonObject& json)
|
||||
{
|
||||
json["Type"] = "Relay";
|
||||
Item::store(json);
|
||||
json["Id"] = static_cast<double>(id_);
|
||||
json["Address"] = address_;
|
||||
}
|
||||
|
||||
void Relay::load(const QJsonObject& json)
|
||||
{
|
||||
Item::load(json);
|
||||
id_ = static_cast<uint8_t>(json["Id"].toInt(0));
|
||||
address_ = static_cast<uint16_t>(json["Address"].toInt(0));
|
||||
itemId_ = address_ | (static_cast<uint32_t>(id_) << 16);
|
||||
}
|
||||
|
||||
void Relay::store(QString subsecton, QSettings* settings)
|
||||
{
|
||||
settings->setValue(subsecton + "Type", "Relay");
|
||||
Item::store(subsecton, settings);
|
||||
settings->setValue(subsecton + "Id", static_cast<unsigned>(id_));
|
||||
settings->setValue(subsecton + "Address", address_);
|
||||
}
|
||||
|
||||
void Relay::load(QString subsecton, QSettings* settings)
|
||||
{
|
||||
Item::load(subsecton, settings);
|
||||
id_ = settings->value(subsecton + "Id").toUInt();
|
||||
address_ = settings->value(subsecton + "Address").toUInt();
|
||||
itemId_ = address_ | ((uint32_t)id_ << 16);
|
||||
}
|
||||
|
||||
uint16_t Relay::getAddress() const
|
||||
{
|
||||
return address_;
|
||||
}
|
||||
|
||||
uint8_t Relay::getId() const
|
||||
{
|
||||
return id_;
|
||||
}
|
||||
|
||||
void Relay::setId(uint8_t id)
|
||||
{
|
||||
id_=id;
|
||||
itemId_ = address_ | ((uint32_t)id << 16);
|
||||
}
|
||||
|
41
src/items/relay.h
Normal file
41
src/items/relay.h
Normal file
@ -0,0 +1,41 @@
|
||||
#ifndef RELAY_H
|
||||
#define RELAY_H
|
||||
|
||||
#include<stdint.h>
|
||||
#include<QObject>
|
||||
|
||||
#include "../sensors/sensor.h"
|
||||
#include "item.h"
|
||||
|
||||
class Microcontroller;
|
||||
|
||||
class Relay : public Item
|
||||
{
|
||||
Q_OBJECT
|
||||
private:
|
||||
Microcontroller* micro_;
|
||||
|
||||
uint8_t id_;
|
||||
uint16_t address_;
|
||||
|
||||
public slots:
|
||||
|
||||
virtual void setValue(uint8_t value);
|
||||
void on();
|
||||
void off();
|
||||
void toggle();
|
||||
|
||||
public:
|
||||
Relay(SensorStore* sensors, Microcontroller* micro, uint8_t id = 0, QString name = "", uint16_t address = 0, bool state = false, QObject* parent = nullptr);
|
||||
|
||||
uint16_t getAddress() const;
|
||||
uint8_t getId() const;
|
||||
void setId(uint8_t id);
|
||||
|
||||
virtual void store(QJsonObject& json);
|
||||
virtual void load(const QJsonObject& json);
|
||||
|
||||
virtual void store(QString subsecton, QSettings* settings);
|
||||
virtual void load(QString subsecton, QSettings* settings);
|
||||
};
|
||||
#endif // RELAY_H
|
24
src/items/rgbitem.cpp
Normal file
24
src/items/rgbitem.cpp
Normal file
@ -0,0 +1,24 @@
|
||||
#include "rgbitem.h"
|
||||
|
||||
RgbItem::RgbItem(SensorStore* sensors, Microcontroller* micro, uint32_t itemIdIn, QString name, uint8_t value, QObject* parent): Item(sensors, itemIdIn, name, value, parent), micro_(micro)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void RgbItem::setValue(uint8_t value)
|
||||
{
|
||||
Item::setValue(value);
|
||||
value ? micro_->rgbOn() : micro_->rgbOff();
|
||||
}
|
||||
|
||||
void RgbItem::store(QJsonObject &json)
|
||||
{
|
||||
json["Type"] = "Rgb";
|
||||
Item::store(json);
|
||||
}
|
||||
|
||||
void RgbItem::store(QString subsecton, QSettings* settings)
|
||||
{
|
||||
settings->setValue(subsecton + "Type", "Rgb");
|
||||
Item::store(subsecton, settings);
|
||||
}
|
21
src/items/rgbitem.h
Normal file
21
src/items/rgbitem.h
Normal file
@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include "../microcontroller.h"
|
||||
#include "item.h"
|
||||
|
||||
class RgbItem: public Item
|
||||
{
|
||||
Q_OBJECT
|
||||
private:
|
||||
Microcontroller* micro_;
|
||||
|
||||
public slots:
|
||||
|
||||
virtual void setValue(uint8_t value);
|
||||
|
||||
public:
|
||||
RgbItem(SensorStore* sensors, Microcontroller* micro, uint32_t itemIdIn = QRandomGenerator::global()->generate(), QString name = "", uint8_t value = 0, QObject* parent = nullptr);
|
||||
|
||||
virtual void store(QJsonObject& json);
|
||||
virtual void store(QString subsecton, QSettings* settings);
|
||||
};
|
Reference in New Issue
Block a user