Add unit support to sensors

This commit is contained in:
Carl Philipp Klemm 2026-03-29 22:11:10 +02:00
parent 3e0ba165e8
commit 37c0c5d17b
12 changed files with 73 additions and 41 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 74 KiB

After

Width:  |  Height:  |  Size: 112 KiB

Before After
Before After

View file

@ -65,7 +65,7 @@ void PolynomalActor::load(const QJsonObject& json, bool preserve)
pow2_ = json["Pow2"].toDouble(0); pow2_ = json["Pow2"].toDouble(0);
pow1_ = json["Pow1"].toDouble(1); pow1_ = json["Pow1"].toDouble(1);
pow0_ = json["Pow0"].toDouble(0); pow0_ = json["Pow0"].toDouble(0);
sensor_.type = json["SensorType"].toInt(0); sensor_.type = static_cast<Sensor::sensor_type_t>(json["SensorType"].toInt(0));
sensor_.id = json["SensorId"].toInt(0); sensor_.id = json["SensorId"].toInt(0);
sensor_.field = json["SensorField"].toInt(0); sensor_.field = json["SensorField"].toInt(0);
sensor_.name = json["SensorName"].toString("Sensor"); sensor_.name = json["SensorName"].toString("Sensor");

View file

@ -100,7 +100,7 @@ void Regulator::load(const QJsonObject& json, bool preserve)
setPoint_ = json["SetPoint"].toDouble(22); setPoint_ = json["SetPoint"].toDouble(22);
safeValue_ = json["SafeValue"].toDouble(0); safeValue_ = json["SafeValue"].toDouble(0);
timeout_ = json["Timeout"].toDouble(1800); timeout_ = json["Timeout"].toDouble(1800);
sensor_.type = json["SensorType"].toInt(0); sensor_.type = static_cast<Sensor::sensor_type_t>(json["SensorType"].toInt(0));
sensor_.id = json["SensorId"].toInt(0); sensor_.id = json["SensorId"].toInt(0);
sensor_.field = json["SensorField"].toInt(0); sensor_.field = json["SensorField"].toInt(0);
sensor_.name = json["SensorName"].toString("Sensor"); sensor_.name = json["SensorName"].toString("Sensor");

View file

@ -65,7 +65,7 @@ void SensorActor::load(const QJsonObject& json, bool preserve)
Actor::load(json, preserve); Actor::load(json, preserve);
sloap_ = json["Sloap"].toInt(0); sloap_ = json["Sloap"].toInt(0);
threshold_ = json["Threshold"].toDouble(0); threshold_ = json["Threshold"].toDouble(0);
sensor_.type = json["SensorType"].toInt(0); sensor_.type = static_cast<Sensor::sensor_type_t>(json["SensorType"].toInt(0));
sensor_.id = json["SensorId"].toInt(0); sensor_.id = json["SensorId"].toInt(0);
sensor_.field = json["SensorField"].toInt(0); sensor_.field = json["SensorField"].toInt(0);
sensor_.name = json["SensorName"].toString("Sensor"); sensor_.name = json["SensorName"].toString("Sensor");

View file

