Clean up code and set propper vendor name

This commit is contained in:
Simon Stürz 2021-12-03 13:15:52 +01:00
parent e20d8e8e86
commit 2b3a44cd6c
3 changed files with 60 additions and 59 deletions

View File

@ -40,49 +40,51 @@ IntegrationPluginSma::IntegrationPluginSma()
void IntegrationPluginSma::discoverThings(ThingDiscoveryInfo *info) void IntegrationPluginSma::discoverThings(ThingDiscoveryInfo *info)
{ {
if (!hardwareManager()->networkDeviceDiscovery()->available()) { if (info->thingClassId() == sunnyWebBoxThingClassId) {
qCWarning(dcSma()) << "Failed to discover network devices. The network device discovery is not available."; if (!hardwareManager()->networkDeviceDiscovery()->available()) {
info->finish(Thing::ThingErrorHardwareNotAvailable, QT_TR_NOOP("Unable to discover devices in your network.")); qCWarning(dcSma()) << "Failed to discover network devices. The network device discovery is not available.";
return; 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);
} }
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() void IntegrationPluginSma::onRefreshTimer()
{ {
Q_FOREACH(Thing *thing, myThings().filterByThingClassId(sunnyWebBoxThingClassId)) { foreach (Thing *thing, myThings().filterByThingClassId(sunnyWebBoxThingClassId)) {
SunnyWebBox *sunnyWebBox = m_sunnyWebBoxes.value(thing); SunnyWebBox *sunnyWebBox = m_sunnyWebBoxes.value(thing);
sunnyWebBox->getPlantOverview(); sunnyWebBox->getPlantOverview();
} }

View File

@ -6,7 +6,7 @@
{ {
"id": "16d5a4a3-36d5-46c0-b7dd-df166ddf5981", "id": "16d5a4a3-36d5-46c0-b7dd-df166ddf5981",
"name": "Sma", "name": "Sma",
"displayName": "SMA", "displayName": "SMA Solar Technology AG",
"thingClasses": [ "thingClasses": [
{ {
"id": "49304127-ce9b-45dd-8511-05030a4ac003", "id": "49304127-ce9b-45dd-8511-05030a4ac003",

View File

@ -69,7 +69,7 @@ QString SunnyWebBox::getProcessData(const QStringList &deviceKeys)
{ {
QJsonObject paramsObj; QJsonObject paramsObj;
QJsonArray devicesArray; QJsonArray devicesArray;
Q_FOREACH(QString key, deviceKeys) { foreach (const QString &key, deviceKeys) {
QJsonObject deviceObj; QJsonObject deviceObj;
deviceObj["key"] = key; deviceObj["key"] = key;
devicesArray.append(deviceObj); devicesArray.append(deviceObj);
@ -93,7 +93,7 @@ QString SunnyWebBox::getParameters(const QStringList &deviceKeys)
{ {
QJsonObject paramsObj; QJsonObject paramsObj;
QJsonArray devicesArray; QJsonArray devicesArray;
Q_FOREACH(QString key, deviceKeys) { foreach (const QString &key, deviceKeys) {
QJsonObject deviceObj; QJsonObject deviceObj;
deviceObj["key"] = key; deviceObj["key"] = key;
devicesArray.append(deviceObj); devicesArray.append(deviceObj);
@ -109,7 +109,7 @@ QString SunnyWebBox::setParameters(const QString &deviceKey, const QHash<QString
QJsonObject deviceObj; QJsonObject deviceObj;
deviceObj["key"] = deviceKey; deviceObj["key"] = deviceKey;
QJsonArray channelsArray; QJsonArray channelsArray;
Q_FOREACH(QString key, channels.keys()) { foreach (const QString &key, channels.keys()) {
QJsonObject channelObj; QJsonObject channelObj;
channelObj["meta"] = key; channelObj["meta"] = key;
channelObj["value"] = channels.value(key).toString(); channelObj["value"] = channels.value(key).toString();
@ -148,7 +148,7 @@ void SunnyWebBox::parseMessage(const QString &messageId, const QString &messageT
Overview overview; Overview overview;
QVariantList overviewList = result.value("overview").toList(); QVariantList overviewList = result.value("overview").toList();
qCDebug(dcSma()) << "SunnyWebBox: GetPlantOverview"; qCDebug(dcSma()) << "SunnyWebBox: GetPlantOverview";
Q_FOREACH(QVariant value, overviewList) { foreach (const QVariant &value, overviewList) {
QVariantMap map = value.toMap(); QVariantMap map = value.toMap();
if (map["meta"].toString() == "GriPwr") { if (map["meta"].toString() == "GriPwr") {
@ -177,7 +177,7 @@ void SunnyWebBox::parseMessage(const QString &messageId, const QString &messageT
QList<Device> devices; QList<Device> devices;
QVariantList deviceList = result.value("devices").toList(); QVariantList deviceList = result.value("devices").toList();
qCDebug(dcSma()) << "SunnyWebBox: GetDevices" << result.value("totalDevicesReturned").toInt(); qCDebug(dcSma()) << "SunnyWebBox: GetDevices" << result.value("totalDevicesReturned").toInt();
Q_FOREACH(QVariant value, deviceList) { foreach (const QVariant &value, deviceList) {
Device device; Device device;
QVariantMap map = value.toMap(); QVariantMap map = value.toMap();
device.name = map["name"].toString(); device.name = map["name"].toString();
@ -185,7 +185,7 @@ void SunnyWebBox::parseMessage(const QString &messageId, const QString &messageT
device.key = map["key"].toString(); device.key = map["key"].toString();
qCDebug(dcSma()) << "SunnyWebBox: - Key" << device.key; qCDebug(dcSma()) << "SunnyWebBox: - Key" << device.key;
QVariantList childrenList = map["children"].toList(); QVariantList childrenList = map["children"].toList();
Q_FOREACH(QVariant childValue, childrenList) { foreach (const QVariant &childValue, childrenList) {
Device child; Device child;
QVariantMap childMap = childValue.toMap(); QVariantMap childMap = childValue.toMap();
device.name = childMap["name"].toString(); device.name = childMap["name"].toString();
@ -194,44 +194,43 @@ void SunnyWebBox::parseMessage(const QString &messageId, const QString &messageT
} }
devices.append(device); devices.append(device);
} }
if (!devices.isEmpty()) if (!devices.isEmpty()) {
emit devicesReceived(messageId, devices); emit devicesReceived(messageId, devices);
}
} else if (messageType == "GetProcessDataChannels" || } else if (messageType == "GetProcessDataChannels" ||
messageType == "GetProDataChannels") { messageType == "GetProDataChannels") {
Q_FOREACH(QString deviceKey, result.keys()) { foreach (const QString &deviceKey, result.keys()) {
QStringList processDataChannels = result.value(deviceKey).toStringList(); QStringList processDataChannels = result.value(deviceKey).toStringList();
if (!processDataChannels.isEmpty()) if (!processDataChannels.isEmpty())
emit processDataChannelsReceived(messageId, deviceKey, processDataChannels); emit processDataChannelsReceived(messageId, deviceKey, processDataChannels);
} }
} else if (messageType == "GetProcessData") { } else if (messageType == "GetProcessData") {
QList<Device> devices;
QVariantList devicesList = result.value("devices").toList(); QVariantList devicesList = result.value("devices").toList();
qCDebug(dcSma()) << "SunnyWebBox: GetProcessData response received"; qCDebug(dcSma()) << "SunnyWebBox: GetProcessData response received";
Q_FOREACH(QVariant value, devicesList) { foreach (const QVariant &value, devicesList) {
QString key = value.toMap().value("key").toString(); QString key = value.toMap().value("key").toString();
QVariantList channelsList = value.toMap().value("channels").toList(); QVariantList channelsList = value.toMap().value("channels").toList();
QHash<QString, QVariant> channels; QHash<QString, QVariant> channels;
Q_FOREACH(QVariant channel, channelsList) { foreach (const QVariant &channel, channelsList) {
channels.insert(channel.toMap().value("meta").toString(), channel.toMap().value("value")); channels.insert(channel.toMap().value("meta").toString(), channel.toMap().value("value"));
} }
emit processDataReceived(messageId, key, channels); emit processDataReceived(messageId, key, channels);
} }
} else if (messageType == "GetParameterChannels") { } else if (messageType == "GetParameterChannels") {
Q_FOREACH(QString deviceKey, result.keys()) { foreach (const QString &deviceKey, result.keys()) {
QStringList parameterChannels = result.value(deviceKey).toStringList(); QStringList parameterChannels = result.value(deviceKey).toStringList();
if (!parameterChannels.isEmpty()) if (!parameterChannels.isEmpty())
emit parameterChannelsReceived(messageId, deviceKey, parameterChannels); emit parameterChannelsReceived(messageId, deviceKey, parameterChannels);
} }
} else if (messageType == "GetParameter"|| messageType == "SetParameter") { } else if (messageType == "GetParameter"|| messageType == "SetParameter") {
QList<Device> devices;
QVariantList devicesList = result.value("devices").toList(); QVariantList devicesList = result.value("devices").toList();
Q_FOREACH(QVariant value, devicesList) { foreach (const QVariant &value, devicesList) {
QString key = value.toMap().value("key").toString(); QString key = value.toMap().value("key").toString();
QVariantList channelsList = value.toMap().value("channels").toList(); QVariantList channelsList = value.toMap().value("channels").toList();
QList<Parameter> parameters; QList<Parameter> parameters;
Q_FOREACH(QVariant channel, channelsList) { foreach (const QVariant &channel, channelsList) {
Parameter parameter; Parameter parameter;
parameter.meta = channel.toMap().value("meta").toString(); parameter.meta = channel.toMap().value("meta").toString();
parameter.name = channel.toMap().value("name").toString(); parameter.name = channel.toMap().value("name").toString();