Merge PR #437: Fronius: Fix timer clean up on things removed and code cleaning
This commit is contained in:
commit
8f112045e4
@ -103,17 +103,19 @@ void IntegrationPluginFronius::setupThing(ThingSetupInfo *info)
|
|||||||
} else if ((thing->thingClassId() == inverterThingClassId) ||
|
} else if ((thing->thingClassId() == inverterThingClassId) ||
|
||||||
(thing->thingClassId() == meterThingClassId) ||
|
(thing->thingClassId() == meterThingClassId) ||
|
||||||
(thing->thingClassId() == storageThingClassId)) {
|
(thing->thingClassId() == storageThingClassId)) {
|
||||||
|
|
||||||
Thing *loggerThing = myThings().findById(thing->parentId());
|
Thing *loggerThing = myThings().findById(thing->parentId());
|
||||||
if (!loggerThing) {
|
if (!loggerThing) {
|
||||||
qCWarning(dcFronius()) << "Could not find Logger Thing for thing " << thing->name();
|
qCWarning(dcFronius()) << "Could not find Logger Thing for thing " << thing->name();
|
||||||
return info->finish(Thing::ThingErrorHardwareNotAvailable, "Please try again");
|
return info->finish(Thing::ThingErrorHardwareNotAvailable, "Please try again");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!loggerThing->setupComplete()) {
|
if (!loggerThing->setupComplete()) {
|
||||||
//wait for the parent to finish the setup process
|
//wait for the parent to finish the setup process
|
||||||
connect(loggerThing, &Thing::setupStatusChanged, info, [this, info, loggerThing] {
|
connect(loggerThing, &Thing::setupStatusChanged, info, [this, info, loggerThing] {
|
||||||
|
if (loggerThing->setupComplete()) {
|
||||||
if (loggerThing->setupComplete())
|
|
||||||
setupChild(info, loggerThing);
|
setupChild(info, loggerThing);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -163,22 +165,18 @@ void IntegrationPluginFronius::thingRemoved(Thing *thing)
|
|||||||
FroniusLogger *logger = m_froniusLoggers.key(thing);
|
FroniusLogger *logger = m_froniusLoggers.key(thing);
|
||||||
m_froniusLoggers.remove(logger);
|
m_froniusLoggers.remove(logger);
|
||||||
logger->deleteLater();
|
logger->deleteLater();
|
||||||
return;
|
|
||||||
} else if (thing->thingClassId() == inverterThingClassId) {
|
} else if (thing->thingClassId() == inverterThingClassId) {
|
||||||
FroniusInverter *inverter = m_froniusInverters.key(thing);
|
FroniusInverter *inverter = m_froniusInverters.key(thing);
|
||||||
m_froniusInverters.remove(inverter);
|
m_froniusInverters.remove(inverter);
|
||||||
inverter->deleteLater();
|
inverter->deleteLater();
|
||||||
return;
|
|
||||||
} else if (thing->thingClassId() == meterThingClassId) {
|
} else if (thing->thingClassId() == meterThingClassId) {
|
||||||
FroniusMeter *meter = m_froniusMeters.key(thing);
|
FroniusMeter *meter = m_froniusMeters.key(thing);
|
||||||
m_froniusMeters.remove(meter);
|
m_froniusMeters.remove(meter);
|
||||||
meter->deleteLater();
|
meter->deleteLater();
|
||||||
return;
|
|
||||||
} else if (thing->thingClassId() == storageThingClassId) {
|
} else if (thing->thingClassId() == storageThingClassId) {
|
||||||
FroniusStorage *storage = m_froniusStorages.key(thing);
|
FroniusStorage *storage = m_froniusStorages.key(thing);
|
||||||
m_froniusStorages.remove(storage);
|
m_froniusStorages.remove(storage);
|
||||||
storage->deleteLater();
|
storage->deleteLater();
|
||||||
return;
|
|
||||||
} else {
|
} else {
|
||||||
Q_ASSERT_X(false, "thingRemoved", QString("Unhandled thingClassId: %1").arg(thing->thingClassId().toString()).toUtf8());
|
Q_ASSERT_X(false, "thingRemoved", QString("Unhandled thingClassId: %1").arg(thing->thingClassId().toString()).toUtf8());
|
||||||
}
|
}
|
||||||
@ -197,7 +195,6 @@ void IntegrationPluginFronius::executeAction(ThingActionInfo *info)
|
|||||||
qCDebug(dcFronius()) << "Execute action" << thing->name() << action.actionTypeId().toString();
|
qCDebug(dcFronius()) << "Execute action" << thing->name() << action.actionTypeId().toString();
|
||||||
|
|
||||||
if (thing->thingClassId() == dataloggerThingClassId) {
|
if (thing->thingClassId() == dataloggerThingClassId) {
|
||||||
|
|
||||||
if (action.actionTypeId() == dataloggerSearchDevicesActionTypeId) {
|
if (action.actionTypeId() == dataloggerSearchDevicesActionTypeId) {
|
||||||
searchNewThings(m_froniusLoggers.key(thing));
|
searchNewThings(m_froniusLoggers.key(thing));
|
||||||
info->finish(Thing::ThingErrorNoError);
|
info->finish(Thing::ThingErrorNoError);
|
||||||
@ -213,7 +210,7 @@ void IntegrationPluginFronius::updateThingStates(Thing *thing)
|
|||||||
{
|
{
|
||||||
qCDebug(dcFronius()) << "Update thing values for" << thing->name();
|
qCDebug(dcFronius()) << "Update thing values for" << thing->name();
|
||||||
|
|
||||||
if(thing->thingClassId() == inverterThingClassId) {
|
if (thing->thingClassId() == inverterThingClassId) {
|
||||||
QNetworkReply *reply = hardwareManager()->networkManager()->get(QNetworkRequest(m_froniusInverters.key(thing)->updateUrl()));
|
QNetworkReply *reply = hardwareManager()->networkManager()->get(QNetworkRequest(m_froniusInverters.key(thing)->updateUrl()));
|
||||||
connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater);
|
connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater);
|
||||||
connect(reply, &QNetworkReply::finished, thing, [this, thing, reply]() {
|
connect(reply, &QNetworkReply::finished, thing, [this, thing, reply]() {
|
||||||
@ -223,7 +220,7 @@ void IntegrationPluginFronius::updateThingStates(Thing *thing)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
QByteArray data = reply->readAll();
|
QByteArray data = reply->readAll();
|
||||||
if(m_froniusInverters.values().contains(thing)){ // check if thing was not removed before reply was received
|
if (m_froniusInverters.values().contains(thing)){ // check if thing was not removed before reply was received
|
||||||
m_froniusInverters.key(thing)->updateThingInfo(data);
|
m_froniusInverters.key(thing)->updateThingInfo(data);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -235,7 +232,7 @@ void IntegrationPluginFronius::updateThingStates(Thing *thing)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
QByteArray data = next_reply->readAll();
|
QByteArray data = next_reply->readAll();
|
||||||
if(m_froniusInverters.values().contains(thing)){ // check if thing was not removed before reply was received
|
if (m_froniusInverters.values().contains(thing)){ // check if thing was not removed before reply was received
|
||||||
m_froniusInverters.key(thing)->updateActivityInfo(data);
|
m_froniusInverters.key(thing)->updateActivityInfo(data);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -249,7 +246,7 @@ void IntegrationPluginFronius::updateThingStates(Thing *thing)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
QByteArray data = reply->readAll();
|
QByteArray data = reply->readAll();
|
||||||
if(m_froniusLoggers.values().contains(thing)){ // check if thing was not removed before reply was received
|
if (m_froniusLoggers.values().contains(thing)){ // check if thing was not removed before reply was received
|
||||||
m_froniusLoggers.key(thing)->updateThingInfo(data);
|
m_froniusLoggers.key(thing)->updateThingInfo(data);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -263,7 +260,7 @@ void IntegrationPluginFronius::updateThingStates(Thing *thing)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
QByteArray data = reply->readAll();
|
QByteArray data = reply->readAll();
|
||||||
if(m_froniusMeters.values().contains(thing)){ // check if thing was not removed before reply was received
|
if (m_froniusMeters.values().contains(thing)){ // check if thing was not removed before reply was received
|
||||||
m_froniusMeters.key(thing)->updateThingInfo(data);
|
m_froniusMeters.key(thing)->updateThingInfo(data);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -278,7 +275,7 @@ void IntegrationPluginFronius::updateThingStates(Thing *thing)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
QByteArray data = reply->readAll();
|
QByteArray data = reply->readAll();
|
||||||
if(m_froniusStorages.values().contains(thing)){ // check if thing was not removed before reply was received
|
if (m_froniusStorages.values().contains(thing)){ // check if thing was not removed before reply was received
|
||||||
m_froniusStorages.key(thing)->updateThingInfo(data);
|
m_froniusStorages.key(thing)->updateThingInfo(data);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -297,7 +294,6 @@ void IntegrationPluginFronius::updateThingStates(Thing *thing)
|
|||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
Q_ASSERT_X(false, "updateThingState", QString("Unhandled thingClassId: %1").arg(thing->thingClassId().toString()).toUtf8());
|
Q_ASSERT_X(false, "updateThingState", QString("Unhandled thingClassId: %1").arg(thing->thingClassId().toString()).toUtf8());
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -333,7 +329,7 @@ void IntegrationPluginFronius::searchNewThings(FroniusLogger *logger)
|
|||||||
QJsonParseError error;
|
QJsonParseError error;
|
||||||
QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &error);
|
QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &error);
|
||||||
if (error.error != QJsonParseError::NoError) {
|
if (error.error != QJsonParseError::NoError) {
|
||||||
qCWarning(dcFronius()) << "Fronius: Failed to parse JSON data" << data << ":" << error.errorString();
|
qCWarning(dcFronius()) << "Failed to parse JSON data" << data << ":" << error.errorString();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -342,12 +338,13 @@ void IntegrationPluginFronius::searchNewThings(FroniusLogger *logger)
|
|||||||
|
|
||||||
// Check reply information
|
// Check reply information
|
||||||
QVariantMap bodyMap = jsonDoc.toVariant().toMap().value("Body").toMap();
|
QVariantMap bodyMap = jsonDoc.toVariant().toMap().value("Body").toMap();
|
||||||
|
qCDebug(dcFronius()) << "System:" << qUtf8Printable(QJsonDocument::fromVariant(bodyMap).toJson(QJsonDocument::Indented));
|
||||||
|
|
||||||
// Parse reply for inverters at the host address
|
// Parse reply for inverters at the host address
|
||||||
QVariantMap inverterMap = bodyMap.value("Data").toMap().value("Inverter").toMap();
|
QVariantMap inverterMap = bodyMap.value("Data").toMap().value("Inverter").toMap();
|
||||||
foreach (QString inverterId, inverterMap.keys()) {
|
foreach (QString inverterId, inverterMap.keys()) {
|
||||||
//check if thing already connected to logger
|
//check if thing already connected to logger
|
||||||
if(!thingExists(inverterThingIdParamTypeId,inverterId)) {
|
if (!thingExists(inverterThingIdParamTypeId,inverterId)) {
|
||||||
QString thingName = loggerThing->name() + " Inverter " + inverterId;
|
QString thingName = loggerThing->name() + " Inverter " + inverterId;
|
||||||
ThingDescriptor descriptor(inverterThingClassId, thingName, "Fronius Solar Inverter", loggerThing->id());
|
ThingDescriptor descriptor(inverterThingClassId, thingName, "Fronius Solar Inverter", loggerThing->id());
|
||||||
ParamList params;
|
ParamList params;
|
||||||
@ -409,10 +406,10 @@ void IntegrationPluginFronius::searchNewThings(FroniusLogger *logger)
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
bool IntegrationPluginFronius::thingExists(ParamTypeId thingParamId, QString thingId)
|
bool IntegrationPluginFronius::thingExists(const ParamTypeId &thingParamId, QString thingId)
|
||||||
{
|
{
|
||||||
foreach(Thing *thing, myThings()) {
|
foreach(Thing *thing, myThings()) {
|
||||||
if(thing->paramValue(thingParamId).toString() == thingId) {
|
if (thing->paramValue(thingParamId).toString() == thingId) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -68,7 +68,7 @@ private:
|
|||||||
void updateThingStates(Thing *thing);
|
void updateThingStates(Thing *thing);
|
||||||
|
|
||||||
void searchNewThings(FroniusLogger *logger);
|
void searchNewThings(FroniusLogger *logger);
|
||||||
bool thingExists(ParamTypeId thingParamId, QString thingId);
|
bool thingExists(const ParamTypeId &thingParamId, QString thingId);
|
||||||
|
|
||||||
void setupChild(ThingSetupInfo *info, Thing *parentThing);
|
void setupChild(ThingSetupInfo *info, Thing *parentThing);
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user