MTec: Update to networkdevice interface
This commit is contained in:
parent
36f9f3c512
commit
55f0d5598b
@ -29,6 +29,7 @@
|
|||||||
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
||||||
|
|
||||||
#include <network/networkdevicediscovery.h>
|
#include <network/networkdevicediscovery.h>
|
||||||
|
#include <network/networkdevicemonitor.h>
|
||||||
|
|
||||||
#include "integrationpluginmtec.h"
|
#include "integrationpluginmtec.h"
|
||||||
#include "plugininfo.h"
|
#include "plugininfo.h"
|
||||||
@ -55,30 +56,46 @@ void IntegrationPluginMTec::discoverThings(ThingDiscoveryInfo *info)
|
|||||||
qCDebug(dcMTec()) << "Found" << networkDeviceInfo;
|
qCDebug(dcMTec()) << "Found" << networkDeviceInfo;
|
||||||
|
|
||||||
QString title;
|
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()) {
|
if (networkDeviceInfo.hostName().isEmpty()) {
|
||||||
title = networkDeviceInfo.address().toString();
|
title = macInfo.macAddress().toString();
|
||||||
} else {
|
} else {
|
||||||
title = networkDeviceInfo.hostName() + " (" + networkDeviceInfo.address().toString() + ")";
|
title = networkDeviceInfo.hostName() + " (" + macInfo.macAddress().toString() + ")";
|
||||||
}
|
}
|
||||||
|
|
||||||
QString description;
|
break;
|
||||||
if (networkDeviceInfo.macAddressManufacturer().isEmpty()) {
|
case NetworkDeviceInfo::MonitorModeHostName:
|
||||||
description = networkDeviceInfo.macAddress();
|
title = networkDeviceInfo.hostName();
|
||||||
} else {
|
description = networkDeviceInfo.address().toString();
|
||||||
description = networkDeviceInfo.macAddress() + " (" + networkDeviceInfo.macAddressManufacturer() + ")";
|
break;
|
||||||
|
case NetworkDeviceInfo::MonitorModeIp:
|
||||||
|
title = "Network device " + networkDeviceInfo.address().toString();
|
||||||
|
description = "Interface: " + networkDeviceInfo.networkInterface().name();
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
ThingDescriptor descriptor(mtecThingClassId, title, description);
|
ThingDescriptor descriptor(mtecThingClassId, title, description);
|
||||||
|
|
||||||
ParamList params;
|
ParamList params;
|
||||||
params << Param(mtecThingIpAddressParamTypeId, networkDeviceInfo.address().toString());
|
params << Param(mtecThingMacAddressParamTypeId, networkDeviceInfo.thingParamValueMacAddress());
|
||||||
params << Param(mtecThingMacAddressParamTypeId, networkDeviceInfo.macAddress());
|
params << Param(mtecThingAddressParamTypeId, networkDeviceInfo.thingParamValueAddress());
|
||||||
|
params << Param(mtecThingHostNameParamTypeId, networkDeviceInfo.thingParamValueHostName());
|
||||||
descriptor.setParams(params);
|
descriptor.setParams(params);
|
||||||
|
|
||||||
// Check if we already have set up this device
|
// Check if we already have set up this device
|
||||||
Things existingThings = myThings().filterByParam(mtecThingMacAddressParamTypeId, networkDeviceInfo.macAddress());
|
Thing *existingThing = myThings().findByParams(params);
|
||||||
if (existingThings.count() == 1) {
|
if (existingThing) {
|
||||||
qCDebug(dcMTec()) << "This heat pump already exists in the system!" << networkDeviceInfo;
|
qCDebug(dcMTec()) << "This heat pump already exists in the system:" << networkDeviceInfo;
|
||||||
descriptor.setThingId(existingThings.first()->id());
|
descriptor.setThingId(existingThing->id());
|
||||||
}
|
}
|
||||||
|
|
||||||
info->addThingDescriptor(descriptor);
|
info->addThingDescriptor(descriptor);
|
||||||
@ -93,16 +110,44 @@ void IntegrationPluginMTec::setupThing(ThingSetupInfo *info)
|
|||||||
Thing *thing = info->thing();
|
Thing *thing = info->thing();
|
||||||
qCDebug(dcMTec()) << "Setup" << thing;
|
qCDebug(dcMTec()) << "Setup" << thing;
|
||||||
|
|
||||||
if (thing->thingClassId() == mtecThingClassId) {
|
NetworkDeviceMonitor *monitor = hardwareManager()->networkDeviceDiscovery()->registerMonitor(thing);
|
||||||
QHostAddress hostAddress = QHostAddress(thing->paramValue(mtecThingIpAddressParamTypeId).toString());
|
if (!monitor) {
|
||||||
if (hostAddress.isNull()) {
|
qCWarning(dcMTec()) << "Unable to register monitor with the given params" << thing->params();
|
||||||
info->finish(Thing::ThingErrorInvalidParameter, QT_TR_NOOP("No IP address given"));
|
info->finish(Thing::ThingErrorInvalidParameter, QT_TR_NOOP("Unable to set up the connection with this configuration, please reconfigure the connection."));
|
||||||
return;
|
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){
|
connect(mtec, &MTec::connectedChanged, thing, [=](bool connected){
|
||||||
qCDebug(dcMTec()) << thing << "Connected changed to" << connected;
|
qCDebug(dcMTec()) << thing << "Connected changed to" << connected;
|
||||||
thing->setStateValue(mtecConnectedStateTypeId, connected);
|
thing->setStateValue(mtecConnectedStateTypeId, connected);
|
||||||
@ -219,15 +264,11 @@ void IntegrationPluginMTec::setupThing(ThingSetupInfo *info)
|
|||||||
|
|
||||||
info->finish(Thing::ThingErrorNoError);
|
info->finish(Thing::ThingErrorNoError);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void IntegrationPluginMTec::postSetupThing(Thing *thing)
|
void IntegrationPluginMTec::postSetupThing(Thing *thing)
|
||||||
{
|
{
|
||||||
if (thing->thingClassId() == mtecThingClassId) {
|
if (m_mtecConnections.contains(thing))
|
||||||
MTec *mtec = m_mtecConnections.value(thing);
|
|
||||||
if (mtec) {
|
|
||||||
update(thing);
|
update(thing);
|
||||||
}
|
|
||||||
|
|
||||||
if (!m_pluginTimer) {
|
if (!m_pluginTimer) {
|
||||||
qCDebug(dcMTec()) << "Starting plugin timer...";
|
qCDebug(dcMTec()) << "Starting plugin timer...";
|
||||||
@ -239,7 +280,6 @@ void IntegrationPluginMTec::postSetupThing(Thing *thing)
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void IntegrationPluginMTec::thingRemoved(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()) {
|
if (myThings().isEmpty()) {
|
||||||
hardwareManager()->pluginTimerManager()->unregisterTimer(m_pluginTimer);
|
hardwareManager()->pluginTimerManager()->unregisterTimer(m_pluginTimer);
|
||||||
m_pluginTimer = nullptr;
|
m_pluginTimer = nullptr;
|
||||||
|
|||||||
@ -37,6 +37,8 @@
|
|||||||
|
|
||||||
#include <QUuid>
|
#include <QUuid>
|
||||||
|
|
||||||
|
class NetworkDeviceMonitor;
|
||||||
|
|
||||||
class IntegrationPluginMTec: public IntegrationPlugin
|
class IntegrationPluginMTec: public IntegrationPlugin
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
@ -57,6 +59,7 @@ public:
|
|||||||
private:
|
private:
|
||||||
PluginTimer *m_pluginTimer = nullptr;
|
PluginTimer *m_pluginTimer = nullptr;
|
||||||
QHash<Thing *, MTec *> m_mtecConnections;
|
QHash<Thing *, MTec *> m_mtecConnections;
|
||||||
|
QHash<Thing *, NetworkDeviceMonitor *> m_monitors;
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void update(Thing *thing);
|
void update(Thing *thing);
|
||||||
|
|||||||
@ -13,15 +13,23 @@
|
|||||||
"displayName": "MTec",
|
"displayName": "MTec",
|
||||||
"id": "451e38d8-50d5-4ae9-8d9f-21af9347128d",
|
"id": "451e38d8-50d5-4ae9-8d9f-21af9347128d",
|
||||||
"createMethods": ["discovery", "user"],
|
"createMethods": ["discovery", "user"],
|
||||||
"interfaces": ["thermostat", "connectable"],
|
"interfaces": ["thermostat", "connectable", "networkdevice"],
|
||||||
"paramTypes": [
|
"paramTypes": [
|
||||||
{
|
{
|
||||||
"id": "f1c43b1e-cffe-4d30-bda0-c23ed648dd71",
|
"id": "f1c43b1e-cffe-4d30-bda0-c23ed648dd71",
|
||||||
"name": "ipAddress",
|
"name": "address",
|
||||||
"displayName": "IP address",
|
"displayName": "IP address",
|
||||||
"type": "QString",
|
"type": "QString",
|
||||||
"inputType": "IPv4Address",
|
"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",
|
"id": "906f6099-d0e1-4297-a2b3-f8ec4482c578",
|
||||||
|
|||||||
@ -51,6 +51,11 @@ MTec::~MTec()
|
|||||||
m_modbusMaster->disconnectDevice();
|
m_modbusMaster->disconnectDevice();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ModbusTcpMaster *MTec::modbusTcpMaster() const
|
||||||
|
{
|
||||||
|
return m_modbusMaster;
|
||||||
|
}
|
||||||
|
|
||||||
QHostAddress MTec::hostAddress() const
|
QHostAddress MTec::hostAddress() const
|
||||||
{
|
{
|
||||||
return m_hostAddress;
|
return m_hostAddress;
|
||||||
|
|||||||
@ -55,7 +55,10 @@ public:
|
|||||||
explicit MTec(const QHostAddress &address, QObject *parent = nullptr);
|
explicit MTec(const QHostAddress &address, QObject *parent = nullptr);
|
||||||
~MTec();
|
~MTec();
|
||||||
|
|
||||||
|
ModbusTcpMaster *modbusTcpMaster() const;
|
||||||
|
|
||||||
QHostAddress hostAddress() const;
|
QHostAddress hostAddress() const;
|
||||||
|
|
||||||
bool connected() const;
|
bool connected() const;
|
||||||
|
|
||||||
QModbusReply *setTargetRoomTemperature(double targetRoomTemperature);
|
QModbusReply *setTargetRoomTemperature(double targetRoomTemperature);
|
||||||
|
|||||||
Reference in New Issue
Block a user