-
Notifications
You must be signed in to change notification settings - Fork 0
/
notifications.cpp
122 lines (110 loc) · 5.06 KB
/
notifications.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include "notifications.h"
#include <QUrlQuery>
Notifications::Notifications(ThingManager *thingManager, Thing *thing, QObject *parent)
: QObject{parent},
m_thingManager(thingManager),
m_thing(thing)
{
m_clearHumidityTimer.setInterval(30*60*1000);
m_clearHumidityTimer.setSingleShot(true);
connect(&m_clearHumidityTimer, &QTimer::timeout, this, [this](){
m_humidityWarningShown = false;
});
m_clearBadAirTimer.setInterval(30*60*1000);
m_clearBadAirTimer.setSingleShot(true);
connect(&m_clearBadAirTimer, &QTimer::timeout, this, [this](){
m_badAirWarningShown = false;
});
}
void Notifications::update(const ZoneInfo &zone)
{
bool supportsUpdate = m_thing->thingClassId() == ThingClassId("f0dd4c03-0aca-42cc-8f34-9902457b05de")
&& m_thing->paramValue("service").toString() == "FB-GCM"
// Updating is only supported with versions that have the notificationId param
&& !m_thing->thingClass().actionTypes().findByName("notify").paramTypes().findByName("notificationId").id().isNull();
QString notificationId = "humidityalert-" + zone.id().toString();
QString title = "High humidity alert";
QString text = QString("Humidity in zone %1: %2 %").arg(zone.name()).arg(zone.humidity());
ThingActionInfo *actionInfo = nullptr;
if (zone.zoneStatus().testFlag(ZoneInfo::ZoneStatusFlagHighHumidity)) {
if (!m_humidityWarningShown) {
// show
actionInfo = updateNotification(notificationId, title, text, false, false);
} else if (supportsUpdate && !qFuzzyCompare(zone.humidity(), m_lastHumidityValue)) {
// update
actionInfo = updateNotification(notificationId, title, text, false, false);
}
} else {
if (m_humidityWarningShown && supportsUpdate) {
// remove
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();
title = "Bad air alert";
text = QString("Bad air in zone %1: %2").arg(zone.name());
QStringList airValues;
if (zone.voc() >= 660) {
airValues.append(QString("%1 ppm").arg(zone.voc()));
}
if (zone.pm25() >= 25) {
airValues.append(QString("%1 µg/m³").arg(zone.pm25()));
}
text = text.arg(airValues.join(","));
actionInfo = nullptr;
if (zone.zoneStatus().testFlag(ZoneInfo::ZoneStatusFlagBadAir)) {
if (!m_badAirWarningShown) {
// show
actionInfo = updateNotification(notificationId, title, text, false, false);
} else if (supportsUpdate && zone.voc() != m_lastBadAirValue) {
// update
actionInfo = updateNotification(notificationId, title, text, false, false);
}
} else {
if (m_badAirWarningShown && supportsUpdate) {
// remove
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();
}
});
}
}
ThingActionInfo* Notifications::updateNotification(const QString &id, const QString &title, const QString &text, bool sound, bool remove)
{
ActionType actionType = m_thing->thingClass().actionTypes().findByName("notify");
Action action(actionType.id(), m_thing->id(), Action::TriggeredByRule);
ParamList params = ParamList{
Param(actionType.paramTypes().findByName("title").id(), title),
Param(actionType.paramTypes().findByName("body").id(), text),
};
if (m_thing->thingClassId() == ThingClassId("f0dd4c03-0aca-42cc-8f34-9902457b05de")) {
QUrlQuery data;
data.addQueryItem("open", "airconditioning");
params.append(Param(actionType.paramTypes().findByName("data").id(), data.toString()));
// For backwards compatibility, only add those if the plugin already has them
if (!m_thing->thingClass().actionTypes().findByName("notify").paramTypes().findByName("notificationId").id().isNull()) {
params.append(Param(actionType.paramTypes().findByName("notificationId").id(), id));
params.append(Param(actionType.paramTypes().findByName("sound").id(), sound));
params.append(Param(actionType.paramTypes().findByName("remove").id(), remove));
}
}
action.setParams(params);
ThingActionInfo *info = m_thingManager->executeAction(action);
return info;
}