MTec: Update to networkdevice interface

This commit is contained in:
Simon Stürz 2024-12-17 15:54:58 +01:00
parent 36f9f3c512
commit 55f0d5598b
5 changed files with 220 additions and 158 deletions

View File

@ -29,6 +29,7 @@
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include <network/networkdevicediscovery.h>
#include <network/networkdevicemonitor.h>
#include "integrationpluginmtec.h"
#include "plugininfo.h"
@ -55,30 +56,46 @@ void IntegrationPluginMTec::discoverThings(ThingDiscoveryInfo *info)
qCDebug(dcMTec()) << "Found" << networkDeviceInfo;
QString title;
QString description;
MacAddressInfo macInfo;
switch (networkDeviceInfo.monitorMode()) {
case NetworkDeviceInfo::MonitorModeMac:
macInfo = networkDeviceInfo.macAddressInfos().constFirst();
description = networkDeviceInfo.address().toString();
if (!macInfo.vendorName().isEmpty())
description += " - " + networkDeviceInfo.macAddressInfos().constFirst().vendorName();
if (networkDeviceInfo.hostName().isEmpty()) {
title = networkDeviceInfo.address().toString();
title = macInfo.macAddress().toString();
} else {
title = networkDeviceInfo.hostName() + " (" + networkDeviceInfo.address().toString() + ")";
title = networkDeviceInfo.hostName() + " (" + macInfo.macAddress().toString() + ")";
}
QString description;
if (networkDeviceInfo.macAddressManufacturer().isEmpty()) {
description = networkDeviceInfo.macAddress();
} else {
description = networkDeviceInfo.macAddress() + " (" + networkDeviceInfo.macAddressManufacturer() + ")";
break;
case NetworkDeviceInfo::MonitorModeHostName:
title = networkDeviceInfo.hostName();
description = networkDeviceInfo.address().toString();
break;
case NetworkDeviceInfo::MonitorModeIp:
title = "Network device " + networkDeviceInfo.address().toString();
description = "Interface: " + networkDeviceInfo.networkInterface().name();
break;
}
ThingDescriptor descriptor(mtecThingClassId, title, description);
ParamList params;
params << Param(mtecThingIpAddressParamTypeId, networkDeviceInfo.address().toString());
params << Param(mtecThingMacAddressParamTypeId, networkDeviceInfo.macAddress());
params << Param(mtecThingMacAddressParamTypeId, networkDeviceInfo.thingParamValueMacAddress());
params << Param(mtecThingAddressParamTypeId, networkDeviceInfo.thingParamValueAddress());
params << Param(mtecThingHostNameParamTypeId, networkDeviceInfo.thingParamValueHostName());
descriptor.setParams(params);
// Check if we already have set up this device
Things existingThings = myThings().filterByParam(mtecThingMacAddressParamTypeId, networkDeviceInfo.macAddress());
if (existingThings.count() == 1) {
qCDebug(dcMTec()) << "This heat pump already exists in the system!" << networkDeviceInfo;
descriptor.setThingId(existingThings.first()->id());
Thing *existingThing = myThings().findByParams(params);
if (existingThing) {
qCDebug(dcMTec()) << "This heat pump already exists in the system:" << networkDeviceInfo;
descriptor.setThingId(existingThing->id());
}
info->addThingDescriptor(descriptor);
@ -93,16 +110,44 @@ void IntegrationPluginMTec::setupThing(ThingSetupInfo *info)
Thing *thing = info->thing();
qCDebug(dcMTec()) << "Setup" << thing;
if (thing->thingClassId() == mtecThingClassId) {
QHostAddress hostAddress = QHostAddress(thing->paramValue(mtecThingIpAddressParamTypeId).toString());
if (hostAddress.isNull()) {
info->finish(Thing::ThingErrorInvalidParameter, QT_TR_NOOP("No IP address given"));
NetworkDeviceMonitor *monitor = hardwareManager()->networkDeviceDiscovery()->registerMonitor(thing);
if (!monitor) {
qCWarning(dcMTec()) << "Unable to register monitor with the given params" << thing->params();
info->finish(Thing::ThingErrorInvalidParameter, QT_TR_NOOP("Unable to set up the connection with this configuration, please reconfigure the connection."));
return;
}
qCDebug(dcMTec()) << "Using ip address" << hostAddress.toString();
qCInfo(dcMTec()) << "Set up MTec modbus connection with" << monitor;
m_monitors.insert(thing, monitor);
connect(info, &ThingSetupInfo::aborted, monitor, [this, thing](){
if (m_monitors.contains(thing)) {
qCDebug(dcMTec()) << "Unregistering monitor because setup has been aborted.";
hardwareManager()->networkDeviceDiscovery()->unregisterMonitor(m_monitors.take(thing));
}
});
QHostAddress address = monitor->networkDeviceInfo().address();
if (address.isNull()) {
qCWarning(dcMTec()) << "Cannot set up thing. The host address is not known yet. Maybe it will be available in the next run...";
hardwareManager()->networkDeviceDiscovery()->unregisterMonitor(m_monitors.take(thing));
info->finish(Thing::ThingErrorHardwareFailure, QT_TR_NOOP("The host address is not known yet. Trying later again."));
return;
}
MTec *mtec = new MTec(address, this);
connect(monitor, &NetworkDeviceMonitor::reachableChanged, this, [=](bool reachable ){
qCDebug(dcMTec()) << "Monitor reachable changed to" << reachable << "for" << thing;
if (reachable && !thing->stateValue("connected").toBool()) {
mtec->modbusTcpMaster()->setHostAddress(monitor->networkDeviceInfo().address());
mtec->connectDevice();
} else if (!reachable) {
// Note: We disable autoreconnect explicitly and we will
// connect the device once the monitor says it is reachable again
mtec->disconnectDevice();
}
});
MTec *mtec = new MTec(hostAddress, this);
connect(mtec, &MTec::connectedChanged, thing, [=](bool connected){
qCDebug(dcMTec()) << thing << "Connected changed to" << connected;
thing->setStateValue(mtecConnectedStateTypeId, connected);
@ -218,16 +263,12 @@ void IntegrationPluginMTec::setupThing(ThingSetupInfo *info)
}
info->finish(Thing::ThingErrorNoError);
}
}
void IntegrationPluginMTec::postSetupThing(Thing *thing)
{
if (thing->thingClassId() == mtecThingClassId) {
MTec *mtec = m_mtecConnections.value(thing);
if (mtec) {
if (m_mtecConnections.contains(thing))
update(thing);
}
if (!m_pluginTimer) {
qCDebug(dcMTec()) << "Starting plugin timer...";
@ -238,7 +279,6 @@ void IntegrationPluginMTec::postSetupThing(Thing *thing)
}
});
}
}
}
void IntegrationPluginMTec::thingRemoved(Thing *thing)
@ -251,6 +291,9 @@ void IntegrationPluginMTec::thingRemoved(Thing *thing)
}
}
if (m_monitors.contains(thing))
hardwareManager()->networkDeviceDiscovery()->unregisterMonitor(m_monitors.take(thing));
if (myThings().isEmpty()) {
hardwareManager()->pluginTimerManager()->unregisterTimer(m_pluginTimer);
m_pluginTimer = nullptr;

View File

@ -37,6 +37,8 @@
#include <QUuid>
class NetworkDeviceMonitor;
class IntegrationPluginMTec: public IntegrationPlugin
{
Q_OBJECT
@ -57,6 +59,7 @@ public:
private:
PluginTimer *m_pluginTimer = nullptr;
QHash<Thing *, MTec *> m_mtecConnections;
QHash<Thing *, NetworkDeviceMonitor *> m_monitors;
private slots:
void update(Thing *thing);

View File

@ -13,15 +13,23 @@
"displayName": "MTec",
"id": "451e38d8-50d5-4ae9-8d9f-21af9347128d",
"createMethods": ["discovery", "user"],
"interfaces": ["thermostat", "connectable"],
"interfaces": ["thermostat", "connectable", "networkdevice"],
"paramTypes": [
{
"id": "f1c43b1e-cffe-4d30-bda0-c23ed648dd71",
"name": "ipAddress",
"name": "address",
"displayName": "IP address",
"type": "QString",
"inputType": "IPv4Address",
"defaultValue": "127.0.0.1"
"defaultValue": ""
},
{
"id": "544348c3-485d-4fba-bb9b-a64031241ac8",
"name": "hostName",
"displayName": "Host name",
"type": "QString",
"inputType": "TextLine",
"defaultValue": ""
},
{
"id": "906f6099-d0e1-4297-a2b3-f8ec4482c578",

View File

@ -51,6 +51,11 @@ MTec::~MTec()
m_modbusMaster->disconnectDevice();
}
ModbusTcpMaster *MTec::modbusTcpMaster() const
{
return m_modbusMaster;
}
QHostAddress MTec::hostAddress() const
{
return m_hostAddress;

View File

@ -55,7 +55,10 @@ public:
explicit MTec(const QHostAddress &address, QObject *parent = nullptr);
~MTec();
ModbusTcpMaster *modbusTcpMaster() const;
QHostAddress hostAddress() const;
bool connected() const;
QModbusReply *setTargetRoomTemperature(double targetRoomTemperature);