switched from qsettings to json added editng of actors

This commit is contained in:
2019-06-06 21:19:12 +02:00
parent b04fbfb5bc
commit df27b622a0
141 changed files with 4402 additions and 5068 deletions

140
src/actors/actor.cpp Normal file
View File

@ -0,0 +1,140 @@
#include "actor.h"
#include<QDebug>
#include "alarmtime.h"
#include "sensoractor.h"
#include "timeractor.h"
#include "regulator.h"
Actor::Actor(QObject *parent): QObject(parent)
{
}
Actor::~Actor()
{
}
void Actor::performAction()
{
if(active)
{
trigger();
sigValue(triggerValue);
}
}
void Actor::makeActive()
{
active = true;
}
void Actor::makeInactive()
{
active = false;
}
QString Actor::actionName()
{
QString string;
if(triggerValue == 0 ) string = "off";
else if(triggerValue == 1 ) string = "on";
else string = "value to " + QString::number(triggerValue);
return string;
}
void Actor::setActive(uint8_t state)
{
state ? makeActive() : makeInactive();
}
bool Actor::isActive()
{
return active;
}
bool Actor::isExausted()
{
return exausted;
}
void Actor::store(QJsonObject& json)
{
json["Active"] = active;
json["Exausted"] = exausted;
if(!name.isEmpty()) json["Name"] = name;
json["TriggerValue"] = triggerValue;
}
void Actor::load(const QJsonObject& json)
{
active = json["Active"].toBool();
exausted = json["Exausted"].toBool();
name = json["Name"].toString("");
triggerValue = json["TriggerValue"].toInt();
}
void Actor::store(QString subsecton, QSettings* settings)
{
settings->setValue(subsecton + "Active", active);
settings->setValue(subsecton + "Exausted", exausted);
if(!name.isEmpty())settings->setValue(subsecton + "Name", name);
settings->setValue(subsecton + "TriggerValue", triggerValue);
}
void Actor::load(QString subsecton, QSettings* settings)
{
active = settings->value(subsecton + "Active").toBool();
exausted = settings->value(subsecton + "Exausted").toBool();
if(settings->contains(subsecton + "Name"))name = settings->value(subsecton + "Name").toString();
triggerValue = settings->value(subsecton + "TriggerValue").toUInt();
}
void Actor::setTriggerValue(uint8_t value)
{
triggerValue=value;
}
uint8_t Actor::getTriggerValue()
{
return triggerValue;
}
QString Actor::getName()
{
if(name.size() > 0) return name;
else return "Actor";
}
void Actor::onValueChanged(uint8_t value)
{
}
Actor* Actor::createActor(const QString& type)
{
Actor* actor = nullptr;
if(type == "Alarm") actor = new AlarmTime();
else if(type == "Sensor") actor = new SensorActor();
else if(type == "Timer") actor = new TimerActor();
else if(type == "Regulator") actor = new Regulator();
else if(type == "Actor") actor = new Actor();
return actor;
}
Actor* Actor::loadActor(const QJsonObject &json)
{
QString type = json["Type"].toString("Actor");
Actor* actor = createActor(type);
if(actor) actor->load(json);
return actor;
}
Actor* Actor::loadActor(QString subsecton, QSettings* settings)
{
QString type = settings->value(subsecton + "Type").toString();
Actor* actor = createActor(type);
if(actor) actor->load(subsecton, settings);
return actor;
}

61
src/actors/actor.h Normal file
View File

@ -0,0 +1,61 @@
#ifndef ACTOR_H
#define ACTOR_H
#include <QObject>
#include <QString>
#include <QSettings>
#include <QJsonObject>
class Actor : public QObject
{
Q_OBJECT
public:
uint8_t triggerValue = 0;
protected:
bool active = true;
bool exausted = false;
void performAction();
QString name;
signals:
void sigValue(uint8_t value);
void trigger();
public slots:
virtual void makeActive();
virtual void makeInactive();
virtual void setActive(uint8_t state);
virtual void onValueChanged(uint8_t state);
public:
Actor(QObject* parent = nullptr);
virtual ~Actor();
bool isExausted();
virtual QString actionName();
virtual QString getName();
bool isActive();
void setTriggerValue(uint8_t value);
uint8_t getTriggerValue();
static Actor* createActor(const QString& type);
virtual void store(QJsonObject& json);
virtual void load(const QJsonObject& json);
static Actor* loadActor(const QJsonObject& json);
virtual void store(QString subsecton, QSettings* settings);
virtual void load(QString subsecton, QSettings* settings);
static Actor* loadActor(QString subsecton, QSettings* settings);
};
#endif // ACTOR_H

145
src/actors/alarmtime.cpp Normal file
View File

@ -0,0 +1,145 @@
#include "alarmtime.h"
AlarmTime::AlarmTime(const QDateTime time, QObject *parent) : Actor(parent), time_(time)
{
connect(&timer, SIGNAL(timeout()), this, SLOT(doTick()));
timer.setInterval(1000);
}
AlarmTime::~AlarmTime()
{
makeInactive();
}
void AlarmTime::run()
{
makeInactive();
active = true;
timer.start();
qDebug()<<"Start Alarm Time Manager\n";
}
void AlarmTime::makeActive()
{
run();
}
QString AlarmTime::getName()
{
if(name.size() > 0)return name;
else
{
QString string;
string = "Alarm: ";
if(repeat_ == REPEAT_DAILY)
{
string.append("daily ");
string.append(time_.toString("HH:mm"));
}
else if(repeat_ == REPEAT_WEEKLY)
{
string.append("weekly ");
string.append(time_.toString("ddd HH:mm"));
}
else if(repeat_ == REPEAT_MONTHLY)
{
string.append("monthly ");
string.append(time_.toString("dd HH:mm"));
}
else if(repeat_ == REPEAT_YEARLY)
{
string.append("yearly ");
string.append(time_.toString("dd.mm HH:mm"));
}
else string.append(time_.toString("dd.mm.yyyy HH:mm"));
return string;
}
}
void AlarmTime::setRepeat(const uint8_t repeat)
{
repeat_=repeat;
}
uint8_t AlarmTime::getRepeat()
{
return repeat_;
}
QDateTime AlarmTime::getDateTime()
{
return time_;
}
void AlarmTime::makeInactive()
{
timer.stop();
active = false;
}
void AlarmTime::doTick()
{
if(
(
(triggerd_ == false) &&
(time_.date().year() == QDate::currentDate().year() || repeat_ != REPEAT_NEVER) &&
(time_.date().month() == QDate::currentDate().month() || repeat_ == REPEAT_MONTHLY || repeat_ == REPEAT_DAILY) &&
(time_.date().day() == QDate::currentDate().day() || repeat_ == REPEAT_DAILY)
)
||
(
(repeat_ == REPEAT_WEEKLY) &&
(time_.date().dayOfWeek() == QDate::currentDate().dayOfWeek())
)
)
{
if(time_.time().hour() == QTime::currentTime().hour() && time_.time().minute() == QTime::currentTime().minute())
{
qDebug()<<"Trigger\n";
triggerd_=true;
performAction();
if(repeat_ == REPEAT_NEVER) exausted = true;
}
}
else if( repeat_ != REPEAT_NEVER && time_.time().hour() != QTime::currentTime().hour() ) triggerd_=false;
}
void AlarmTime::changeTime(const QDateTime& time)
{
time_=time;
}
void AlarmTime::store(QJsonObject& json)
{
json["Type"] = "Alarm";
Actor::store(json);
json["Time"] = time_.toString();
json["Repeat"] = repeat_;
}
void AlarmTime::load(const QJsonObject& json)
{
Actor::load(json);
time_ = QDateTime::fromString(json["Time"].toString(""));
repeat_ = json["Repeat"].toInt(REPEAT_NEVER);
}
void AlarmTime::store(QString subsecton, QSettings* settings)
{
settings->setValue(subsecton + "Type", "Alarm");
Actor::store(subsecton, settings);
settings->setValue(subsecton + "Time", time_);
settings->setValue(subsecton + "Repeat", repeat_);
}
void AlarmTime::load(QString subsecton, QSettings* settings)
{
Actor::load(subsecton, settings);
time_ = settings->value(subsecton + "Time").toDateTime();
repeat_ = settings->value(subsecton + "Repeat").toUInt();
}

57
src/actors/alarmtime.h Normal file
View File

@ -0,0 +1,57 @@
#ifndef ALARMTIME_H
#define ALARMTIME_H
#include <QDateTime>
#include <QObject>
#include <QRunnable>
#include <QScopedPointer>
#include <QEventLoop>
#include <QTimer>
#include <QProcess>
#include <QDebug>
#include <stdint.h>
#include "actor.h"
class AlarmTime : public Actor, public QRunnable
{
Q_OBJECT
public:
static const uint8_t REPEAT_NEVER = 0;
static const uint8_t REPEAT_DAILY = 1;
static const uint8_t REPEAT_WEEKLY = 2;
static const uint8_t REPEAT_MONTHLY = 3;
static const uint8_t REPEAT_YEARLY = 4;
private:
bool triggerd_ = false;
QDateTime time_;
QTimer timer;
uint8_t repeat_ = REPEAT_NEVER;
public:
explicit AlarmTime(const QDateTime time = QDateTime::currentDateTime(), QObject *parent = nullptr);
~AlarmTime();
QDateTime getDateTime();
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);
uint8_t getRepeat();
public slots:
void run();
virtual void makeActive();
virtual void makeInactive();
virtual QString getName();
void doTick();
void changeTime(const QDateTime& time);
void setRepeat(const uint8_t repeat);
};
#endif // ALARMTIME_H

111
src/actors/regulator.cpp Normal file
View File

@ -0,0 +1,111 @@
#include "regulator.h"
#include <QDebug>
Regulator::Regulator(const Sensor sensor, QObject* parent): Actor(parent), sensor_(sensor)
{
}
Regulator::Regulator(QObject* parent): Actor(parent)
{
}
void Regulator::setSensor(const Sensor sensor)
{
sensor_ = sensor;
}
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_) )
{
trigger();
sigValue(triggerValue);
}
else if( sensor.field > setPoint_+band_ && (sensor.field > sensor_.field || sensor_.field < setPoint_+band_) )
{
trigger();
sigValue(!triggerValue);
}
sensor_ = sensor;
}
}
void Regulator::setPoint(float setPoint)
{
setPoint_ = setPoint;
}
void Regulator::setBand ( float band )
{
band_ = band;
}
void Regulator::setInvert( bool invert )
{
invert_ = invert;
}
void Regulator::store(QJsonObject& json)
{
json["Type"] = "Regulator";
Actor::store(json);
json["Band"] = band_;
json["SetPoint"] = setPoint_;
json["SensorType"] = static_cast<int>(sensor_.type);
json["SensorId"] = static_cast<int>(sensor_.id);
json["SensorField"] = sensor_.field;
json["SensorName"] = sensor_.name;
}
void Regulator::load(const QJsonObject& json)
{
Actor::load(json);
band_ = json["Band"].toInt(1);
setPoint_ = json["SetPoint"].toInt(22);
sensor_.type = json["SensorType"].toInt(0);
sensor_.id = json["SensorId"].toInt(0);
sensor_.field = json["SensorField"].toInt(0);
sensor_.name = json["SensorName"].toString("Sensor");
}
void Regulator::store(QString subsecton, QSettings* settings)
{
settings->setValue(subsecton + "Type", "Regulator");
Actor::store(subsecton, settings);
settings->setValue(subsecton + "Band", band_);
settings->setValue(subsecton + "SetPoint", setPoint_);
settings->setValue(subsecton + "SensorType", static_cast<int>(sensor_.type));
settings->setValue(subsecton + "SensorId", static_cast<int>(sensor_.id));
settings->setValue(subsecton + "SensorField", sensor_.field);
settings->setValue(subsecton + "SensorName", sensor_.name);
}
void Regulator::load(QString subsecton, QSettings* settings)
{
Actor::load(subsecton, settings);
setPoint_ = settings->value(subsecton + "SetPoint").toUInt();
band_ = settings->value(subsecton + "Band").toFloat();
sensor_.type = settings->value(subsecton + "SensorType").toUInt();
sensor_.id = settings->value(subsecton + "SensorId").toUInt();
sensor_.field = settings->value(subsecton + "SensorField").toFloat();
sensor_.name = settings->value(subsecton + "SensorName").toString();
}
QString Regulator::getName()
{
if(name.size() > 0) return name;
else
{
QString string;
string = "Regulate \"" + sensor_.name + "\" to ";
string.append(QString::number(setPoint_) + " ");
return string;
}
}

