Merge PR #190: Huawei: Update to networkdevice interface

sunspec-fix-storage-charging-enabled-action
jenkins 2025-03-30 20:37:14 +02:00
commit e310d72b29
5 changed files with 279 additions and 257 deletions

View File

@ -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<HuaweiFusionSolar *> 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);

View File

@ -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<HuaweiFusionSolar *> m_connections;
QList<Result> 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

View File

@ -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,28 +458,16 @@ 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();
connect(info, &ThingSetupInfo::aborted, connection, [this, connection, thing](){
connection->deleteLater();
info->finish(Thing::ThingErrorHardwareFailure, QT_TR_NOOP("Could not initialize the communication with the SmartDongle."));
return;
}
m_connections.remove(thing);
});
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();
@ -656,9 +658,7 @@ void IntegrationPluginHuawei::setupFusionSolar(ThingSetupInfo *info)
batteryThings.first()->setStateValue(huaweiBatteryBatteryCriticalStateTypeId, lunaBattery2Soc < 10);
}
});
});
if (monitor->reachable())
connection->connectDevice();
}

View File

@ -1,6 +1,6 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* Copyright 2013 - 2022, nymea GmbH
* Copyright 2013 - 2024, nymea GmbH
* Contact: contact@nymea.io
*
* This fileDescriptor is part of nymea.

View File

@ -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",