Fix notifications being updated too often

This commit is contained in:
Michael Zanetti 2023-05-08 23:59:23 +02:00
parent 65107f88fe
commit d0cbd5cc1f
2 changed files with 52 additions and 27 deletions

View File

@ -7,10 +7,16 @@ Notifications::Notifications(ThingManager *thingManager, Thing *thing, QObject *
m_thingManager(thingManager), m_thingManager(thingManager),
m_thing(thing) m_thing(thing)
{ {
m_clearTimer.setInterval(30*60*1000); m_clearHumidityTimer.setInterval(30*60*1000);
m_clearTimer.setSingleShot(true); m_clearHumidityTimer.setSingleShot(true);
connect(&m_clearTimer, &QTimer::timeout, this, [this](){ connect(&m_clearHumidityTimer, &QTimer::timeout, this, [this](){
m_isShown = false; m_humidityWarningShown = false;
});
m_clearBadAirTimer.setInterval(30*60*1000);
m_clearBadAirTimer.setSingleShot(true);
connect(&m_clearBadAirTimer, &QTimer::timeout, this, [this](){
m_badAirWarningShown = false;
}); });
} }
@ -24,20 +30,31 @@ void Notifications::update(const ZoneInfo &zone)
QString notificationId = "humidityalert-" + zone.id().toString(); QString notificationId = "humidityalert-" + zone.id().toString();
QString title = "High humidity alert"; QString title = "High humidity alert";
QString text = QString("Humidity in zone %1: %2 %").arg(zone.name()).arg(zone.humidity()); QString text = QString("Humidity in zone %1: %2 %").arg(zone.name()).arg(zone.humidity());
ThingActionInfo *actionInfo = nullptr;
if (zone.zoneStatus().testFlag(ZoneInfo::ZoneStatusFlagHighHumidity)) { if (zone.zoneStatus().testFlag(ZoneInfo::ZoneStatusFlagHighHumidity)) {
if (!m_isShown) { if (!m_humidityWarningShown) {
// show // show
updateNotification(notificationId, title, text, false, false); actionInfo = updateNotification(notificationId, title, text, false, false);
} else if (supportsUpdate) { } else if (supportsUpdate && !qFuzzyCompare(zone.humidity(), m_lastHumidityValue)) {
// update // update
updateNotification(notificationId, title, text, false, false); actionInfo = updateNotification(notificationId, title, text, false, false);
} }
} else { } else {
if (m_isShown && supportsUpdate) { if (m_humidityWarningShown && supportsUpdate) {
// remove // remove
updateNotification(notificationId, title, text, false, true); actionInfo = updateNotification(notificationId, title, text, false, true);
} }
} }
if (actionInfo) {
connect(actionInfo, &ThingActionInfo::finished, this, [=](){
if (actionInfo->status() == Thing::ThingErrorNoError) {
m_humidityWarningShown = zone.zoneStatus().testFlag(ZoneInfo::ZoneStatusFlagHighHumidity);
m_lastHumidityValue = zone.humidity();
m_clearHumidityTimer.start();
}
});
}
notificationId = "airalert-" + zone.id().toString(); notificationId = "airalert-" + zone.id().toString();
title = "Bad air alert"; title = "Bad air alert";
@ -50,23 +67,33 @@ void Notifications::update(const ZoneInfo &zone)
airValues.append(QString("%1 µg/m³").arg(zone.pm25())); airValues.append(QString("%1 µg/m³").arg(zone.pm25()));
} }
text = text.arg(airValues.join(",")); text = text.arg(airValues.join(","));
actionInfo = nullptr;
if (zone.zoneStatus().testFlag(ZoneInfo::ZoneStatusFlagBadAir)) { if (zone.zoneStatus().testFlag(ZoneInfo::ZoneStatusFlagBadAir)) {
if (!m_isShown) { if (!m_badAirWarningShown) {
// show // show
updateNotification(notificationId, title, text, false, false); actionInfo = updateNotification(notificationId, title, text, false, false);
} else if (supportsUpdate) { } else if (supportsUpdate && zone.voc() != m_lastBadAirValue) {
// update // update
updateNotification(notificationId, title, text, false, false); actionInfo = updateNotification(notificationId, title, text, false, false);
} }
} else { } else {
if (m_isShown && supportsUpdate) { if (m_badAirWarningShown && supportsUpdate) {
// remove // remove
updateNotification(notificationId, title, text, false, true); actionInfo = updateNotification(notificationId, title, text, false, true);
} }
} }
if (actionInfo) {
connect(actionInfo, &ThingActionInfo::finished, this, [=](){
if (actionInfo->status() == Thing::ThingErrorNoError) {
m_badAirWarningShown = zone.zoneStatus().testFlag(ZoneInfo::ZoneStatusFlagBadAir);
m_lastBadAirValue = zone.voc();
m_clearHumidityTimer.start();
}
});
}
} }
void Notifications::updateNotification(const QString &id, const QString &title, const QString &text, bool sound, bool remove) ThingActionInfo* Notifications::updateNotification(const QString &id, const QString &title, const QString &text, bool sound, bool remove)
{ {
ActionType actionType = m_thing->thingClass().actionTypes().findByName("notify"); ActionType actionType = m_thing->thingClass().actionTypes().findByName("notify");
Action action(actionType.id(), m_thing->id(), Action::TriggeredByRule); Action action(actionType.id(), m_thing->id(), Action::TriggeredByRule);
@ -91,11 +118,5 @@ void Notifications::updateNotification(const QString &id, const QString &title,
action.setParams(params); action.setParams(params);
ThingActionInfo *info = m_thingManager->executeAction(action); ThingActionInfo *info = m_thingManager->executeAction(action);
connect(info, &ThingActionInfo::finished, this, [=](){ return info;
if (info->status() == Thing::ThingErrorNoError) {
m_isShown = !remove;
m_clearTimer.start();
}
});
} }

View File

@ -20,17 +20,21 @@ public:
signals: signals:
private: private:
void updateNotification(const QString &id, const QString &title, const QString &text, bool sound, bool remove); ThingActionInfo *updateNotification(const QString &id, const QString &title, const QString &text, bool sound, bool remove);
private: private:
ThingManager *m_thingManager = nullptr; ThingManager *m_thingManager = nullptr;
Thing *m_thing = nullptr; Thing *m_thing = nullptr;
ZoneInfo::ZoneStatus m_zoneStatus; ZoneInfo::ZoneStatus m_zoneStatus;
bool m_isShown = false; bool m_humidityWarningShown = false;
double m_lastHumidityValue = 0;
bool m_badAirWarningShown = false;
uint m_lastBadAirValue = 0;
// For devices that don't support updates/removals, we'll assume after some time that it's gone and we may need to show again // For devices that don't support updates/removals, we'll assume after some time that it's gone and we may need to show again
QTimer m_clearTimer; QTimer m_clearHumidityTimer;
QTimer m_clearBadAirTimer;
}; };