@ -126,6 +126,7 @@ void Item::store(QJsonObject &json)
} }
} }
json["Actors"] = actorsArray; json["Actors"] = actorsArray;
json["ValueType"] = type_;
} }
void Item::load(const QJsonObject &json, const bool preserve) void Item::load(const QJsonObject &json, const bool preserve)
@ -249,30 +250,22 @@ void Item::mergeLoaded(Item& item)
std::shared_ptr<Item> Item::loadItem(const QJsonObject& json) std::shared_ptr<Item> Item::loadItem(const QJsonObject& json)
{ {
std::shared_ptr<Item> newItem = nullptr; std::shared_ptr<Item> newItem = nullptr;
if(json["Type"].toString("") == "Relay") if(json["Type"].toString("Item") == "Item")
{ newItem = std::shared_ptr<Item>(new Item);
else if(json["Type"].toString("") == "Relay")
newItem = std::shared_ptr<Relay>(new Relay); newItem = std::shared_ptr<Relay>(new Relay);
}
else if(json["Type"].toString("") == "Message") else if(json["Type"].toString("") == "Message")
{
newItem = std::shared_ptr<MessageItem>(new MessageItem); newItem = std::shared_ptr<MessageItem>(new MessageItem);
}
else if(json["Type"].toString("") == "System") else if(json["Type"].toString("") == "System")
{
newItem = std::shared_ptr<SystemItem>(new SystemItem); newItem = std::shared_ptr<SystemItem>(new SystemItem);
}
else if(json["Type"].toString("") == "Aux") else if(json["Type"].toString("") == "Aux")
{
newItem = std::shared_ptr<AuxItem>(new AuxItem); newItem = std::shared_ptr<AuxItem>(new AuxItem);
}
else if(json["Type"].toString("") == "Power") else if(json["Type"].toString("") == "Power")
{
newItem = std::shared_ptr<PowerItem>(new PowerItem); newItem = std::shared_ptr<PowerItem>(new PowerItem);
}
else if(json["Type"].toString("") == "Rgb") else if(json["Type"].toString("") == "Rgb")
{
newItem = std::shared_ptr<RgbItem>(new RgbItem); newItem = std::shared_ptr<RgbItem>(new RgbItem);
} else
qWarning()<<"Unable to load unkown item type: "<<json["Type"].toString();
if(newItem) if(newItem)
{ {
newItem->load(json); newItem->load(json);

View file

@ -9,8 +9,10 @@ ItemStore::ItemStore(QObject *parent): QObject(parent)
void ItemStore::addItem(std::shared_ptr<Item> item, bool inform) void ItemStore::addItem(std::shared_ptr<Item> item, bool inform)
{ {
std::shared_ptr<Item> matched = nullptr; std::shared_ptr<Item> matched = nullptr;
qDebug()<<"referance"<<item->id();
for(unsigned i = 0; i < items_.size(); i++ ) for(unsigned i = 0; i < items_.size(); i++ )
{ {
qDebug()<<"avail"<<items_[i]->id();
if(*items_[i] == *item) if(*items_[i] == *item)
{ {
matched = items_[i]; matched = items_[i];

View file

@ -89,6 +89,7 @@ PrimaryMainObject::PrimaryMainObject(QIODevice* microDevice, const QString& sett
globalItems.registerItemSource(&fixedItems); globalItems.registerItemSource(&fixedItems);
globalItems.registerItemSource(tcpServer); globalItems.registerItemSource(tcpServer);
globalItems.registerItemSource(webServer);
globalItems.registerItemSource(&micro); globalItems.registerItemSource(&micro);
globalItems.registerItemSource(&itemLoader); globalItems.registerItemSource(&itemLoader);

View file

@ -6,8 +6,8 @@ SensorStore globalSensors;
SensorStore::SensorStore(QObject *parent): QObject(parent) SensorStore::SensorStore(QObject *parent): QObject(parent)
{ {
sensors_.push_back(Sensor(0,1,0,"Front door")); sensors_.push_back(Sensor(Sensor::TYPE_DOOR,1,0,"Front door"));
sensors_.push_back(Sensor(0,0,0,"Bedroom door")); sensors_.push_back(Sensor(Sensor::TYPE_DOOR,0,0,"Bedroom door"));
} }
void SensorStore::sensorGotState(const Sensor& sensor) void SensorStore::sensorGotState(const Sensor& sensor)

View file

@ -10,32 +10,34 @@ class Sensor
{ {
public: public:
static constexpr uint8_t TYPE_DOOR = 0; typedef enum {
static constexpr uint8_t TYPE_TEMPERATURE = 1; TYPE_DOOR = 0,
static constexpr uint8_t TYPE_HUMIDITY = 2; TYPE_TEMPERATURE,
static constexpr uint8_t TYPE_PRESSURE = 3; TYPE_HUMIDITY,
static constexpr uint8_t TYPE_BRIGHTNESS = 4; TYPE_PRESSURE,
static constexpr uint8_t TYPE_BUTTON = 5; TYPE_BRIGHTNESS,
static constexpr uint8_t TYPE_ADC = 6; TYPE_BUTTON,
static constexpr uint8_t TYPE_CO2 = 7; TYPE_ADC,
static constexpr uint8_t TYPE_FORMALDEHYD= 8; TYPE_CO2,
static constexpr uint8_t TYPE_PM25 = 9; TYPE_FORMALDEHYD,
static constexpr uint8_t TYPE_TOTAL_VOC = 10; TYPE_PM25,
static constexpr uint8_t TYPE_LOWBATTERY = 128; TYPE_TOTAL_VOC,
static constexpr uint8_t TYPE_SHUTDOWN_IMMINENT = 251; TYPE_LOWBATTERY = 128,
static constexpr uint8_t TYPE_OCUPANCY = 252; TYPE_SHUTDOWN_IMMINENT = 251,
static constexpr uint8_t TYPE_SUN_ALTITUDE = 253; TYPE_OCUPANCY,
static constexpr uint8_t TYPE_AUDIO_OUTPUT = 254; TYPE_SUN_ALTITUDE,
static constexpr uint8_t TYPE_DUMMY = 255; TYPE_AUDIO_OUTPUT,
TYPE_DUMMY,
} sensor_type_t;
uint8_t type; sensor_type_t type;
uint64_t id; uint64_t id;
float field; float field;
QString name; QString name;
QDateTime lastSeen; QDateTime lastSeen;
bool hidden; bool hidden;
Sensor(uint64_t typeIn, uint8_t idIn, float fieldIn = 0, QString nameIn = "", bool hiddenIn = false): type(typeIn), Sensor(sensor_type_t typeIn, uint64_t idIn, float fieldIn = 0, QString nameIn = "", bool hiddenIn = false): type(typeIn),
id(idIn), field(fieldIn), name(nameIn), hidden(hiddenIn) id(idIn), field(fieldIn), name(nameIn), hidden(hiddenIn)
{ {
lastSeen = QDateTime::currentDateTime(); lastSeen = QDateTime::currentDateTime();
@ -48,7 +50,7 @@ public:
} }
Sensor(const QJsonObject& json) Sensor(const QJsonObject& json)
{ {
type = json["SensorType"].toInt(0); type = static_cast<sensor_type_t>(json["SensorType"].toInt(0));
id = json["Id"].toInt(0); id = json["Id"].toInt(0);
field = json["Field"].toDouble(0); field = json["Field"].toDouble(0);
name = json["Name"].toString("Sensor"); name = json["Name"].toString("Sensor");
@ -72,7 +74,7 @@ public:
QStringList bufferList = str.split(' '); QStringList bufferList = str.split(' ');
if(bufferList.size() >= 7) if(bufferList.size() >= 7)
{ {
Sensor sensor(bufferList[2].toUInt(), bufferList[4].toUInt(), bufferList[6].toUInt()); Sensor sensor(static_cast<sensor_type_t>(bufferList[2].toUInt()), bufferList[4].toUInt(), bufferList[6].toUInt());
if(sensor.type == Sensor::TYPE_HUMIDITY || sensor.type == Sensor::TYPE_TEMPERATURE) if(sensor.type == Sensor::TYPE_HUMIDITY || sensor.type == Sensor::TYPE_TEMPERATURE)
sensor.field = sensor.field/10; sensor.field = sensor.field/10;
@ -100,6 +102,7 @@ public:
json["Name"] = name; json["Name"] = name;
json["LastSeen"] = lastSeen.toString(); json["LastSeen"] = lastSeen.toString();
json["Hidden"] = hidden; json["Hidden"] = hidden;
json["Unit"] = getUnit();
} }
inline void generateName() inline void generateName()
{ {
@ -119,6 +122,31 @@ public:
name = "Shutdown Imminent"; name = "Shutdown Imminent";
else name = "Sensor Type " + QString::number(type) + " Id " + QString::number(id); else name = "Sensor Type " + QString::number(type) + " Id " + QString::number(id);
} }
QString getUnit()
{
switch(type)
{
case TYPE_TEMPERATURE:
return "°C";
case TYPE_HUMIDITY:
return "%";
case TYPE_PRESSURE:
return "hPa";
case TYPE_BRIGHTNESS:
return "lx";
case TYPE_CO2:
return "ppm";
case TYPE_FORMALDEHYD:
case TYPE_PM25:
return "µg/m³";
case TYPE_TOTAL_VOC:
return "ppb";
case TYPE_SUN_ALTITUDE:
return "°";
default:
return "";
}
}
}; };
class SensorStore: public QObject class SensorStore: public QObject

View file

@ -38,10 +38,12 @@ void TcpServer::processIncomeingJson(const QByteArray& jsonbytes)
{ {
QJsonObject jsonobject = itemjson.toObject(); QJsonObject jsonobject = itemjson.toObject();
std::shared_ptr<Item> item = Item::loadItem(jsonobject); std::shared_ptr<Item> item = Item::loadItem(jsonobject);
item->setLoaded(FullList);
if(item) if(item)
{
item->setLoaded(FullList);
items.push_back(item); items.push_back(item);
} }
}
if(FullList && !items.empty()) if(FullList && !items.empty())
{ {
requestReplaceItems(items); requestReplaceItems(items);
@ -60,6 +62,7 @@ void TcpServer::processIncomeingJson(const QByteArray& jsonbytes)
bool TcpServer::launch(const QHostAddress &address, quint16 port) bool TcpServer::launch(const QHostAddress &address, quint16 port)
{ {
qInfo()<<"Tcp server launched on"<<address<<"port"<<port;
return server.listen(address, port); return server.listen(address, port);
} }

View file

@ -94,7 +94,7 @@
</property> </property>
<property name="minimumSize"> <property name="minimumSize">
<size> <size>
<width>300</width> <width>400</width>
<height>0</height> <height>0</height>
</size> </size>
</property> </property>

View file

@ -55,6 +55,11 @@ void SensorListWidget::sensorsChanged(std::vector<Sensor> sensors)
itemString.append("\"Playing\""); itemString.append("\"Playing\"");
else itemString.append("\"Silent\""); else itemString.append("\"Silent\"");
} }
else if(!sensors[i].getUnit().isEmpty())
{
itemString.append(" ");
itemString.append(sensors[i].getUnit());
}
setItem(static_cast<int>(row), 0, new SensorListItem(sensors[i].name + (sensors[i].hidden ? " (H)" : ""), sensors[i])); setItem(static_cast<int>(row), 0, new SensorListItem(sensors[i].name + (sensors[i].hidden ? " (H)" : ""), sensors[i]));
setItem(static_cast<int>(row), 1, new QTableWidgetItem(itemString)); setItem(static_cast<int>(row), 1, new QTableWidgetItem(itemString));