Clean code

This commit is contained in:
l.heizinger 2022-02-17 16:20:31 +01:00 committed by Simon Stürz
parent 06ef2eb01a
commit 27cd789b7f

View File

@ -38,20 +38,16 @@ IntegrationPluginStiebelEltron::IntegrationPluginStiebelEltron() {}
void IntegrationPluginStiebelEltron::discoverThings(ThingDiscoveryInfo *info) { void IntegrationPluginStiebelEltron::discoverThings(ThingDiscoveryInfo *info) {
if (!hardwareManager()->networkDeviceDiscovery()->available()) { if (!hardwareManager()->networkDeviceDiscovery()->available()) {
qCWarning(dcStiebelEltron()) qCWarning(dcStiebelEltron()) << "The network discovery is not available on this platform.";
<< "The network discovery is not available on this platform."; info->finish(Thing::ThingErrorUnsupportedFeature,
info->finish(
Thing::ThingErrorUnsupportedFeature,
QT_TR_NOOP("The network device discovery is not available.")); QT_TR_NOOP("The network device discovery is not available."));
return; return;
} }
NetworkDeviceDiscoveryReply *discoveryReply = NetworkDeviceDiscoveryReply *discoveryReply =
hardwareManager()->networkDeviceDiscovery()->discover(); hardwareManager()->networkDeviceDiscovery()->discover();
connect( connect(discoveryReply, &NetworkDeviceDiscoveryReply::finished, this, [=]() {
discoveryReply, &NetworkDeviceDiscoveryReply::finished, this, [=]() { foreach (const NetworkDeviceInfo &networkDeviceInfo, discoveryReply->networkDeviceInfos()) {
foreach (const NetworkDeviceInfo &networkDeviceInfo,
discoveryReply->networkDeviceInfos()) {
qCDebug(dcStiebelEltron()) << "Found" << networkDeviceInfo; qCDebug(dcStiebelEltron()) << "Found" << networkDeviceInfo;
QString title; QString title;
@ -67,12 +63,10 @@ void IntegrationPluginStiebelEltron::discoverThings(ThingDiscoveryInfo *info) {
description = networkDeviceInfo.macAddress(); description = networkDeviceInfo.macAddress();
} else { } else {
description = networkDeviceInfo.macAddress() + " (" + description = networkDeviceInfo.macAddress() + " (" +
networkDeviceInfo.macAddressManufacturer() + networkDeviceInfo.macAddressManufacturer() + ")";
")";
} }
ThingDescriptor descriptor(stiebelEltronThingClassId, title, ThingDescriptor descriptor(stiebelEltronThingClassId, title, description);
description);
ParamList params; ParamList params;
params << Param(stiebelEltronThingIpAddressParamTypeId, params << Param(stiebelEltronThingIpAddressParamTypeId,
networkDeviceInfo.address().toString()); networkDeviceInfo.address().toString());
@ -82,12 +76,10 @@ void IntegrationPluginStiebelEltron::discoverThings(ThingDiscoveryInfo *info) {
// Check if we already have set up this device // Check if we already have set up this device
Things existingThings = myThings().filterByParam( Things existingThings = myThings().filterByParam(
stiebelEltronThingMacAddressParamTypeId, stiebelEltronThingMacAddressParamTypeId, networkDeviceInfo.macAddress());
networkDeviceInfo.macAddress());
if (existingThings.count() == 1) { if (existingThings.count() == 1) {
qCDebug(dcStiebelEltron()) qCDebug(dcStiebelEltron())
<< "This connection already exists in the system:" << "This connection already exists in the system:" << networkDeviceInfo;
<< networkDeviceInfo;
descriptor.setThingId(existingThings.first()->id()); descriptor.setThingId(existingThings.first()->id());
} }
@ -105,20 +97,15 @@ void IntegrationPluginStiebelEltron::setupThing(ThingSetupInfo *info) {
qCDebug(dcStiebelEltron()) << "Setup" << thing << thing->params(); qCDebug(dcStiebelEltron()) << "Setup" << thing << thing->params();
if (thing->thingClassId() == stiebelEltronThingClassId) { if (thing->thingClassId() == stiebelEltronThingClassId) {
QHostAddress address( QHostAddress address(thing->paramValue(stiebelEltronThingIpAddressParamTypeId).toString());
thing->paramValue(stiebelEltronThingIpAddressParamTypeId) quint16 port = thing->paramValue(stiebelEltronThingPortParamTypeId).toUInt();
.toString()); quint16 slaveId = thing->paramValue(stiebelEltronThingSlaveIdParamTypeId).toUInt();
quint16 port =
thing->paramValue(stiebelEltronThingPortParamTypeId).toUInt();
quint16 slaveId =
thing->paramValue(stiebelEltronThingSlaveIdParamTypeId).toUInt();
StiebelEltronModbusConnection *connection = StiebelEltronModbusConnection *connection =
new StiebelEltronModbusConnection(address, port, slaveId, this); new StiebelEltronModbusConnection(address, port, slaveId, this);
connect( connect(connection, &StiebelEltronModbusConnection::connectionStateChanged, this,
connection, &StiebelEltronModbusConnection::connectionStateChanged, [thing, connection](bool status) {
this, [thing, connection](bool status) {
qCDebug(dcStiebelEltron()) qCDebug(dcStiebelEltron())
<< "Connected changed to" << status << "for" << thing; << "Connected changed to" << status << "for" << thing;
if (status) { if (status) {
@ -128,212 +115,157 @@ void IntegrationPluginStiebelEltron::setupThing(ThingSetupInfo *info) {
thing->setStateValue(stiebelEltronConnectedStateTypeId, status); thing->setStateValue(stiebelEltronConnectedStateTypeId, status);
}); });
connect(connection, connect(connection, &StiebelEltronModbusConnection::outdoorTemperatureChanged, this,
&StiebelEltronModbusConnection::outdoorTemperatureChanged, this,
[thing](float outdoorTemperature) { [thing](float outdoorTemperature) {
qCDebug(dcStiebelEltron()) qCDebug(dcStiebelEltron())
<< thing << "outdoor temperature changed" << thing << "outdoor temperature changed" << outdoorTemperature << "°C";
<< outdoorTemperature << "°C"; thing->setStateValue(stiebelEltronOutdoorTemperatureStateTypeId,
thing->setStateValue(
stiebelEltronOutdoorTemperatureStateTypeId,
outdoorTemperature); outdoorTemperature);
}); });
connect( connect(connection, &StiebelEltronModbusConnection::flowTemperatureChanged, this,
connection, &StiebelEltronModbusConnection::flowTemperatureChanged, [thing](float flowTemperature) {
this, [thing](float flowTemperature) {
qCDebug(dcStiebelEltron()) qCDebug(dcStiebelEltron())
<< thing << "flow temperature changed" << flowTemperature << thing << "flow temperature changed" << flowTemperature << "°C";
<< "°C"; thing->setStateValue(stiebelEltronFlowTemperatureStateTypeId, flowTemperature);
thing->setStateValue(stiebelEltronFlowTemperatureStateTypeId,
flowTemperature);
}); });
connect(connection, connect(connection, &StiebelEltronModbusConnection::hotWaterTemperatureChanged, this,
&StiebelEltronModbusConnection::hotWaterTemperatureChanged, [thing](float hotWaterTemperature) {
this, [thing](float hotWaterTemperature) {
qCDebug(dcStiebelEltron()) qCDebug(dcStiebelEltron())
<< thing << "hot water temperature changed" << thing << "hot water temperature changed" << hotWaterTemperature << "°C";
<< hotWaterTemperature << "°C"; thing->setStateValue(stiebelEltronHotWaterTemperatureStateTypeId,
thing->setStateValue(
stiebelEltronHotWaterTemperatureStateTypeId,
hotWaterTemperature); hotWaterTemperature);
}); });
connect(connection,
&StiebelEltronModbusConnection::storageTankTemperatureChanged, connect(connection, &StiebelEltronModbusConnection::storageTankTemperatureChanged, this,
this, [thing](float storageTankTemperature) { [thing](float storageTankTemperature) {
qCDebug(dcStiebelEltron()) qCDebug(dcStiebelEltron()) << thing << "Storage tank temperature changed"
<< thing << "Storage tank temperature changed"
<< storageTankTemperature << "°C"; << storageTankTemperature << "°C";
thing->setStateValue( thing->setStateValue(stiebelEltronStorageTankTemperatureStateTypeId,
stiebelEltronStorageTankTemperatureStateTypeId,
storageTankTemperature); storageTankTemperature);
}); });
connect(connection,
&StiebelEltronModbusConnection::returnTemperatureChanged, this, connect(connection, &StiebelEltronModbusConnection::returnTemperatureChanged, this,
[thing](float returnTemperature) { [thing](float returnTemperature) {
qCDebug(dcStiebelEltron()) qCDebug(dcStiebelEltron())
<< thing << "return temperature changed" << thing << "return temperature changed" << returnTemperature << "°C";
<< returnTemperature << "°C"; thing->setStateValue(stiebelEltronReturnTemperatureStateTypeId,
thing->setStateValue(
stiebelEltronReturnTemperatureStateTypeId,
returnTemperature); returnTemperature);
}); });
connect(
connection, &StiebelEltronModbusConnection::heatingEnergyChanged, connect(connection, &StiebelEltronModbusConnection::heatingEnergyChanged, this,
this, [thing](quint32 heatingEnergy) { [thing](quint32 heatingEnergy) {
// kWh and MWh of energy are stored in two registers an read as // kWh and MWh of energy are stored in two registers an read as
// an uint32. The following arithmetic splits the uint32 into // an uint32. The following arithmetic splits the uint32 into
// two uint16 and sums up the MWh and kWh values. // two uint16 and sums up the MWh and kWh values.
quint32 correctedEnergy = quint32 correctedEnergy =
(heatingEnergy >> 16) + (heatingEnergy & 0xFFFF) * 1000; (heatingEnergy >> 16) + (heatingEnergy & 0xFFFF) * 1000;
qCDebug(dcStiebelEltron()) << thing << "Heating energy changed" qCDebug(dcStiebelEltron())
<< correctedEnergy << "kWh"; << thing << "Heating energy changed" << correctedEnergy << "kWh";
thing->setStateValue(stiebelEltronHeatingEnergyStateTypeId, thing->setStateValue(stiebelEltronHeatingEnergyStateTypeId, correctedEnergy);
correctedEnergy);
}); });
connect(connection,
&StiebelEltronModbusConnection::hotWaterEnergyChanged, this, connect(connection, &StiebelEltronModbusConnection::hotWaterEnergyChanged, this,
[thing](quint32 hotWaterEnergy) { [thing](quint32 hotWaterEnergy) {
// see comment in heatingEnergyChanged
quint32 correctedEnergy = (hotWaterEnergy >> 16) +
(hotWaterEnergy & 0xFFFF) * 1000;
qCDebug(dcStiebelEltron())
<< thing << "Hot Water energy changed"
<< correctedEnergy << "kWh";
thing->setStateValue(stiebelEltronHotWaterEnergyStateTypeId,
correctedEnergy);
});
connect(connection,
&StiebelEltronModbusConnection::consumedEnergyHeatingChanged,
this, [thing](quint32 consumedEnergyHeatingEnergy) {
// see comment in heatingEnergyChanged // see comment in heatingEnergyChanged
quint32 correctedEnergy = quint32 correctedEnergy =
(consumedEnergyHeatingEnergy >> 16) + (hotWaterEnergy >> 16) + (hotWaterEnergy & 0xFFFF) * 1000;
qCDebug(dcStiebelEltron())
<< thing << "Hot Water energy changed" << correctedEnergy << "kWh";
thing->setStateValue(stiebelEltronHotWaterEnergyStateTypeId, correctedEnergy);
});
connect(connection, &StiebelEltronModbusConnection::consumedEnergyHeatingChanged, this,
[thing](quint32 consumedEnergyHeatingEnergy) {
// see comment in heatingEnergyChanged
quint32 correctedEnergy = (consumedEnergyHeatingEnergy >> 16) +
(consumedEnergyHeatingEnergy & 0xFFFF) * 1000; (consumedEnergyHeatingEnergy & 0xFFFF) * 1000;
qCDebug(dcStiebelEltron()) qCDebug(dcStiebelEltron())
<< thing << "Consumed energy Heating changed" << thing << "Consumed energy Heating changed" << correctedEnergy << "kWh";
<< correctedEnergy << "kWh"; thing->setStateValue(stiebelEltronConsumedEnergyHeatingStateTypeId,
thing->setStateValue(
stiebelEltronConsumedEnergyHeatingStateTypeId,
correctedEnergy); correctedEnergy);
}); });
connect(connection,
&StiebelEltronModbusConnection::consumedEnergyHotWaterChanged, connect(connection, &StiebelEltronModbusConnection::consumedEnergyHotWaterChanged, this,
this, [thing](quint32 consumedEnergyHotWaterEnergy) { [thing](quint32 consumedEnergyHotWaterEnergy) {
// see comment in heatingEnergyChanged // see comment in heatingEnergyChanged
quint32 correctedEnergy = quint32 correctedEnergy = (consumedEnergyHotWaterEnergy >> 16) +
(consumedEnergyHotWaterEnergy >> 16) +
(consumedEnergyHotWaterEnergy & 0xFFFF) * 1000; (consumedEnergyHotWaterEnergy & 0xFFFF) * 1000;
qCDebug(dcStiebelEltron()) qCDebug(dcStiebelEltron())
<< thing << "Consumed energy hot water changed" << thing << "Consumed energy hot water changed" << correctedEnergy << "kWh";
<< correctedEnergy << "kWh"; thing->setStateValue(stiebelEltronConsumedEnergyHotWaterStateTypeId,
thing->setStateValue(
stiebelEltronConsumedEnergyHotWaterStateTypeId,
correctedEnergy); correctedEnergy);
}); });
connect( connect(
connection, &StiebelEltronModbusConnection::operatingModeChanged, connection, &StiebelEltronModbusConnection::operatingModeChanged, this,
this, [thing](StiebelEltronModbusConnection::OperatingMode operatingMode) {
[thing]( qCDebug(dcStiebelEltron()) << thing << "operating mode changed " << operatingMode;
StiebelEltronModbusConnection::OperatingMode operatingMode) {
qCDebug(dcStiebelEltron())
<< thing << "operating mode changed " << operatingMode;
switch (operatingMode) { switch (operatingMode) {
case StiebelEltronModbusConnection::OperatingModeEmergency: case StiebelEltronModbusConnection::OperatingModeEmergency:
thing->setStateValue( thing->setStateValue(stiebelEltronOperatingModeStateTypeId, "Emergency");
stiebelEltronOperatingModeStateTypeId, "Emergency");
break; break;
case StiebelEltronModbusConnection::OperatingModeStandby: case StiebelEltronModbusConnection::OperatingModeStandby:
thing->setStateValue( thing->setStateValue(stiebelEltronOperatingModeStateTypeId, "Standby");
stiebelEltronOperatingModeStateTypeId, "Standby");
break; break;
case StiebelEltronModbusConnection::OperatingModeProgram: case StiebelEltronModbusConnection::OperatingModeProgram:
thing->setStateValue( thing->setStateValue(stiebelEltronOperatingModeStateTypeId, "Program");
stiebelEltronOperatingModeStateTypeId, "Program");
break; break;
case StiebelEltronModbusConnection::OperatingModeComfort: case StiebelEltronModbusConnection::OperatingModeComfort:
thing->setStateValue( thing->setStateValue(stiebelEltronOperatingModeStateTypeId, "Comfort");
stiebelEltronOperatingModeStateTypeId, "Comfort");
break; break;
case StiebelEltronModbusConnection::OperatingModeEco: case StiebelEltronModbusConnection::OperatingModeEco:
thing->setStateValue( thing->setStateValue(stiebelEltronOperatingModeStateTypeId, "Eco");
stiebelEltronOperatingModeStateTypeId, "Eco");
break; break;
case StiebelEltronModbusConnection::OperatingModeHotWater: case StiebelEltronModbusConnection::OperatingModeHotWater:
thing->setStateValue( thing->setStateValue(stiebelEltronOperatingModeStateTypeId, "Hot water");
stiebelEltronOperatingModeStateTypeId, "Hot water");
break; break;
} }
}); });
connect(connection, &StiebelEltronModbusConnection::systemStatusChanged,
this, [thing](uint16_t systemStatus) {
qCDebug(dcStiebelEltron())
<< thing << "System status changed " << systemStatus;
thing->setStateValue(stiebelEltronPumpOneStateTypeId,
systemStatus & (1 << 0));
thing->setStateValue(stiebelEltronPumpTwoStateTypeId,
systemStatus & (1 << 1));
thing->setStateValue(stiebelEltronHeatingUpStateTypeId,
systemStatus & (1 << 2));
thing->setStateValue(stiebelEltronAuxHeatingStateTypeId,
systemStatus & (1 << 3));
thing->setStateValue(stiebelEltronHeatingStateTypeId,
systemStatus & (1 << 4));
thing->setStateValue(stiebelEltronHotWaterStateTypeId,
systemStatus & (1 << 5));
thing->setStateValue(stiebelEltronCompressorStateTypeId,
systemStatus & (1 << 6));
thing->setStateValue(stiebelEltronSummerModeStateTypeId,
systemStatus & (1 << 7));
thing->setStateValue(stiebelEltronCoolingModeStateTypeId,
systemStatus & (1 << 8));
thing->setStateValue(stiebelEltronDefrostingStateTypeId,
systemStatus & (1 << 9));
thing->setStateValue(stiebelEltronSilentModeStateTypeId,
systemStatus & (1 << 10));
thing->setStateValue(stiebelEltronSilentMode2StateTypeId,
systemStatus & (1 << 11));
});
connect( connect(
connection, &StiebelEltronModbusConnection::sgReadyStateChanged, connection, &StiebelEltronModbusConnection::systemStatusChanged, this,
this, [thing](uint16_t systemStatus) {
[thing]( qCDebug(dcStiebelEltron()) << thing << "System status changed " << systemStatus;
StiebelEltronModbusConnection::SmartGridState smartGridState) { thing->setStateValue(stiebelEltronPumpOneStateTypeId, systemStatus & (1 << 0));
thing->setStateValue(stiebelEltronPumpTwoStateTypeId, systemStatus & (1 << 1));
thing->setStateValue(stiebelEltronHeatingUpStateTypeId, systemStatus & (1 << 2));
thing->setStateValue(stiebelEltronAuxHeatingStateTypeId, systemStatus & (1 << 3));
thing->setStateValue(stiebelEltronHeatingStateTypeId, systemStatus & (1 << 4));
thing->setStateValue(stiebelEltronHotWaterStateTypeId, systemStatus & (1 << 5));
thing->setStateValue(stiebelEltronCompressorStateTypeId, systemStatus & (1 << 6));
thing->setStateValue(stiebelEltronSummerModeStateTypeId, systemStatus & (1 << 7));
thing->setStateValue(stiebelEltronCoolingModeStateTypeId, systemStatus & (1 << 8));
thing->setStateValue(stiebelEltronDefrostingStateTypeId, systemStatus & (1 << 9));
thing->setStateValue(stiebelEltronSilentModeStateTypeId, systemStatus & (1 << 10));
thing->setStateValue(stiebelEltronSilentMode2StateTypeId, systemStatus & (1 << 11));
});
connect(connection, &StiebelEltronModbusConnection::sgReadyStateChanged, this,
[thing](StiebelEltronModbusConnection::SmartGridState smartGridState) {
qCDebug(dcStiebelEltron()) qCDebug(dcStiebelEltron())
<< thing << "SG Ready activation changed" << smartGridState; << thing << "SG Ready activation changed" << smartGridState;
switch (smartGridState) { switch (smartGridState) {
case StiebelEltronModbusConnection::SmartGridStateModeOne: case StiebelEltronModbusConnection::SmartGridStateModeOne:
thing->setStateValue( thing->setStateValue(stiebelEltronSgReadyModeStateTypeId, "Mode 1");
stiebelEltronSgReadyModeStateTypeId, "Mode 1");
break; break;
case StiebelEltronModbusConnection::SmartGridStateModeTwo: case StiebelEltronModbusConnection::SmartGridStateModeTwo:
thing->setStateValue( thing->setStateValue(stiebelEltronSgReadyModeStateTypeId, "Mode 2");
stiebelEltronSgReadyModeStateTypeId, "Mode 2");
break; break;
case StiebelEltronModbusConnection::SmartGridStateModeThree: case StiebelEltronModbusConnection::SmartGridStateModeThree:
thing->setStateValue( thing->setStateValue(stiebelEltronSgReadyModeStateTypeId, "Mode 3");
stiebelEltronSgReadyModeStateTypeId, "Mode 3");
break; break;
case StiebelEltronModbusConnection::SmartGridStateModeFour: case StiebelEltronModbusConnection::SmartGridStateModeFour:
thing->setStateValue( thing->setStateValue(stiebelEltronSgReadyModeStateTypeId, "Mode 4");
stiebelEltronSgReadyModeStateTypeId, "Mode 4");
break; break;
} }
}); });
connect(connection, connect(connection, &StiebelEltronModbusConnection::sgReadyActiveChanged, this,
&StiebelEltronModbusConnection::sgReadyActiveChanged, this,
[thing](bool smartGridActive) { [thing](bool smartGridActive) {
qCDebug(dcStiebelEltron()) qCDebug(dcStiebelEltron())
<< thing << "SG Ready activation changed" << thing << "SG Ready activation changed" << smartGridActive;
<< smartGridActive; thing->setStateValue(stiebelEltronSgReadyActiveStateTypeId, smartGridActive);
thing->setStateValue(stiebelEltronSgReadyActiveStateTypeId,
smartGridActive);
}); });
m_connections.insert(thing, connection); m_connections.insert(thing, connection);
@ -347,11 +279,9 @@ void IntegrationPluginStiebelEltron::postSetupThing(Thing *thing) {
if (thing->thingClassId() == stiebelEltronThingClassId) { if (thing->thingClassId() == stiebelEltronThingClassId) {
if (!m_pluginTimer) { if (!m_pluginTimer) {
qCDebug(dcStiebelEltron()) << "Starting plugin timer..."; qCDebug(dcStiebelEltron()) << "Starting plugin timer...";
m_pluginTimer = m_pluginTimer = hardwareManager()->pluginTimerManager()->registerTimer(10);
hardwareManager()->pluginTimerManager()->registerTimer(10);
connect(m_pluginTimer, &PluginTimer::timeout, this, [this] { connect(m_pluginTimer, &PluginTimer::timeout, this, [this] {
foreach (StiebelEltronModbusConnection *connection, foreach (StiebelEltronModbusConnection *connection, m_connections) {
m_connections) {
if (connection->connected()) { if (connection->connected()) {
connection->update(); connection->update();
} }
@ -364,8 +294,7 @@ void IntegrationPluginStiebelEltron::postSetupThing(Thing *thing) {
} }
void IntegrationPluginStiebelEltron::thingRemoved(Thing *thing) { void IntegrationPluginStiebelEltron::thingRemoved(Thing *thing) {
if (thing->thingClassId() == stiebelEltronThingClassId && if (thing->thingClassId() == stiebelEltronThingClassId && m_connections.contains(thing)) {
m_connections.contains(thing)) {
m_connections.take(thing)->deleteLater(); m_connections.take(thing)->deleteLater();
} }
@ -392,15 +321,12 @@ void IntegrationPluginStiebelEltron::executeAction(ThingActionInfo *info) {
info->finish(Thing::ThingErrorNoError); info->finish(Thing::ThingErrorNoError);
} }
if (info->action().actionTypeId() == if (info->action().actionTypeId() == stiebelEltronSgReadyActiveActionTypeId) {
stiebelEltronSgReadyActiveActionTypeId) {
bool sgReadyActiveBool = bool sgReadyActiveBool =
info->action() info->action()
.paramValue( .paramValue(stiebelEltronSgReadyActiveActionSgReadyActiveParamTypeId)
stiebelEltronSgReadyActiveActionSgReadyActiveParamTypeId)
.toBool(); .toBool();
qCDebug(dcStiebelEltron()) qCDebug(dcStiebelEltron()) << "Execute action" << info->action().actionTypeId().toString()
<< "Execute action" << info->action().actionTypeId().toString()
<< info->action().params(); << info->action().params();
qCDebug(dcStiebelEltron()) << "Value: " << sgReadyActiveBool; qCDebug(dcStiebelEltron()) << "Value: " << sgReadyActiveBool;
@ -412,58 +338,43 @@ void IntegrationPluginStiebelEltron::executeAction(ThingActionInfo *info) {
return; return;
} }
connect(reply, &QModbusReply::finished, reply, connect(reply, &QModbusReply::finished, reply, &QModbusReply::deleteLater);
&QModbusReply::deleteLater); connect(reply, &QModbusReply::finished, info, [info, reply, sgReadyActiveBool] {
connect(reply, &QModbusReply::finished, info,
[info, reply, sgReadyActiveBool] {
if (reply->error() != QModbusDevice::NoError) { if (reply->error() != QModbusDevice::NoError) {
qCWarning(dcStiebelEltron()) qCWarning(dcStiebelEltron())
<< "Set SG ready activation finished with error" << "Set SG ready activation finished with error" << reply->errorString();
<< reply->errorString();
info->finish(Thing::ThingErrorHardwareFailure); info->finish(Thing::ThingErrorHardwareFailure);
return; return;
} }
qCDebug(dcStiebelEltron()) qCDebug(dcStiebelEltron())
<< "Execute action finished successfully" << "Execute action finished successfully"
<< info->action().actionTypeId().toString() << info->action().actionTypeId().toString() << info->action().params();
<< info->action().params(); info->thing()->setStateValue(stiebelEltronSgReadyActiveStateTypeId, sgReadyActiveBool);
info->thing()->setStateValue(
stiebelEltronSgReadyActiveStateTypeId,
sgReadyActiveBool);
info->finish(Thing::ThingErrorNoError); info->finish(Thing::ThingErrorNoError);
}); });
connect(reply, &QModbusReply::errorOccurred, this, connect(reply, &QModbusReply::errorOccurred, this, [reply](QModbusDevice::Error error) {
[reply](QModbusDevice::Error error) { qCWarning(dcStiebelEltron()) << "Modbus reply error occurred while execute action"
qCWarning(dcStiebelEltron())
<< "Modbus reply error occurred while execute action"
<< error << reply->errorString(); << error << reply->errorString();
emit reply->finished(); // To make sure it will be deleted emit reply->finished(); // To make sure it will be deleted
}); });
} else if (info->action().actionTypeId() == } else if (info->action().actionTypeId() == stiebelEltronSgReadyModeActionTypeId) {
stiebelEltronSgReadyModeActionTypeId) {
QString sgReadyModeString = QString sgReadyModeString =
info->action() info->action()
.paramValue( .paramValue(stiebelEltronSgReadyModeActionSgReadyModeParamTypeId)
stiebelEltronSgReadyModeActionSgReadyModeParamTypeId)
.toString(); .toString();
qCDebug(dcStiebelEltron()) qCDebug(dcStiebelEltron()) << "Execute action" << info->action().actionTypeId().toString()
<< "Execute action" << info->action().actionTypeId().toString()
<< info->action().params(); << info->action().params();
StiebelEltronModbusConnection::SmartGridState sgReadyState; StiebelEltronModbusConnection::SmartGridState sgReadyState;
if (sgReadyModeString == "Mode 1") { if (sgReadyModeString == "Mode 1") {
sgReadyState = sgReadyState = StiebelEltronModbusConnection::SmartGridStateModeOne;
StiebelEltronModbusConnection::SmartGridStateModeOne;
} else if (sgReadyModeString == "Mode 2") { } else if (sgReadyModeString == "Mode 2") {
sgReadyState = sgReadyState = StiebelEltronModbusConnection::SmartGridStateModeTwo;
StiebelEltronModbusConnection::SmartGridStateModeTwo;
} else if (sgReadyModeString == "Mode 3") { } else if (sgReadyModeString == "Mode 3") {
sgReadyState = sgReadyState = StiebelEltronModbusConnection::SmartGridStateModeThree;
StiebelEltronModbusConnection::SmartGridStateModeThree;
} else { } else {
sgReadyState = sgReadyState = StiebelEltronModbusConnection::SmartGridStateModeFour;
StiebelEltronModbusConnection::SmartGridStateModeFour;
} }
QModbusReply *reply = connection->setSgReadyState(sgReadyState); QModbusReply *reply = connection->setSgReadyState(sgReadyState);
@ -474,31 +385,24 @@ void IntegrationPluginStiebelEltron::executeAction(ThingActionInfo *info) {
return; return;
} }
connect(reply, &QModbusReply::finished, reply, connect(reply, &QModbusReply::finished, reply, &QModbusReply::deleteLater);
&QModbusReply::deleteLater); connect(reply, &QModbusReply::finished, info, [info, reply, sgReadyModeString] {
connect(reply, &QModbusReply::finished, info,
[info, reply, sgReadyModeString] {
if (reply->error() != QModbusDevice::NoError) { if (reply->error() != QModbusDevice::NoError) {
qCWarning(dcStiebelEltron()) qCWarning(dcStiebelEltron())
<< "Set SG ready mode finished with error" << "Set SG ready mode finished with error" << reply->errorString();
<< reply->errorString();
info->finish(Thing::ThingErrorHardwareFailure); info->finish(Thing::ThingErrorHardwareFailure);
return; return;
} }
qCDebug(dcStiebelEltron()) qCDebug(dcStiebelEltron())
<< "Execute action finished successfully" << "Execute action finished successfully"
<< info->action().actionTypeId().toString() << info->action().actionTypeId().toString() << info->action().params();
<< info->action().params(); info->thing()->setStateValue(stiebelEltronSgReadyModeStateTypeId, sgReadyModeString);
info->thing()->setStateValue(
stiebelEltronSgReadyModeStateTypeId, sgReadyModeString);
info->finish(Thing::ThingErrorNoError); info->finish(Thing::ThingErrorNoError);
}); });
connect(reply, &QModbusReply::errorOccurred, this, connect(reply, &QModbusReply::errorOccurred, this, [reply](QModbusDevice::Error error) {
[reply](QModbusDevice::Error error) { qCWarning(dcStiebelEltron()) << "Modbus reply error occurred while execute action"
qCWarning(dcStiebelEltron())
<< "Modbus reply error occurred while execute action"
<< error << reply->errorString(); << error << reply->errorString();
emit reply->finished(); // To make sure it will be deleted emit reply->finished(); // To make sure it will be deleted
}); });