diff --git a/huawei/huaweifusionsolardiscovery.cpp b/huawei/huaweifusionsolardiscovery.cpp index ca9316a..6f109ae 100644 --- a/huawei/huaweifusionsolardiscovery.cpp +++ b/huawei/huaweifusionsolardiscovery.cpp @@ -1,6 +1,6 @@ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * -* Copyright 2013 - 2022, nymea GmbH +* Copyright 2013 - 2024, nymea GmbH * Contact: contact@nymea.io * * This file is part of nymea. @@ -45,9 +45,11 @@ void HuaweiFusionSolarDiscovery::startDiscovery() qCInfo(dcHuawei()) << "Discovery: Start searching for Huawei FusionSolar SmartDongle in the network..."; m_startDateTime = QDateTime::currentDateTime(); NetworkDeviceDiscoveryReply *discoveryReply = m_networkDeviceDiscovery->discover(); - connect(discoveryReply, &NetworkDeviceDiscoveryReply::networkDeviceInfoAdded, this, &HuaweiFusionSolarDiscovery::checkNetworkDevice); + connect(discoveryReply, &NetworkDeviceDiscoveryReply::hostAddressDiscovered, this, &HuaweiFusionSolarDiscovery::checkNetworkDevice); connect(discoveryReply, &NetworkDeviceDiscoveryReply::finished, discoveryReply, &NetworkDeviceDiscoveryReply::deleteLater); - connect(discoveryReply, &NetworkDeviceDiscoveryReply::finished, this, [=](){ + connect(discoveryReply, &NetworkDeviceDiscoveryReply::finished, this, [this, discoveryReply](){ + m_networkDeviceInfos = discoveryReply->networkDeviceInfos(); + // Finish with some delay so the last added network device information objects still can be checked. QTimer::singleShot(3000, this, [this](){ qCDebug(dcHuawei()) << "Discovery: Grace period timer triggered."; @@ -82,15 +84,15 @@ void HuaweiFusionSolarDiscovery::testNextConnection(const QHostAddress &address) } } -void HuaweiFusionSolarDiscovery::checkNetworkDevice(const NetworkDeviceInfo &networkDeviceInfo) +void HuaweiFusionSolarDiscovery::checkNetworkDevice(const QHostAddress &address) { QQueue connectionQueue; foreach (quint16 slaveId, m_slaveIds) { - HuaweiFusionSolar *connection = new HuaweiFusionSolar(networkDeviceInfo.address(), m_port, slaveId, this); + HuaweiFusionSolar *connection = new HuaweiFusionSolar(address, m_port, slaveId, this); m_connections.append(connection); connectionQueue.enqueue(connection); - connect(connection, &HuaweiFusionSolar::reachableChanged, this, [=](bool reachable){ + connect(connection, &HuaweiFusionSolar::reachableChanged, this, [this, connection](bool reachable){ if (!reachable) { // Disconnected ... done with this connection cleanupConnection(connection); @@ -98,10 +100,10 @@ void HuaweiFusionSolarDiscovery::checkNetworkDevice(const NetworkDeviceInfo &net } // Todo: initialize and check if available - connect(connection, &HuaweiFusionSolar::initializationFinished, this, [=](bool success){ + connect(connection, &HuaweiFusionSolar::initializationFinished, this, [this, connection](bool success){ Result result; - result.networkDeviceInfo = networkDeviceInfo; - result.slaveId = slaveId; + result.address = connection->modbusTcpMaster()->hostAddress(); + result.slaveId = connection->slaveId(); if (success) { qCDebug(dcHuawei()) << "Huawei init finished successfully:" << connection->model() << connection->serialNumber() << connection->productNumber(); @@ -109,7 +111,7 @@ void HuaweiFusionSolarDiscovery::checkNetworkDevice(const NetworkDeviceInfo &net result.serialNumber = connection->serialNumber(); } - qCInfo(dcHuawei()) << "Discovery: --> Found" << networkDeviceInfo << "slave ID:" << slaveId; + qCInfo(dcHuawei()) << "Discovery: --> Found" << result.address.toString() << "slave ID:" << result.slaveId; m_results.append(result); }); @@ -117,23 +119,22 @@ void HuaweiFusionSolarDiscovery::checkNetworkDevice(const NetworkDeviceInfo &net }); // If we get any error...skip this host... - connect(connection->modbusTcpMaster(), &ModbusTcpMaster::connectionErrorOccurred, this, [=](QModbusDevice::Error error){ + connect(connection->modbusTcpMaster(), &ModbusTcpMaster::connectionErrorOccurred, this, [this, connection](QModbusDevice::Error error){ if (error != QModbusDevice::NoError) { - qCDebug(dcHuawei()) << "Discovery: Connection error on" << networkDeviceInfo.address().toString() << "Continue...";; + qCDebug(dcHuawei()) << "Discovery: Connection error on" << connection->modbusTcpMaster()->hostAddress().toString() << "Continue...";; cleanupConnection(connection); } }); // If check reachability failed...skip this host... - connect(connection, &HuaweiFusionSolar::checkReachabilityFailed, this, [=](){ - qCDebug(dcHuawei()) << "Discovery: Check reachability failed on" << networkDeviceInfo.address().toString() << "Continue...";; + connect(connection, &HuaweiFusionSolar::checkReachabilityFailed, this, [this, connection](){ + qCDebug(dcHuawei()) << "Discovery: Check reachability failed on" << connection->modbusTcpMaster()->hostAddress().toString() << "Continue...";; cleanupConnection(connection); }); - } - m_pendingConnectionAttempts[networkDeviceInfo.address()] = connectionQueue; - testNextConnection(networkDeviceInfo.address()); + m_pendingConnectionAttempts[address] = connectionQueue; + testNextConnection(address); } void HuaweiFusionSolarDiscovery::cleanupConnection(HuaweiFusionSolar *connection) @@ -151,6 +152,10 @@ void HuaweiFusionSolarDiscovery::finishDiscovery() { qint64 durationMilliSeconds = QDateTime::currentMSecsSinceEpoch() - m_startDateTime.toMSecsSinceEpoch(); + // Fill in finished network device information + for (int i = 0; i < m_results.count(); i++) + m_results[i].networkDeviceInfo = m_networkDeviceInfos.get(m_results.value(i).address); + // Cleanup any leftovers...we don't care any more foreach (HuaweiFusionSolar *connection, m_connections) cleanupConnection(connection); diff --git a/huawei/huaweifusionsolardiscovery.h b/huawei/huaweifusionsolardiscovery.h index 4e46bf7..4f8661d 100644 --- a/huawei/huaweifusionsolardiscovery.h +++ b/huawei/huaweifusionsolardiscovery.h @@ -1,6 +1,6 @@ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * -* Copyright 2013 - 2022, nymea GmbH +* Copyright 2013 - 2024, nymea GmbH * Contact: contact@nymea.io * * This file is part of nymea. @@ -47,6 +47,7 @@ public: QString modelName; QString serialNumber; quint16 slaveId; + QHostAddress address; NetworkDeviceInfo networkDeviceInfo; } Result; @@ -67,13 +68,14 @@ private: QList m_connections; QList m_results; + NetworkDeviceInfos m_networkDeviceInfos; + void testNextConnection(const QHostAddress &address); - void checkNetworkDevice(const NetworkDeviceInfo &networkDeviceInfo); + void checkNetworkDevice(const QHostAddress &address); void cleanupConnection(HuaweiFusionSolar *connection); void finishDiscovery(); - }; #endif // HUAWEIFUSIONSOLARDISCOVERY_H diff --git a/huawei/integrationpluginhuawei.cpp b/huawei/integrationpluginhuawei.cpp index 14f66e6..c4207b0 100644 --- a/huawei/integrationpluginhuawei.cpp +++ b/huawei/integrationpluginhuawei.cpp @@ -1,6 +1,6 @@ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * -* Copyright 2013 - 2022, nymea GmbH +* Copyright 2013 - 2024, nymea GmbH * Contact: contact@nymea.io * * This fileDescriptor is part of nymea. @@ -59,25 +59,42 @@ void IntegrationPluginHuawei::discoverThings(ThingDiscoveryInfo *info) if (!result.modelName.isEmpty()) name = "Huawei " + result.modelName; - QString desctiption = result.networkDeviceInfo.macAddress() + " - " + result.networkDeviceInfo.address().toString(); - if (!result.serialNumber.isEmpty()) { - desctiption = "SN: " + result.serialNumber + " " + result.networkDeviceInfo.macAddress() + " - " + result.networkDeviceInfo.address().toString(); + QString description; + + if (!result.serialNumber.isEmpty()) + description = "SN: " + result.serialNumber; + + switch (result.networkDeviceInfo.monitorMode()) { + case NetworkDeviceInfo::MonitorModeMac: + description += " MAC: " + result.networkDeviceInfo.macAddressInfos().constFirst().macAddress().toString() + + " - " + result.networkDeviceInfo.address().toString(); + break; + case NetworkDeviceInfo::MonitorModeHostName: + description += " Host name: " + result.networkDeviceInfo.hostName() + + " - " + result.networkDeviceInfo.address().toString(); + break; + case NetworkDeviceInfo::MonitorModeIp: + description += " IP: " + result.networkDeviceInfo.address().toString(); + break; } - ThingDescriptor descriptor(huaweiFusionSolarInverterThingClassId, name, desctiption) ; + ThingDescriptor descriptor(huaweiFusionSolarInverterThingClassId, name, description) ; qCDebug(dcHuawei()) << "Discovered:" << descriptor.title() << descriptor.description(); - // Check if we already have set up this device - Things existingThings = myThings().filterByParam(huaweiFusionSolarInverterThingMacAddressParamTypeId, result.networkDeviceInfo.macAddress()); - if (existingThings.count() == 1) { - qCDebug(dcHuawei()) << "This inverter already exists in the system:" << result.networkDeviceInfo; - descriptor.setThingId(existingThings.first()->id()); - } - ParamList params; - params << Param(huaweiFusionSolarInverterThingMacAddressParamTypeId, result.networkDeviceInfo.macAddress()); + params << Param(huaweiFusionSolarInverterThingMacAddressParamTypeId, result.networkDeviceInfo.thingParamValueMacAddress()); + params << Param(huaweiFusionSolarInverterThingHostNameParamTypeId, result.networkDeviceInfo.thingParamValueHostName()); + params << Param(huaweiFusionSolarInverterThingAddressParamTypeId, result.networkDeviceInfo.thingParamValueAddress()); params << Param(huaweiFusionSolarInverterThingSlaveIdParamTypeId, result.slaveId); descriptor.setParams(params); + + // Check if we already have set up this device + Thing *existingThing = myThings().findByParams(params); + if (existingThing) { + qCDebug(dcHuawei()) << "This inverter already exists in the system:" << result.networkDeviceInfo; + descriptor.setThingId(existingThing->id()); + } + info->addThingDescriptor(descriptor); } @@ -131,22 +148,19 @@ void IntegrationPluginHuawei::setupThing(ThingSetupInfo *info) if (m_monitors.contains(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 - MacAddress macAddress = MacAddress(thing->paramValue(huaweiFusionSolarInverterThingMacAddressParamTypeId).toString()); - if (macAddress.isNull()) { - qCWarning(dcHuawei()) << "Failed to set up Fusion Solar because the MAC address is not valid:" << thing->paramValue(huaweiFusionSolarInverterThingMacAddressParamTypeId).toString() << macAddress.toString(); - info->finish(Thing::ThingErrorInvalidParameter, QT_TR_NOOP("The MAC address is not vaild. Please reconfigure the device to fix this.")); + // Create a monitor so we always get the correct IP in the network and see if the device is reachable without polling on our own + NetworkDeviceMonitor *monitor = hardwareManager()->networkDeviceDiscovery()->registerMonitor(thing); + if (!monitor) { + qCWarning(dcHuawei()) << "Failed to set up Fusion Solar because the params are incomplete for creating a monitor:" << thing->params(); + info->finish(Thing::ThingErrorInvalidParameter, QT_TR_NOOP("The parameters are incomplete. Please reconfigure the device to fix this.")); return; } - // Create a monitor so we always get the correct IP in the network and see if the device is reachable without polling on our own - NetworkDeviceMonitor *monitor = hardwareManager()->networkDeviceDiscovery()->registerMonitor(macAddress); m_monitors.insert(thing, monitor); connect(info, &ThingSetupInfo::aborted, monitor, [=](){ hardwareManager()->networkDeviceDiscovery()->unregisterMonitor(m_monitors.take(thing)); }); - // Continue with setup only if we know that the network device is reachable if (info->isInitialSetup()) { if (monitor->reachable()) { @@ -444,222 +458,208 @@ void IntegrationPluginHuawei::setupFusionSolar(ThingSetupInfo *info) qCDebug(dcHuawei()) << "Setup connection to 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->modbusTcpMaster()->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); - } - - // Reset history, just incase - m_inverterEnergyProducedHistory[thing].clear(); - - // Add the current value to the history - evaluateEnergyProducedValue(thing, thing->stateValue(huaweiFusionSolarInverterTotalEnergyProducedStateTypeId).toFloat()); - - connect(connection, &HuaweiFusionSolar::reachableChanged, thing, [=](bool reachable){ - qCDebug(dcHuawei()) << "Reachable changed to" << reachable << "for" << thing; - thing->setStateValue("connected", reachable); - foreach (Thing *childThing, myThings().filterByParentId(thing->id())) { - childThing->setStateValue("connected", reachable); - - if (!reachable) { - // Set power values to 0 since we don't know what the current value is - if (childThing->thingClassId() == huaweiFusionSolarInverterThingClassId) { - thing->setStateValue(huaweiFusionSolarInverterCurrentPowerStateTypeId, 0); - } - - if (childThing->thingClassId() == huaweiMeterThingClassId) { - thing->setStateValue(huaweiMeterCurrentPowerStateTypeId, 0); - } - - if (childThing->thingClassId() == huaweiBatteryThingClassId) { - thing->setStateValue(huaweiBatteryCurrentPowerStateTypeId, 0); - } - } - } - }); - - connect(monitor, &NetworkDeviceMonitor::reachableChanged, thing, [=](bool reachable){ - if (!thing->setupComplete()) - return; - - qCDebug(dcHuawei()) << "Network device monitor for" << thing->name() << (reachable ? "is now reachable" : "is not reachable any more" ); - - if (reachable && !thing->stateValue("connected").toBool()) { - connection->modbusTcpMaster()->setHostAddress(monitor->networkDeviceInfo().address()); - connection->connectDevice(); - } else if (!reachable) { - // Note: We disable autoreconnect explicitly and we will - // connect the device once the monitor says it is reachable again - connection->disconnectDevice(); - } - }); - - connect(connection, &HuaweiFusionSolar::inverterActivePowerChanged, thing, [thing](float inverterActivePower){ - thing->setStateValue(huaweiFusionSolarInverterActivePowerStateTypeId, inverterActivePower * -1000.0); - }); - - connect(connection, &HuaweiFusionSolar::inverterInputPowerChanged, thing, [thing](float inverterInputPower){ - thing->setStateValue(huaweiFusionSolarInverterCurrentPowerStateTypeId, inverterInputPower * -1000.0); - }); - - connect(connection, &HuaweiFusionSolar::inverterDeviceStatusReadFinished, thing, [thing](HuaweiFusionSolar::InverterDeviceStatus inverterDeviceStatus){ - qCDebug(dcHuawei()) << "Inverter device status changed" << inverterDeviceStatus; - Q_UNUSED(thing) - }); - - connect(connection, &HuaweiFusionSolar::inverterEnergyProducedReadFinished, thing, [this, thing](float inverterEnergyProduced){ - qCDebug(dcHuawei()) << "Inverter total energy produced changed" << inverterEnergyProduced << "kWh"; - - // Note: sometimes this value is suddenly 0 or absurd high > 100000000 - // We try here to filer out such random values. Sadly the values seem to - // come like that from the device, without exception or error. - evaluateEnergyProducedValue(thing, inverterEnergyProduced); - }); - - // Meter - connect(connection, &HuaweiFusionSolar::powerMeterActivePowerReadFinished, thing, [this, thing](qint32 powerMeterActivePower){ - Things meterThings = myThings().filterByParentId(thing->id()).filterByThingClassId(huaweiMeterThingClassId); - if (!meterThings.isEmpty()) { - qCDebug(dcHuawei()) << "Meter power changed" << powerMeterActivePower << "W"; - // Note: > 0 -> return, < 0 consume - meterThings.first()->setStateValue(huaweiMeterCurrentPowerStateTypeId, -powerMeterActivePower); - } - }); - connect(connection, &HuaweiFusionSolar::powerMeterEnergyReturnedReadFinished, thing, [this, thing](float powerMeterEnergyReturned){ - Things meterThings = myThings().filterByParentId(thing->id()).filterByThingClassId(huaweiMeterThingClassId); - if (!meterThings.isEmpty()) { - qCDebug(dcHuawei()) << "Meter power Returned changed" << powerMeterEnergyReturned << "kWh"; - meterThings.first()->setStateValue(huaweiMeterTotalEnergyProducedStateTypeId, powerMeterEnergyReturned); - } - }); - connect(connection, &HuaweiFusionSolar::powerMeterEnergyAquiredReadFinished, thing, [this, thing](float powerMeterEnergyAquired){ - Things meterThings = myThings().filterByParentId(thing->id()).filterByThingClassId(huaweiMeterThingClassId); - if (!meterThings.isEmpty()) { - qCDebug(dcHuawei()) << "Meter power Aquired changed" << powerMeterEnergyAquired << "kWh"; - meterThings.first()->setStateValue(huaweiMeterTotalEnergyConsumedStateTypeId, powerMeterEnergyAquired); - } - }); - - // Battery 1 - connect(connection, &HuaweiFusionSolar::lunaBattery1StatusReadFinished, thing, [this, thing](HuaweiFusionSolar::BatteryDeviceStatus lunaBattery1Status){ - qCDebug(dcHuawei()) << "Battery 1 status changed of" << thing << lunaBattery1Status; - Thing *batteryThing = nullptr; - foreach (Thing *bt, myThings().filterByParentId(thing->id()).filterByThingClassId(huaweiBatteryThingClassId)) { - if (bt->paramValue(huaweiBatteryThingUnitParamTypeId).toUInt() == 1) { - batteryThing = bt; - break; - } - } - - // Check if w have to create the energy storage - if (lunaBattery1Status != HuaweiFusionSolar::BatteryDeviceStatusOffline && !batteryThing) { - qCDebug(dcHuawei()) << "Set up huawei energy storage 1 for" << thing; - ThingDescriptor descriptor(huaweiBatteryThingClassId, "Luna 2000 Battery 1", QString(), thing->id()); - ParamList params; - params.append(Param(huaweiBatteryThingUnitParamTypeId, 1)); - descriptor.setParams(params); - emit autoThingsAppeared(ThingDescriptors() << descriptor); - } else if (lunaBattery1Status == HuaweiFusionSolar::BatteryDeviceStatusOffline && batteryThing) { - qCDebug(dcHuawei()) << "Autoremove huawei energy storage 1 for" << thing << "because the battery is offline" << batteryThing; - emit autoThingDisappeared(batteryThing->id()); - } - }); - - connect(connection, &HuaweiFusionSolar::lunaBattery1PowerReadFinished, thing, [this, thing](qint32 lunaBattery1Power){ - qCDebug(dcHuawei()) << "Battery 1 power changed" << lunaBattery1Power << "W"; - Things batteryThings = myThings().filterByParentId(thing->id()).filterByThingClassId(huaweiBatteryThingClassId).filterByParam(huaweiBatteryThingUnitParamTypeId, 1); - if (!batteryThings.isEmpty()) { - batteryThings.first()->setStateValue(huaweiBatteryCurrentPowerStateTypeId, lunaBattery1Power); - if (lunaBattery1Power < 0) { - batteryThings.first()->setStateValue(huaweiBatteryChargingStateStateTypeId, "discharging"); - } else if (lunaBattery1Power > 0) { - batteryThings.first()->setStateValue(huaweiBatteryChargingStateStateTypeId, "charging"); - } else { - batteryThings.first()->setStateValue(huaweiBatteryChargingStateStateTypeId, "idle"); - } - } - }); - - connect(connection, &HuaweiFusionSolar::lunaBattery1SocReadFinished, thing, [this, thing](float lunaBattery1Soc){ - qCDebug(dcHuawei()) << "Battery 1 SOC changed" << lunaBattery1Soc << "%"; - Things batteryThings = myThings().filterByParentId(thing->id()).filterByThingClassId(huaweiBatteryThingClassId).filterByParam(huaweiBatteryThingUnitParamTypeId, 1); - if (!batteryThings.isEmpty()) { - batteryThings.first()->setStateValue(huaweiBatteryBatteryLevelStateTypeId, lunaBattery1Soc); - batteryThings.first()->setStateValue(huaweiBatteryBatteryCriticalStateTypeId, lunaBattery1Soc < 10); - } - }); - - // Battery 2 - connect(connection, &HuaweiFusionSolar::lunaBattery2StatusReadFinished, thing, [this, thing](HuaweiFusionSolar::BatteryDeviceStatus lunaBattery2Status){ - - qCDebug(dcHuawei()) << "Battery 2 status changed of" << thing << lunaBattery2Status; - Thing *batteryThing = nullptr; - foreach (Thing *bt, myThings().filterByParentId(thing->id()).filterByThingClassId(huaweiBatteryThingClassId)) { - if (bt->paramValue(huaweiBatteryThingUnitParamTypeId).toUInt() == 2) { - batteryThing = bt; - break; - } - } - - // Check if w have to create the energy storage - if (lunaBattery2Status != HuaweiFusionSolar::BatteryDeviceStatusOffline && !batteryThing) { - qCDebug(dcHuawei()) << "Set up huawei energy storage 2 for" << thing; - ThingDescriptor descriptor(huaweiBatteryThingClassId, "Luna 2000 Battery 2", QString(), thing->id()); - ParamList params; - params.append(Param(huaweiBatteryThingUnitParamTypeId, 2)); - descriptor.setParams(params); - emit autoThingsAppeared(ThingDescriptors() << descriptor); - } else if (lunaBattery2Status == HuaweiFusionSolar::BatteryDeviceStatusOffline && batteryThing) { - qCDebug(dcHuawei()) << "Autoremove huawei energy storage 2 for" << thing << "because the battery is offline" << batteryThing; - emit autoThingDisappeared(batteryThing->id()); - } - }); - - connect(connection, &HuaweiFusionSolar::lunaBattery2PowerReadFinished, thing, [this, thing](qint32 lunaBattery2Power){ - qCDebug(dcHuawei()) << "Battery 2 power changed" << lunaBattery2Power << "W"; - Things batteryThings = myThings().filterByParentId(thing->id()).filterByThingClassId(huaweiBatteryThingClassId).filterByParam(huaweiBatteryThingUnitParamTypeId, 2); - if (!batteryThings.isEmpty()) { - batteryThings.first()->setStateValue(huaweiBatteryCurrentPowerStateTypeId, lunaBattery2Power); - - if (lunaBattery2Power < 0) { - batteryThings.first()->setStateValue(huaweiBatteryChargingStateStateTypeId, "discharging"); - } else if (lunaBattery2Power > 0) { - batteryThings.first()->setStateValue(huaweiBatteryChargingStateStateTypeId, "charging"); - } else { - batteryThings.first()->setStateValue(huaweiBatteryChargingStateStateTypeId, "idle"); - } - } - }); - - connect(connection, &HuaweiFusionSolar::lunaBattery2SocReadFinished, thing, [this, thing](float lunaBattery2Soc){ - qCDebug(dcHuawei()) << "Battery 2 SOC changed" << lunaBattery2Soc << "%"; - Things batteryThings = myThings().filterByParentId(thing->id()).filterByThingClassId(huaweiBatteryThingClassId).filterByParam(huaweiBatteryThingUnitParamTypeId, 2); - if (!batteryThings.isEmpty()) { - batteryThings.first()->setStateValue(huaweiBatteryBatteryLevelStateTypeId, lunaBattery2Soc); - batteryThings.first()->setStateValue(huaweiBatteryBatteryCriticalStateTypeId, lunaBattery2Soc < 10); - } - }); + connect(info, &ThingSetupInfo::aborted, connection, [this, connection, thing](){ + connection->deleteLater(); + m_connections.remove(thing); }); - if (monitor->reachable()) - connection->connectDevice(); + 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; + + // Reset history, just incase + m_inverterEnergyProducedHistory[thing].clear(); + + // Add the current value to the history + evaluateEnergyProducedValue(thing, thing->stateValue(huaweiFusionSolarInverterTotalEnergyProducedStateTypeId).toFloat()); + + connect(connection, &HuaweiFusionSolar::reachableChanged, thing, [=](bool reachable){ + qCDebug(dcHuawei()) << "Reachable changed to" << reachable << "for" << thing; + thing->setStateValue("connected", reachable); + foreach (Thing *childThing, myThings().filterByParentId(thing->id())) { + childThing->setStateValue("connected", reachable); + + if (!reachable) { + // Set power values to 0 since we don't know what the current value is + if (childThing->thingClassId() == huaweiFusionSolarInverterThingClassId) { + thing->setStateValue(huaweiFusionSolarInverterCurrentPowerStateTypeId, 0); + } + + if (childThing->thingClassId() == huaweiMeterThingClassId) { + thing->setStateValue(huaweiMeterCurrentPowerStateTypeId, 0); + } + + if (childThing->thingClassId() == huaweiBatteryThingClassId) { + thing->setStateValue(huaweiBatteryCurrentPowerStateTypeId, 0); + } + } + } + }); + + connect(monitor, &NetworkDeviceMonitor::reachableChanged, thing, [=](bool reachable){ + if (!thing->setupComplete()) + return; + + qCDebug(dcHuawei()) << "Network device monitor for" << thing->name() << (reachable ? "is now reachable" : "is not reachable any more" ); + + if (reachable && !thing->stateValue("connected").toBool()) { + connection->modbusTcpMaster()->setHostAddress(monitor->networkDeviceInfo().address()); + connection->connectDevice(); + } else if (!reachable) { + // Note: We disable autoreconnect explicitly and we will + // connect the device once the monitor says it is reachable again + connection->disconnectDevice(); + } + }); + + connect(connection, &HuaweiFusionSolar::inverterActivePowerChanged, thing, [thing](float inverterActivePower){ + thing->setStateValue(huaweiFusionSolarInverterActivePowerStateTypeId, inverterActivePower * -1000.0); + }); + + connect(connection, &HuaweiFusionSolar::inverterInputPowerChanged, thing, [thing](float inverterInputPower){ + thing->setStateValue(huaweiFusionSolarInverterCurrentPowerStateTypeId, inverterInputPower * -1000.0); + }); + + connect(connection, &HuaweiFusionSolar::inverterDeviceStatusReadFinished, thing, [thing](HuaweiFusionSolar::InverterDeviceStatus inverterDeviceStatus){ + qCDebug(dcHuawei()) << "Inverter device status changed" << inverterDeviceStatus; + Q_UNUSED(thing) + }); + + connect(connection, &HuaweiFusionSolar::inverterEnergyProducedReadFinished, thing, [this, thing](float inverterEnergyProduced){ + qCDebug(dcHuawei()) << "Inverter total energy produced changed" << inverterEnergyProduced << "kWh"; + + // Note: sometimes this value is suddenly 0 or absurd high > 100000000 + // We try here to filer out such random values. Sadly the values seem to + // come like that from the device, without exception or error. + evaluateEnergyProducedValue(thing, inverterEnergyProduced); + }); + + // Meter + connect(connection, &HuaweiFusionSolar::powerMeterActivePowerReadFinished, thing, [this, thing](qint32 powerMeterActivePower){ + Things meterThings = myThings().filterByParentId(thing->id()).filterByThingClassId(huaweiMeterThingClassId); + if (!meterThings.isEmpty()) { + qCDebug(dcHuawei()) << "Meter power changed" << powerMeterActivePower << "W"; + // Note: > 0 -> return, < 0 consume + meterThings.first()->setStateValue(huaweiMeterCurrentPowerStateTypeId, -powerMeterActivePower); + } + }); + connect(connection, &HuaweiFusionSolar::powerMeterEnergyReturnedReadFinished, thing, [this, thing](float powerMeterEnergyReturned){ + Things meterThings = myThings().filterByParentId(thing->id()).filterByThingClassId(huaweiMeterThingClassId); + if (!meterThings.isEmpty()) { + qCDebug(dcHuawei()) << "Meter power Returned changed" << powerMeterEnergyReturned << "kWh"; + meterThings.first()->setStateValue(huaweiMeterTotalEnergyProducedStateTypeId, powerMeterEnergyReturned); + } + }); + connect(connection, &HuaweiFusionSolar::powerMeterEnergyAquiredReadFinished, thing, [this, thing](float powerMeterEnergyAquired){ + Things meterThings = myThings().filterByParentId(thing->id()).filterByThingClassId(huaweiMeterThingClassId); + if (!meterThings.isEmpty()) { + qCDebug(dcHuawei()) << "Meter power Aquired changed" << powerMeterEnergyAquired << "kWh"; + meterThings.first()->setStateValue(huaweiMeterTotalEnergyConsumedStateTypeId, powerMeterEnergyAquired); + } + }); + + // Battery 1 + connect(connection, &HuaweiFusionSolar::lunaBattery1StatusReadFinished, thing, [this, thing](HuaweiFusionSolar::BatteryDeviceStatus lunaBattery1Status){ + qCDebug(dcHuawei()) << "Battery 1 status changed of" << thing << lunaBattery1Status; + Thing *batteryThing = nullptr; + foreach (Thing *bt, myThings().filterByParentId(thing->id()).filterByThingClassId(huaweiBatteryThingClassId)) { + if (bt->paramValue(huaweiBatteryThingUnitParamTypeId).toUInt() == 1) { + batteryThing = bt; + break; + } + } + + // Check if w have to create the energy storage + if (lunaBattery1Status != HuaweiFusionSolar::BatteryDeviceStatusOffline && !batteryThing) { + qCDebug(dcHuawei()) << "Set up huawei energy storage 1 for" << thing; + ThingDescriptor descriptor(huaweiBatteryThingClassId, "Luna 2000 Battery 1", QString(), thing->id()); + ParamList params; + params.append(Param(huaweiBatteryThingUnitParamTypeId, 1)); + descriptor.setParams(params); + emit autoThingsAppeared(ThingDescriptors() << descriptor); + } else if (lunaBattery1Status == HuaweiFusionSolar::BatteryDeviceStatusOffline && batteryThing) { + qCDebug(dcHuawei()) << "Autoremove huawei energy storage 1 for" << thing << "because the battery is offline" << batteryThing; + emit autoThingDisappeared(batteryThing->id()); + } + }); + + connect(connection, &HuaweiFusionSolar::lunaBattery1PowerReadFinished, thing, [this, thing](qint32 lunaBattery1Power){ + qCDebug(dcHuawei()) << "Battery 1 power changed" << lunaBattery1Power << "W"; + Things batteryThings = myThings().filterByParentId(thing->id()).filterByThingClassId(huaweiBatteryThingClassId).filterByParam(huaweiBatteryThingUnitParamTypeId, 1); + if (!batteryThings.isEmpty()) { + batteryThings.first()->setStateValue(huaweiBatteryCurrentPowerStateTypeId, lunaBattery1Power); + if (lunaBattery1Power < 0) { + batteryThings.first()->setStateValue(huaweiBatteryChargingStateStateTypeId, "discharging"); + } else if (lunaBattery1Power > 0) { + batteryThings.first()->setStateValue(huaweiBatteryChargingStateStateTypeId, "charging"); + } else { + batteryThings.first()->setStateValue(huaweiBatteryChargingStateStateTypeId, "idle"); + } + } + }); + + connect(connection, &HuaweiFusionSolar::lunaBattery1SocReadFinished, thing, [this, thing](float lunaBattery1Soc){ + qCDebug(dcHuawei()) << "Battery 1 SOC changed" << lunaBattery1Soc << "%"; + Things batteryThings = myThings().filterByParentId(thing->id()).filterByThingClassId(huaweiBatteryThingClassId).filterByParam(huaweiBatteryThingUnitParamTypeId, 1); + if (!batteryThings.isEmpty()) { + batteryThings.first()->setStateValue(huaweiBatteryBatteryLevelStateTypeId, lunaBattery1Soc); + batteryThings.first()->setStateValue(huaweiBatteryBatteryCriticalStateTypeId, lunaBattery1Soc < 10); + } + }); + + // Battery 2 + connect(connection, &HuaweiFusionSolar::lunaBattery2StatusReadFinished, thing, [this, thing](HuaweiFusionSolar::BatteryDeviceStatus lunaBattery2Status){ + + qCDebug(dcHuawei()) << "Battery 2 status changed of" << thing << lunaBattery2Status; + Thing *batteryThing = nullptr; + foreach (Thing *bt, myThings().filterByParentId(thing->id()).filterByThingClassId(huaweiBatteryThingClassId)) { + if (bt->paramValue(huaweiBatteryThingUnitParamTypeId).toUInt() == 2) { + batteryThing = bt; + break; + } + } + + // Check if w have to create the energy storage + if (lunaBattery2Status != HuaweiFusionSolar::BatteryDeviceStatusOffline && !batteryThing) { + qCDebug(dcHuawei()) << "Set up huawei energy storage 2 for" << thing; + ThingDescriptor descriptor(huaweiBatteryThingClassId, "Luna 2000 Battery 2", QString(), thing->id()); + ParamList params; + params.append(Param(huaweiBatteryThingUnitParamTypeId, 2)); + descriptor.setParams(params); + emit autoThingsAppeared(ThingDescriptors() << descriptor); + } else if (lunaBattery2Status == HuaweiFusionSolar::BatteryDeviceStatusOffline && batteryThing) { + qCDebug(dcHuawei()) << "Autoremove huawei energy storage 2 for" << thing << "because the battery is offline" << batteryThing; + emit autoThingDisappeared(batteryThing->id()); + } + }); + + connect(connection, &HuaweiFusionSolar::lunaBattery2PowerReadFinished, thing, [this, thing](qint32 lunaBattery2Power){ + qCDebug(dcHuawei()) << "Battery 2 power changed" << lunaBattery2Power << "W"; + Things batteryThings = myThings().filterByParentId(thing->id()).filterByThingClassId(huaweiBatteryThingClassId).filterByParam(huaweiBatteryThingUnitParamTypeId, 2); + if (!batteryThings.isEmpty()) { + batteryThings.first()->setStateValue(huaweiBatteryCurrentPowerStateTypeId, lunaBattery2Power); + + if (lunaBattery2Power < 0) { + batteryThings.first()->setStateValue(huaweiBatteryChargingStateStateTypeId, "discharging"); + } else if (lunaBattery2Power > 0) { + batteryThings.first()->setStateValue(huaweiBatteryChargingStateStateTypeId, "charging"); + } else { + batteryThings.first()->setStateValue(huaweiBatteryChargingStateStateTypeId, "idle"); + } + } + }); + + connect(connection, &HuaweiFusionSolar::lunaBattery2SocReadFinished, thing, [this, thing](float lunaBattery2Soc){ + qCDebug(dcHuawei()) << "Battery 2 SOC changed" << lunaBattery2Soc << "%"; + Things batteryThings = myThings().filterByParentId(thing->id()).filterByThingClassId(huaweiBatteryThingClassId).filterByParam(huaweiBatteryThingUnitParamTypeId, 2); + if (!batteryThings.isEmpty()) { + batteryThings.first()->setStateValue(huaweiBatteryBatteryLevelStateTypeId, lunaBattery2Soc); + batteryThings.first()->setStateValue(huaweiBatteryBatteryCriticalStateTypeId, lunaBattery2Soc < 10); + } + }); + + connection->connectDevice(); } void IntegrationPluginHuawei::evaluateEnergyProducedValue(Thing *inverterThing, float energyProduced) diff --git a/huawei/integrationpluginhuawei.h b/huawei/integrationpluginhuawei.h index 5985dd4..f76081b 100644 --- a/huawei/integrationpluginhuawei.h +++ b/huawei/integrationpluginhuawei.h @@ -1,6 +1,6 @@ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * -* Copyright 2013 - 2022, nymea GmbH +* Copyright 2013 - 2024, nymea GmbH * Contact: contact@nymea.io * * This fileDescriptor is part of nymea. diff --git a/huawei/integrationpluginhuawei.json b/huawei/integrationpluginhuawei.json index b57e038..2bd2380 100644 --- a/huawei/integrationpluginhuawei.json +++ b/huawei/integrationpluginhuawei.json @@ -13,7 +13,7 @@ "displayName": "Huawei FusionSolar Inverter (SmartDongle)", "id": "87e75ee0-d544-457b-add3-bd4e58160fcd", "createMethods": ["discovery", "user"], - "interfaces": ["solarinverter", "connectable"], + "interfaces": ["solarinverter", "connectable", "networkdevice"], "providedInterfaces": [ "solarinverter", "energymeter", "energystorage"], "paramTypes": [ { @@ -24,6 +24,21 @@ "inputType": "MacAddress", "defaultValue": "" }, + { + "id": "55f90597-e3ba-4d7e-a33e-2e3b7dc7e095", + "name": "address", + "displayName": "Host address", + "type": "QString", + "inputType": "IPv4Address", + "defaultValue": "" + }, + { + "id": "00c3880d-3687-4955-8563-648684b02cbd", + "name": "hostName", + "displayName": "Host name", + "type": "QString", + "defaultValue": "" + }, { "id": "55c4ec99-6342-4309-84a8-d1615f19b2e8", "name":"port",