|
|
|
|
@ -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)
|
|
|
|
|
|