Merge PR #423: Fix erraneous state changes during thing setup

This commit is contained in:
Jenkins nymea 2021-05-21 15:13:52 +02:00
commit d15d2f510d
3 changed files with 42 additions and 38 deletions

View File

@ -694,13 +694,7 @@ ThingPairingInfo *ThingManagerImplementation::confirmPairing(const PairingTransa
ParamList settings = buildParams(thingClass.settingsTypes(), ParamList());
thing->setSettings(settings);
QList<EventTypeId> loggedEventTypeIds;
foreach (const EventType &eventType, thingClass.eventTypes()) {
if (eventType.suggestLogging()) {
loggedEventTypeIds.append(eventType.id());
}
}
thing->setLoggedEventTypeIds(loggedEventTypeIds);
initThing(thing);
ThingSetupInfo *info = setupThing(thing);
connect(info, &ThingSetupInfo::finished, thing, [this, info, externalInfo, addNewThing](){
@ -798,13 +792,8 @@ ThingSetupInfo* ThingManagerImplementation::addConfiguredThingInternal(const Thi
ParamList settings = buildParams(thingClass.settingsTypes(), ParamList());
thing->setSettings(settings);
QList<EventTypeId> loggedEventTypeIds;
foreach (const EventType &eventType, thingClass.eventTypes()) {
if (eventType.suggestLogging()) {
loggedEventTypeIds.append(eventType.id());
}
}
thing->setLoggedEventTypeIds(loggedEventTypeIds);
initThing(thing);
ThingSetupInfo *info = setupThing(thing);
connect(info, &ThingSetupInfo::finished, this, [this, info](){
@ -1614,13 +1603,7 @@ void ThingManagerImplementation::loadConfiguredThings()
thing->setSettings(thingSettings);
QList<EventTypeId> loggedEventTypeIds;
foreach (const EventType &eventType, thingClass.eventTypes()) {
if (eventType.suggestLogging()) {
loggedEventTypeIds.append(eventType.id());
}
}
thing->setLoggedEventTypeIds(loggedEventTypeIds);
initThing(thing);
settings.endGroup(); // ThingId
@ -1740,13 +1723,7 @@ void ThingManagerImplementation::onAutoThingsAppeared(const ThingDescriptors &th
thing->setSettings(settings);
thing->setParentId(thingDescriptor.parentId());
QList<EventTypeId> loggedEventTypeIds;
foreach (const EventType &eventType, thingClass.eventTypes()) {
if (eventType.suggestLogging()) {
loggedEventTypeIds.append(eventType.id());
}
}
thing->setLoggedEventTypeIds(loggedEventTypeIds);
initThing(thing);
qCDebug(dcThingManager()) << "Setting up auto thing:" << thing->name() << thing->id().toString();
@ -2065,14 +2042,6 @@ ThingSetupInfo* ThingManagerImplementation::setupThing(Thing *thing)
ThingClass thingClass = findThingClass(thing->thingClassId());
IntegrationPlugin *plugin = m_integrationPlugins.value(thingClass.pluginId());
QList<State> states;
foreach (const StateType &stateType, thingClass.stateTypes()) {
State state(stateType.id(), thing->id());
states.append(state);
}
thing->setStates(states);
loadThingStates(thing);
ThingSetupInfo *info = new ThingSetupInfo(thing, this, 30000);
if (!plugin) {
@ -2091,6 +2060,27 @@ ThingSetupInfo* ThingManagerImplementation::setupThing(Thing *thing)
return info;
}
void ThingManagerImplementation::initThing(Thing *thing)
{
ThingClass thingClass = findThingClass(thing->thingClassId());
QList<State> states;
foreach (const StateType &stateType, thingClass.stateTypes()) {
State state(stateType.id(), thing->id());
states.append(state);
}
thing->setStates(states);
loadThingStates(thing);
QList<EventTypeId> loggedEventTypeIds;
foreach (const EventType &eventType, thingClass.eventTypes()) {
if (eventType.suggestLogging()) {
loggedEventTypeIds.append(eventType.id());
}
}
thing->setLoggedEventTypeIds(loggedEventTypeIds);
}
void ThingManagerImplementation::postSetupThing(Thing *thing)
{
ThingClass thingClass = findThingClass(thing->thingClassId());

View File

@ -158,6 +158,7 @@ private:
ThingSetupInfo *addConfiguredThingInternal(const ThingClassId &thingClassId, const QString &name, const ParamList &params, const ThingId &parentId = ThingId());
ThingSetupInfo *reconfigureThingInternal(Thing *thing, const ParamList &params, const QString &name = QString());
ThingSetupInfo *setupThing(Thing *thing);
void initThing(Thing *thing);
void trySetupThing(Thing *thing);
void registerThing(Thing *thing);
void postSetupThing(Thing *thing);

View File

@ -612,6 +612,8 @@ void TestIntegrations::storedThings()
ThingId addedThingId = ThingId(response.toMap().value("params").toMap().value("thingId").toString());
QVERIFY(!addedThingId.isNull());
clearLoggingDatabase();
// Restart the core instance to check if settings are loaded at startup
restartServer();
@ -620,8 +622,8 @@ void TestIntegrations::storedThings()
bool found = false;
foreach (const QVariant &thing, response.toMap().value("params").toMap().value("things").toList()) {
if (ThingId(thing.toMap().value("id").toString()) == addedThingId) {
qDebug() << "found added thing" << thing.toMap().value("params");
qDebug() << "expected thingParams:" << thingParams;
qCDebug(dcTests()) << "found added thing" << thing.toMap().value("params");
qCDebug(dcTests()) << "expected thingParams:" << thingParams;
verifyParams(thingParams, thing.toMap().value("params").toList());
found = true;
break;
@ -629,6 +631,17 @@ void TestIntegrations::storedThings()
}
QVERIFY2(found, "thing missing in config!");
// Wait for the DB to sync up and then verify that just restarting did not add state changes for this thing
waitForDBSync();
params.clear();
params.insert("thingIds", QVariantList() << addedThingId);
params.insert("loggingSources", QVariantList() << "LoggingSourceStates");
response = injectAndWait("Logging.GetLogEntries", params);
QVERIFY2(response.toMap().value("params").toMap().contains("logEntries"), "Huh? GetLogEntries failed!");
qCDebug(dcTests()) << "log response:" << response.toMap().value("params").toMap().value("logEntries");
QVERIFY2(response.toMap().value("params").toMap().value("logEntries").toList().isEmpty(), "There are state changed events after a core restart even though states did not change!");
params.clear();
params.insert("thingId", addedThingId);
response = injectAndWait("Integrations.RemoveThing", params);