Update duplicated sma setup check

master
Simon Stürz 2021-09-02 11:32:15 +02:00
parent ae77bcef7c
commit 4ee7015117
3 changed files with 28 additions and 10 deletions

View File

@ -75,8 +75,8 @@ void IntegrationPluginSma::discoverThings(ThingDiscoveryInfo *info)
}
ParamList params;
params << Param(sunnyWebBoxThingMacAddressParamTypeId, networkDeviceInfo.macAddress());
params << Param(sunnyWebBoxThingHostParamTypeId, networkDeviceInfo.address().toString());
params << Param(sunnyWebBoxThingMacAddressParamTypeId, networkDeviceInfo.macAddress());
descriptor.setParams(params);
descriptors.append(descriptor);
}
@ -89,25 +89,29 @@ void IntegrationPluginSma::discoverThings(ThingDiscoveryInfo *info)
void IntegrationPluginSma::setupThing(ThingSetupInfo *info)
{
Thing *thing = info->thing();
qCDebug(dcSma()) << "Setup thing" << thing->name();
qCDebug(dcSma()) << "Setup thing" << thing << thing->params();
if (thing->thingClassId() == sunnyWebBoxThingClassId) {
//check if a Sunny WebBox is already added with this IPv4Address
foreach(SunnyWebBox *sunnyWebBox, m_sunnyWebBoxes.values()) {
if(sunnyWebBox->hostAddress().toString() == thing->paramValue(sunnyWebBoxThingHostParamTypeId).toString()){
//this logger at this IPv4 address is already added
qCWarning(dcSma()) << "thing at " << thing->paramValue(sunnyWebBoxThingHostParamTypeId).toString() << " already added!";
// Check if a Sunny WebBox is already added with this mac address
foreach (SunnyWebBox *sunnyWebBox, m_sunnyWebBoxes.values()) {
if (sunnyWebBox->macAddress() == thing->paramValue(sunnyWebBoxThingMacAddressParamTypeId).toString()){
qCWarning(dcSma()) << "Thing with mac address" << thing->paramValue(sunnyWebBoxThingMacAddressParamTypeId).toString() << " already added!";
info->finish(Thing::ThingErrorThingInUse);
return;
}
}
if (m_sunnyWebBoxes.contains(thing)) {
qCDebug(dcSma()) << "Setup after reconfiguration, cleaning up...";
m_sunnyWebBoxes.take(thing)->deleteLater();
}
SunnyWebBox *sunnyWebBox = new SunnyWebBox(hardwareManager()->networkManager(), QHostAddress(thing->paramValue(sunnyWebBoxThingHostParamTypeId).toString()), this);
sunnyWebBox->setMacAddress(thing->paramValue(sunnyWebBoxThingMacAddressParamTypeId).toString());
connect(info, &ThingSetupInfo::aborted, sunnyWebBox, &SunnyWebBox::deleteLater);
connect(sunnyWebBox, &SunnyWebBox::destroyed, this, [thing, this] { m_sunnyWebBoxes.remove(thing);});
QString requestId = sunnyWebBox->getPlantOverview();
connect(sunnyWebBox, &SunnyWebBox::plantOverviewReceived, info, [=] (const QString &messageId, SunnyWebBox::Overview overview) {
qCDebug(dcSma()) << "Received plant overview" << messageId << "Finish setup";

View File

@ -121,15 +121,25 @@ QString SunnyWebBox::setParameters(const QString &deviceKey, const QHash<QString
return sendMessage(m_hostAddresss, "SetParameter", paramsObj);
}
QHostAddress SunnyWebBox::hostAddress() const
{
return m_hostAddresss;
}
void SunnyWebBox::setHostAddress(const QHostAddress &address)
{
qCDebug(dcSma()) << "SunnyWebBox: Setting host address to" << address.toString();
m_hostAddresss = address;
}
QHostAddress SunnyWebBox::hostAddress()
QString SunnyWebBox::macAddress() const
{
return m_hostAddresss;
return m_macAddress;
}
void SunnyWebBox::setMacAddress(const QString &macAddress)
{
m_macAddress = macAddress;
}
void SunnyWebBox::parseMessage(const QString &messageId, const QString &messageType, const QVariantMap &result)

View File

@ -84,12 +84,16 @@ public:
QString getParameters(const QStringList &deviceKeys); //Returns the parameter values of up to 5 devices
QString setParameters(const QString &deviceKeys, const QHash<QString, QVariant> &channels); //Sets parameter values
QHostAddress hostAddress() const;
void setHostAddress(const QHostAddress &address);
QHostAddress hostAddress();
QString macAddress() const;
void setMacAddress(const QString &macAddress);
private:
bool m_connected = false;
QHostAddress m_hostAddresss;
QString m_macAddress;
NetworkAccessManager *m_networkManager = nullptr;
QString sendMessage(const QHostAddress &address, const QString &procedure);