39
src/actors/regulator.h Normal file
View File

@ -0,0 +1,39 @@
#pragma once
#include "actor.h"
#include "../sensors/sensor.h"
class Regulator : public Actor
{
Q_OBJECT
private:
Sensor sensor_;
float setPoint_ = 0;
float band_ = 1;
bool invert_ = false;
public slots:
void sensorEvent(Sensor sensor);
void setSensor(const Sensor sensor);
void setPoint( float setPoint );
void setBand ( float band );
void setInvert( bool invert );
public:
float getBand() {return band_;}
float getSetPoint() {return setPoint_;}
Regulator(const Sensor sensor, QObject* parent = nullptr);
Regulator(QObject* parent = nullptr);
Sensor getSensor(){return sensor_;}
virtual QString getName();
virtual ~Regulator(){}
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);
};

112
src/actors/sensoractor.cpp Normal file
View File

@ -0,0 +1,112 @@
#include "sensoractor.h"
#include <QDebug>
SensorActor::SensorActor(const Sensor sensor, QObject* parent): Actor(parent), sensor_(sensor)
{
}
SensorActor::SensorActor(QObject* parent): Actor(parent)
{
}
void SensorActor::setSensor(const Sensor sensor)
{
sensor_ = sensor;
}
void SensorActor::sensorEvent(Sensor sensor)
{
if(sensor == sensor_)
{
qDebug()<<"got sensor: "<<sensor.type<<" "<<sensor.id<<" want: "<<sensor_.type<<" "<<sensor_.id;
if((sloap_ == SLOPE_UP || sloap_ == SLOPE_BOTH) && sensor_.field < threshold_ && sensor.field >= threshold_ ) performAction();
else if((sloap_ == SLOPE_DOWN || sloap_ == SLOPE_BOTH) && sensor_.field > threshold_ && sensor.field <= threshold_) performAction();
sensor_ = sensor;
}
}
void SensorActor::setSloap(uint8_t sloap)
{
sloap_=sloap;
}
void SensorActor::setThreshold(float threshold)
{
threshold_ = threshold;
}
float SensorActor::getThreshold()
{
return threshold_;
}
uint8_t SensorActor::getSloap()
{
return sloap_;
}
void SensorActor::store(QJsonObject& json)
{
json["Type"] = "Sensor";
Actor::store(json);
json["Sloap"] = sloap_;
json["Threshold"] = threshold_;
json["SensorType"] = static_cast<int>(sensor_.type);
json["SensorId"] = static_cast<int>(sensor_.id);
json["SensorField"] = sensor_.field;
json["SensorName"] = sensor_.name;
}
void SensorActor::load(const QJsonObject& json)
{
Actor::load(json);
sloap_ = json["Sloap"].toInt(0);
threshold_ = json["Threshold"].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");
}
void SensorActor::store(QString subsecton, QSettings* settings)
{
settings->setValue(subsecton + "Type", "Sensor");
Actor::store(subsecton, settings);
settings->setValue(subsecton + "Sloap", sloap_);
settings->setValue(subsecton + "Threshold", threshold_);
settings->setValue(subsecton + "SensorType", static_cast<int>(sensor_.type));
settings->setValue(subsecton + "SensorId", static_cast<int>(sensor_.id));
settings->setValue(subsecton + "SensorField", sensor_.field);
settings->setValue(subsecton + "SensorName", sensor_.name);
}
void SensorActor::load(QString subsecton, QSettings* settings)
{
Actor::load(subsecton, settings);
sloap_ = settings->value(subsecton + "Sloap").toUInt();
threshold_ = settings->value(subsecton + "Threshold").toFloat();
sensor_.type = settings->value(subsecton + "SensorType").toUInt();
sensor_.id = settings->value(subsecton + "SensorId").toUInt();
sensor_.field = settings->value(subsecton + "SensorField").toFloat();
sensor_.name = settings->value(subsecton + "SensorName").toString();
}
QString SensorActor::getName()
{
if(name.size() > 0) return name;
else
{
QString string;
string = "Sensor \"" + sensor_.name + "\"";
if(sloap_ == SLOPE_UP) string.append(" rises to ");
else if (sloap_ == SLOPE_DOWN) string.append(" drops to ");
else if (sloap_ == SLOPE_BOTH) string.append(" passes ");
string.append(QString::number(threshold_) + " ");
return string;
}
}

