ocupancy sensor now uses libnl to collect connected devices
fixed regulator saving values as int instead of double
This commit is contained in:
@ -11,6 +11,10 @@ QT += core gui widgets network
|
||||
TARGET = SHinterface
|
||||
TEMPLATE = app
|
||||
|
||||
INCLUDEPATH += /usr/include/libnl3/
|
||||
|
||||
LIBS += -lnl-3 -lnl-genl-3
|
||||
|
||||
# The following define makes your compiler emit warnings if you use
|
||||
# any feature of Qt which as been marked as deprecated (the exact warnings
|
||||
# depend on your compiler). Please consult the documentation of the
|
||||
@ -29,6 +33,7 @@ SOURCES += \
|
||||
src/ui/itemcreationdialog.cpp \
|
||||
src/ui/itemsettingswidgets/messageitemsettingswidget.cpp \
|
||||
src/mainobject.cpp \
|
||||
src/apgetconnected.cpp \
|
||||
src/ui/actorwidgets/polynomalactorwidget.cpp \
|
||||
src/ui/actorwidgets/sensoractorwidget.cpp \
|
||||
src/ui/actorwidgets/alarmwidget.cpp \
|
||||
@ -81,6 +86,7 @@ HEADERS += \
|
||||
src/ui/itemcreationdialog.h \
|
||||
src/ui/itemsettingswidgets/messageitemsettingswidget.h \
|
||||
src/mainobject.h \
|
||||
src/apgetconnected.h \
|
||||
src/ui/actorwidgets/alarmwidget.h \
|
||||
src/ui/actorwidgets/polynomalactorwidget.h \
|
||||
src/ui/actorwidgets/sensoractorwidget.h \
|
||||
|
@ -65,8 +65,8 @@ void Regulator::store(QJsonObject& json)
|
||||
void Regulator::load(const QJsonObject& json, bool preserve)
|
||||
{
|
||||
Actor::load(json, preserve);
|
||||
band_ = json["Band"].toInt(1);
|
||||
setPoint_ = json["SetPoint"].toInt(22);
|
||||
band_ = json["Band"].toDouble(1);
|
||||
setPoint_ = json["SetPoint"].toDouble(22);
|
||||
sensor_.type = json["SensorType"].toInt(0);
|
||||
sensor_.id = json["SensorId"].toInt(0);
|
||||
sensor_.field = json["SensorField"].toInt(0);
|
||||
|
@ -94,6 +94,11 @@ void BroadCast::decode()
|
||||
}
|
||||
}
|
||||
|
||||
void BroadCast::sendMessage(const QString &title, const QString &body)
|
||||
{
|
||||
write(QByteArray("MESG ") + title.toLatin1() + " BODY " + body.toLatin1());
|
||||
}
|
||||
|
||||
void BroadCast::readyRead()
|
||||
{
|
||||
buffer_.append(iodevice_->readAll());
|
||||
|
@ -45,6 +45,7 @@ public:
|
||||
|
||||
BroadCast(QIODevice* const iodevice = nullptr, bool master = true);
|
||||
void sendJson(const QJsonObject& json);
|
||||
void sendMessage(const QString& title, const QString& body);
|
||||
|
||||
};
|
||||
|
||||
|
@ -15,7 +15,6 @@ void ItemStore::addItem(std::shared_ptr<Item> item)
|
||||
{
|
||||
items_.push_back(std::shared_ptr<Item>(item));
|
||||
itemAdded(std::weak_ptr<Item>(items_.back()));
|
||||
qDebug()<<"item added";
|
||||
}
|
||||
}
|
||||
|
||||
@ -75,7 +74,6 @@ void ItemStore::itemStateChanged(const ItemData& item)
|
||||
{
|
||||
if(items_[i]->operator==(item))
|
||||
{
|
||||
qDebug()<<"is item "<<i<<" with ids: "<<item.id()<<" "<<items_[i]->id()<<"\nHas state: "<<items_[i]->getValue()<<" wants state: "<<item.getValue();
|
||||
if(items_[i]->getValue() != item.getValue())items_[i]->informValue(item.getValue());
|
||||
}
|
||||
|
||||
@ -95,7 +93,7 @@ void ItemStore::store(QJsonObject& json)
|
||||
json["Items"] = itemsArray;
|
||||
}
|
||||
|
||||
void ItemStore::load(const QJsonObject& json, Microcontroller * const micro)
|
||||
void ItemStore::load(const QJsonObject& json)
|
||||
{
|
||||
const QJsonArray itemsArray(json["Items"].toArray(QJsonArray()));
|
||||
for(int i = 0; i < itemsArray.size(); ++i)
|
||||
@ -106,7 +104,7 @@ void ItemStore::load(const QJsonObject& json, Microcontroller * const micro)
|
||||
std::shared_ptr<Item> newItem;
|
||||
if(itemObject["Type"].toString("") == "Relay")
|
||||
{
|
||||
newItem = std::shared_ptr<Relay>(new Relay(micro));
|
||||
newItem = std::shared_ptr<Relay>(new Relay());
|
||||
}
|
||||
else if(itemObject["Type"].toString("") == "Message")
|
||||
{
|
||||
|
@ -3,7 +3,6 @@
|
||||
#include <memory>
|
||||
#include "item.h"
|
||||
#include "../sensors/sensor.h"
|
||||
#include "../microcontroller.h"
|
||||
|
||||
#include <QJsonObject>
|
||||
|
||||
@ -21,7 +20,7 @@ public:
|
||||
inline std::vector< std::shared_ptr<Item> >* getItems(){ return &items_; }
|
||||
|
||||
void store(QJsonObject &json);
|
||||
void load(const QJsonObject &json, Microcontroller * const micro);
|
||||
void load(const QJsonObject &json);
|
||||
|
||||
void clear();
|
||||
|
||||
|
@ -2,6 +2,8 @@
|
||||
|
||||
#include <QTimer>
|
||||
|
||||
BroadCast* MessageItem::broadCast_ = nullptr;
|
||||
|
||||
MessageItem::MessageItem(uint32_t itemIdIn, QString name, uint8_t value, QObject *parent):
|
||||
Item(itemIdIn, name, value, parent)
|
||||
{
|
||||
@ -28,6 +30,8 @@ void MessageItem::setValue(uint8_t value)
|
||||
messageBox_->setModal(false);
|
||||
connect(messageBox_, &QMessageBox::finished, this, &MessageItem::closeMessageBox);
|
||||
messageBox_->show();
|
||||
if(broadCast_) broadCast_->sendMessage(name_, message_);
|
||||
|
||||
//QTimer::singleShot(600000, this, &MessageItem::closeMessageBox);
|
||||
}
|
||||
else if(!value && messageBox_)
|
||||
|
@ -4,6 +4,7 @@
|
||||
#include <QMessageBox>
|
||||
|
||||
#include "item.h"
|
||||
#include "../broadcast.h"
|
||||
|
||||
class MessageItem : public Item
|
||||
{
|
||||
@ -13,6 +14,8 @@ private:
|
||||
QString message_;
|
||||
QMessageBox* messageBox_ = nullptr;
|
||||
|
||||
static BroadCast* broadCast_;
|
||||
|
||||
private slots:
|
||||
|
||||
void closeMessageBox();
|
||||
|
@ -3,7 +3,9 @@
|
||||
|
||||
//Relay
|
||||
|
||||
Relay::Relay(Microcontroller* micro, uint8_t id, QString name, uint16_t address, bool state, QObject* parent): Item(0, name, state, parent), micro_(micro), id_(id), address_(address)
|
||||
Microcontroller* Relay::micro_ = nullptr;
|
||||
|
||||
Relay::Relay(uint8_t id, QString name, uint16_t address, bool state, QObject* parent): Item(0, name, state, parent), id_(id), address_(address)
|
||||
{
|
||||
itemId_ = address | ((uint32_t)id << 16);
|
||||
}
|
||||
@ -11,9 +13,12 @@ Relay::Relay(Microcontroller* micro, uint8_t id, QString name, uint16_t address,
|
||||
void Relay::setValue(uint8_t value)
|
||||
{
|
||||
Item::setValue(value);
|
||||
if(micro_)
|
||||
{
|
||||
if(value)micro_->relayOn(id_);
|
||||
else micro_->relayOff(id_);
|
||||
}
|
||||
}
|
||||
|
||||
void Relay::on()
|
||||
{
|
||||
|
@ -13,7 +13,7 @@ class Relay : public Item
|
||||
{
|
||||
Q_OBJECT
|
||||
private:
|
||||
Microcontroller* micro_;
|
||||
static Microcontroller* micro_;
|
||||
|
||||
uint8_t id_;
|
||||
uint16_t address_;
|
||||
@ -26,12 +26,14 @@ public slots:
|
||||
void toggle();
|
||||
|
||||
public:
|
||||
Relay(Microcontroller* micro, uint8_t id = 0, QString name = "", uint16_t address = 0, bool state = false, QObject* parent = nullptr);
|
||||
Relay(uint8_t id = 0, QString name = "", uint16_t address = 0, bool state = false, QObject* parent = nullptr);
|
||||
|
||||
uint16_t getAddress() const;
|
||||
uint8_t getId() const;
|
||||
void setId(uint8_t id);
|
||||
|
||||
inline static void setMicrocontroller(Microcontroller* micro){ micro_ = micro; }
|
||||
|
||||
virtual void store(QJsonObject& json);
|
||||
virtual void load(const QJsonObject& json, const bool preserve = false);
|
||||
};
|
||||
|
@ -59,8 +59,6 @@ int main(int argc, char *argv[])
|
||||
parser.addOption(settingsPathOption);
|
||||
QCommandLineOption secondaryOption(QStringList() << "e" << "secondary", QCoreApplication::translate("main", "Set if instance is not main instance"));
|
||||
parser.addOption(secondaryOption);
|
||||
QCommandLineOption qsettingsOption(QStringList() << "q" << "qsettigns", QCoreApplication::translate("main", "Set if jsettings file format should be used."));
|
||||
parser.addOption(secondaryOption);
|
||||
parser.process(a);
|
||||
#endif
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
#include "mainobject.h"
|
||||
#include "mainobject.h"
|
||||
|
||||
MainObject::MainObject(QIODevice* ioDevice, const QString& settingsPathIn, const bool masterIn, QObject *parent) :
|
||||
QObject(parent),
|
||||
@ -20,6 +20,8 @@ MainObject::MainObject(QIODevice* ioDevice, const QString& settingsPathIn, const
|
||||
QObject::connect(&globalSensors, &SensorStore::sensorChangedState, &ocupancySensor, &OcupancySensorSource::sensorEvent);
|
||||
QObject::connect(&ocupancySensor, &OcupancySensorSource::stateChanged, &globalSensors, &SensorStore::sensorGotState);
|
||||
|
||||
sunSensorSource.run();
|
||||
|
||||
//connect item store
|
||||
QObject::connect(µ, &Microcontroller::gotRelayList, &items, &ItemStore::addItems);
|
||||
QObject::connect(µ, &Microcontroller::itemChanged, &items, &ItemStore::itemStateChanged);
|
||||
@ -30,6 +32,8 @@ MainObject::MainObject(QIODevice* ioDevice, const QString& settingsPathIn, const
|
||||
items.addItem(rgbItem);
|
||||
items.addItem(auxItem);
|
||||
|
||||
Relay::setMicrocontroller(µ);
|
||||
|
||||
connect(&broadCast, &BroadCast::gotJson, this, &MainObject::recivedJson);
|
||||
QObject::connect(&broadCast, &BroadCast::gotSensorState, &globalSensors, &SensorStore::sensorGotState);
|
||||
if(master)connect(&broadCast, &BroadCast::jsonRequested, this, &MainObject::sendJson);
|
||||
@ -60,6 +64,9 @@ void MainObject::store(QJsonObject &json)
|
||||
items.store(json);
|
||||
QJsonObject powerObject;
|
||||
powerItem.store(powerObject);
|
||||
QJsonObject ocupancyObject;
|
||||
ocupancySensor.store(ocupancyObject);
|
||||
json.insert("Ocupancy", ocupancyObject);
|
||||
json.insert("Power", powerObject);
|
||||
}
|
||||
|
||||
@ -71,8 +78,9 @@ void MainObject::load(const QJsonObject& json)
|
||||
powerItem.removeAllActors();
|
||||
items.addItem(rgbItem);
|
||||
items.addItem(auxItem);
|
||||
items.load(json, µ);
|
||||
items.load(json);
|
||||
powerItem.load(json["Power"].toObject());
|
||||
ocupancySensor.load(json["Ocupancy"].toObject());
|
||||
qDebug()<<"aray size: "<<json.isEmpty();
|
||||
if(json["Items"].toArray().size() >= 2)
|
||||
{
|
||||
@ -84,7 +92,7 @@ void MainObject::load(const QJsonObject& json)
|
||||
|
||||
void MainObject::recivedJson(const QJsonObject json)
|
||||
{
|
||||
if(master)storeJsonObjectToDisk(json, settingsPath);
|
||||
if(master && !noSave)storeJsonObjectToDisk(json, settingsPath);
|
||||
load(json);
|
||||
}
|
||||
|
||||
|
@ -131,7 +131,7 @@ std::shared_ptr<Relay> Microcontroller::processRelayLine(const QString& buffer)
|
||||
if(name.size() > 1)name.remove(name.size()-1, 1);
|
||||
else name = "Relay " + QString::number(bufferList[1].toInt(nullptr, 2));
|
||||
qDebug()<<"Relay "<<bufferList[2].toInt()<<"Name "<<name<<" id "<<bufferList[4].toInt(nullptr, 2)<<" state "<<bufferList[6].toInt();
|
||||
return std::shared_ptr<Relay>( new Relay(this, bufferList[2].toInt(), name, bufferList[4].toInt(nullptr, 2), bufferList[6].toInt()));
|
||||
return std::shared_ptr<Relay>( new Relay(bufferList[2].toInt(), name, bufferList[4].toInt(nullptr, 2), bufferList[6].toInt()));
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
@ -2,41 +2,57 @@
|
||||
#include <QTimer>
|
||||
#include <QDebug>
|
||||
|
||||
#include "../apgetconnected.h"
|
||||
|
||||
OcupancySensorSource::OcupancySensorSource(QObject *parent): QObject (parent)
|
||||
|
||||
OcupancySensorSource::OcupancySensorSource(QObject *parent, const QString& device, const QString& deviceMac): QObject (parent), deviceMac_(deviceMac), device_(device)
|
||||
{
|
||||
connect(&ping, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(pingExit(int, QProcess::ExitStatus)));
|
||||
Timeout();
|
||||
|
||||
}
|
||||
|
||||
void OcupancySensorSource::sensorEvent(Sensor sensor)
|
||||
{
|
||||
if(sensor.type == Sensor::TYPE_DOOR && sensor.id == 1 && sensor.field != 0.0f)
|
||||
{
|
||||
QTimer::singleShot(240000, this, &OcupancySensorSource::Timeout);
|
||||
qDebug()<<"starting timer";
|
||||
if(occupied == false) stateChanged(Sensor(Sensor::TYPE_OCUPANCY, 0, 1, "Occupancy"));
|
||||
QTimer::singleShot(600000, this, &OcupancySensorSource::Timeout);
|
||||
}
|
||||
}
|
||||
|
||||
void OcupancySensorSource::Timeout()
|
||||
{
|
||||
qDebug()<<"starting ping";
|
||||
ping.start("ping 192.168.0.104 -c 1 -W 1");
|
||||
int error = 0;
|
||||
std::vector<uint64_t> devices = ap::connectedDevices(device_.toLatin1().toStdString(), error);
|
||||
if(error == 0)
|
||||
{
|
||||
bool found = false;
|
||||
for(size_t i = 0; i < devices.size(); ++i)
|
||||
{
|
||||
std::string mac = ap::macAddrToString(devices[i]);
|
||||
if(mac.find(deviceMac_.toLatin1().toStdString()) != std::string::npos)
|
||||
{
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
|
||||
void OcupancySensorSource::pingExit(int exitCode, QProcess::ExitStatus exitStatus)
|
||||
{
|
||||
qDebug()<<"ping finished";
|
||||
if(exitStatus == QProcess::ExitStatus::NormalExit)
|
||||
{
|
||||
qDebug()<<"Exit Code "<<exitCode;
|
||||
if(exitCode == 0)
|
||||
{
|
||||
stateChanged(Sensor(Sensor::TYPE_OCUPANCY, 0, 1, "Occupancy"));
|
||||
}
|
||||
occupied = found;
|
||||
}
|
||||
else
|
||||
{
|
||||
stateChanged(Sensor(Sensor::TYPE_OCUPANCY, 0, 0, "Occupancy"));
|
||||
stateChanged(Sensor(Sensor::TYPE_OCUPANCY, 0, true, "Occupancy"));
|
||||
qDebug()<<"occupancy sensor error";
|
||||
}
|
||||
}
|
||||
|
||||
void OcupancySensorSource::store(QJsonObject &json)
|
||||
{
|
||||
json["Device"] = device_;
|
||||
json["MacAddres"] = deviceMac_;
|
||||
}
|
||||
|
||||
void OcupancySensorSource::load(const QJsonObject &json)
|
||||
{
|
||||
device_ = json["Device"].toString("wlan0");
|
||||
deviceMac_ = json["MacAddres"].toString("60:BE:B5:25:8C:E0");
|
||||
QTimer::singleShot(600000, this, &OcupancySensorSource::Timeout);
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <QObject>
|
||||
#include <QProcess>
|
||||
#include <QJsonObject>
|
||||
#include "sensor.h"
|
||||
|
||||
class OcupancySensorSource : public QObject
|
||||
@ -9,10 +9,15 @@ class OcupancySensorSource : public QObject
|
||||
Q_OBJECT
|
||||
private:
|
||||
|
||||
QProcess ping;
|
||||
QString deviceMac_;
|
||||
QString device_;
|
||||
bool occupied = true;
|
||||
|
||||
public:
|
||||
explicit OcupancySensorSource(QObject *parent = nullptr);
|
||||
explicit OcupancySensorSource(QObject *parent = nullptr, const QString& device = "wlan0", const QString& deviceMac = "60:BE:B5:25:8C:E0");
|
||||
|
||||
void store(QJsonObject& json);
|
||||
void load(const QJsonObject& json);
|
||||
|
||||
public slots:
|
||||
void sensorEvent(Sensor sensor);
|
||||
@ -20,7 +25,6 @@ public slots:
|
||||
private slots:
|
||||
|
||||
void Timeout();
|
||||
void pingExit(int exitCode, QProcess::ExitStatus exitStatus);
|
||||
|
||||
signals:
|
||||
void stateChanged(Sensor sensor);
|
||||
|
Reference in New Issue
Block a user