Added json broadcasting
This commit is contained in:
@ -21,6 +21,8 @@ QMAKE_CXXFLAGS += -flto -std=c++17 -O2
|
||||
|
||||
SOURCES += \
|
||||
src/broadcast.cpp \
|
||||
src/iomuliplexer.cpp \
|
||||
src/mainobject.cpp \
|
||||
src/ui/actorwidgets/sensoractorwidget.cpp \
|
||||
src/ui/actorwidgets/alarmwidget.cpp \
|
||||
src/ui/actorwidgets/timeractorwidget.cpp \
|
||||
@ -64,6 +66,8 @@ SOURCES += \
|
||||
|
||||
HEADERS += \
|
||||
src/broadcast.h \
|
||||
src/iomuliplexer.h \
|
||||
src/mainobject.h \
|
||||
src/ui/actorwidgets/alarmwidget.h \
|
||||
src/ui/actorwidgets/sensoractorwidget.h \
|
||||
src/ui/actorwidgets/timeractorwidget.h \
|
||||
|
@ -75,22 +75,6 @@ void Actor::load(const QJsonObject& json)
|
||||
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;
|
||||
@ -130,11 +114,3 @@ Actor* Actor::loadActor(const QJsonObject &json)
|
||||
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;
|
||||
}
|
||||
|
@ -3,7 +3,6 @@
|
||||
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
#include <QSettings>
|
||||
#include <QJsonObject>
|
||||
|
||||
class Actor : public QObject
|
||||
@ -52,10 +51,6 @@ public:
|
||||
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
|
||||
|
@ -128,18 +128,3 @@ void AlarmTime::load(const QJsonObject& 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();
|
||||
}
|
||||
|
@ -38,9 +38,6 @@ public:
|
||||
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:
|
||||
|
@ -74,30 +74,6 @@ void Regulator::load(const QJsonObject& json)
|
||||
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;
|
||||
|
@ -33,7 +33,4 @@ public:
|
||||
|
||||
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);
|
||||
};
|
||||
|
@ -70,30 +70,6 @@ void SensorActor::load(const QJsonObject& json)
|
||||
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;
|
||||
|
@ -37,9 +37,6 @@ public:
|
||||
|
||||
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);
|
||||
};
|
||||
|
||||
|
||||
|
@ -30,18 +30,6 @@ void TimerActor::load(const QJsonObject& 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;
|
||||
@ -49,7 +37,7 @@ void TimerActor::setTimeout(const int timeoutSec)
|
||||
|
||||
int TimerActor::getTimeout()
|
||||
{
|
||||
return timeoutMsec_*1000;
|
||||
return timeoutMsec_/1000;
|
||||
}
|
||||
|
||||
void TimerActor::timeout()
|
||||
|
@ -27,7 +27,4 @@ public:
|
||||
|
||||
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);
|
||||
};
|
||||
|
@ -1,15 +1,17 @@
|
||||
#include "broadcast.h"
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonArray>
|
||||
#include <QDebug>
|
||||
|
||||
BroadCast::BroadCast(QIODevice* const iodevice): iodevice_(iodevice)
|
||||
BroadCast::BroadCast(QIODevice* const iodevice, bool master ): master_(master), iodevice_(iodevice)
|
||||
{
|
||||
if(iodevice_ != nullptr) connect(iodevice_, &QIODevice::readyRead, this, &BroadCast::readyRead);
|
||||
}
|
||||
|
||||
void BroadCast::write(const char * const buffer, const size_t length)
|
||||
{
|
||||
QByteArray mBuffer("BCST: ");
|
||||
QByteArray mBuffer("bcst: ");
|
||||
for (size_t i = 0; i < length; ++i)
|
||||
{
|
||||
if(buffer[i] != '\n' && buffer[i] != '\0') mBuffer.push_back(buffer[i]);
|
||||
@ -20,6 +22,7 @@ void BroadCast::write(const char * const buffer, const size_t length)
|
||||
else mBuffer.push_back('0');
|
||||
}
|
||||
}
|
||||
mBuffer.push_back('\n');
|
||||
if(iodevice_)iodevice_->write(mBuffer);
|
||||
}
|
||||
|
||||
@ -31,14 +34,30 @@ void BroadCast::write(const QByteArray& buffer)
|
||||
void BroadCast::sendJson(const QJsonObject& json)
|
||||
{
|
||||
QJsonDocument jsonDocument(json);
|
||||
write("JSON: ");
|
||||
write(jsonDocument.toJson());
|
||||
QByteArray buffer("JSON: ");
|
||||
buffer.append(jsonDocument.toJson());
|
||||
write(buffer);
|
||||
}
|
||||
|
||||
void BroadCast::requestJson()
|
||||
{
|
||||
if(iodevice_)iodevice_->write("bcst: GETJSN\n");
|
||||
}
|
||||
|
||||
void BroadCast::decodeMaster()
|
||||
{
|
||||
if(buffer_.size() >= 6 && buffer_[0] == 'G' && buffer_[1] == 'E' && buffer_[2] == 'T' && buffer_[3] == 'J' && buffer_[4] == 'S' && buffer_[5] == 'N')
|
||||
{
|
||||
qDebug()<<"json requested";
|
||||
jsonRequested();
|
||||
}
|
||||
}
|
||||
|
||||
void BroadCast::decode()
|
||||
{
|
||||
if(buffer_.size() >= 6 && buffer_[0] == 'J' && buffer_[1] == 'S' && buffer_[2] == 'O' && buffer_[3] == 'N' && buffer_[4] == ':')
|
||||
{
|
||||
qDebug()<<"got json";
|
||||
buffer_.remove(0,6);
|
||||
for(int i = 0; i < buffer_.size()-1; ++i)
|
||||
{
|
||||
@ -53,10 +72,16 @@ void BroadCast::decode()
|
||||
buffer_.remove(i+1,1);
|
||||
}
|
||||
}
|
||||
QJsonDocument document;
|
||||
|
||||
QJsonParseError error;
|
||||
document.fromJson(buffer_, &error);
|
||||
if(error.error == QJsonParseError::NoError) gotJson(document.object());
|
||||
QJsonDocument document = QJsonDocument::fromJson(buffer_, &error);
|
||||
|
||||
qDebug()<<"orig json:";
|
||||
qDebug()<<buffer_.data();
|
||||
qDebug()<<"after parse:";
|
||||
qDebug()<<document.toJson().data();
|
||||
QJsonObject jsonObject = document.object();
|
||||
if(error.error == QJsonParseError::NoError || document.isEmpty() || jsonObject.isEmpty()) gotJson(jsonObject);
|
||||
else
|
||||
{
|
||||
qDebug()<<error.errorString();
|
||||
@ -69,12 +94,13 @@ void BroadCast::readyRead()
|
||||
buffer_.append(iodevice_->readAll());
|
||||
if(buffer_.size() >= 6)
|
||||
{
|
||||
if(buffer_[0] == 'B' && buffer_[1] == 'C' && buffer_[2] == 'S' && buffer_[3] == 'T' && buffer_[4] == ':')
|
||||
if(buffer_[0] == 'b' && buffer_[1] == 'c' && buffer_[2] == 's' && buffer_[3] == 't' && buffer_[4] == ':')
|
||||
{
|
||||
if(buffer_.contains('\n'))
|
||||
{
|
||||
buffer_.remove(0,6);
|
||||
decode();
|
||||
if(master_)decodeMaster();
|
||||
buffer_.clear();
|
||||
}
|
||||
}
|
||||
|
@ -11,6 +11,8 @@ class BroadCast: public QObject
|
||||
|
||||
private:
|
||||
|
||||
bool master_;
|
||||
|
||||
static constexpr uint8_t MODE_PREPACKET = 0;
|
||||
static constexpr uint8_t MODE_PACKET = 1;
|
||||
|
||||
@ -22,18 +24,24 @@ private:
|
||||
void write(const QByteArray& buffer);
|
||||
|
||||
void decode();
|
||||
void decodeMaster();
|
||||
|
||||
private slots:
|
||||
|
||||
void readyRead();
|
||||
|
||||
public slots:
|
||||
|
||||
void requestJson();
|
||||
|
||||
signals:
|
||||
|
||||
void jsonRequested();
|
||||
void gotJson(QJsonObject json);
|
||||
|
||||
public:
|
||||
|
||||
BroadCast(QIODevice* const iodevice = nullptr);
|
||||
BroadCast(QIODevice* const iodevice = nullptr, bool master = true);
|
||||
void sendJson(const QJsonObject& json);
|
||||
|
||||
};
|
||||
|
76
src/iomuliplexer.cpp
Normal file
76
src/iomuliplexer.cpp
Normal file
@ -0,0 +1,76 @@
|
||||
#include "iomuliplexer.h"
|
||||
#include<QDebug>
|
||||
|
||||
VirutalIODevice::VirutalIODevice(QObject* parent): QBuffer(parent)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
qint64 VirutalIODevice::writeData(const char *data, qint64 len)
|
||||
{
|
||||
blockSignals(true);
|
||||
qint64 ret = QBuffer::writeData(data, len);
|
||||
blockSignals(false);
|
||||
masterReadyRead();
|
||||
return ret;
|
||||
}
|
||||
|
||||
qint64 VirutalIODevice::masterWrite(const QByteArray &byteArray)
|
||||
{
|
||||
return masterWrite(byteArray.data(), byteArray.length());
|
||||
}
|
||||
|
||||
qint64 VirutalIODevice::masterWrite(const char *data, qint64 maxSize)
|
||||
{
|
||||
blockSignals(true);
|
||||
qint64 ret = QBuffer::writeData(data, maxSize);
|
||||
blockSignals(false);
|
||||
readyRead();
|
||||
return ret;
|
||||
}
|
||||
|
||||
IoMuliplexer::IoMuliplexer(QIODevice* mainDevice, QObject* Parent): IoMuliplexer(Parent)
|
||||
{
|
||||
setIoDevice(mainDevice);
|
||||
}
|
||||
|
||||
IoMuliplexer::IoMuliplexer(QObject* Parent): QObject(Parent)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
IoMuliplexer::~IoMuliplexer()
|
||||
{
|
||||
for(size_t i = 0; i < ioDevices_.size(); ++i) delete ioDevices_[i];
|
||||
ioDevices_.clear();
|
||||
}
|
||||
|
||||
void IoMuliplexer::setIoDevice(QIODevice* mainDevice)
|
||||
{
|
||||
mainDevice_ = mainDevice;
|
||||
connect(mainDevice_, &QIODevice::readyRead, this, &IoMuliplexer::mainIsReadyRead);
|
||||
}
|
||||
|
||||
QIODevice* IoMuliplexer::getIoDevice()
|
||||
{
|
||||
ioDevices_.push_back(new VirutalIODevice);
|
||||
ioDevices_.back()->open(QIODevice::ReadWrite);
|
||||
connect(ioDevices_.back(), &VirutalIODevice::masterReadyRead, this, &IoMuliplexer::clientIsReadyRead);
|
||||
return ioDevices_.back();
|
||||
}
|
||||
|
||||
void IoMuliplexer::clientIsReadyRead()
|
||||
{
|
||||
VirutalIODevice* device = dynamic_cast<VirutalIODevice*>(sender());
|
||||
if(device)
|
||||
{
|
||||
QByteArray array = device->readAll();
|
||||
mainDevice_->write(array);
|
||||
}
|
||||
}
|
||||
|
||||
void IoMuliplexer::mainIsReadyRead()
|
||||
{
|
||||
QByteArray array = mainDevice_->readAll();
|
||||
for(size_t i = 0; i < ioDevices_.size(); ++i) ioDevices_[i]->masterWrite(array);
|
||||
}
|
54
src/iomuliplexer.h
Normal file
54
src/iomuliplexer.h
Normal file
@ -0,0 +1,54 @@
|
||||
#ifndef IOMULIPLEXER_H
|
||||
#define IOMULIPLEXER_H
|
||||
|
||||
#include <QObject>
|
||||
#include <vector>
|
||||
#include <QIODevice>
|
||||
#include <QBuffer>
|
||||
|
||||
class VirutalIODevice: public QBuffer
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
|
||||
VirutalIODevice(QObject* parent = nullptr);
|
||||
virtual ~VirutalIODevice() override {}
|
||||
|
||||
virtual qint64 writeData(const char *data, qint64 len) override;
|
||||
|
||||
qint64 masterWrite(const QByteArray &byteArray);
|
||||
qint64 masterWrite(const char *data, qint64 maxSize);
|
||||
|
||||
signals:
|
||||
|
||||
void masterReadyRead();
|
||||
|
||||
};
|
||||
|
||||
|
||||
class IoMuliplexer: public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
private:
|
||||
|
||||
QIODevice* mainDevice_;
|
||||
std::vector< VirutalIODevice* > ioDevices_;
|
||||
|
||||
public:
|
||||
explicit IoMuliplexer(QIODevice* mainDevice, QObject* Parent = nullptr);
|
||||
explicit IoMuliplexer(QObject* Parent = nullptr);
|
||||
|
||||
~IoMuliplexer();
|
||||
|
||||
void setIoDevice(QIODevice* mainDevice);
|
||||
|
||||
QIODevice* getIoDevice();
|
||||
|
||||
private slots:
|
||||
|
||||
void mainIsReadyRead();
|
||||
void clientIsReadyRead();
|
||||
|
||||
};
|
||||
|
||||
#endif // IOMULIPLEXER_H
|
@ -16,9 +16,3 @@ 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);
|
||||
}
|
||||
|
@ -17,5 +17,4 @@ 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);
|
||||
};
|
||||
|
@ -86,34 +86,6 @@ void Item::load(const QJsonObject &json, const bool preserve)
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
@ -68,8 +68,5 @@ public:
|
||||
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);
|
||||
|
||||
};
|
||||
|
||||
|
@ -49,6 +49,14 @@ void ItemStore::addItems(const std::vector<std::shared_ptr<Item>>& itemIn)
|
||||
|
||||
}
|
||||
|
||||
|
||||
void ItemStore::clear()
|
||||
{
|
||||
for(size_t i = 0; i < items_.size(); ++i) itemDeleted(*items_[i]);
|
||||
items_.clear();
|
||||
}
|
||||
|
||||
|
||||
void ItemStore::itemStateChanged(const ItemData& item)
|
||||
{
|
||||
for(unsigned i = 0; i < items_.size(); i++ )
|
||||
@ -100,33 +108,3 @@ void ItemStore::load(const QJsonObject& json, Microcontroller * const micro)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -25,8 +25,7 @@ public:
|
||||
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);
|
||||
void clear();
|
||||
|
||||
signals:
|
||||
|
||||
|
@ -2,7 +2,7 @@
|
||||
#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)
|
||||
PowerItem::PowerItem(SensorStore* sensors, uint32_t itemIdIn, QString name, uint8_t value, QObject* parent): Item(sensors, itemIdIn, name, value, parent)
|
||||
{
|
||||
stateChanged(Sensor(Sensor::TYPE_SHUTDOWN_IMMINENT, 0, 0));
|
||||
setValue(true);
|
||||
@ -21,7 +21,6 @@ void PowerItem::setValue(uint8_t value)
|
||||
void PowerItem::timeout()
|
||||
{
|
||||
QProcess::startDetached("syncoff");
|
||||
a_->exit(0);
|
||||
}
|
||||
|
||||
void PowerItem::store(QJsonObject& json)
|
||||
@ -29,9 +28,3 @@ 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);
|
||||
}
|
||||
|
@ -10,7 +10,6 @@ class PowerItem: public Item
|
||||
{
|
||||
Q_OBJECT
|
||||
private:
|
||||
QApplication* a_;
|
||||
|
||||
signals:
|
||||
|
||||
@ -25,8 +24,7 @@ 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);
|
||||
PowerItem(SensorStore* sensors, 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);
|
||||
};
|
||||
|
@ -46,22 +46,6 @@ void Relay::load(const QJsonObject& json)
|
||||
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_;
|
||||
|
@ -34,8 +34,5 @@ public:
|
||||
|
||||
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
|
||||
|
@ -16,9 +16,3 @@ 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);
|
||||
}
|
||||
|
@ -17,5 +17,4 @@ 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);
|
||||
};
|
||||
|
187
src/main.cpp
187
src/main.cpp
@ -1,14 +1,8 @@
|
||||
#include <QtWidgets/QApplication>
|
||||
#include <stdio.h>
|
||||
#include <QDir>
|
||||
#include <QDebug>
|
||||
#include <QString>
|
||||
#include <QSettings>
|
||||
#include <QSignalMapper>
|
||||
#include <QTcpSocket>
|
||||
#include <QFileInfo>
|
||||
#include <QJsonDocument>
|
||||
#include <QStandardPaths>
|
||||
|
||||
|
||||
#ifndef Q_OS_ANDROID
|
||||
#include <QtSerialPort/QtSerialPort>
|
||||
@ -28,96 +22,11 @@
|
||||
#include "items/auxitem.h"
|
||||
#include "items/rgbitem.h"
|
||||
#include "items/poweritem.h"
|
||||
#include "mainobject.h"
|
||||
|
||||
|
||||
#define BAUD QSerialPort::Baud38400
|
||||
|
||||
QJsonDocument* createJsonDocument(const bool pathOption, const QString& filePath)
|
||||
{
|
||||
QFile file;
|
||||
|
||||
#ifndef Q_OS_ANDROID
|
||||
if(pathOption && filePath.size() > 0) file.setFileName(filePath);
|
||||
else
|
||||
#endif
|
||||
{
|
||||
file.setFileName(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + "/shinterface.json");
|
||||
}
|
||||
|
||||
file.open(QIODevice::ReadOnly);
|
||||
if(!file.isOpen()) std::cerr<<"Can not open config file: "<<filePath.toLatin1().data()<<std::endl;
|
||||
else
|
||||
{
|
||||
QJsonDocument* document = new QJsonDocument(QJsonDocument::fromJson(file.readAll()));
|
||||
file.close();
|
||||
return document;
|
||||
}
|
||||
return new QJsonDocument;
|
||||
}
|
||||
|
||||
void saveJsonDocument(const QJsonDocument * const document, const bool pathOption, const QString& filePath)
|
||||
{
|
||||
QFile file;
|
||||
|
||||
#ifndef Q_OS_ANDROID
|
||||
if(pathOption && filePath.size() > 0) file.setFileName(filePath);
|
||||
else
|
||||
#endif
|
||||
{
|
||||
file.setFileName(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + "/shinterface.json");
|
||||
}
|
||||
std::cout<<"config file: "<<file.fileName().toLatin1().data()<<std::endl;
|
||||
file.open(QIODevice::WriteOnly);
|
||||
if(!file.isOpen()) std::cerr<<"Can not open config file: "<<filePath.toLatin1().data()<<std::endl;
|
||||
else
|
||||
{
|
||||
file.write(document->toJson());
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
|
||||
QSettings* createQSettings(const bool pathOption, const QString& filePath )
|
||||
{
|
||||
QSettings* settings;
|
||||
#ifndef Q_OS_ANDROID
|
||||
if(pathOption && filePath.size() > 0)
|
||||
{
|
||||
settings = new QSettings(filePath, QSettings::IniFormat);
|
||||
std::cout<<"Config file: "<<settings->fileName().toLatin1().data()<<std::endl;
|
||||
}
|
||||
else if(pathOption)
|
||||
{
|
||||
std::cerr<<"Can not open config file: "<<filePath.toLatin1().data()<<std::endl;
|
||||
settings = new QSettings;
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
settings = new QSettings;
|
||||
}
|
||||
return settings;
|
||||
}
|
||||
|
||||
void loadItemState(const QJsonDocument * const jsonDocument, QSettings * const settings, Microcontroller* micro, ItemStore* items, PowerItem* power, RgbItem* rgb, AuxItem* aux )
|
||||
{
|
||||
if(jsonDocument)
|
||||
{
|
||||
const QJsonObject jsonObject = jsonDocument->object();
|
||||
items->load(jsonObject, micro);
|
||||
power->load(jsonObject["Power"].toObject());
|
||||
if(jsonObject["Items"].toArray().size() >= 2)
|
||||
{
|
||||
rgb->load(jsonObject["Items"].toArray()[0].toObject());
|
||||
aux->load(jsonObject["Items"].toArray()[1].toObject());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
items->load("Items", settings, micro);
|
||||
power->load("Power", settings);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication a(argc, argv);
|
||||
@ -155,14 +64,7 @@ int main(int argc, char *argv[])
|
||||
parser.process(a);
|
||||
#endif
|
||||
|
||||
QSettings* settings = nullptr;
|
||||
QJsonDocument* jsonDocument = nullptr;
|
||||
if(parser.isSet(qsettingsOption)) settings = createQSettings(parser.isSet(settingsPathOption), parser.value(settingsPathOption));
|
||||
else jsonDocument = createJsonDocument(parser.isSet(settingsPathOption), parser.value(settingsPathOption));
|
||||
|
||||
|
||||
//connect to microcontoler
|
||||
Microcontroller micro;
|
||||
QIODevice* masterIODevice = nullptr;
|
||||
|
||||
#ifndef Q_OS_ANDROID
|
||||
if(parser.isSet(tcpOption))
|
||||
@ -181,7 +83,7 @@ int main(int argc, char *argv[])
|
||||
std::cout<<"Can not connect to to Server.\n";
|
||||
return 1;
|
||||
}
|
||||
micro.setIODevice(microSocket);
|
||||
masterIODevice = microSocket;
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -193,7 +95,7 @@ int main(int argc, char *argv[])
|
||||
else microPort->setBaudRate(BAUD);
|
||||
|
||||
if(!microPort->open(QIODevice::ReadWrite)) std::cout<<"Can not open serial port "<<microPort->portName().toStdString()<<". Continueing in demo mode"<<'\n';
|
||||
else micro.setIODevice(microPort);
|
||||
masterIODevice = microPort;
|
||||
}
|
||||
#else
|
||||
QTcpSocket* microSocket = new QTcpSocket;
|
||||
@ -203,87 +105,22 @@ int main(int argc, char *argv[])
|
||||
std::cout<<"Can not connect to to Server.\n";
|
||||
return 1;
|
||||
}
|
||||
micro.setIODevice(microSocket);
|
||||
masterIODevice = microSocket;
|
||||
#endif
|
||||
|
||||
|
||||
AlarmActions alarmActions(&a, µ);
|
||||
|
||||
//sensors
|
||||
SpeakerSensorSource livingroomSpeakerSensorSource("Livingroom Speakers");
|
||||
SunSensorSource sunSensorSource(49.884450, 8.650536);
|
||||
OcupancySensorSource ocupancySensor;
|
||||
|
||||
SensorStore sensors;
|
||||
QObject::connect(µ, &Microcontroller::gotSensorState, &sensors, &SensorStore::sensorGotState);
|
||||
QObject::connect(&livingroomSpeakerSensorSource, &SpeakerSensorSource::stateChanged, &sensors, &SensorStore::sensorGotState);
|
||||
QObject::connect(&sunSensorSource, &SunSensorSource::stateChanged, &sensors, &SensorStore::sensorGotState);
|
||||
QObject::connect(&sensors, &SensorStore::sensorChangedState, &ocupancySensor, &OcupancySensorSource::sensorEvent);
|
||||
QObject::connect(&ocupancySensor, &OcupancySensorSource::stateChanged, &sensors, &SensorStore::sensorGotState);
|
||||
|
||||
QMetaObject::invokeMethod(&livingroomSpeakerSensorSource, "run", Qt::QueuedConnection);
|
||||
sunSensorSource.run();
|
||||
|
||||
//item store
|
||||
ItemStore items(&sensors);
|
||||
QObject::connect(µ, &Microcontroller::gotRelayList, &items, &ItemStore::addItems);
|
||||
QObject::connect(µ, &Microcontroller::itemChanged, &items, &ItemStore::itemStateChanged);
|
||||
|
||||
//Power Item
|
||||
PowerItem powerItem(&sensors, &a, 5487423, "Power");
|
||||
QObject::connect(&powerItem, &PowerItem::stateChanged, &sensors, &SensorStore::sensorGotState);
|
||||
powerItem.emmitSensor();
|
||||
MainObject mainObject(masterIODevice, parser.isSet(settingsPathOption) ? parser.value(settingsPathOption) : "", !parser.isSet(secondaryOption));
|
||||
|
||||
//mainwindow
|
||||
MainWindow w(µ, &powerItem, &items, &sensors);
|
||||
QObject::connect(µ, SIGNAL(textRecived(QString)), &w, SLOT(changeHeaderLableText(QString)));
|
||||
if(!micro.connected()) w.changeHeaderLableText("No io debug only!");
|
||||
|
||||
//special items
|
||||
std::shared_ptr<RgbItem> rgbItem(new RgbItem(&sensors, µ, 5487422, "Rgb Lights"));
|
||||
items.addItem(rgbItem);
|
||||
|
||||
std::shared_ptr<AuxItem> auxItem(new AuxItem(&sensors, µ, 5487421, "Desk Light"));
|
||||
items.addItem(auxItem);
|
||||
|
||||
QMetaObject::invokeMethod(µ, "run", Qt::QueuedConnection );
|
||||
|
||||
loadItemState(jsonDocument, settings, µ, &items, &powerItem, rgbItem.get(), auxItem.get());
|
||||
|
||||
#ifndef Q_OS_ANDROID
|
||||
if(!parser.isSet(secondaryOption))
|
||||
{
|
||||
Item::secondaryFlag = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
Item::secondaryFlag = true;
|
||||
}
|
||||
#endif
|
||||
MainWindow w(&mainObject.micro, &mainObject.powerItem, &mainObject.items, &mainObject.sensors, !parser.isSet(secondaryOption));
|
||||
QObject::connect(&mainObject.micro, SIGNAL(textRecived(QString)), &w, SLOT(changeHeaderLableText(QString)));
|
||||
QObject::connect(&w, &MainWindow::sigBrodcast, &mainObject, &MainObject::sendJson);
|
||||
if(!mainObject.micro.connected()) w.changeHeaderLableText("No io debug only!");
|
||||
|
||||
w.show();
|
||||
|
||||
//micro.requestState();
|
||||
int retVal = a.exec();
|
||||
|
||||
if(settings)
|
||||
{
|
||||
items.store("Items", settings);
|
||||
powerItem.store("Power/", settings);
|
||||
settings->sync();
|
||||
}
|
||||
else
|
||||
{
|
||||
QJsonObject itemsJsonObject;
|
||||
items.store(itemsJsonObject);
|
||||
QJsonObject powerObject;
|
||||
powerItem.store(powerObject);
|
||||
itemsJsonObject.insert("Power", powerObject);
|
||||
jsonDocument->setObject(itemsJsonObject);
|
||||
saveJsonDocument(jsonDocument, parser.isSet(settingsPathOption), parser.value(settingsPathOption));
|
||||
}
|
||||
if(settings) delete settings;
|
||||
else delete jsonDocument;
|
||||
if(masterIODevice) delete masterIODevice;
|
||||
return retVal;
|
||||
}
|
||||
|
||||
|
144
src/mainobject.cpp
Normal file
144
src/mainobject.cpp
Normal file
@ -0,0 +1,144 @@
|
||||
#include "mainobject.h"
|
||||
|
||||
MainObject::MainObject(QIODevice* ioDevice, const QString& settingsPathIn, const bool masterIn, QObject *parent) :
|
||||
QObject(parent),
|
||||
master(masterIn),
|
||||
masterIODevice(ioDevice),
|
||||
ioMultiplexer(masterIODevice),
|
||||
micro(ioMultiplexer.getIoDevice()),
|
||||
broadCast(ioMultiplexer.getIoDevice()),
|
||||
settingsPath(settingsPathIn),
|
||||
sunSensorSource(49.884450, 8.650536),
|
||||
items(&sensors),
|
||||
powerItem(&sensors),
|
||||
rgbItem(new RgbItem(&sensors, µ, 5487422, "Rgb Lights")),
|
||||
auxItem(new AuxItem(&sensors, µ, 5487421, "Desk Light"))
|
||||
|
||||
{
|
||||
qDebug()<<"Is master:"<<master;
|
||||
//connect sensors subsystem
|
||||
QObject::connect(µ, &Microcontroller::gotSensorState, &sensors, &SensorStore::sensorGotState);
|
||||
QObject::connect(&livingroomSpeakerSensorSource, &SpeakerSensorSource::stateChanged, &sensors, &SensorStore::sensorGotState);
|
||||
QObject::connect(&sunSensorSource, &SunSensorSource::stateChanged, &sensors, &SensorStore::sensorGotState);
|
||||
QObject::connect(&sensors, &SensorStore::sensorChangedState, &ocupancySensor, &OcupancySensorSource::sensorEvent);
|
||||
QObject::connect(&ocupancySensor, &OcupancySensorSource::stateChanged, &sensors, &SensorStore::sensorGotState);
|
||||
|
||||
QMetaObject::invokeMethod(&livingroomSpeakerSensorSource, "run", Qt::QueuedConnection);
|
||||
sunSensorSource.run();
|
||||
|
||||
//connect item store
|
||||
QObject::connect(µ, &Microcontroller::gotRelayList, &items, &ItemStore::addItems);
|
||||
QObject::connect(µ, &Microcontroller::itemChanged, &items, &ItemStore::itemStateChanged);
|
||||
|
||||
//special items
|
||||
QObject::connect(&powerItem, &PowerItem::stateChanged, &sensors, &SensorStore::sensorGotState);
|
||||
powerItem.emmitSensor();
|
||||
items.addItem(rgbItem);
|
||||
items.addItem(auxItem);
|
||||
|
||||
connect(&broadCast, &BroadCast::gotJson, this, &MainObject::load);
|
||||
connect(&broadCast, &BroadCast::jsonRequested, this, &MainObject::sendJson);
|
||||
|
||||
if(master) load(getJsonObjectFromDisk(settingsPath));
|
||||
else
|
||||
{
|
||||
broadCast.requestJson();
|
||||
}
|
||||
|
||||
#ifndef Q_OS_ANDROID
|
||||
Item::secondaryFlag = !master;
|
||||
#endif
|
||||
}
|
||||
|
||||
MainObject::~MainObject()
|
||||
{
|
||||
if(master)
|
||||
{
|
||||
QJsonObject json;
|
||||
store(json);
|
||||
storeJsonObjectToDisk(json, settingsPath);
|
||||
}
|
||||
}
|
||||
|
||||
void MainObject::store(QJsonObject &json)
|
||||
{
|
||||
items.store(json);
|
||||
QJsonObject powerObject;
|
||||
powerItem.store(powerObject);
|
||||
json.insert("Power", powerObject);
|
||||
}
|
||||
|
||||
void MainObject::load(const QJsonObject json)
|
||||
{
|
||||
items.clear();
|
||||
items.addItem(rgbItem);
|
||||
items.addItem(auxItem);
|
||||
items.load(json, µ);
|
||||
powerItem.load(json["Power"].toObject());
|
||||
qDebug()<<"aray size: "<<json.isEmpty();
|
||||
if(json["Items"].toArray().size() >= 2)
|
||||
{
|
||||
|
||||
rgbItem->load(json["Items"].toArray()[0].toObject());
|
||||
auxItem->load(json["Items"].toArray()[1].toObject());
|
||||
}
|
||||
micro.requestState();
|
||||
}
|
||||
|
||||
void MainObject::sendJson()
|
||||
{
|
||||
QJsonObject json;
|
||||
store(json);
|
||||
broadCast.sendJson(json);
|
||||
}
|
||||
|
||||
QJsonObject MainObject::getJsonObjectFromDisk(const QString& filePath)
|
||||
{
|
||||
QFile file;
|
||||
|
||||
#ifndef Q_OS_ANDROID
|
||||
if(filePath.size() > 0) file.setFileName(filePath);
|
||||
else
|
||||
#endif
|
||||
{
|
||||
file.setFileName(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + "/shinterface.json");
|
||||
}
|
||||
|
||||
file.open(QIODevice::ReadOnly);
|
||||
if(!file.isOpen()) std::cerr<<"Can not open config file: "<<filePath.toLatin1().data()<<std::endl;
|
||||
else
|
||||
{
|
||||
QJsonDocument document(QJsonDocument::fromJson(file.readAll()));
|
||||
file.close();
|
||||
return document.object();
|
||||
}
|
||||
return QJsonObject();
|
||||
}
|
||||
|
||||
bool MainObject::storeJsonObjectToDisk(const QJsonObject& json, const QString& filePath)
|
||||
{
|
||||
QFile file;
|
||||
|
||||
#ifndef Q_OS_ANDROID
|
||||
if(filePath.size() > 0) file.setFileName(filePath);
|
||||
else
|
||||
#endif
|
||||
{
|
||||
file.setFileName(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + "/shinterface.json");
|
||||
}
|
||||
std::cout<<"config file: "<<file.fileName().toLatin1().data()<<std::endl;
|
||||
file.open(QIODevice::WriteOnly);
|
||||
if(!file.isOpen())
|
||||
{
|
||||
std::cerr<<"Can not open config file: "<<filePath.toLatin1().data()<<std::endl;
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
QJsonDocument document(json);
|
||||
file.write(document.toJson());
|
||||
file.close();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
86
src/mainobject.h
Normal file
86
src/mainobject.h
Normal file
@ -0,0 +1,86 @@
|
||||
#ifndef MAINOBJECT_H
|
||||
#define MAINOBJECT_H
|
||||
|
||||
#include <QObject>
|
||||
#include <stdio.h>
|
||||
#include <QDir>
|
||||
#include <QDebug>
|
||||
#include <QString>
|
||||
#include <QTcpSocket>
|
||||
#include <QFileInfo>
|
||||
#include <QJsonDocument>
|
||||
#include <QStandardPaths>
|
||||
#include <memory>
|
||||
|
||||
|
||||
#ifndef Q_OS_ANDROID
|
||||
#include <QtSerialPort/QtSerialPort>
|
||||
#include <QtSerialPort/QSerialPortInfo>
|
||||
#include <QCommandLineParser>
|
||||
#endif
|
||||
|
||||
#include "actors/alarmtime.h"
|
||||
#include "microcontroller.h"
|
||||
#include "ui/mainwindow.h"
|
||||
#include "sensors/speakersensor.h"
|
||||
#include "sensors/sunsensor.h"
|
||||
#include "sensors/ocupancysensor.h"
|
||||
#include "sensors/sensor.h"
|
||||
#include "items/itemstore.h"
|
||||
#include "items/auxitem.h"
|
||||
#include "items/rgbitem.h"
|
||||
#include "items/poweritem.h"
|
||||
#include "iomuliplexer.h"
|
||||
#include "broadcast.h"
|
||||
|
||||
class MainObject : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
|
||||
//io
|
||||
const bool master;
|
||||
|
||||
QIODevice * const masterIODevice = nullptr;
|
||||
IoMuliplexer ioMultiplexer;
|
||||
|
||||
Microcontroller micro;
|
||||
BroadCast broadCast;
|
||||
|
||||
const QString settingsPath;
|
||||
|
||||
//sensors
|
||||
SpeakerSensorSource livingroomSpeakerSensorSource;
|
||||
SunSensorSource sunSensorSource;
|
||||
OcupancySensorSource ocupancySensor;
|
||||
|
||||
SensorStore sensors;
|
||||
|
||||
//items
|
||||
ItemStore items;
|
||||
|
||||
PowerItem powerItem;
|
||||
std::shared_ptr<RgbItem> rgbItem;
|
||||
std::shared_ptr<AuxItem> auxItem;
|
||||
|
||||
private:
|
||||
|
||||
static QJsonObject getJsonObjectFromDisk(const QString& filePath = "");
|
||||
static bool storeJsonObjectToDisk(const QJsonObject& json, const QString& filePath = "");
|
||||
|
||||
public:
|
||||
explicit MainObject(QIODevice* ioDevice, const QString& settingsPathIn, const bool masterIn, QObject *parent = nullptr);
|
||||
~MainObject();
|
||||
|
||||
void store(QJsonObject& json);
|
||||
|
||||
signals:
|
||||
|
||||
public slots:
|
||||
|
||||
void load(const QJsonObject json);
|
||||
void sendJson();
|
||||
|
||||
};
|
||||
|
||||
#endif // MAINOBJECT_H
|
@ -95,38 +95,16 @@ bool Microcontroller::connected()
|
||||
else return false;
|
||||
}
|
||||
|
||||
void Microcontroller::run()
|
||||
{
|
||||
abort();
|
||||
|
||||
loop.reset(new QEventLoop);
|
||||
QTimer timer;
|
||||
connect(&timer, SIGNAL(timeout()), this, SLOT(doTick()));
|
||||
timer.setInterval(500);
|
||||
timer.start();
|
||||
|
||||
loop->exec();
|
||||
}
|
||||
|
||||
void Microcontroller::requestState()
|
||||
{
|
||||
write("state\n");
|
||||
}
|
||||
|
||||
void Microcontroller::abort()
|
||||
{
|
||||
if (!loop.isNull())
|
||||
{
|
||||
loop->quit();
|
||||
}
|
||||
}
|
||||
|
||||
//housekeeping
|
||||
|
||||
Microcontroller::Microcontroller(QIODevice* port): _port(port)
|
||||
Microcontroller::Microcontroller(QIODevice* port)
|
||||
{
|
||||
_port->readAll();
|
||||
_port->write("\n");
|
||||
setIODevice(port);
|
||||
}
|
||||
|
||||
Microcontroller::Microcontroller()
|
||||
@ -135,14 +113,12 @@ Microcontroller::Microcontroller()
|
||||
|
||||
Microcontroller::~Microcontroller()
|
||||
{
|
||||
if(_port != nullptr) delete _port;
|
||||
}
|
||||
|
||||
void Microcontroller::setIODevice(QIODevice *port)
|
||||
{
|
||||
_port = port;
|
||||
_port->readAll();
|
||||
_port->write("\n");
|
||||
QObject::connect(_port, &QIODevice::readyRead, this, &Microcontroller::isReadyRead);
|
||||
}
|
||||
|
||||
std::shared_ptr<Relay> Microcontroller::processRelayLine(const QString& buffer)
|
||||
@ -193,7 +169,6 @@ void Microcontroller::processSensorState(const QString& buffer)
|
||||
|
||||
void Microcontroller::processMicroReturn()
|
||||
{
|
||||
qDebug()<<_buffer;
|
||||
if(listMode) processList(_buffer);
|
||||
else
|
||||
{
|
||||
@ -208,10 +183,8 @@ void Microcontroller::processMicroReturn()
|
||||
|
||||
}
|
||||
|
||||
void Microcontroller::doTick()
|
||||
void Microcontroller::isReadyRead()
|
||||
{
|
||||
if(_port != nullptr)
|
||||
{
|
||||
char charBuf;
|
||||
while(_port->getChar(&charBuf))
|
||||
{
|
||||
@ -224,5 +197,4 @@ void Microcontroller::doTick()
|
||||
_buffer.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -51,7 +51,6 @@ private:
|
||||
void write(char *buffer, const size_t length);
|
||||
void write(const QByteArray& buffer);
|
||||
|
||||
|
||||
public:
|
||||
Microcontroller(QIODevice* port);
|
||||
Microcontroller();
|
||||
@ -74,9 +73,9 @@ public slots:
|
||||
void relayOff(int relay);
|
||||
void relayToggle(int state, int id);
|
||||
|
||||
void run();
|
||||
void abort();
|
||||
void doTick();
|
||||
private slots:
|
||||
|
||||
void isReadyRead();
|
||||
|
||||
signals:
|
||||
void textRecived(const QString string);
|
||||
|
@ -144,5 +144,12 @@ void ItemSettingsDialog::editActor()
|
||||
dialog->setParent(this);
|
||||
dialog->show();
|
||||
dialog->exec();
|
||||
|
||||
for(int i = 0; i < ui->tableWidget->rowCount() && i < item_->getActors().size(); ++i)
|
||||
{
|
||||
ui->tableWidget->item(i, 0)->setText(item_->getActors()[i]->getName());
|
||||
ui->tableWidget->item(i, 1)->setText(item_->getActors()[i]->actionName());
|
||||
ui->tableWidget->item(i, 3)->setText(item_->getActors()[i]->isActive() ? "Y" : "N");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -16,7 +16,6 @@ ItemWidget::ItemWidget(std::weak_ptr<Item> item, bool analog, QWidget *parent) :
|
||||
{
|
||||
ui->horizontalSpacer->changeSize(0,0);
|
||||
ui->checkBox->hide();
|
||||
//ui->label->hide();
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -4,7 +4,7 @@
|
||||
|
||||
#include "itemsettingsdialog.h"
|
||||
|
||||
MainWindow::MainWindow(Microcontroller *micro, PowerItem* powerItem, ItemStore* itemStore, SensorStore *sensorStore, QWidget *parent) :
|
||||
MainWindow::MainWindow(Microcontroller *micro, PowerItem* powerItem, ItemStore* itemStore, SensorStore *sensorStore, bool master, QWidget *parent) :
|
||||
QMainWindow(parent),
|
||||
ui(new Ui::MainWindow),
|
||||
colorChooser(this),
|
||||
@ -13,6 +13,9 @@ MainWindow::MainWindow(Microcontroller *micro, PowerItem* powerItem, ItemStore*
|
||||
{
|
||||
ui->setupUi(this);
|
||||
|
||||
if(!master) connect(ui->pushButton_broadcast, &QPushButton::clicked, this, &MainWindow::sigBrodcast);
|
||||
else ui->pushButton_broadcast->hide();
|
||||
|
||||
connect(ui->pushButton_power, SIGNAL(clicked()), this, SLOT(showPowerItemDialog()));
|
||||
|
||||
//Relays
|
||||
@ -20,6 +23,11 @@ MainWindow::MainWindow(Microcontroller *micro, PowerItem* powerItem, ItemStore*
|
||||
connect(itemStore, &ItemStore::itemAdded, ui->relayList, &ItemScrollBox::addItem);
|
||||
connect(itemStore, &ItemStore::itemDeleted, ui->relayList, &ItemScrollBox::removeItem);
|
||||
|
||||
for(size_t i = 0; i < itemStore->getItems()->size(); ++i)
|
||||
{
|
||||
ui->relayList->addItem(itemStore->getItems()->at(i));
|
||||
}
|
||||
|
||||
//Sensors
|
||||
|
||||
ui->sensorListView->sensorsChanged(*(sensorStore->getSensors()));
|
||||
|
@ -11,6 +11,7 @@
|
||||
#include "../sensors/sensor.h"
|
||||
#include "../items/itemstore.h"
|
||||
#include "../items/poweritem.h"
|
||||
#include "../broadcast.h"
|
||||
|
||||
|
||||
namespace Ui
|
||||
@ -23,7 +24,7 @@ class MainWindow : public QMainWindow
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit MainWindow(Microcontroller *micro, PowerItem* powerItem, ItemStore* itemStore, SensorStore *sensorStore, QWidget *parent = nullptr);
|
||||
explicit MainWindow(Microcontroller *micro, PowerItem* powerItem, ItemStore* itemStore, SensorStore *sensorStore, bool master, QWidget *parent = nullptr);
|
||||
~MainWindow();
|
||||
|
||||
private:
|
||||
@ -35,6 +36,10 @@ private:
|
||||
|
||||
PowerItem *_powerItem;
|
||||
|
||||
signals:
|
||||
|
||||
void sigBrodcast();
|
||||
|
||||
private slots:
|
||||
|
||||
//RGB
|
||||
|
@ -95,6 +95,11 @@
|
||||
</layout>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout_2">
|
||||
<property name="topMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<item>
|
||||
<widget class="QPushButton" name="button_color">
|
||||
<property name="sizePolicy">
|
||||
@ -126,6 +131,15 @@
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="pushButton_power">
|
||||
<property name="text">
|
||||
<string>Config Shutdown</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item>
|
||||
@ -167,16 +181,16 @@
|
||||
<number>0</number>
|
||||
</property>
|
||||
<item>
|
||||
<widget class="QPushButton" name="pushButton_power">
|
||||
<widget class="QPushButton" name="pushButton_refesh">
|
||||
<property name="text">
|
||||
<string>Config Shutdown</string>
|
||||
<string>Refesh</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="pushButton_refesh">
|
||||
<widget class="QPushButton" name="pushButton_broadcast">
|
||||
<property name="text">
|
||||
<string>Refesh</string>
|
||||
<string>Broadcast</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
|
Reference in New Issue
Block a user