diff --git a/sma/integrationpluginsma.cpp b/sma/integrationpluginsma.cpp index 6dfa1835..6419d7f7 100644 --- a/sma/integrationpluginsma.cpp +++ b/sma/integrationpluginsma.cpp @@ -40,49 +40,51 @@ IntegrationPluginSma::IntegrationPluginSma() void IntegrationPluginSma::discoverThings(ThingDiscoveryInfo *info) { - if (!hardwareManager()->networkDeviceDiscovery()->available()) { - qCWarning(dcSma()) << "Failed to discover network devices. The network device discovery is not available."; - info->finish(Thing::ThingErrorHardwareNotAvailable, QT_TR_NOOP("Unable to discover devices in your network.")); - return; - } - - qCDebug(dcSma()) << "Starting network discovery..."; - NetworkDeviceDiscoveryReply *discoveryReply = hardwareManager()->networkDeviceDiscovery()->discover(); - connect(discoveryReply, &NetworkDeviceDiscoveryReply::finished, this, [=](){ - ThingDescriptors descriptors; - qCDebug(dcSma()) << "Discovery finished. Found" << discoveryReply->networkDeviceInfos().count() << "devices"; - foreach (const NetworkDeviceInfo &networkDeviceInfo, discoveryReply->networkDeviceInfos()) { - // Filter for sma hosts - if (!networkDeviceInfo.hostName().toLower().contains("sma")) - continue; - - QString title = networkDeviceInfo.hostName() + " (" + networkDeviceInfo.address().toString() + ")"; - QString description; - if (networkDeviceInfo.macAddressManufacturer().isEmpty()) { - description = networkDeviceInfo.macAddress(); - } else { - description = networkDeviceInfo.macAddress() + " (" + networkDeviceInfo.macAddressManufacturer() + ")"; - } - - ThingDescriptor descriptor(sunnyWebBoxThingClassId, title, description); - - // Check for reconfiguration - foreach (Thing *existingThing, myThings()) { - if (existingThing->paramValue(sunnyWebBoxThingMacAddressParamTypeId).toString() == networkDeviceInfo.macAddress()) { - descriptor.setThingId(existingThing->id()); - break; - } - } - - ParamList params; - params << Param(sunnyWebBoxThingHostParamTypeId, networkDeviceInfo.address().toString()); - params << Param(sunnyWebBoxThingMacAddressParamTypeId, networkDeviceInfo.macAddress()); - descriptor.setParams(params); - descriptors.append(descriptor); + if (info->thingClassId() == sunnyWebBoxThingClassId) { + if (!hardwareManager()->networkDeviceDiscovery()->available()) { + qCWarning(dcSma()) << "Failed to discover network devices. The network device discovery is not available."; + info->finish(Thing::ThingErrorHardwareNotAvailable, QT_TR_NOOP("Unable to discover devices in your network.")); + return; } - info->addThingDescriptors(descriptors); - info->finish(Thing::ThingErrorNoError); - }); + + qCDebug(dcSma()) << "Starting network discovery..."; + NetworkDeviceDiscoveryReply *discoveryReply = hardwareManager()->networkDeviceDiscovery()->discover(); + connect(discoveryReply, &NetworkDeviceDiscoveryReply::finished, this, [=](){ + ThingDescriptors descriptors; + qCDebug(dcSma()) << "Discovery finished. Found" << discoveryReply->networkDeviceInfos().count() << "devices"; + foreach (const NetworkDeviceInfo &networkDeviceInfo, discoveryReply->networkDeviceInfos()) { + // Filter for sma hosts + if (!networkDeviceInfo.hostName().toLower().contains("sma")) + continue; + + QString title = networkDeviceInfo.hostName() + " (" + networkDeviceInfo.address().toString() + ")"; + QString description; + if (networkDeviceInfo.macAddressManufacturer().isEmpty()) { + description = networkDeviceInfo.macAddress(); + } else { + description = networkDeviceInfo.macAddress() + " (" + networkDeviceInfo.macAddressManufacturer() + ")"; + } + + ThingDescriptor descriptor(sunnyWebBoxThingClassId, title, description); + + // Check for reconfiguration + foreach (Thing *existingThing, myThings()) { + if (existingThing->paramValue(sunnyWebBoxThingMacAddressParamTypeId).toString() == networkDeviceInfo.macAddress()) { + descriptor.setThingId(existingThing->id()); + break; + } + } + + ParamList params; + params << Param(sunnyWebBoxThingHostParamTypeId, networkDeviceInfo.address().toString()); + params << Param(sunnyWebBoxThingMacAddressParamTypeId, networkDeviceInfo.macAddress()); + descriptor.setParams(params); + descriptors.append(descriptor); + } + info->addThingDescriptors(descriptors); + info->finish(Thing::ThingErrorNoError); + }); + } } @@ -160,7 +162,7 @@ void IntegrationPluginSma::thingRemoved(Thing *thing) void IntegrationPluginSma::onRefreshTimer() { - Q_FOREACH(Thing *thing, myThings().filterByThingClassId(sunnyWebBoxThingClassId)) { + foreach (Thing *thing, myThings().filterByThingClassId(sunnyWebBoxThingClassId)) { SunnyWebBox *sunnyWebBox = m_sunnyWebBoxes.value(thing); sunnyWebBox->getPlantOverview(); } diff --git a/sma/integrationpluginsma.json b/sma/integrationpluginsma.json index 0a542fe4..5953b4f4 100644 --- a/sma/integrationpluginsma.json +++ b/sma/integrationpluginsma.json @@ -6,7 +6,7 @@ { "id": "16d5a4a3-36d5-46c0-b7dd-df166ddf5981", "name": "Sma", - "displayName": "SMA", + "displayName": "SMA Solar Technology AG", "thingClasses": [ { "id": "49304127-ce9b-45dd-8511-05030a4ac003", diff --git a/sma/sunnywebbox.cpp b/sma/sunnywebbox.cpp index f2b2c56a..dd1a6e07 100644 --- a/sma/sunnywebbox.cpp +++ b/sma/sunnywebbox.cpp @@ -69,7 +69,7 @@ QString SunnyWebBox::getProcessData(const QStringList &deviceKeys) { QJsonObject paramsObj; QJsonArray devicesArray; - Q_FOREACH(QString key, deviceKeys) { + foreach (const QString &key, deviceKeys) { QJsonObject deviceObj; deviceObj["key"] = key; devicesArray.append(deviceObj); @@ -93,7 +93,7 @@ QString SunnyWebBox::getParameters(const QStringList &deviceKeys) { QJsonObject paramsObj; QJsonArray devicesArray; - Q_FOREACH(QString key, deviceKeys) { + foreach (const QString &key, deviceKeys) { QJsonObject deviceObj; deviceObj["key"] = key; devicesArray.append(deviceObj); @@ -109,7 +109,7 @@ QString SunnyWebBox::setParameters(const QString &deviceKey, const QHash devices; QVariantList deviceList = result.value("devices").toList(); qCDebug(dcSma()) << "SunnyWebBox: GetDevices" << result.value("totalDevicesReturned").toInt(); - Q_FOREACH(QVariant value, deviceList) { + foreach (const QVariant &value, deviceList) { Device device; QVariantMap map = value.toMap(); device.name = map["name"].toString(); @@ -185,7 +185,7 @@ void SunnyWebBox::parseMessage(const QString &messageId, const QString &messageT device.key = map["key"].toString(); qCDebug(dcSma()) << "SunnyWebBox: - Key" << device.key; QVariantList childrenList = map["children"].toList(); - Q_FOREACH(QVariant childValue, childrenList) { + foreach (const QVariant &childValue, childrenList) { Device child; QVariantMap childMap = childValue.toMap(); device.name = childMap["name"].toString(); @@ -194,44 +194,43 @@ void SunnyWebBox::parseMessage(const QString &messageId, const QString &messageT } devices.append(device); } - if (!devices.isEmpty()) + if (!devices.isEmpty()) { emit devicesReceived(messageId, devices); + } } else if (messageType == "GetProcessDataChannels" || messageType == "GetProDataChannels") { - Q_FOREACH(QString deviceKey, result.keys()) { + foreach (const QString &deviceKey, result.keys()) { QStringList processDataChannels = result.value(deviceKey).toStringList(); if (!processDataChannels.isEmpty()) emit processDataChannelsReceived(messageId, deviceKey, processDataChannels); } } else if (messageType == "GetProcessData") { - QList devices; QVariantList devicesList = result.value("devices").toList(); qCDebug(dcSma()) << "SunnyWebBox: GetProcessData response received"; - Q_FOREACH(QVariant value, devicesList) { + foreach (const QVariant &value, devicesList) { QString key = value.toMap().value("key").toString(); QVariantList channelsList = value.toMap().value("channels").toList(); QHash channels; - Q_FOREACH(QVariant channel, channelsList) { + foreach (const QVariant &channel, channelsList) { channels.insert(channel.toMap().value("meta").toString(), channel.toMap().value("value")); } emit processDataReceived(messageId, key, channels); } } else if (messageType == "GetParameterChannels") { - Q_FOREACH(QString deviceKey, result.keys()) { + foreach (const QString &deviceKey, result.keys()) { QStringList parameterChannels = result.value(deviceKey).toStringList(); if (!parameterChannels.isEmpty()) emit parameterChannelsReceived(messageId, deviceKey, parameterChannels); } } else if (messageType == "GetParameter"|| messageType == "SetParameter") { - QList devices; QVariantList devicesList = result.value("devices").toList(); - Q_FOREACH(QVariant value, devicesList) { + foreach (const QVariant &value, devicesList) { QString key = value.toMap().value("key").toString(); QVariantList channelsList = value.toMap().value("channels").toList(); QList parameters; - Q_FOREACH(QVariant channel, channelsList) { + foreach (const QVariant &channel, channelsList) { Parameter parameter; parameter.meta = channel.toMap().value("meta").toString(); parameter.name = channel.toMap().value("name").toString();