diff --git a/libnymea-core/integrations/thingmanagerimplementation.cpp b/libnymea-core/integrations/thingmanagerimplementation.cpp index e9238265..2fbd9822 100644 --- a/libnymea-core/integrations/thingmanagerimplementation.cpp +++ b/libnymea-core/integrations/thingmanagerimplementation.cpp @@ -694,13 +694,7 @@ ThingPairingInfo *ThingManagerImplementation::confirmPairing(const PairingTransa ParamList settings = buildParams(thingClass.settingsTypes(), ParamList()); thing->setSettings(settings); - QList 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 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 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 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 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 states; + foreach (const StateType &stateType, thingClass.stateTypes()) { + State state(stateType.id(), thing->id()); + states.append(state); + } + thing->setStates(states); + loadThingStates(thing); + + QList 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()); diff --git a/libnymea-core/integrations/thingmanagerimplementation.h b/libnymea-core/integrations/thingmanagerimplementation.h index 33e539d5..28d84387 100644 --- a/libnymea-core/integrations/thingmanagerimplementation.h +++ b/libnymea-core/integrations/thingmanagerimplementation.h @@ -158,6 +158,7 @@ private: ThingSetupInfo *addConfiguredThingInternal(const ThingClassId &thingClassId, const QString &name, const ParamList ¶ms, const ThingId &parentId = ThingId()); ThingSetupInfo *reconfigureThingInternal(Thing *thing, const ParamList ¶ms, const QString &name = QString()); ThingSetupInfo *setupThing(Thing *thing); + void initThing(Thing *thing); void trySetupThing(Thing *thing); void registerThing(Thing *thing); void postSetupThing(Thing *thing); diff --git a/tests/auto/integrations/testintegrations.cpp b/tests/auto/integrations/testintegrations.cpp index ce2d6c82..e3e193ad 100644 --- a/tests/auto/integrations/testintegrations.cpp +++ b/tests/auto/integrations/testintegrations.cpp @@ -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);