45
src/actors/sensoractor.h Normal file
View File

@ -0,0 +1,45 @@
#pragma once
#include "actor.h"
#include "../sensors/sensor.h"
class SensorActor : public Actor
{
Q_OBJECT
public:
static constexpr uint8_t SLOPE_UP = 0;
static constexpr uint8_t SLOPE_DOWN = 1;
static constexpr uint8_t SLOPE_BOTH = 2;
private:
Sensor sensor_;
uint8_t sloap_ = SLOPE_UP;
float threshold_ = 0;
public slots:
void sensorEvent(Sensor sensor);
void setSloap(uint8_t sloap);
void setSensor(const Sensor sensor);
void setThreshold( float threshold );
public:
SensorActor(const Sensor sensor, QObject* parent = nullptr);
SensorActor(QObject* parent = nullptr);
Sensor getSensor(){return sensor_;}
virtual QString getName();
virtual ~SensorActor(){}
float getThreshold();
uint8_t getSloap();
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);
};

69
src/actors/timeractor.cpp Normal file
View File

@ -0,0 +1,69 @@
#include "timeractor.h"
#include <QDebug>
TimerActor::TimerActor(const int timeoutSec, QObject *parent): Actor(parent), timeoutMsec_(timeoutSec*1000)
{
connect(&timer, &QTimer::timeout, this, &TimerActor::timeout);
timer.setSingleShot(true);
}
void TimerActor::onValueChanged(uint8_t state)
{
if((state && !triggerValue) || (!state && triggerValue))
{
if(timer.isActive()) timer.stop();
timer.setInterval(timeoutMsec_);
timer.start();
}
}
void TimerActor::store(QJsonObject& json)
{
json["Type"] = "Timer";
Actor::store(json);
json["Timeout"] = timeoutMsec_;
}
void TimerActor::load(const QJsonObject& json)
{
Actor::load(json);
timeoutMsec_ = json["Timeout"].toInt(10000);
}
void TimerActor::store(QString subsecton, QSettings* settings)
{
settings->setValue(subsecton + "Type", "Timer");
Actor::store(subsecton, settings);
settings->setValue(subsecton + "Timeout", timeoutMsec_);
}
void TimerActor::load(QString subsecton, QSettings* settings)
{
Actor::load(subsecton, settings);
timeoutMsec_ = settings->value(subsecton + "Timeout").toInt();
}
void TimerActor::setTimeout(const int timeoutSec)
{
timeoutMsec_ = timeoutSec*1000;
}
int TimerActor::getTimeout()
{
return timeoutMsec_*1000;
}
void TimerActor::timeout()
{
performAction();
}
QString TimerActor::getName()
{
if(name.size() > 0) return name;
else
{
QString string;
string = "Timeout after " + QString::number(timeoutMsec_/1000) + " seconds. ";
return string;
}
}

33
src/actors/timeractor.h Normal file
View File

@ -0,0 +1,33 @@
#pragma once
#include <QTimer>
#include "actor.h"
class TimerActor: public Actor
{
Q_OBJECT
private:
int timeoutMsec_;
QTimer timer;
private slots:
void timeout();
public slots:
virtual void onValueChanged(uint8_t state);
void setTimeout(const int timeoutSec);
public:
explicit TimerActor(const int timeoutSec = 60, QObject *parent = nullptr);
virtual QString getName();
int getTimeout();
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);
};