Fix battery state

This commit is contained in:
Simon Stürz 2022-08-09 14:11:54 +02:00
parent 33a32169f1
commit afaaa04d62
5 changed files with 184 additions and 186 deletions

View File

@ -135,15 +135,15 @@
}, },
{ {
"key": "Running", "key": "Running",
"value": 1 "value": 2
}, },
{ {
"key": "Fault", "key": "Fault",
"value": 1 "value": 3
}, },
{ {
"key": "SleepMode", "key": "SleepMode",
"value": 1 "value": 4
} }
] ]
} }

View File

@ -135,15 +135,15 @@
}, },
{ {
"key": "Running", "key": "Running",
"value": 1 "value": 2
}, },
{ {
"key": "Fault", "key": "Fault",
"value": 1 "value": 3
}, },
{ {
"key": "SleepMode", "key": "SleepMode",
"value": 1 "value": 4
} }
] ]
} }

View File

@ -99,26 +99,8 @@ void HuaweiFusionSolarDiscovery::checkNetworkDevice(const NetworkDeviceInfo &net
return; return;
} }
// Modbus TCP connected...ok, let's try to initialize it!
connect(connection, &HuaweiFusionSolar::initializationFinished, this, [=](bool success){
if (!success) {
qCDebug(dcHuawei()) << "Discovery: Initialization failed on" << networkDeviceInfo.address().toString() << "Continue...";;
cleanupConnection(connection);
return;
}
qCDebug(dcHuawei()) << "Discovery: --> Found" << networkDeviceInfo; qCDebug(dcHuawei()) << "Discovery: --> Found" << networkDeviceInfo;
m_discoveryResults.append(networkDeviceInfo); m_discoveryResults.append(networkDeviceInfo);
// Done with this connection
cleanupConnection(connection);
});
// Initializing...
if (!connection->initialize()) {
qCDebug(dcHuawei()) << "Discovery: Unable to initialize connection on" << networkDeviceInfo.address().toString() << "Continue...";;
cleanupConnection(connection);
}
}); });
// If we get any error...skip this host... // If we get any error...skip this host...
@ -145,10 +127,12 @@ void HuaweiFusionSolarDiscovery::checkNetworkDevice(const NetworkDeviceInfo &net
void HuaweiFusionSolarDiscovery::cleanupConnection(HuaweiFusionSolar *connection) void HuaweiFusionSolarDiscovery::cleanupConnection(HuaweiFusionSolar *connection)
{ {
if (m_connections.contains(connection)) {
m_connections.removeAll(connection); m_connections.removeAll(connection);
connection->disconnectDevice(); connection->disconnectDevice();
connection->deleteLater(); connection->deleteLater();
} }
}
void HuaweiFusionSolarDiscovery::finishDiscovery() void HuaweiFusionSolarDiscovery::finishDiscovery()
{ {

View File

@ -115,13 +115,12 @@ void IntegrationPluginHuawei::setupThing(ThingSetupInfo *info)
if (thing->thingClassId() == huaweiFusionSolarInverterThingClassId) { if (thing->thingClassId() == huaweiFusionSolarInverterThingClassId) {
// Handle reconfigure // Handle reconfigure
if (m_connections.contains(thing)) { if (m_connections.contains(thing))
m_connections.take(thing)->deleteLater(); delete m_connections.take(thing);
if (m_monitors.contains(thing)) { if (m_monitors.contains(thing))
hardwareManager()->networkDeviceDiscovery()->unregisterMonitor(m_monitors.take(thing)); hardwareManager()->networkDeviceDiscovery()->unregisterMonitor(m_monitors.take(thing));
}
}
// Make sure we have a valid mac address, otherwise no monitor and not auto searching is possible // Make sure we have a valid mac address, otherwise no monitor and not auto searching is possible
MacAddress macAddress = MacAddress(thing->paramValue(huaweiFusionSolarInverterThingMacAddressParamTypeId).toString()); MacAddress macAddress = MacAddress(thing->paramValue(huaweiFusionSolarInverterThingMacAddressParamTypeId).toString());
@ -385,15 +384,17 @@ void IntegrationPluginHuawei::postSetupThing(Thing *thing)
void IntegrationPluginHuawei::thingRemoved(Thing *thing) void IntegrationPluginHuawei::thingRemoved(Thing *thing)
{ {
if (thing->thingClassId() == huaweiFusionSolarInverterThingClassId && m_connections.contains(thing)) {
m_connections.take(thing)->deleteLater();
if (m_monitors.contains(thing)) { if (m_monitors.contains(thing)) {
hardwareManager()->networkDeviceDiscovery()->unregisterMonitor(m_monitors.take(thing)); hardwareManager()->networkDeviceDiscovery()->unregisterMonitor(m_monitors.take(thing));
} }
if (m_connections.contains(thing)) {
HuaweiFusionSolar *connection = m_connections.take(thing);
connection->disconnectDevice();
delete connection;
} }
if (thing->thingClassId() == huaweiRtuInverterThingClassId && m_rtuConnections.contains(thing)) { if (m_rtuConnections.contains(thing)) {
m_rtuConnections.take(thing)->deleteLater(); m_rtuConnections.take(thing)->deleteLater();
} }
@ -410,9 +411,30 @@ void IntegrationPluginHuawei::setupFusionSolar(ThingSetupInfo *info)
uint port = thing->paramValue(huaweiFusionSolarInverterThingPortParamTypeId).toUInt(); uint port = thing->paramValue(huaweiFusionSolarInverterThingPortParamTypeId).toUInt();
quint16 slaveId = thing->paramValue(huaweiFusionSolarInverterThingSlaveIdParamTypeId).toUInt(); quint16 slaveId = thing->paramValue(huaweiFusionSolarInverterThingSlaveIdParamTypeId).toUInt();
HuaweiFusionSolar *connection = new HuaweiFusionSolar(monitor->networkDeviceInfo().address(), port, slaveId, this); qCDebug(dcHuawei()) << "Setup connection to fusion solar dongle" << monitor->networkDeviceInfo().address().toString() << port << slaveId;
qCDebug(dcHuawei()) << "Finish setup huawei fusion solar dongle" << monitor->networkDeviceInfo().address().toString() << port << slaveId; HuaweiFusionSolar *connection = new HuaweiFusionSolar(monitor->networkDeviceInfo().address(), port, slaveId, this);
connect(info, &ThingSetupInfo::aborted, connection, &HuaweiFusionSolar::deleteLater);
connect(connection, &HuaweiFusionSolar::reachableChanged, info, [=](bool reachable){
if (!reachable) {
qCWarning(dcHuawei()) << "Connection init finished with errors" << thing->name() << connection->hostAddress().toString();
hardwareManager()->networkDeviceDiscovery()->unregisterMonitor(monitor);
connection->disconnectDevice();
connection->deleteLater();
info->finish(Thing::ThingErrorHardwareFailure, QT_TR_NOOP("Could not initialize the communication with the SmartDongle."));
return;
}
m_connections.insert(thing, connection);
info->finish(Thing::ThingErrorNoError);
qCDebug(dcHuawei()) << "Setup huawei fusion solar smart dongle finished successfully" << monitor->networkDeviceInfo().address().toString() << port << slaveId;
// Set connected state
thing->setStateValue("connected", true);
foreach (Thing *childThing, myThings().filterByParentId(thing->id())) {
childThing->setStateValue("connected", true);
}
connect(connection, &HuaweiFusionSolar::reachableChanged, thing, [=](bool reachable){ connect(connection, &HuaweiFusionSolar::reachableChanged, thing, [=](bool reachable){
qCDebug(dcHuawei()) << "Reachable changed to" << reachable << "for" << thing; qCDebug(dcHuawei()) << "Reachable changed to" << reachable << "for" << thing;
@ -438,28 +460,23 @@ void IntegrationPluginHuawei::setupFusionSolar(ThingSetupInfo *info)
} }
}); });
connect(connection, &HuaweiFusionSolar::inverterActivePowerChanged, thing, [](float inverterActivePower){
qCDebug(dcHuawei()) << "Inverter power changed" << inverterActivePower * -1000.0 << "W";
//thing->setStateValue(huaweiFusionSolarInverterCurrentPowerStateTypeId, inverterActivePower * -1000.0);
});
connect(connection, &HuaweiFusionSolar::actualInverterPowerChanged, thing, [thing](float actualInverterPower){ connect(connection, &HuaweiFusionSolar::actualInverterPowerChanged, thing, [thing](float actualInverterPower){
qCDebug(dcHuawei()) << "Inverter actual power changed" << actualInverterPower << "W"; qCDebug(dcHuawei()) << "Inverter actual power changed" << actualInverterPower << "W";
thing->setStateValue(huaweiFusionSolarInverterCurrentPowerStateTypeId, actualInverterPower); thing->setStateValue(huaweiFusionSolarInverterCurrentPowerStateTypeId, actualInverterPower);
}); });
connect(connection, &HuaweiFusionSolar::inverterDeviceStatusChanged, thing, [thing](HuaweiFusionSolar::InverterDeviceStatus inverterDeviceStatus){ connect(connection, &HuaweiFusionSolar::inverterDeviceStatusReadFinished, thing, [thing](HuaweiFusionSolar::InverterDeviceStatus inverterDeviceStatus){
qCDebug(dcHuawei()) << "Inverter device status changed" << inverterDeviceStatus; qCDebug(dcHuawei()) << "Inverter device status changed" << inverterDeviceStatus;
Q_UNUSED(thing) Q_UNUSED(thing)
}); });
connect(connection, &HuaweiFusionSolar::inverterEnergyProducedChanged, thing, [thing](float inverterEnergyProduced){ connect(connection, &HuaweiFusionSolar::inverterEnergyProducedReadFinished, thing, [thing](float inverterEnergyProduced){
qCDebug(dcHuawei()) << "Inverter total energy produced changed" << inverterEnergyProduced << "kWh"; qCDebug(dcHuawei()) << "Inverter total energy produced changed" << inverterEnergyProduced << "kWh";
thing->setStateValue(huaweiFusionSolarInverterTotalEnergyProducedStateTypeId, inverterEnergyProduced); thing->setStateValue(huaweiFusionSolarInverterTotalEnergyProducedStateTypeId, inverterEnergyProduced);
}); });
// Meter // Meter
connect(connection, &HuaweiFusionSolar::powerMeterActivePowerChanged, thing, [this, thing](qint32 powerMeterActivePower){ connect(connection, &HuaweiFusionSolar::powerMeterActivePowerReadFinished, thing, [this, thing](qint32 powerMeterActivePower){
Things meterThings = myThings().filterByParentId(thing->id()).filterByThingClassId(huaweiMeterThingClassId); Things meterThings = myThings().filterByParentId(thing->id()).filterByThingClassId(huaweiMeterThingClassId);
if (!meterThings.isEmpty()) { if (!meterThings.isEmpty()) {
qCDebug(dcHuawei()) << "Meter power changed" << powerMeterActivePower << "W"; qCDebug(dcHuawei()) << "Meter power changed" << powerMeterActivePower << "W";
@ -469,7 +486,7 @@ void IntegrationPluginHuawei::setupFusionSolar(ThingSetupInfo *info)
}); });
// Battery 1 // Battery 1
connect(connection, &HuaweiFusionSolar::lunaBattery1StatusChanged, thing, [this, thing](HuaweiFusionSolar::BatteryDeviceStatus lunaBattery1Status){ connect(connection, &HuaweiFusionSolar::lunaBattery1StatusReadFinished, thing, [this, thing](HuaweiFusionSolar::BatteryDeviceStatus lunaBattery1Status){
qCDebug(dcHuawei()) << "Battery 1 status changed" << lunaBattery1Status; qCDebug(dcHuawei()) << "Battery 1 status changed" << lunaBattery1Status;
if (lunaBattery1Status != HuaweiFusionSolar::BatteryDeviceStatusOffline) { if (lunaBattery1Status != HuaweiFusionSolar::BatteryDeviceStatusOffline) {
// Check if w have to create the energy storage // Check if w have to create the energy storage
@ -492,7 +509,7 @@ void IntegrationPluginHuawei::setupFusionSolar(ThingSetupInfo *info)
} }
}); });
connect(connection, &HuaweiFusionSolar::lunaBattery1PowerChanged, thing, [this, thing](qint32 lunaBattery1Power){ connect(connection, &HuaweiFusionSolar::lunaBattery1PowerReadFinished, thing, [this, thing](qint32 lunaBattery1Power){
qCDebug(dcHuawei()) << "Battery 1 power changed" << lunaBattery1Power << "W"; qCDebug(dcHuawei()) << "Battery 1 power changed" << lunaBattery1Power << "W";
Things batteryThings = myThings().filterByParentId(thing->id()).filterByThingClassId(huaweiBatteryThingClassId).filterByParam(huaweiBatteryThingUnitParamTypeId, 1); Things batteryThings = myThings().filterByParentId(thing->id()).filterByThingClassId(huaweiBatteryThingClassId).filterByParam(huaweiBatteryThingUnitParamTypeId, 1);
if (!batteryThings.isEmpty()) { if (!batteryThings.isEmpty()) {
@ -507,7 +524,7 @@ void IntegrationPluginHuawei::setupFusionSolar(ThingSetupInfo *info)
} }
}); });
connect(connection, &HuaweiFusionSolar::lunaBattery1SocChanged, thing, [this, thing](float lunaBattery1Soc){ connect(connection, &HuaweiFusionSolar::lunaBattery1SocReadFinished, thing, [this, thing](float lunaBattery1Soc){
qCDebug(dcHuawei()) << "Battery 1 SOC changed" << lunaBattery1Soc << "%"; qCDebug(dcHuawei()) << "Battery 1 SOC changed" << lunaBattery1Soc << "%";
Things batteryThings = myThings().filterByParentId(thing->id()).filterByThingClassId(huaweiBatteryThingClassId).filterByParam(huaweiBatteryThingUnitParamTypeId, 1); Things batteryThings = myThings().filterByParentId(thing->id()).filterByThingClassId(huaweiBatteryThingClassId).filterByParam(huaweiBatteryThingUnitParamTypeId, 1);
if (!batteryThings.isEmpty()) { if (!batteryThings.isEmpty()) {
@ -517,9 +534,9 @@ void IntegrationPluginHuawei::setupFusionSolar(ThingSetupInfo *info)
}); });
// Battery 2 // Battery 2
connect(connection, &HuaweiFusionSolar::lunaBattery2StatusChanged, thing, [this, thing](HuaweiFusionSolar::BatteryDeviceStatus lunaBattery1Status){ connect(connection, &HuaweiFusionSolar::lunaBattery2StatusReadFinished, thing, [this, thing](HuaweiFusionSolar::BatteryDeviceStatus lunaBattery2Status){
qCDebug(dcHuawei()) << "Battery 2 status changed" << lunaBattery1Status; qCDebug(dcHuawei()) << "Battery 2 status changed" << lunaBattery2Status;
if (lunaBattery1Status != HuaweiFusionSolar::BatteryDeviceStatusOffline) { if (lunaBattery2Status != HuaweiFusionSolar::BatteryDeviceStatusOffline) {
// Check if w have to create the energy storage // Check if w have to create the energy storage
Things batteryThings = myThings().filterByParentId(thing->id()).filterByThingClassId(huaweiBatteryThingClassId); Things batteryThings = myThings().filterByParentId(thing->id()).filterByThingClassId(huaweiBatteryThingClassId);
bool alreadySetUp = false; bool alreadySetUp = false;
@ -540,7 +557,7 @@ void IntegrationPluginHuawei::setupFusionSolar(ThingSetupInfo *info)
} }
}); });
connect(connection, &HuaweiFusionSolar::lunaBattery2PowerChanged, thing, [this, thing](qint32 lunaBattery2Power){ connect(connection, &HuaweiFusionSolar::lunaBattery2PowerReadFinished, thing, [this, thing](qint32 lunaBattery2Power){
qCDebug(dcHuawei()) << "Battery 2 power changed" << lunaBattery2Power << "W"; qCDebug(dcHuawei()) << "Battery 2 power changed" << lunaBattery2Power << "W";
Things batteryThings = myThings().filterByParentId(thing->id()).filterByThingClassId(huaweiBatteryThingClassId).filterByParam(huaweiBatteryThingUnitParamTypeId, 2); Things batteryThings = myThings().filterByParentId(thing->id()).filterByThingClassId(huaweiBatteryThingClassId).filterByParam(huaweiBatteryThingUnitParamTypeId, 2);
if (!batteryThings.isEmpty()) { if (!batteryThings.isEmpty()) {
@ -556,7 +573,7 @@ void IntegrationPluginHuawei::setupFusionSolar(ThingSetupInfo *info)
} }
}); });
connect(connection, &HuaweiFusionSolar::lunaBattery2SocChanged, thing, [this, thing](float lunaBattery2Soc){ connect(connection, &HuaweiFusionSolar::lunaBattery2SocReadFinished, thing, [this, thing](float lunaBattery2Soc){
qCDebug(dcHuawei()) << "Battery 2 SOC changed" << lunaBattery2Soc << "%"; qCDebug(dcHuawei()) << "Battery 2 SOC changed" << lunaBattery2Soc << "%";
Things batteryThings = myThings().filterByParentId(thing->id()).filterByThingClassId(huaweiBatteryThingClassId).filterByParam(huaweiBatteryThingUnitParamTypeId, 2); Things batteryThings = myThings().filterByParentId(thing->id()).filterByThingClassId(huaweiBatteryThingClassId).filterByParam(huaweiBatteryThingUnitParamTypeId, 2);
if (!batteryThings.isEmpty()) { if (!batteryThings.isEmpty()) {
@ -564,11 +581,8 @@ void IntegrationPluginHuawei::setupFusionSolar(ThingSetupInfo *info)
batteryThings.first()->setStateValue(huaweiBatteryBatteryCriticalStateTypeId, lunaBattery2Soc < 10); batteryThings.first()->setStateValue(huaweiBatteryBatteryCriticalStateTypeId, lunaBattery2Soc < 10);
} }
}); });
});
m_connections.insert(thing, connection);
connection->connectDevice(); connection->connectDevice();
// FIXME: make async and check if this is really a huawei
info->finish(Thing::ThingErrorNoError);
} }

View File

@ -56,11 +56,11 @@ public:
private: private:
PluginTimer *m_pluginTimer = nullptr; PluginTimer *m_pluginTimer = nullptr;
QHash<Thing *, NetworkDeviceMonitor *> m_monitors; QHash<Thing *, NetworkDeviceMonitor *> m_monitors;
QHash<Thing *, HuaweiFusionSolar *> m_connections; QHash<Thing *, HuaweiFusionSolar *> m_connections;
QHash<Thing *, HuaweiModbusRtuConnection *> m_rtuConnections; QHash<Thing *, HuaweiModbusRtuConnection *> m_rtuConnections;
void setupFusionSolar(ThingSetupInfo *info); void setupFusionSolar(ThingSetupInfo *info);
}; };