Sensors now work over broadcast pipe
Added Polynomal actor Added Item adding dialog Added Factor Actor
This commit is contained in:
@ -5,8 +5,10 @@
|
||||
#include "sensoractor.h"
|
||||
#include "timeractor.h"
|
||||
#include "regulator.h"
|
||||
#include "polynomalactor.h"
|
||||
#include "factoractor.h"
|
||||
|
||||
Actor::Actor(QObject *parent): QObject(parent)
|
||||
Actor::Actor(QObject *parent): Item(QRandomGenerator::global()->generate(), "", 0, parent)
|
||||
{
|
||||
}
|
||||
|
||||
@ -19,7 +21,6 @@ void Actor::performAction()
|
||||
{
|
||||
if(active)
|
||||
{
|
||||
trigger();
|
||||
sigValue(triggerValue);
|
||||
}
|
||||
}
|
||||
@ -61,17 +62,17 @@ bool Actor::isExausted()
|
||||
|
||||
void Actor::store(QJsonObject& json)
|
||||
{
|
||||
Item::store(json);
|
||||
json["Active"] = active;
|
||||
json["Exausted"] = exausted;
|
||||
if(!name.isEmpty()) json["Name"] = name;
|
||||
json["TriggerValue"] = triggerValue;
|
||||
}
|
||||
|
||||
void Actor::load(const QJsonObject& json)
|
||||
void Actor::load(const QJsonObject& json, const bool preserve)
|
||||
{
|
||||
Item::load(json, preserve);
|
||||
active = json["Active"].toBool();
|
||||
exausted = json["Exausted"].toBool();
|
||||
name = json["Name"].toString("");
|
||||
triggerValue = json["TriggerValue"].toInt();
|
||||
}
|
||||
|
||||
@ -85,12 +86,6 @@ uint8_t Actor::getTriggerValue()
|
||||
return triggerValue;
|
||||
}
|
||||
|
||||
QString Actor::getName()
|
||||
{
|
||||
if(name.size() > 0) return name;
|
||||
else return "Actor";
|
||||
}
|
||||
|
||||
void Actor::onValueChanged(uint8_t value)
|
||||
{
|
||||
|
||||
@ -103,6 +98,8 @@ Actor* Actor::createActor(const QString& type)
|
||||
else if(type == "Sensor") actor = new SensorActor();
|
||||
else if(type == "Timer") actor = new TimerActor();
|
||||
else if(type == "Regulator") actor = new Regulator();
|
||||
else if(type == "Polynomal") actor = new PolynomalActor();
|
||||
else if(type == "MultiFactor") actor = new MultiFactorActor();
|
||||
else if(type == "Actor") actor = new Actor();
|
||||
return actor;
|
||||
}
|
||||
@ -114,3 +111,10 @@ Actor* Actor::loadActor(const QJsonObject &json)
|
||||
if(actor) actor->load(json);
|
||||
return actor;
|
||||
}
|
||||
|
||||
void Actor::setValue(uint8_t value)
|
||||
{
|
||||
Item::setValue(value);
|
||||
setActive(value);
|
||||
}
|
||||
|
||||
|
@ -5,7 +5,9 @@
|
||||
#include <QString>
|
||||
#include <QJsonObject>
|
||||
|
||||
class Actor : public QObject
|
||||
#include "../items/item.h"
|
||||
|
||||
class Actor : public Item
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
@ -18,12 +20,9 @@ protected:
|
||||
|
||||
void performAction();
|
||||
|
||||
QString name;
|
||||
|
||||
signals:
|
||||
|
||||
void sigValue(uint8_t value);
|
||||
void trigger();
|
||||
|
||||
public slots:
|
||||
virtual void makeActive();
|
||||
@ -31,6 +30,7 @@ public slots:
|
||||
virtual void setActive(uint8_t state);
|
||||
virtual void onValueChanged(uint8_t state);
|
||||
|
||||
virtual void setValue(uint8_t value);
|
||||
|
||||
public:
|
||||
Actor(QObject* parent = nullptr);
|
||||
@ -39,8 +39,6 @@ public:
|
||||
|
||||
virtual QString actionName();
|
||||
|
||||
virtual QString getName();
|
||||
|
||||
bool isActive();
|
||||
void setTriggerValue(uint8_t value);
|
||||
|
||||
@ -49,7 +47,7 @@ public:
|
||||
static Actor* createActor(const QString& type);
|
||||
|
||||
virtual void store(QJsonObject& json);
|
||||
virtual void load(const QJsonObject& json);
|
||||
virtual void load(const QJsonObject& json, const bool preserve = false);
|
||||
static Actor* loadActor(const QJsonObject& json);
|
||||
};
|
||||
|
||||
|
@ -4,6 +4,7 @@ AlarmTime::AlarmTime(const QDateTime time, QObject *parent) : Actor(parent), tim
|
||||
{
|
||||
connect(&timer, SIGNAL(timeout()), this, SLOT(doTick()));
|
||||
timer.setInterval(1000);
|
||||
run();
|
||||
}
|
||||
|
||||
AlarmTime::~AlarmTime()
|
||||
@ -26,9 +27,9 @@ void AlarmTime::makeActive()
|
||||
run();
|
||||
}
|
||||
|
||||
QString AlarmTime::getName()
|
||||
QString AlarmTime::getName() const
|
||||
{
|
||||
if(name.size() > 0)return name;
|
||||
if(name_.size() > 0)return name_;
|
||||
else
|
||||
{
|
||||
QString string;
|
||||
@ -111,6 +112,7 @@ void AlarmTime::doTick()
|
||||
void AlarmTime::changeTime(const QDateTime& time)
|
||||
{
|
||||
time_=time;
|
||||
qDebug()<<"time: "<<time_;
|
||||
}
|
||||
|
||||
|
||||
@ -122,9 +124,11 @@ void AlarmTime::store(QJsonObject& json)
|
||||
json["Repeat"] = repeat_;
|
||||
}
|
||||
|
||||
void AlarmTime::load(const QJsonObject& json)
|
||||
void AlarmTime::load(const QJsonObject& json, const bool preserve)
|
||||
{
|
||||
Actor::load(json);
|
||||
bool oldActive = isActive();
|
||||
Actor::load(json, preserve);
|
||||
time_ = QDateTime::fromString(json["Time"].toString(""));
|
||||
repeat_ = json["Repeat"].toInt(REPEAT_NEVER);
|
||||
if(oldActive != isActive()) setActive(isActive());
|
||||
}
|
||||
|
@ -36,7 +36,7 @@ public:
|
||||
QDateTime getDateTime();
|
||||
|
||||
virtual void store(QJsonObject& json);
|
||||
virtual void load(const QJsonObject& json);
|
||||
virtual void load(const QJsonObject& json, const bool preserve = false);
|
||||
|
||||
uint8_t getRepeat();
|
||||
|
||||
@ -45,7 +45,7 @@ public slots:
|
||||
void run();
|
||||
virtual void makeActive();
|
||||
virtual void makeInactive();
|
||||
virtual QString getName();
|
||||
virtual QString getName() const;
|
||||
void doTick();
|
||||
void changeTime(const QDateTime& time);
|
||||
void setRepeat(const uint8_t repeat);
|
||||
|
80
src/actors/factoractor.cpp
Normal file
80
src/actors/factoractor.cpp
Normal file
@ -0,0 +1,80 @@
|
||||
#include "factoractor.h"
|
||||
|
||||
MultiFactorActor::MultiFactorActor(Actor* factorActor, const uint preCancleMin, QObject *parent):
|
||||
Actor(parent),
|
||||
factorActor_(factorActor),
|
||||
preCancleMin_(preCancleMin)
|
||||
{
|
||||
activationTime.setMSecsSinceEpoch(0);
|
||||
if(factorActor) connect(factorActor, &Actor::sigValue, this, &MultiFactorActor::factorActorSlot);
|
||||
}
|
||||
|
||||
void MultiFactorActor::factorActorSlot(uint8_t value)
|
||||
{
|
||||
if(value == factorDirection)
|
||||
{
|
||||
activationTime = QDateTime::currentDateTime();
|
||||
}
|
||||
}
|
||||
|
||||
void MultiFactorActor::setValue(uint8_t value)
|
||||
{
|
||||
if(value)
|
||||
{
|
||||
QDateTime current = QDateTime::currentDateTime();
|
||||
if(current.addSecs(-preCancleMin_*60) > activationTime )
|
||||
{
|
||||
performAction();
|
||||
}
|
||||
bool exausted = true;
|
||||
for(size_t i = 0; i < getActors().size(); ++i) if(!getActors()[i]->isExausted()) exausted = false;
|
||||
}
|
||||
}
|
||||
|
||||
QString MultiFactorActor::getName() const
|
||||
{
|
||||
if(name_.size() > 0) return name_;
|
||||
else
|
||||
{
|
||||
QString string;
|
||||
string = "Multi Factor \"" + (factorActor_ ? factorActor_->getName() : "NULL") + "\"";
|
||||
return string;
|
||||
}
|
||||
}
|
||||
|
||||
void MultiFactorActor::setFactorActor(Actor* factorActor)
|
||||
{
|
||||
if(factorActor_) delete factorActor_;
|
||||
factorActor_=factorActor;
|
||||
connect(factorActor_, &Actor::sigValue, this, &MultiFactorActor::factorActorSlot);
|
||||
}
|
||||
|
||||
void MultiFactorActor::store(QJsonObject &json)
|
||||
{
|
||||
json["Type"] = "MultiFactor";
|
||||
Actor::store(json);
|
||||
json["PreCancleMinutes"] = static_cast<int>(preCancleMin_);
|
||||
json["FactorDirection"] = factorDirection;
|
||||
QJsonObject factorActorObject;
|
||||
if(factorActor_)
|
||||
{
|
||||
factorActor_->store(factorActorObject);
|
||||
}
|
||||
}
|
||||
|
||||
void MultiFactorActor::load(const QJsonObject &json, bool preserve)
|
||||
{
|
||||
Actor::load(json, preserve);
|
||||
preCancleMin_ = static_cast<uint>(json["PreCancleMinutes"].toInt(10));
|
||||
factorDirection = json["FacotorDirection"].toBool(true);
|
||||
if(json["FactorActor"].isObject())
|
||||
{
|
||||
factorActor_ = Actor::loadActor(json["FactorActor"].toObject());
|
||||
}
|
||||
if(factorActor_)
|
||||
{
|
||||
connect(factorActor_, &Actor::sigValue, this, &MultiFactorActor::factorActorSlot);
|
||||
}
|
||||
}
|
||||
|
||||
|
44
src/actors/factoractor.h
Normal file
44
src/actors/factoractor.h
Normal file
@ -0,0 +1,44 @@
|
||||
#ifndef REMINDERACTOR_H
|
||||
#define REMINDERACTOR_H
|
||||
|
||||
#include <QDateTime>
|
||||
#include "actor.h"
|
||||
|
||||
class MultiFactorActor: public Actor
|
||||
{
|
||||
private:
|
||||
|
||||
Actor* factorActor_;
|
||||
QDateTime activationTime;
|
||||
uint preCancleMin_;
|
||||
|
||||
bool factorDirection = true;
|
||||
|
||||
private slots:
|
||||
|
||||
void factorActorSlot(uint8_t value);
|
||||
|
||||
public slots:
|
||||
|
||||
virtual void setValue(uint8_t value);
|
||||
|
||||
public:
|
||||
|
||||
MultiFactorActor(Actor* FactorActor = nullptr, const uint preCancleMin = 10, QObject *parent = nullptr);
|
||||
|
||||
virtual QString getName() const;
|
||||
|
||||
void setFactorActor(Actor* factorActor);
|
||||
Actor* getFactorActor(){return factorActor_;}
|
||||
void setFactorDirection(const bool direction){factorDirection = direction;}
|
||||
bool getFactorDirection(){return factorDirection;}
|
||||
uint getPreCancleTime(){return preCancleMin_;}
|
||||
void setPreCancleTime(uint minutes){preCancleMin_ = minutes;}
|
||||
|
||||
virtual ~MultiFactorActor(){}
|
||||
|
||||
virtual void store(QJsonObject& json);
|
||||
virtual void load(const QJsonObject& json, bool preserve);
|
||||
};
|
||||
|
||||
#endif // REMINDERACTOR_H
|
83
src/actors/polynomalactor.cpp
Normal file
83
src/actors/polynomalactor.cpp
Normal file
@ -0,0 +1,83 @@
|
||||
#include "polynomalactor.h"
|
||||
|
||||
PolynomalActor::PolynomalActor(const Sensor sensor, QObject* parent): Actor(parent), sensor_(sensor)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
PolynomalActor::PolynomalActor(QObject* parent): Actor(parent)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void PolynomalActor::setSensor(const Sensor sensor)
|
||||
{
|
||||
sensor_ = sensor;
|
||||
}
|
||||
|
||||
|
||||
void PolynomalActor::setCoeffiancts( const double pow3, const double pow2, const double pow1, const double pow0 )
|
||||
{
|
||||
pow3_=pow3;
|
||||
pow2_=pow2;
|
||||
pow1_=pow1;
|
||||
pow0_=pow0;
|
||||
}
|
||||
void PolynomalActor::getCoeffiancts( double& pow3, double& pow2, double& pow1, double& pow0 )
|
||||
{
|
||||
pow3=pow3_;
|
||||
pow2=pow2_;
|
||||
pow1=pow1_;
|
||||
pow0=pow0_;
|
||||
}
|
||||
|
||||
void PolynomalActor::sensorEvent(Sensor sensor)
|
||||
{
|
||||
if(active && sensor == sensor_)
|
||||
{
|
||||
double result = pow3_*(sensor.field*sensor.field*sensor.field)+pow2_*(sensor.field*sensor.field)+pow1_*sensor.field+pow0_;
|
||||
if(result < 0) result = 0;
|
||||
else if(result > 254) result = 255;
|
||||
if(result != prevValue)sigValue(static_cast<uint8_t>(result));
|
||||
prevValue = result;
|
||||
}
|
||||
}
|
||||
|
||||
void PolynomalActor::store(QJsonObject& json)
|
||||
{
|
||||
json["Type"] = "Polynomal";
|
||||
Actor::store(json);
|
||||
json["Pow3"] = pow3_;
|
||||
json["Pow2"] = pow2_;
|
||||
json["Pow1"] = pow1_;
|
||||
json["Pow0"] = pow0_;
|
||||
json["SensorType"] = static_cast<int>(sensor_.type);
|
||||
json["SensorId"] = static_cast<int>(sensor_.id);
|
||||
json["SensorField"] = sensor_.field;
|
||||
json["SensorName"] = sensor_.name;
|
||||
}
|
||||
|
||||
void PolynomalActor::load(const QJsonObject& json, bool preserve)
|
||||
{
|
||||
Actor::load(json, preserve);
|
||||
pow3_ = json["Pow3"].toDouble(0);
|
||||
pow2_ = json["Pow2"].toDouble(0);
|
||||
pow1_ = json["Pow1"].toDouble(1);
|
||||
pow0_ = json["Pow0"].toDouble(0);
|
||||
sensor_.type = json["SensorType"].toInt(0);
|
||||
sensor_.id = json["SensorId"].toInt(0);
|
||||
sensor_.field = json["SensorField"].toInt(0);
|
||||
sensor_.name = json["SensorName"].toString("Sensor");
|
||||
}
|
||||
|
||||
QString PolynomalActor::getName() const
|
||||
{
|
||||
if(name_.size() > 0) return name_;
|
||||
else
|
||||
{
|
||||
QString string;
|
||||
string = QString::number(pow3_) + "x^2 + " + QString::number(pow2_) + "x^2 + " + QString::number(pow1_) + "x + " + QString::number(pow0_) + " (x: " + sensor_.name + ")";
|
||||
return string;
|
||||
}
|
||||
}
|
||||
|
39
src/actors/polynomalactor.h
Normal file
39
src/actors/polynomalactor.h
Normal file
@ -0,0 +1,39 @@
|
||||
#ifndef POLYNOMALACTOR_H
|
||||
#define POLYNOMALACTOR_H
|
||||
#include "actor.h"
|
||||
#include "../sensors/sensor.h"
|
||||
|
||||
class PolynomalActor: public Actor
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
private:
|
||||
Sensor sensor_;
|
||||
double pow3_ = 0;
|
||||
double pow2_ = 0;
|
||||
double pow1_ = 1;
|
||||
double pow0_ = 0;
|
||||
|
||||
double prevValue = -1;
|
||||
|
||||
public slots:
|
||||
|
||||
void sensorEvent(Sensor sensor);
|
||||
|
||||
public:
|
||||
|
||||
void setCoeffiancts( const double pow3, const double pow2, const double pow1, const double pow0 );
|
||||
void getCoeffiancts( double& pow3, double& pow2, double& pow1, double& pow0 );
|
||||
|
||||
PolynomalActor(const Sensor sensor, QObject* parent = nullptr);
|
||||
PolynomalActor(QObject* parent = nullptr);
|
||||
void setSensor(const Sensor sensor);
|
||||
Sensor getSensor(){return sensor_;}
|
||||
virtual QString getName() const;
|
||||
virtual ~PolynomalActor(){}
|
||||
|
||||
virtual void store(QJsonObject& json);
|
||||
virtual void load(const QJsonObject& json, bool preserve);
|
||||
};
|
||||
|
||||
#endif // POLYNOMALACTOR_H
|
@ -22,16 +22,15 @@ void Regulator::sensorEvent(Sensor sensor)
|
||||
if(active && sensor == sensor_)
|
||||
{
|
||||
qDebug()<<"got sensor: "<<sensor.type<<" "<<sensor.id<<" want: "<<sensor_.type<<" "<<sensor_.id;
|
||||
if( sensor.field < setPoint_-band_ && (sensor.field < sensor_.field || sensor_.field > setPoint_-band_) )
|
||||
if( sensor.field < setPoint_-band_ && (sensor.field < sensor_.field || sensor_.field > setPoint_-band_ || first) )
|
||||
{
|
||||
trigger();
|
||||
sigValue(triggerValue);
|
||||
}
|
||||
else if( sensor.field > setPoint_+band_ && (sensor.field > sensor_.field || sensor_.field < setPoint_+band_) )
|
||||
else if( sensor.field > setPoint_+band_ && (sensor.field > sensor_.field || sensor_.field < setPoint_+band_ || first) )
|
||||
{
|
||||
trigger();
|
||||
sigValue(!triggerValue);
|
||||
}
|
||||
first = false;
|
||||
sensor_ = sensor;
|
||||
}
|
||||
}
|
||||
@ -63,9 +62,9 @@ void Regulator::store(QJsonObject& json)
|
||||
json["SensorName"] = sensor_.name;
|
||||
}
|
||||
|
||||
void Regulator::load(const QJsonObject& json)
|
||||
void Regulator::load(const QJsonObject& json, bool preserve)
|
||||
{
|
||||
Actor::load(json);
|
||||
Actor::load(json, preserve);
|
||||
band_ = json["Band"].toInt(1);
|
||||
setPoint_ = json["SetPoint"].toInt(22);
|
||||
sensor_.type = json["SensorType"].toInt(0);
|
||||
@ -74,9 +73,9 @@ void Regulator::load(const QJsonObject& json)
|
||||
sensor_.name = json["SensorName"].toString("Sensor");
|
||||
}
|
||||
|
||||
QString Regulator::getName()
|
||||
QString Regulator::getName() const
|
||||
{
|
||||
if(name.size() > 0) return name;
|
||||
if(name_.size() > 0) return name_;
|
||||
else
|
||||
{
|
||||
QString string;
|
||||
|
@ -12,6 +12,8 @@ private:
|
||||
float band_ = 1;
|
||||
bool invert_ = false;
|
||||
|
||||
bool first = true;
|
||||
|
||||
public slots:
|
||||
|
||||
void sensorEvent(Sensor sensor);
|
||||
@ -28,9 +30,9 @@ public:
|
||||
Regulator(const Sensor sensor, QObject* parent = nullptr);
|
||||
Regulator(QObject* parent = nullptr);
|
||||
Sensor getSensor(){return sensor_;}
|
||||
virtual QString getName();
|
||||
virtual QString getName() const;
|
||||
virtual ~Regulator(){}
|
||||
|
||||
virtual void store(QJsonObject& json);
|
||||
virtual void load(const QJsonObject& json);
|
||||
virtual void load(const QJsonObject& json, bool preserve);
|
||||
};
|
||||
|
@ -59,9 +59,9 @@ void SensorActor::store(QJsonObject& json)
|
||||
json["SensorName"] = sensor_.name;
|
||||
}
|
||||
|
||||
void SensorActor::load(const QJsonObject& json)
|
||||
void SensorActor::load(const QJsonObject& json, bool preserve)
|
||||
{
|
||||
Actor::load(json);
|
||||
Actor::load(json, preserve);
|
||||
sloap_ = json["Sloap"].toInt(0);
|
||||
threshold_ = json["Threshold"].toDouble(0);
|
||||
sensor_.type = json["SensorType"].toInt(0);
|
||||
@ -70,9 +70,9 @@ void SensorActor::load(const QJsonObject& json)
|
||||
sensor_.name = json["SensorName"].toString("Sensor");
|
||||
}
|
||||
|
||||
QString SensorActor::getName()
|
||||
QString SensorActor::getName() const
|
||||
{
|
||||
if(name.size() > 0) return name;
|
||||
if(name_.size() > 0) return name_;
|
||||
else
|
||||
{
|
||||
QString string;
|
||||
|
@ -29,14 +29,14 @@ public:
|
||||
SensorActor(const Sensor sensor, QObject* parent = nullptr);
|
||||
SensorActor(QObject* parent = nullptr);
|
||||
Sensor getSensor(){return sensor_;}
|
||||
virtual QString getName();
|
||||
virtual QString getName() const;
|
||||
virtual ~SensorActor(){}
|
||||
|
||||
float getThreshold();
|
||||
uint8_t getSloap();
|
||||
|
||||
virtual void store(QJsonObject& json);
|
||||
virtual void load(const QJsonObject& json);
|
||||
virtual void load(const QJsonObject& json, bool preserve);
|
||||
};
|
||||
|
||||
|
||||
|
@ -24,9 +24,9 @@ void TimerActor::store(QJsonObject& json)
|
||||
json["Timeout"] = timeoutMsec_;
|
||||
}
|
||||
|
||||
void TimerActor::load(const QJsonObject& json)
|
||||
void TimerActor::load(const QJsonObject& json, bool preserve)
|
||||
{
|
||||
Actor::load(json);
|
||||
Actor::load(json, preserve);
|
||||
timeoutMsec_ = json["Timeout"].toInt(10000);
|
||||
}
|
||||
|
||||
@ -45,9 +45,9 @@ void TimerActor::timeout()
|
||||
performAction();
|
||||
}
|
||||
|
||||
QString TimerActor::getName()
|
||||
QString TimerActor::getName() const
|
||||
{
|
||||
if(name.size() > 0) return name;
|
||||
if(name_.size() > 0) return name_;
|
||||
else
|
||||
{
|
||||
QString string;
|
||||
|
@ -21,10 +21,10 @@ public slots:
|
||||
|
||||
public:
|
||||
explicit TimerActor(const int timeoutSec = 60, QObject *parent = nullptr);
|
||||
virtual QString getName();
|
||||
virtual QString getName() const;
|
||||
|
||||
int getTimeout();
|
||||
|
||||
virtual void store(QJsonObject& json);
|
||||
virtual void load(const QJsonObject& json);
|
||||
virtual void load(const QJsonObject& json, bool preserve);
|
||||
};
|
||||
|
Reference in New Issue
Block a user