fixed update cycle
parent
799a831081
commit
cd91ae28c8
|
|
@ -139,7 +139,16 @@ void EnergyMeter::getRegister(ModbusRegisterType type, ModbusRegisterDescriptor
|
|||
connect(reply, &ModbusRtuReply::finished, reply, &ModbusRtuReply::deleteLater);
|
||||
connect(reply, &ModbusRtuReply::finished, this, [reply, type, descriptor, this] {
|
||||
if (reply->error() != ModbusRtuReply::NoError) {
|
||||
if (m_connected) {
|
||||
m_connected = false;
|
||||
emit connectedChanged(m_connected);
|
||||
}
|
||||
return;
|
||||
} else {
|
||||
if (!m_connected) {
|
||||
m_connected = true;
|
||||
emit connectedChanged(m_connected);
|
||||
}
|
||||
}
|
||||
double value = 0;
|
||||
if (reply->result().length() == 1) {
|
||||
|
|
|
|||
|
|
@ -177,23 +177,6 @@ void IntegrationPluginEnergyMeters::postSetupThing(Thing *thing)
|
|||
if (m_connectionStateTypeIds.contains(thing->thingClassId())) {
|
||||
thing->setStateValue(m_connectionStateTypeIds.value(thing->thingClassId()), true);
|
||||
}
|
||||
|
||||
if (!m_updateTimer) {
|
||||
qCDebug(dcEnergyMeters()) << "Creating update timer";
|
||||
m_updateTimer = new QTimer(this);
|
||||
m_updateTimer->start(configValue(energyMetersPluginUpdateIntervalParamTypeId).toInt());
|
||||
connect(m_updateTimer, &QTimer::timeout, this, [this] {
|
||||
foreach (EnergyMeter *meter, m_energyMeters) {
|
||||
meter->getVoltage();
|
||||
}
|
||||
});
|
||||
connect(this, &IntegrationPlugin::configValueChanged, [this] (const ParamTypeId ¶mTypeId, const QVariant value) {
|
||||
if (m_updateTimer && (paramTypeId == energyMetersPluginUpdateIntervalParamTypeId)) {
|
||||
qCDebug(dcEnergyMeters()) << "Updating update interval to" << value;
|
||||
m_updateTimer->setInterval(value.toInt());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void IntegrationPluginEnergyMeters::thingRemoved(Thing *thing)
|
||||
|
|
@ -203,12 +186,6 @@ void IntegrationPluginEnergyMeters::thingRemoved(Thing *thing)
|
|||
if (m_energyMeters.contains(thing)) {
|
||||
m_energyMeters.take(thing)->deleteLater();
|
||||
}
|
||||
|
||||
if (myThings().isEmpty() && !m_updateTimer) {
|
||||
qCDebug(dcEnergyMeters()) << "Deleting update timer";
|
||||
m_updateTimer->deleteLater();
|
||||
m_updateTimer = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void IntegrationPluginEnergyMeters::onConnectionStateChanged(bool status)
|
||||
|
|
@ -219,6 +196,14 @@ void IntegrationPluginEnergyMeters::onConnectionStateChanged(bool status)
|
|||
return;
|
||||
|
||||
thing->setStateValue(m_connectionStateTypeIds.value(thing->thingClassId()), status);
|
||||
if (!status) {
|
||||
QTimer::singleShot(5000, meter, [this, meter, thing] {
|
||||
if (!thing->stateValue(m_connectionStateTypeIds.value(thing->thingClassId())).toBool()) {
|
||||
// Check if the device is still disconnected
|
||||
meter->getVoltage(); // restart update cycle
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void IntegrationPluginEnergyMeters::onVoltageReceived(double voltage)
|
||||
|
|
@ -228,6 +213,7 @@ void IntegrationPluginEnergyMeters::onVoltageReceived(double voltage)
|
|||
if (!thing)
|
||||
return;
|
||||
|
||||
meter->getCurrent();
|
||||
thing->setStateValue(m_voltageStateTypeIds.value(thing->thingClassId()), voltage);
|
||||
}
|
||||
|
||||
|
|
@ -238,6 +224,7 @@ void IntegrationPluginEnergyMeters::onCurrentReceived(double current)
|
|||
if (!thing)
|
||||
return;
|
||||
|
||||
meter->getActivePower();
|
||||
thing->setStateValue(m_currentStateTypeIds.value(thing->thingClassId()), current);
|
||||
}
|
||||
|
||||
|
|
@ -248,6 +235,7 @@ void IntegrationPluginEnergyMeters::onActivePowerReceived(double power)
|
|||
if (!thing)
|
||||
return;
|
||||
|
||||
meter->getFrequency();
|
||||
thing->setStateValue(m_activePowerStateTypeIds.value(thing->thingClassId()), power);
|
||||
}
|
||||
|
||||
|
|
@ -258,6 +246,7 @@ void IntegrationPluginEnergyMeters::onFrequencyReceived(double frequency)
|
|||
if (!thing)
|
||||
return;
|
||||
|
||||
meter->getPowerFactor();
|
||||
thing->setStateValue(m_frequencyStateTypeIds.value(thing->thingClassId()), frequency);
|
||||
}
|
||||
|
||||
|
|
@ -268,6 +257,7 @@ void IntegrationPluginEnergyMeters::onPowerFactorReceived(double powerFactor)
|
|||
if (!thing)
|
||||
return;
|
||||
|
||||
meter->getEnergyProduced();
|
||||
thing->setStateValue(m_powerFactorStateTypeIds.value(thing->thingClassId()), powerFactor);
|
||||
}
|
||||
|
||||
|
|
@ -278,6 +268,7 @@ void IntegrationPluginEnergyMeters::onProducedEnergyReceived(double energy)
|
|||
if (!thing)
|
||||
return;
|
||||
|
||||
meter->getEnergyConsumed();
|
||||
thing->setStateValue(m_totalEnergyProducedStateTypeIds.value(thing->thingClassId()), energy);
|
||||
}
|
||||
|
||||
|
|
@ -288,5 +279,9 @@ void IntegrationPluginEnergyMeters::onConsumedEnergyReceived(double energy)
|
|||
if (!thing)
|
||||
return;
|
||||
|
||||
int updateInterval = configValue(energyMetersPluginUpdateIntervalParamTypeId).toInt();
|
||||
QTimer::singleShot(updateInterval, meter, [meter] {
|
||||
meter->getVoltage(); // restart update cycle
|
||||
});
|
||||
thing->setStateValue(m_totalEnergyConsumedStateTypeIds.value(thing->thingClassId()), energy);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -56,7 +56,6 @@ public:
|
|||
void thingRemoved(Thing *thing) override;
|
||||
|
||||
private:
|
||||
QTimer *m_updateTimer = nullptr;
|
||||
QHash<ThingClassId, StateTypeId> m_connectionStateTypeIds;
|
||||
QHash<ThingClassId, StateTypeId> m_voltageStateTypeIds;
|
||||
QHash<ThingClassId, StateTypeId> m_currentStateTypeIds;
|
||||
|
|
|
|||
Loading…
Reference in New Issue