Commit 709a6ef5 authored by Your Name's avatar Your Name
Browse files

Added feature to send a notification when a user-defined digital input changes

parent fd97b2df
Pipeline #317965909 failed
......@@ -17,6 +17,9 @@ public:
signals:
void NewTemperatureReading(const TemperatureReadingData& reading);
void HighTemperatureAlert(const HighTemperatureAlertMessage& payload);
void InputChanged(QString inputId, bool on);
//...tons of other signals
};
......
#include "CommonTypes.hpp"
float getRandomFloat(float min, float max) {
float random = ((float) rand()) / (float) RAND_MAX;
float diff = max - min;
float r = random * diff;
return min + r;
}
......@@ -20,5 +20,7 @@ struct HighTemperatureAlertMessage {
{}
};
float getRandomFloat(float min, float max);
#endif // COMMONTYPES_HPP
#include "Hardware.hpp"
#include <Sensor.hpp>
#include <InputManager.hpp>
#include <QDebug>
#include <QThread>
#include <QTimer>
Hardware::Hardware(AppMessageBus* bus, QObject *parent) : QObject(parent)
{
......@@ -11,4 +15,10 @@ Hardware::Hardware(AppMessageBus* bus, QObject *parent) : QObject(parent)
auto sensor = new Sensor("sensor" + QString::number(i), bus);
qDebug() << "Created sensor" << sensor->objectName();
}
auto input = new InputManager(bus);
auto inputThread = new QThread(this);
input->moveToThread(inputThread);
inputThread->start();
QTimer::singleShot(0, input, &InputManager::Start);
}
#include "InputManager.hpp"
#include "AppMessageBus.hpp"
#include <QDebug>
#include <QTimer>
InputManager::InputManager(AppMessageBus *bus, QObject *parent) : QObject(parent), m_bus(bus)
{
setObjectName("InputManager");
}
void InputManager::Start()
{
//Simulate 30% chance of an input every 500ms
auto timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, [=](){
float rand = getRandomFloat(1, 100);
if (rand < 30) {
Q_EMIT m_bus->InputChanged("input1", true);
}
});
timer->start(500);
}
#ifndef INPUTMANAGER_HPP
#define INPUTMANAGER_HPP
#include <QObject>
class AppMessageBus;
//Reads hardware inputs such as buttons, GPIOs, etc, and notifies the rest of the application when it happens
//Runs on a separate thread to make this example a bit more complex
class InputManager : public QObject
{
Q_OBJECT
public:
explicit InputManager(AppMessageBus *bus, QObject *parent = nullptr);
public slots:
void Start();
private:
AppMessageBus* m_bus;
};
#endif // INPUTMANAGER_HPP
......@@ -14,6 +14,16 @@ NotificationManager::NotificationManager(AppMessageBus *bus, QObject *parent) :
sendNotification(msg);
});
//Also send a notification when a user-defined input has changed
// if (config.userCustomizations.inputsToNotifyOn.size() > 0) {
connect(bus, &AppMessageBus::InputChanged, this, [this](QString inputId, bool on) {
// if (config.userCustomizations.inputsToNotifyOn.contains(inputId)) {
QString msg = QString("Input %0 changed to %1").arg(inputId, QString::number(on));
sendNotification(msg);
// }
});
// }
// connect(bus, &AppMessageBus::ExcessiveFailedLoginsAlert, this, [this](const FailedLoginsAlertData& data) {
// Qstring msg = ...
// sendNotification(msg);
......@@ -22,5 +32,6 @@ NotificationManager::NotificationManager(AppMessageBus *bus, QObject *parent) :
void NotificationManager::sendNotification(QString msg)
{
//buffer/batch the sends here
qDebug() << "NotificationManager sending notification to ADMIN: " << msg;
}
......@@ -2,12 +2,6 @@
#include <QTimer>
float getRandomFloat(float a, float b) {
float random = ((float) rand()) / (float) RAND_MAX;
float diff = b - a;
float r = random * diff;
return a + r;
}
Sensor::Sensor(QString id, AppMessageBus* bus, QObject *parent) : QObject(parent)
{
......
......@@ -7,6 +7,7 @@ SOURCES += \
AppMessageBus.cpp \
CommonTypes.cpp \
Hardware.cpp \
InputManager.cpp \
NotificationManager.cpp \
ReadingsLogger.cpp \
Sensor.cpp \
......@@ -17,6 +18,7 @@ HEADERS += \
AppMessageBus.hpp \
CommonTypes.hpp \
Hardware.hpp \
InputManager.hpp \
NotificationManager.hpp \
ReadingsLogger.hpp \
Sensor.hpp \
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment