123 lines
3.4 KiB
C++
123 lines
3.4 KiB
C++
#pragma once
|
|
#include <stdint.h>
|
|
#include<QString>
|
|
#include<QDateTime>
|
|
#include<QObject>
|
|
#include<vector>
|
|
|
|
class Sensor
|
|
{
|
|
public:
|
|
|
|
static constexpr uint8_t TYPE_DOOR = 0;
|
|
static constexpr uint8_t TYPE_TEMPERATURE = 1;
|
|
static constexpr uint8_t TYPE_HUMIDITY = 2;
|
|
static constexpr uint8_t TYPE_PRESSURE = 3;
|
|
static constexpr uint8_t TYPE_BRIGHTNESS = 4;
|
|
static constexpr uint8_t TYPE_BUTTON = 5;
|
|
static constexpr uint8_t TYPE_ADC = 6;
|
|
static constexpr uint8_t TYPE_LOWBATTERY = 128;
|
|
static constexpr uint8_t TYPE_SHUTDOWN_IMMINENT = 251;
|
|
static constexpr uint8_t TYPE_OCUPANCY = 252;
|
|
static constexpr uint8_t TYPE_SUN_ALTITUDE = 253;
|
|
static constexpr uint8_t TYPE_AUDIO_OUTPUT = 254;
|
|
static constexpr uint8_t TYPE_DUMMY = 255;
|
|
|
|
uint8_t type;
|
|
uint8_t id;
|
|
float field;
|
|
QString name;
|
|
QDateTime lastSeen;
|
|
bool hidden;
|
|
|
|
Sensor(uint8_t typeIn, uint8_t idIn, float fieldIn = 0, QString nameIn = "", bool hiddenIn = false): type(typeIn),
|
|
id(idIn), field(fieldIn), name(nameIn), hidden(hiddenIn)
|
|
{
|
|
lastSeen = QDateTime::currentDateTime();
|
|
if(nameIn == "")
|
|
generateName();
|
|
}
|
|
Sensor(QString nameIn = "dummy"): type(TYPE_DUMMY), id(0), field(0), name(nameIn), hidden(false)
|
|
{
|
|
lastSeen = QDateTime::currentDateTime();
|
|
}
|
|
inline bool operator==(const Sensor& in) const
|
|
{
|
|
return type==in.type && id == in.id;
|
|
}
|
|
inline bool operator!=(const Sensor& in) const
|
|
{
|
|
return !(*this==in);
|
|
}
|
|
inline void updateSeen()
|
|
{
|
|
lastSeen = QDateTime::currentDateTime();
|
|
}
|
|
static Sensor sensorFromString(const QString& str)
|
|
{
|
|
QStringList bufferList = str.split(' ');
|
|
if(bufferList.size() >= 7)
|
|
{
|
|
Sensor sensor(bufferList[2].toUInt(), bufferList[4].toUInt(), bufferList[6].toUInt());
|
|
if(sensor.type == Sensor::TYPE_HUMIDITY || sensor.type == Sensor::TYPE_TEMPERATURE)
|
|
sensor.field = sensor.field/10;
|
|
|
|
if(bufferList.size() >= 9)
|
|
sensor.lastSeen = QDateTime::fromSecsSinceEpoch(bufferList[8].toLongLong());
|
|
return sensor;
|
|
}
|
|
else
|
|
{
|
|
return Sensor(TYPE_DUMMY, 0, 0, "", true);
|
|
}
|
|
}
|
|
QString toString()
|
|
{
|
|
return QString("SENSOR TYPE: ")+QString::number(type)+" ID: "+QString::number(id)+" FIELD: "+
|
|
QString::number((type == Sensor::TYPE_HUMIDITY || type == Sensor::TYPE_TEMPERATURE) ? field*10 : field) +
|
|
" TIME: " + QString::number(lastSeen.toSecsSinceEpoch());
|
|
}
|
|
inline void generateName()
|
|
{
|
|
if(type == TYPE_TEMPERATURE) name = "Temperature " + QString::number(id);
|
|
else if(type == TYPE_DOOR) name = "Door " + QString::number(id);
|
|
else if(type == TYPE_BUTTON) name = "Button " + QString::number(id);
|
|
else if(type == TYPE_AUDIO_OUTPUT) name = "Speakers " + QString::number(id);
|
|
else if(type == TYPE_HUMIDITY) name = "Humidity " + QString::number(id);
|
|
else if(type == TYPE_SUN_ALTITUDE) name = "Solar Altitude";
|
|
else if(type == TYPE_SHUTDOWN_IMMINENT) name = "Shutdown Imminent";
|
|
else name = "Sensor Type " + QString::number(type) + " Id " + QString::number(id);
|
|
}
|
|
};
|
|
|
|
class SensorStore: public QObject
|
|
{
|
|
Q_OBJECT
|
|
private:
|
|
std::vector<Sensor> sensors_;
|
|
|
|
public:
|
|
|
|
SensorStore(QObject *parent = nullptr);
|
|
virtual ~SensorStore() {}
|
|
|
|
inline std::vector<Sensor>* getSensors()
|
|
{
|
|
return &sensors_;
|
|
}
|
|
|
|
|
|
public slots:
|
|
|
|
void sensorGotState(const Sensor& sensor);
|
|
|
|
signals:
|
|
|
|
void stateChenged(std::vector<Sensor> sensors);
|
|
void sensorChangedState(Sensor sensor);
|
|
void sensorDeleted(Sensor sensor);
|
|
|
|
};
|
|
|
|
extern SensorStore globalSensors;
|