303 lines
6.8 KiB
C++
303 lines
6.8 KiB
C++
#include "item.h"
|
|
|
|
#include "actors/sensoractor.h"
|
|
#include "actors/regulator.h"
|
|
#include "actors/polynomalactor.h"
|
|
#include "programmode.h"
|
|
#include "relay.h"
|
|
#include "messageitem.h"
|
|
#include "systemitem.h"
|
|
#include "auxitem.h"
|
|
#include "poweritem.h"
|
|
#include "rgbitem.h"
|
|
|
|
#include <QJsonArray>
|
|
|
|
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)
|
|
{
|
|
|
|
}
|
|
|
|
QString ItemData::getName() const
|
|
{
|
|
return name_;
|
|
}
|
|
|
|
void ItemData::setName(QString name)
|
|
{
|
|
name_ = name;
|
|
}
|
|
|
|
uint8_t ItemData::getValue() const
|
|
{
|
|
return value_;
|
|
}
|
|
|
|
void ItemData::setValueData(uint8_t value)
|
|
{
|
|
value_ = value;
|
|
}
|
|
|
|
uint32_t ItemData::id() const
|
|
{
|
|
return itemId_;
|
|
}
|
|
|
|
void ItemData::store(QJsonObject &json)
|
|
{
|
|
json["Name"] = name_;
|
|
json["ItemId"] = static_cast<double>(itemId_);
|
|
json["Value"] = static_cast<double>(value_);
|
|
}
|
|
|
|
void ItemData::load(const QJsonObject &json, const bool preserve)
|
|
{
|
|
if(!preserve)
|
|
{
|
|
name_ = json["Name"].toString(name_);
|
|
itemId_ = static_cast<uint32_t>(json["ItemId"].toDouble(0));
|
|
value_ = json["Value"].toInt();
|
|
}
|
|
}
|
|
|
|
bool ItemData::getLoaded() const
|
|
{
|
|
return loaded_;
|
|
}
|
|
|
|
void ItemData::setLoaded(bool loaded)
|
|
{
|
|
loaded_ = loaded;
|
|
}
|
|
|
|
bool ItemData::hasChanged(const ItemData& other)
|
|
{
|
|
if(other != *this)
|
|
return false;
|
|
if(other.getName() != getName())
|
|
return true;
|
|
if(other.getValue() != getValue())
|
|
return true;
|
|
if(other.getLoaded() != getLoaded())
|
|
return true;
|
|
return false;
|
|
}
|
|
|
|
bool ItemData::isHidden() const
|
|
{
|
|
return hidden_;
|
|
}
|
|
|
|
void ItemData::setHidden(bool hidden)
|
|
{
|
|
hidden_ = hidden;
|
|
}
|
|
|
|
item_value_type_t ItemData::getValueType()
|
|
{
|
|
return type_;
|
|
}
|
|
|
|
//item
|
|
|
|
Item::Item(uint32_t itemIdIn, QString name, uint8_t value, QObject *parent): QObject(parent), ItemData (itemIdIn, name,
|
|
value)
|
|
{
|
|
|
|
}
|
|
|
|
Item::Item(const ItemData& itemData, QObject *parent): QObject(parent), ItemData(itemData)
|
|
{
|
|
|
|
}
|
|
|
|
Item::~Item()
|
|
{
|
|
}
|
|
|
|
void Item::store(QJsonObject &json)
|
|
{
|
|
ItemData::store(json);
|
|
json["override"] = override_;
|
|
QJsonArray actorsArray;
|
|
for(size_t i = 0; i < actors_.size(); ++i)
|
|
{
|
|
if(!actors_[i]->isExausted())
|
|
{
|
|
QJsonObject actorObject;
|
|
actors_[i]->store(actorObject);
|
|
actorsArray.append(actorObject);
|
|
}
|
|
}
|
|
json["Actors"] = actorsArray;
|
|
json["ValueType"] = type_;
|
|
}
|
|
|
|
void Item::load(const QJsonObject &json, const bool preserve)
|
|
{
|
|
ItemData::load(json, preserve);
|
|
override_ = json["override"].toBool(false);
|
|
const QJsonArray actorsArray(json["Actors"].toArray(QJsonArray()));
|
|
for(int i = 0; i < actorsArray.size(); ++i)
|
|
{
|
|
if(actorsArray[i].isObject())
|
|
{
|
|
std::shared_ptr<Actor> actor = Actor::loadActor(actorsArray[i].toObject());
|
|
if(actor != nullptr)
|
|
addActor(actor);
|
|
}
|
|
}
|
|
}
|
|
|
|
Item& Item::operator=(const ItemData& other)
|
|
{
|
|
name_ = other.getName();
|
|
value_ = other.getValue();
|
|
itemId_ = other.id();
|
|
hidden_ = other.isHidden();
|
|
return *this;
|
|
}
|
|
|
|
void Item::requestUpdate(ItemUpdateRequest update)
|
|
{
|
|
assert(update.type != ITEM_UPDATE_INVALID);
|
|
if(update.type != ITEM_UPDATE_LOADED && value_ == update.payload.getValue())
|
|
return;
|
|
|
|
if(update.type == ITEM_UPDATE_ACTOR && override_)
|
|
return;
|
|
|
|
qDebug()<<"Item Update Request for"<<getName()<<" type "<<update.type<<" value "<<update.payload.getValue();
|
|
|
|
if(update.type != ITEM_UPDATE_LOADED &&
|
|
update.type != ITEM_UPDATE_BACKEND &&
|
|
(programMode == PROGRAM_MODE_PRIMARY || programMode == PROGRAM_MODE_HEADLESS_PRIMARY))
|
|
enactValue(update.payload.getValue());
|
|
|
|
if(update.type != ITEM_UPDATE_LOADED)
|
|
{
|
|
value_ = update.payload.getValue();
|
|
}
|
|
else
|
|
{
|
|
name_ = update.payload.getName();
|
|
//itemId_ = update.payload.id();
|
|
hidden_ = update.payload.isHidden();
|
|
actors_.clear();
|
|
for(std::shared_ptr<Actor>& actor : update.newActors)
|
|
addActor(actor);
|
|
}
|
|
update.payload = *this;
|
|
updated(update);
|
|
}
|
|
|
|
void Item::enactValue(uint8_t value)
|
|
{
|
|
(void)value;
|
|
}
|
|
|
|
void Item::addActor(std::shared_ptr<Actor> actor)
|
|
{
|
|
actor->setParent(this);
|
|
actors_.push_back(actor);
|
|
if(programMode == PROGRAM_MODE_PRIMARY || programMode == PROGRAM_MODE_HEADLESS_PRIMARY)
|
|
connect(actor.get(), &Actor::sigItemUpdate, this, &Item::requestUpdate);
|
|
connect(this, &Item::updated, actor.get(), &Actor::onItemUpdated);
|
|
|
|
std::shared_ptr<SensorActor> sensorActor = std::dynamic_pointer_cast<SensorActor>(actor);
|
|
if(sensorActor)
|
|
connect(&globalSensors, &SensorStore::sensorChangedState, sensorActor.get(), &SensorActor::sensorEvent);
|
|
|
|
std::shared_ptr<Regulator> regulator = std::dynamic_pointer_cast<Regulator>(actor);
|
|
if(regulator)
|
|
connect(&globalSensors, &SensorStore::sensorChangedState, regulator.get(), &Regulator::sensorEvent);
|
|
|
|
std::shared_ptr<PolynomalActor> polynomalActor = std::dynamic_pointer_cast<PolynomalActor>(actor);
|
|
if(polynomalActor != nullptr )
|
|
connect(&globalSensors, &SensorStore::sensorChangedState, polynomalActor.get(), &PolynomalActor::sensorEvent);
|
|
}
|
|
|
|
bool Item::removeActor(std::shared_ptr<Actor> actor)
|
|
{
|
|
for(unsigned int i = 0; i < actors_.size(); i++)
|
|
{
|
|
if(actors_[i] == actor)
|
|
{
|
|
actors_.erase(actors_.begin()+i);
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void Item::setOverride(const bool in)
|
|
{
|
|
override_ = in;
|
|
}
|
|
|
|
bool Item::getOverride()
|
|
{
|
|
return override_;
|
|
}
|
|
|
|
void Item::removeAllActors()
|
|
{
|
|
actors_.clear();
|
|
}
|
|
|
|
std::vector< std::shared_ptr<Actor> >& Item::getActors()
|
|
{
|
|
return actors_;
|
|
}
|
|
|
|
bool Item::hasActors()
|
|
{
|
|
return actors_.size() > 0;
|
|
}
|
|
|
|
void Item::setActorsActive(bool in)
|
|
{
|
|
for(unsigned i = 0; i < actors_.size(); i++)
|
|
in ? actors_[i]->makeActive() : actors_[i]->makeInactive();
|
|
}
|
|
|
|
std::shared_ptr<Item> Item::loadItem(const QJsonObject& json)
|
|
{
|
|
std::shared_ptr<Item> newItem = nullptr;
|
|
if(json["Type"].toString("Item") == "Item")
|
|
newItem = std::shared_ptr<Item>(new Item);
|
|
else if(json["Type"].toString("") == "Relay")
|
|
newItem = std::shared_ptr<Relay>(new Relay);
|
|
else if(json["Type"].toString("") == "Message")
|
|
newItem = std::shared_ptr<MessageItem>(new MessageItem);
|
|
else if(json["Type"].toString("") == "System")
|
|
newItem = std::shared_ptr<SystemItem>(new SystemItem);
|
|
else if(json["Type"].toString("") == "Aux")
|
|
newItem = std::shared_ptr<AuxItem>(new AuxItem);
|
|
else if(json["Type"].toString("") == "Power")
|
|
newItem = std::shared_ptr<PowerItem>(new PowerItem);
|
|
else if(json["Type"].toString("") == "Rgb")
|
|
newItem = std::shared_ptr<RgbItem>(new RgbItem);
|
|
else
|
|
qWarning()<<"Unable to load unkown item type: "<<json["Type"].toString();
|
|
if(newItem)
|
|
{
|
|
newItem->load(json);
|
|
newItem->setLoaded(true);
|
|
}
|
|
return newItem;
|
|
}
|
|
|
|
ItemUpdateRequest Item::createValueUpdateRequest(uint8_t value,
|
|
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;
|
|
}
|