Add the ability to add mqtt sensors at runtime
This commit is contained in:
parent
36171a221a
commit
25b9f87285
11 changed files with 208 additions and 0 deletions
|
|
@ -10,6 +10,7 @@
|
|||
#include "service/server.h"
|
||||
#include "service/service.h"
|
||||
#include "items/item.h"
|
||||
#include "sensors/sensor.h"
|
||||
|
||||
class TestTcp : public QObject
|
||||
{
|
||||
|
|
@ -332,6 +333,94 @@ private slots:
|
|||
QVERIFY(size == jsonData.size());
|
||||
}
|
||||
|
||||
void testAddSensorMessageFormat()
|
||||
{
|
||||
// Test creating an AddSensor message format
|
||||
Sensor sensor(Sensor::TYPE_TEMPERATURE, 1, 25.0, "temp_sensor");
|
||||
|
||||
QJsonObject sensorJson;
|
||||
sensor.store(sensorJson);
|
||||
|
||||
QJsonObject payload;
|
||||
payload["Topic"] = "home/temperature";
|
||||
payload["Name"] = "Living Room";
|
||||
|
||||
// Manually create the message (since createMessage is protected)
|
||||
QJsonObject message;
|
||||
message["MessageType"] = "AddSensor";
|
||||
message["Data"] = QJsonArray();
|
||||
message["Sensor"] = sensorJson;
|
||||
message["Backend"] = static_cast<int>(Sensor::BACKEND_MQTT);
|
||||
message["Payload"] = payload;
|
||||
|
||||
QVERIFY(message["MessageType"].toString() == "AddSensor");
|
||||
QVERIFY(message["Sensor"].toObject()["Name"].toString() == "temp_sensor");
|
||||
QVERIFY(message["Backend"].toInt() == Sensor::BACKEND_MQTT);
|
||||
QVERIFY(message["Payload"].toObject()["Topic"].toString() == "home/temperature");
|
||||
}
|
||||
|
||||
void testSensorBackendTypeEnum()
|
||||
{
|
||||
// Test that the backend type enum values are correct
|
||||
QVERIFY(Sensor::BACKEND_MICROCONTROLLER == 0);
|
||||
QVERIFY(Sensor::BACKEND_MQTT == 1);
|
||||
QVERIFY(Sensor::BACKEND_SUN == 2);
|
||||
}
|
||||
|
||||
void testServiceAddSensor()
|
||||
{
|
||||
// Test that Service::addSensor can be called without crashing
|
||||
// and creates proper JSON message format
|
||||
TcpServer server;
|
||||
bool result = server.launch(QHostAddress::LocalHost, 0);
|
||||
QVERIFY(result);
|
||||
|
||||
// Create a sensor and payload
|
||||
Sensor sensor(Sensor::TYPE_TEMPERATURE, 1, 25.0, "temp_sensor");
|
||||
QJsonObject payload;
|
||||
payload["Topic"] = "home/temperature";
|
||||
payload["Name"] = "Living Room";
|
||||
|
||||
// Call addSensor - this should not crash
|
||||
// The actual sending to clients depends on network timing
|
||||
server.addSensor(sensor, Sensor::BACKEND_MQTT, payload);
|
||||
|
||||
// Test passed if no crash occurred
|
||||
QVERIFY(true);
|
||||
}
|
||||
|
||||
void testServiceProcessAddSensorMessage()
|
||||
{
|
||||
// Test processing an incoming AddSensor message
|
||||
// We test the JSON parsing directly without network
|
||||
QJsonObject sensorJson;
|
||||
sensorJson["SensorType"] = static_cast<int>(Sensor::TYPE_TEMPERATURE);
|
||||
sensorJson["Id"] = 1;
|
||||
sensorJson["Field"] = 25.0;
|
||||
sensorJson["Name"] = "temp_sensor";
|
||||
|
||||
QJsonObject payload;
|
||||
payload["Topic"] = "home/temperature";
|
||||
payload["Name"] = "Living Room";
|
||||
|
||||
QJsonObject message;
|
||||
message["MessageType"] = "AddSensor";
|
||||
message["Data"] = QJsonArray();
|
||||
message["Sensor"] = sensorJson;
|
||||
message["Backend"] = static_cast<int>(Sensor::BACKEND_MQTT);
|
||||
message["Payload"] = payload;
|
||||
|
||||
// Test that the message can be parsed correctly
|
||||
QJsonDocument doc(message);
|
||||
QVERIFY(doc.isObject());
|
||||
|
||||
QJsonObject parsed = doc.object();
|
||||
QVERIFY(parsed["MessageType"].toString() == "AddSensor");
|
||||
QVERIFY(parsed["Backend"].toInt() == Sensor::BACKEND_MQTT);
|
||||
QVERIFY(parsed["Payload"].toObject()["Topic"].toString() == "home/temperature");
|
||||
QVERIFY(parsed["Sensor"].toObject()["Name"].toString() == "temp_sensor");
|
||||
}
|
||||
|
||||
void cleanupTestCase()
|
||||
{
|
||||
// Cleanup after all tests
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue