diff --git a/SHinterface.pro b/SHinterface.pro index 56869ae..f3a806e 100644 --- a/SHinterface.pro +++ b/SHinterface.pro @@ -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 \ diff --git a/src/actors/actor.cpp b/src/actors/actor.cpp index 942fa20..9466ccc 100644 --- a/src/actors/actor.cpp +++ b/src/actors/actor.cpp @@ -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; -} diff --git a/src/actors/actor.h b/src/actors/actor.h index 5abe2d5..4a36b78 100644 --- a/src/actors/actor.h +++ b/src/actors/actor.h @@ -3,7 +3,6 @@ #include #include -#include #include 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 diff --git a/src/actors/alarmtime.cpp b/src/actors/alarmtime.cpp index 89f480e..11b1e1e 100644 --- a/src/actors/alarmtime.cpp +++ b/src/actors/alarmtime.cpp @@ -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(); -} diff --git a/src/actors/alarmtime.h b/src/actors/alarmtime.h index eebbc1b..3c119f0 100644 --- a/src/actors/alarmtime.h +++ b/src/actors/alarmtime.h @@ -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: diff --git a/src/actors/regulator.cpp b/src/actors/regulator.cpp index c3d3e6f..3f72518 100644 --- a/src/actors/regulator.cpp +++ b/src/actors/regulator.cpp @@ -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(sensor_.type)); - settings->setValue(subsecton + "SensorId", static_cast(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; diff --git a/src/actors/regulator.h b/src/actors/regulator.h index 95e4332..2626b2d 100644 --- a/src/actors/regulator.h +++ b/src/actors/regulator.h @@ -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); }; diff --git a/src/actors/sensoractor.cpp b/src/actors/sensoractor.cpp index ccc0714..3832c24 100644 --- a/src/actors/sensoractor.cpp +++ b/src/actors/sensoractor.cpp @@ -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(sensor_.type)); - settings->setValue(subsecton + "SensorId", static_cast(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; diff --git a/src/actors/sensoractor.h b/src/actors/sensoractor.h index 74acdae..c638074 100644 --- a/src/actors/sensoractor.h +++ b/src/actors/sensoractor.h @@ -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); }; diff --git a/src/actors/timeractor.cpp b/src/actors/timeractor.cpp index d90fbc3..99ff340 100644 --- a/src/actors/timeractor.cpp +++ b/src/actors/timeractor.cpp @@ -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() diff --git a/src/actors/timeractor.h b/src/actors/timeractor.h index 8eff8b1..db4ef66 100644 --- a/src/actors/timeractor.h +++ b/src/actors/timeractor.h @@ -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); }; diff --git a/src/broadcast.cpp b/src/broadcast.cpp index 17456c4..02ba768 100644 --- a/src/broadcast.cpp +++ b/src/broadcast.cpp @@ -1,15 +1,17 @@ #include "broadcast.h" #include +#include +#include #include -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()<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(); } } diff --git a/src/broadcast.h b/src/broadcast.h index c05b62a..f7964b7 100644 --- a/src/broadcast.h +++ b/src/broadcast.h @@ -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); }; diff --git a/src/iomuliplexer.cpp b/src/iomuliplexer.cpp new file mode 100644 index 0000000..7099575 --- /dev/null +++ b/src/iomuliplexer.cpp @@ -0,0 +1,76 @@ +#include "iomuliplexer.h" +#include + +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(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); +} diff --git a/src/iomuliplexer.h b/src/iomuliplexer.h new file mode 100644 index 0000000..8afb26d --- /dev/null +++ b/src/iomuliplexer.h @@ -0,0 +1,54 @@ +#ifndef IOMULIPLEXER_H +#define IOMULIPLEXER_H + +#include +#include +#include +#include + +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 diff --git a/src/items/auxitem.cpp b/src/items/auxitem.cpp index 8104804..ea7acee 100644 --- a/src/items/auxitem.cpp +++ b/src/items/auxitem.cpp @@ -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); -} diff --git a/src/items/auxitem.h b/src/items/auxitem.h index 0578ac1..0821dc8 100644 --- a/src/items/auxitem.h +++ b/src/items/auxitem.h @@ -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); }; diff --git a/src/items/item.cpp b/src/items/item.cpp index 95cf1a0..609335f 100644 --- a/src/items/item.cpp +++ b/src/items/item.cpp @@ -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(itemId_)); - settings->setValue(subsecton + "ActorsActive", actorsActive_); - settings->setValue(subsecton + "Actors", static_cast(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; diff --git a/src/items/item.h b/src/items/item.h index f6f957b..0f02d26 100644 --- a/src/items/item.h +++ b/src/items/item.h @@ -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); - }; diff --git a/src/items/itemstore.cpp b/src/items/itemstore.cpp index 7e067ee..9aac242 100644 --- a/src/items/itemstore.cpp +++ b/src/items/itemstore.cpp @@ -49,6 +49,14 @@ void ItemStore::addItems(const std::vector>& 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(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 newItem; - if(settings->value(subsecton + "/Item" + QString::number(i)+ "Type").toString() == "Relay") - { - newItem = std::shared_ptr(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); - } - } -} diff --git a/src/items/itemstore.h b/src/items/itemstore.h index fbebd29..405abea 100644 --- a/src/items/itemstore.h +++ b/src/items/itemstore.h @@ -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: diff --git a/src/items/poweritem.cpp b/src/items/poweritem.cpp index 1142827..df09107 100644 --- a/src/items/poweritem.cpp +++ b/src/items/poweritem.cpp @@ -2,7 +2,7 @@ #include #include -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); -} diff --git a/src/items/poweritem.h b/src/items/poweritem.h index eaaac9e..6cf16f0 100644 --- a/src/items/poweritem.h +++ b/src/items/poweritem.h @@ -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); }; diff --git a/src/items/relay.cpp b/src/items/relay.cpp index 6fcd5b2..eb10d55 100644 --- a/src/items/relay.cpp +++ b/src/items/relay.cpp @@ -46,22 +46,6 @@ void Relay::load(const QJsonObject& json) itemId_ = address_ | (static_cast(id_) << 16); } -void Relay::store(QString subsecton, QSettings* settings) -{ - settings->setValue(subsecton + "Type", "Relay"); - Item::store(subsecton, settings); - settings->setValue(subsecton + "Id", static_cast(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_; diff --git a/src/items/relay.h b/src/items/relay.h index 15e7d3c..4cd9f64 100644 --- a/src/items/relay.h +++ b/src/items/relay.h @@ -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 diff --git a/src/items/rgbitem.cpp b/src/items/rgbitem.cpp index 16520a7..0c9a7de 100644 --- a/src/items/rgbitem.cpp +++ b/src/items/rgbitem.cpp @@ -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); -} diff --git a/src/items/rgbitem.h b/src/items/rgbitem.h index 3d4aedf..e8826ea 100644 --- a/src/items/rgbitem.h +++ b/src/items/rgbitem.h @@ -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); }; diff --git a/src/main.cpp b/src/main.cpp index e529f30..3be9032 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,14 +1,8 @@ #include #include -#include #include -#include -#include -#include #include -#include -#include -#include + #ifndef Q_OS_ANDROID #include @@ -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: "< 0) file.setFileName(filePath); - else - #endif - { - file.setFileName(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + "/shinterface.json"); - } - std::cout<<"config file: "<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: "<fileName().toLatin1().data()<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 "<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(new RgbItem(&sensors, µ, 5487422, "Rgb Lights")); - items.addItem(rgbItem); - - std::shared_ptr 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; } diff --git a/src/mainobject.cpp b/src/mainobject.cpp new file mode 100644 index 0000000..3f4dca0 --- /dev/null +++ b/src/mainobject.cpp @@ -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:"<= 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: "< 0) file.setFileName(filePath); + else + #endif + { + file.setFileName(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + "/shinterface.json"); + } + std::cout<<"config file: "< +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#ifndef Q_OS_ANDROID +#include +#include +#include +#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; + std::shared_ptr 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 diff --git a/src/microcontroller.cpp b/src/microcontroller.cpp index 2374afa..344fe3c 100644 --- a/src/microcontroller.cpp +++ b/src/microcontroller.cpp @@ -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 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,21 +183,18 @@ void Microcontroller::processMicroReturn() } -void Microcontroller::doTick() +void Microcontroller::isReadyRead() { - if(_port != nullptr) + char charBuf; + while(_port->getChar(&charBuf)) { - char charBuf; - while(_port->getChar(&charBuf)) + _buffer.push_back(charBuf); + if( _buffer.endsWith('\n') ) { - _buffer.push_back(charBuf); - if( _buffer.endsWith('\n') ) - { - _buffer.remove('\n'); - processMicroReturn(); - textRecived(_buffer); - _buffer.clear(); - } + _buffer.remove('\n'); + processMicroReturn(); + textRecived(_buffer); + _buffer.clear(); } } } diff --git a/src/microcontroller.h b/src/microcontroller.h index 61840a0..f6de49e 100644 --- a/src/microcontroller.h +++ b/src/microcontroller.h @@ -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); diff --git a/src/ui/itemsettingsdialog.cpp b/src/ui/itemsettingsdialog.cpp index 54b08b5..11ca109 100644 --- a/src/ui/itemsettingsdialog.cpp +++ b/src/ui/itemsettingsdialog.cpp @@ -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"); + } } } diff --git a/src/ui/itemwidget.cpp b/src/ui/itemwidget.cpp index 0a42612..63e6002 100644 --- a/src/ui/itemwidget.cpp +++ b/src/ui/itemwidget.cpp @@ -16,7 +16,6 @@ ItemWidget::ItemWidget(std::weak_ptr item, bool analog, QWidget *parent) : { ui->horizontalSpacer->changeSize(0,0); ui->checkBox->hide(); - //ui->label->hide(); } else { diff --git a/src/ui/mainwindow.cpp b/src/ui/mainwindow.cpp index 8731eb5..230da79 100644 --- a/src/ui/mainwindow.cpp +++ b/src/ui/mainwindow.cpp @@ -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())); diff --git a/src/ui/mainwindow.h b/src/ui/mainwindow.h index c3dc078..c20fc87 100644 --- a/src/ui/mainwindow.h +++ b/src/ui/mainwindow.h @@ -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 diff --git a/src/ui/mainwindow.ui b/src/ui/mainwindow.ui index 2da8b89..0a3f5c2 100644 --- a/src/ui/mainwindow.ui +++ b/src/ui/mainwindow.ui @@ -96,35 +96,49 @@ - - - - 0 - 0 - + + + 0 - - - 0 - 0 - - - - - 16777215 - 48 - - - - - 0 - 128 - - - - Color - - + + + + + 0 + 0 + + + + + 0 + 0 + + + + + 16777215 + 48 + + + + + 0 + 128 + + + + Color + + + + + + + Config Shutdown + + + + @@ -167,16 +181,16 @@ 0 - + - Config Shutdown + Refesh - + - Refesh + Broadcast