Allow propagation of sensor updates from secondary to main
This commit is contained in:
parent
da50a89866
commit
34f129967b
7 changed files with 45 additions and 8 deletions
|
|
@ -165,6 +165,7 @@ SecondaryMainObject::SecondaryMainObject(QString host, int port, QObject *parent
|
||||||
{
|
{
|
||||||
connect(tcpClient, &TcpClient::gotSensor, &globalSensors, &SensorStore::sensorGotState);
|
connect(tcpClient, &TcpClient::gotSensor, &globalSensors, &SensorStore::sensorGotState);
|
||||||
globalItems.registerItemSource(tcpClient);
|
globalItems.registerItemSource(tcpClient);
|
||||||
|
connect(&globalSensors, &SensorStore::sensorChangedState, tcpClient, &TcpClient::sensorEvent);
|
||||||
|
|
||||||
if(!tcpClient->launch(QHostAddress(host), port))
|
if(!tcpClient->launch(QHostAddress(host), port))
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -89,6 +89,31 @@ void SensorStore::sensorGotState(const Sensor& sensor, sensor_update_type_t type
|
||||||
needsUpdate = true;
|
needsUpdate = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else if(type == SENSOR_UPDATE_REMOTE)
|
||||||
|
{
|
||||||
|
if(sensors_[i].name != sensor.name || sensors_[i].hidden != sensor.hidden || sensors_[i].groupName != sensor.groupName)
|
||||||
|
{
|
||||||
|
sensors_[i].name = sensor.name;
|
||||||
|
sensors_[i].hidden = sensor.hidden;
|
||||||
|
sensors_[i].groupName = sensor.groupName;
|
||||||
|
for(Sensor& known : knownSensors_)
|
||||||
|
{
|
||||||
|
if(sensor.type == known.type && sensor.id == known.id)
|
||||||
|
{
|
||||||
|
known.name = sensor.name;
|
||||||
|
known.hidden = sensor.hidden;
|
||||||
|
known.groupName = sensor.groupName;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
needsUpdate = true;
|
||||||
|
}
|
||||||
|
if(sensors_[i].field != sensor.field)
|
||||||
|
{
|
||||||
|
needsUpdate = true;
|
||||||
|
sensors_[i].field = sensor.field;
|
||||||
|
}
|
||||||
|
}
|
||||||
else if(sensors_[i].field != sensor.field)
|
else if(sensors_[i].field != sensor.field)
|
||||||
{
|
{
|
||||||
needsUpdate = true;
|
needsUpdate = true;
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@ signals:
|
||||||
void sensorAdded(Sensor sensor, Sensor::sensor_backend_type_t backend, QJsonObject payload);
|
void sensorAdded(Sensor sensor, Sensor::sensor_backend_type_t backend, QJsonObject payload);
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void sensorEvent(Sensor sensor, sensor_update_type_t type);
|
virtual void sensorEvent(Sensor sensor, sensor_update_type_t type);
|
||||||
virtual void itemUpdated(ItemUpdateRequest update);
|
virtual void itemUpdated(ItemUpdateRequest update);
|
||||||
virtual void refresh() override;
|
virtual void refresh() override;
|
||||||
virtual void addSensor(Sensor sensor, Sensor::sensor_backend_type_t backend, QJsonObject payload = {});
|
virtual void addSensor(Sensor sensor, Sensor::sensor_backend_type_t backend, QJsonObject payload = {});
|
||||||
|
|
|
||||||
|
|
@ -128,6 +128,16 @@ void TcpClient::itemUpdated(ItemUpdateRequest update)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TcpClient::sensorEvent(Sensor sensor, sensor_update_type_t type)
|
||||||
|
{
|
||||||
|
// Only forward user-initiated sensor updates to the server
|
||||||
|
// to prevent feedback loops with backend/remote updates
|
||||||
|
if(type == SENSOR_UPDATE_USER)
|
||||||
|
{
|
||||||
|
Service::sensorEvent(sensor, type);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
TcpClient::~TcpClient()
|
TcpClient::~TcpClient()
|
||||||
{
|
{
|
||||||
delete socket;
|
delete socket;
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@ class TcpClient : public Service
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
virtual void itemUpdated(ItemUpdateRequest update) override;
|
virtual void itemUpdated(ItemUpdateRequest update) override;
|
||||||
|
virtual void sensorEvent(Sensor sensor, sensor_update_type_t type) override;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
TcpClient(QObject* parent = nullptr);
|
TcpClient(QObject* parent = nullptr);
|
||||||
|
|
|
||||||
|
|
@ -37,7 +37,7 @@ void SensorListWidget::onDoubleClick(QTreeWidgetItem *item, int column)
|
||||||
{
|
{
|
||||||
if(item && item->type() == 1001)
|
if(item && item->type() == 1001)
|
||||||
{
|
{
|
||||||
const Sensor& sensor = getSensorForIndex(currentIndex());
|
const Sensor& sensor = static_cast<SensorListItem*>(item)->getSensor();
|
||||||
SensorSettingsDialog diag(sensor, this);
|
SensorSettingsDialog diag(sensor, this);
|
||||||
if(diag.exec())
|
if(diag.exec())
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -362,16 +362,16 @@ private slots:
|
||||||
// Add initial sensor
|
// Add initial sensor
|
||||||
Sensor initialSensor(Sensor::TYPE_TEMPERATURE, 1, 20.0, "Initial Name", false);
|
Sensor initialSensor(Sensor::TYPE_TEMPERATURE, 1, 20.0, "Initial Name", false);
|
||||||
store.sensorGotState(initialSensor, SENSOR_UPDATE_BACKEND);
|
store.sensorGotState(initialSensor, SENSOR_UPDATE_BACKEND);
|
||||||
|
|
||||||
// Send REMOTE update with new name and hidden state
|
// Send REMOTE update with new name and hidden state (e.g., from secondary instance)
|
||||||
Sensor remoteUpdate(Sensor::TYPE_TEMPERATURE, 1, 25.0, "Remote Name", true);
|
Sensor remoteUpdate(Sensor::TYPE_TEMPERATURE, 1, 25.0, "Remote Name", true);
|
||||||
store.sensorGotState(remoteUpdate, SENSOR_UPDATE_REMOTE);
|
store.sensorGotState(remoteUpdate, SENSOR_UPDATE_REMOTE);
|
||||||
|
|
||||||
// Verify name and hidden were NOT updated
|
// Verify name, hidden and field were updated (remote updates should sync user-configurable fields)
|
||||||
std::vector<Sensor>* sensors = store.getSensors();
|
std::vector<Sensor>* sensors = store.getSensors();
|
||||||
QVERIFY(sensors->size() == 1);
|
QVERIFY(sensors->size() == 1);
|
||||||
QVERIFY(sensors->at(0).name == "Initial Name");
|
QVERIFY(sensors->at(0).name == "Remote Name");
|
||||||
QVERIFY(sensors->at(0).hidden == false);
|
QVERIFY(sensors->at(0).hidden == true);
|
||||||
QVERIFY(sensors->at(0).field == 25.0);
|
QVERIFY(sensors->at(0).field == 25.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue