-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlarms.cpp
143 lines (126 loc) · 4.23 KB
/
Alarms.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "Alarms.h"
#include "units.h"
#include <sstream>
Alarms::Alarms( Config cfg, WeatherData *_wd ):
WeatherSink( cfg, _wd, "Alarms" )
{
pthread_mutex_init( ¬ify, NULL );
pthread_mutex_lock( ¬ify );
startMain();
}
Alarms::~Alarms()
{
endMain();
}
void Alarms::setLimits()
{
for ( int p = WeatherData::insideTemp; p != WeatherData::END; p++ )
{
lowerLimit[ ( WeatherData::PROPERTY)p ] = NO_VALUE;
upperLimit[ ( WeatherData::PROPERTY)p ] = NO_VALUE;
alarming[ (WeatherData::PROPERTY)p ] = false;
}
lowerLimit[WeatherData::insideTemp] = 55;
upperLimit[WeatherData::insideTemp] = 78;
lowerLimit[WeatherData::insideHumidity] = 10;
upperLimit[WeatherData::insideHumidity] = 90;
lowerLimit[WeatherData::outsideTemp] = 0;
upperLimit[WeatherData::outsideTemp] = 90;
upperLimit[WeatherData::windSpeed] = 30;
upperLimit[WeatherData::averageWindSpeed] = 30;
upperLimit[WeatherData::windGust] = 30;
upperLimit[WeatherData::dailyRain] = 100;
lowerLimit[WeatherData::SLP] = (29.00);
upperLimit[WeatherData::SLP] = (31.00);
lowerLimit[WeatherData::apparentTemp] = 0;
upperLimit[WeatherData::apparentTemp] = 90;
upperLimit[WeatherData::rain24Hour] = 100;
upperLimit[WeatherData::instantRain] = 100;
// TODO: Need to add calculated values too
}
void Alarms::newData()
{
pthread_mutex_unlock( ¬ify );
}
void Alarms::main()
{
bool alarmOccured;
setLimits();
while ( threadRunning )
{
std::ostringstream oss;
alarmOccured = false;
pthread_mutex_lock( ¬ify );
if( !threadRunning )
{
dbg.printf(NOTICE, "Exiting\n");
return;
}
float timeSinceLast = alarmCheckTime.timeSinceLast();
dbg.printf(DEBUG, "time since last alarm check: %f\n", timeSinceLast);
if ( timeSinceLast <= ALARM_INTERVAL )
{
dbg.printf(DEBUG, "too soon since last update.. not doing anything\n");
continue;
}
for ( int i = WeatherData::insideTemp; i != WeatherData::END; i++ )
{
WeatherData::PROPERTY p = (WeatherData::PROPERTY)i;
// localAlarm is used in the loop to determine if we need to throw
// another alarm condition. It is set anytime an alarm condition is
// encountered. We can use this to see if we were alarming in our
// previous iteration (in which case we don't throw the alarm), or if
// this is a new alarm condition
bool localAlarm = false;
if ( lowerLimit[p] != NO_VALUE )
{
// If we are alarming, we won't send out another notice until this
// alarm clears
if ( wd->getValue( p, 0 ) <
lowerLimit[ p ] )
{
localAlarm = true;
if ( !alarming[ p ] )
{
alarming[ p] = true;
oss << wd->propertyToString(p);
oss << " exceeding alarm condition. Current value is ";
oss << wd->getValue( p, 0 ) << ", lower limit is ";
oss << lowerLimit[ p ] << std::endl;
alarmOccured = true;
}
}
}
if ( upperLimit[p] != NO_VALUE )
{
// If we are alarming, we won't send out another notice until this
// alarm clears
if ( wd->getValue( p, 0 ) > upperLimit[ p ] )
{
localAlarm = true;
if ( !alarming[ p ] )
{
alarming[ p ] = true;
oss << wd->propertyToString(p);
oss << " exceeding alarm condition. Current value is ";
oss << wd->getValue( p, 0 ) << ", upper limit is ";
oss << upperLimit[ p ] << std::endl;
alarmOccured = true;
}
}
}
// If this was alarming, but now we aren't...
if ( !localAlarm && alarming[p] )
{
dbg.printf(NOTICE, "Resetting alarm condition on %s\n",
wd->propertyToString(p));
alarming[p] = false;
}
}
if ( alarmOccured )
{
dbg.printf(NOTICE, oss.str().c_str());
}
alarmCheckTime.updateTime();
}
}