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);
pow1_ = json["Pow1"].toDouble(1);
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_.field = json["SensorField"].toInt(0);
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);
safeValue_ = json["SafeValue"].toDouble(0);
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_.field = json["SensorField"].toInt(0);
sensor_.name = json["SensorName"].toString("Sensor");

View file

@ -65,7 +65,7 @@ void SensorActor::load(const QJsonObject& json, bool preserve)
Actor::load(json, preserve);
sloap_ = json["Sloap"].toInt(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_.field = json["SensorField"].toInt(0);
sensor_.name = json["SensorName"].toString("Sensor");

View file

@ -126,6 +126,7 @@ void Item::store(QJsonObject &json)
}
}
json["Actors"] = actorsArray;
json["ValueType"] = type_;
}
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> 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);
}
else if(json["Type"].toString("") == "Message")
{
newItem = std::shared_ptr<MessageItem>(new MessageItem);
}
else if(json["Type"].toString("") == "System")
{
newItem = std::shared_ptr<SystemItem>(new SystemItem);
}
else if(json["Type"].toString("") == "Aux")
{
newItem = std::shared_ptr<AuxItem>(new AuxItem);
}
else if(json["Type"].toString("") == "Power")
{
newItem = std::shared_ptr<PowerItem>(new PowerItem);
}
else if(json["Type"].toString("") == "Rgb")
{
newItem = std::shared_ptr<RgbItem>(new RgbItem);
}
else
qWarning()<<"Unable to load unkown item type: "<<json["Type"].toString();
if(newItem)
{
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)
{
std::shared_ptr<Item> matched = nullptr;
qDebug()<<"referance"<<item->id();
for(unsigned i = 0; i < items_.size(); i++ )
{
qDebug()<<"avail"<<items_[i]->id();
if(*items_[i] == *item)
{
matched = items_[i];

View file

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

View file

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

View file

@ -10,32 +10,34 @@ 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_CO2 = 7;
static constexpr uint8_t TYPE_FORMALDEHYD= 8;
static constexpr uint8_t TYPE_PM25 = 9;
static constexpr uint8_t TYPE_TOTAL_VOC = 10;
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;
typedef enum {
TYPE_DOOR = 0,
TYPE_TEMPERATURE,
TYPE_HUMIDITY,
TYPE_PRESSURE,
TYPE_BRIGHTNESS,
TYPE_BUTTON,
TYPE_ADC,
TYPE_CO2,
TYPE_FORMALDEHYD,
TYPE_PM25,
TYPE_TOTAL_VOC,
TYPE_LOWBATTERY = 128,
TYPE_SHUTDOWN_IMMINENT = 251,
TYPE_OCUPANCY,
TYPE_SUN_ALTITUDE,
TYPE_AUDIO_OUTPUT,
TYPE_DUMMY,
} sensor_type_t;
uint8_t type;
sensor_type_t type;
uint64_t id;
float field;
QString name;
QDateTime lastSeen;
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)
{
lastSeen = QDateTime::currentDateTime();
@ -48,7 +50,7 @@ public:
}
Sensor(const QJsonObject& json)
{
type = json["SensorType"].toInt(0);
type = static_cast<sensor_type_t>(json["SensorType"].toInt(0));
id = json["Id"].toInt(0);
field = json["Field"].toDouble(0);
name = json["Name"].toString("Sensor");
@ -72,7 +74,7 @@ public:
QStringList bufferList = str.split(' ');
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)
sensor.field = sensor.field/10;
@ -100,6 +102,7 @@ public:
json["Name"] = name;
json["LastSeen"] = lastSeen.toString();
json["Hidden"] = hidden;
json["Unit"] = getUnit();
}
inline void generateName()
{
@ -119,6 +122,31 @@ public:
name = "Shutdown Imminent";
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

View file

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

View file

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

View file

@ -55,6 +55,11 @@ void SensorListWidget::sensorsChanged(std::vector<Sensor> sensors)
itemString.append("\"Playing\"");
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), 1, new QTableWidgetItem(itemString));