-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge PR #197: PhoenixConnect: Update to networkdevice interface
- Loading branch information
Showing
5 changed files
with
157 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * | ||
* | ||
* Copyright 2013 - 2020, nymea GmbH | ||
* Copyright 2013 - 2024, nymea GmbH | ||
* Contact: [email protected] | ||
* | ||
* This file is part of nymea. | ||
|
@@ -66,16 +66,24 @@ void IntegrationPluginPhoenixConnect::discoverThings(ThingDiscoveryInfo *info) | |
ThingDescriptor descriptor(info->thingClassId(), name, description); | ||
qCDebug(dcPhoenixConnect()) << "Discovered:" << descriptor.title() << descriptor.description(); | ||
|
||
ParamTypeId macParamTypeId = supportedThings().findById(info->thingClassId()).paramTypes().findByName("mac").id(); | ||
Things existingThings = myThings().filterByParam(macParamTypeId, result.networkDeviceInfo.macAddress()); | ||
if (existingThings.count() == 1) { | ||
qCDebug(dcPhoenixConnect()) << "This wallbox already exists in the system:" << result.networkDeviceInfo; | ||
descriptor.setThingId(existingThings.first()->id()); | ||
} | ||
ParamTypeId macAddressParamTypeId = supportedThings().findById(info->thingClassId()).paramTypes().findByName("macAddress").id(); | ||
ParamTypeId hostNameParamTypeId = supportedThings().findById(info->thingClassId()).paramTypes().findByName("hostName").id(); | ||
ParamTypeId addressParamTypeId = supportedThings().findById(info->thingClassId()).paramTypes().findByName("address").id(); | ||
|
||
ParamList params; | ||
params << Param(macParamTypeId, result.networkDeviceInfo.macAddress()); | ||
params << Param(macAddressParamTypeId, result.networkDeviceInfo.thingParamValueMacAddress()); | ||
params << Param(hostNameParamTypeId, result.networkDeviceInfo.thingParamValueHostName()); | ||
params << Param(addressParamTypeId, result.networkDeviceInfo.thingParamValueAddress()); | ||
descriptor.setParams(params); | ||
|
||
// Check if we already have set up this device | ||
// FIXME: maybe we should save the serialnumber as parameter in order to identify already known devices | ||
Thing *existingThing = myThings().findByParams(params); | ||
if (existingThing) { | ||
qCDebug(dcPhoenixConnect()) << "This wallbox already exists in the system:" << result.networkDeviceInfo; | ||
descriptor.setThingId(existingThing->id()); | ||
} | ||
|
||
info->addThingDescriptor(descriptor); | ||
} | ||
|
||
|
@@ -96,13 +104,12 @@ void IntegrationPluginPhoenixConnect::setupThing(ThingSetupInfo *info) | |
qCDebug(dcPhoenixConnect()) << "Setting up a new device:" << thing->params(); | ||
} | ||
|
||
|
||
MacAddress mac = MacAddress(thing->paramValue("mac").toString()); | ||
if (!mac.isValid()) { | ||
info->finish(Thing::ThingErrorInvalidParameter, QT_TR_NOOP("The given MAC address is not valid.")); | ||
NetworkDeviceMonitor *monitor = hardwareManager()->networkDeviceDiscovery()->registerMonitor(thing); | ||
if (!monitor) { | ||
qCWarning(dcPhoenixConnect()) << "Unable to create monitor with the given parameters" << thing->params(); | ||
info->finish(Thing::ThingErrorInvalidParameter); | ||
return; | ||
} | ||
NetworkDeviceMonitor *monitor = hardwareManager()->networkDeviceDiscovery()->registerMonitor(mac); | ||
|
||
PhoenixModbusTcpConnection *connection = new PhoenixModbusTcpConnection(monitor->networkDeviceInfo().address(), 502, 255, this); | ||
connect(info, &ThingSetupInfo::aborted, connection, &PhoenixModbusTcpConnection::deleteLater); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * | ||
* | ||
* Copyright 2013 - 2022, nymea GmbH | ||
* Copyright 2013 - 2024, nymea GmbH | ||
* Contact: [email protected] | ||
* | ||
* This file is part of nymea. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * | ||
* | ||
* Copyright 2013 - 2022, nymea GmbH | ||
* Copyright 2013 - 2024, nymea GmbH | ||
* Contact: [email protected] | ||
* | ||
* This file is part of nymea. | ||
|
@@ -46,14 +46,15 @@ PhoenixDiscovery::PhoenixDiscovery(NetworkDeviceDiscovery *networkDeviceDiscover | |
void PhoenixDiscovery::startDiscovery() | ||
{ | ||
qCInfo(dcPhoenixConnect()) << "Discovery: Searching for PhoenixConnect wallboxes in the network..."; | ||
NetworkDeviceDiscoveryReply *discoveryReply = m_networkDeviceDiscovery->discover(); | ||
|
||
connect(discoveryReply, &NetworkDeviceDiscoveryReply::networkDeviceInfoAdded, this, &PhoenixDiscovery::checkNetworkDevice); | ||
m_startDateTime = QDateTime::currentDateTime(); | ||
|
||
NetworkDeviceDiscoveryReply *discoveryReply = m_networkDeviceDiscovery->discover(); | ||
connect(discoveryReply, &NetworkDeviceDiscoveryReply::hostAddressDiscovered, this, &PhoenixDiscovery::checkNetworkDevice); | ||
connect(discoveryReply, &NetworkDeviceDiscoveryReply::finished, this, [=](){ | ||
qCDebug(dcPhoenixConnect()) << "Discovery: Network discovery finished. Found" << discoveryReply->networkDeviceInfos().count() << "network devices"; | ||
m_gracePeriodTimer.start(); | ||
m_networkDeviceInfos = discoveryReply->networkDeviceInfos(); | ||
discoveryReply->deleteLater(); | ||
m_gracePeriodTimer.start(); | ||
}); | ||
} | ||
|
||
|
@@ -62,17 +63,14 @@ QList<PhoenixDiscovery::Result> PhoenixDiscovery::discoveryResults() const | |
return m_discoveryResults; | ||
} | ||
|
||
void PhoenixDiscovery::checkNetworkDevice(const NetworkDeviceInfo &networkDeviceInfo) | ||
void PhoenixDiscovery::checkNetworkDevice(const QHostAddress &address) | ||
{ | ||
// if (networkDeviceInfo.macAddressManufacturer() != "wallbe GmbH" && networkDeviceInfo.macAddressManufacturer() != "Phoenix") { | ||
// return; | ||
// } | ||
|
||
int port = 502; | ||
int slaveId = 0xff; | ||
qCDebug(dcPhoenixConnect()) << "Checking network device:" << networkDeviceInfo << "Port:" << port << "Slave ID:" << slaveId; | ||
|
||
PhoenixModbusTcpConnection *connection = new PhoenixModbusTcpConnection(networkDeviceInfo.address(), port, slaveId, this); | ||
qCDebug(dcPhoenixConnect()) << "Discovery: Checking network device:" << address << "Port:" << port << "Slave ID:" << slaveId; | ||
|
||
PhoenixModbusTcpConnection *connection = new PhoenixModbusTcpConnection(address, port, slaveId, this); | ||
m_connections.append(connection); | ||
|
||
connect(connection, &PhoenixModbusTcpConnection::reachableChanged, this, [=](bool reachable){ | ||
|
@@ -83,15 +81,15 @@ void PhoenixDiscovery::checkNetworkDevice(const NetworkDeviceInfo &networkDevice | |
|
||
connect(connection, &PhoenixModbusTcpConnection::initializationFinished, this, [=](bool success){ | ||
if (!success) { | ||
qCDebug(dcPhoenixConnect()) << "Discovery: Initialization failed on" << networkDeviceInfo.address().toString(); | ||
qCDebug(dcPhoenixConnect()) << "Discovery: Initialization failed on" << address.toString(); | ||
cleanupConnection(connection); | ||
return; | ||
} | ||
Result result; | ||
result.firmwareVersion = connection->firmwareVersion(); | ||
result.model = connection->deviceName(); | ||
result.serialNumber = connection->serial(); | ||
result.networkDeviceInfo = networkDeviceInfo; | ||
result.address = address; | ||
m_discoveryResults.append(result); | ||
|
||
qCDebug(dcPhoenixConnect()) << "Discovery: Found wallbox with firmware version:" << result.firmwareVersion << result.networkDeviceInfo; | ||
|
@@ -100,13 +98,13 @@ void PhoenixDiscovery::checkNetworkDevice(const NetworkDeviceInfo &networkDevice | |
}); | ||
|
||
if (!connection->initialize()) { | ||
qCDebug(dcPhoenixConnect()) << "Discovery: Unable to initialize connection on" << networkDeviceInfo.address().toString(); | ||
qCDebug(dcPhoenixConnect()) << "Discovery: Unable to initialize connection on" << address.toString(); | ||
cleanupConnection(connection); | ||
} | ||
}); | ||
|
||
connect(connection, &PhoenixModbusTcpConnection::checkReachabilityFailed, this, [=](){ | ||
qCDebug(dcPhoenixConnect()) << "Discovery: Checking reachability failed on" << networkDeviceInfo.address().toString(); | ||
qCDebug(dcPhoenixConnect()) << "Discovery: Checking reachability failed on" << address.toString(); | ||
cleanupConnection(connection); | ||
}); | ||
|
||
|
@@ -124,13 +122,16 @@ void PhoenixDiscovery::finishDiscovery() | |
{ | ||
qint64 durationMilliSeconds = QDateTime::currentMSecsSinceEpoch() - m_startDateTime.toMSecsSinceEpoch(); | ||
|
||
// Fill in all network device infos we have | ||
for (int i = 0; i < m_discoveryResults.count(); i++) | ||
m_discoveryResults[i].networkDeviceInfo = m_networkDeviceInfos.get(m_discoveryResults.at(i).address); | ||
|
||
// Cleanup any leftovers...we don't care any more | ||
foreach (PhoenixModbusTcpConnection *connection, m_connections) | ||
cleanupConnection(connection); | ||
|
||
qCInfo(dcPhoenixConnect()) << "Discovery: Finished the discovery process. Found" << m_discoveryResults.count() | ||
<< "Phoenix connect wallboxes in" << QTime::fromMSecsSinceStartOfDay(durationMilliSeconds).toString("mm:ss.zzz"); | ||
m_gracePeriodTimer.stop(); | ||
|
||
emit discoveryFinished(); | ||
} |
Oops, something went wrong.