-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rain.cpp
148 lines (127 loc) · 3.43 KB
/
Rain.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
143
144
145
146
147
#include "Rain.h"
#include "units.h"
#include "math.h"
using namespace std;
Rain::Rain( Now *_now, Database &_db ):
oldDayRain(-1),
now(_now),
dbg("Rain"),
db( _db ),
instantRain(0),
lastRainTime(0)
{
restore();
}
Rain::~Rain()
{
dump();
}
void Rain::restore()
{
dbResult result = db.query("select * from rainData;");
for ( dbResult::iterator iter = result.begin(); iter!=result.end(); ++iter )
{
time_t t = iter->second.asInteger("time");
if ( t > now->unixtime() - MAX_RAIN_DURATION )
{
unsigned int amount = iter->second.asInteger("amount");
rainAccum[t] = amount;
}
}
dbg.printf(NOTICE, "Restored %d rain values (sum = %.2f)\n", result.size(),
getSum(MAX_RAIN_DURATION)/100.0);
}
void Rain::dump()
{
time_t t;
float amount;
dbg.printf(NOTICE, "Dumping %d rain values to database\n", rainAccum.size());
db.query("BEGIN TRANSACTION;");
db.query("DELETE FROM rainData;");
for ( rainAccumulation_t::iterator iter = rainAccum.begin();
iter != rainAccum.end(); ++iter )
{
t = iter->first;
amount = iter->second;
db.query("insert into rainData ( time, amount ) VALUES ( %d, %f );", (int)t, amount );
}
db.query("COMMIT;");
}
void Rain::update( unsigned int value )
{
if ( oldDayRain < 0 )
{
dbg.printf(CRIT, "initializing oldDayRain to %f\n", value );
oldDayRain = value;
return;
}
int newRain = value - oldDayRain;
// Handle rollover midnight case.
if ( newRain < 0 )
{
dbg.printf(CRIT, "Rain Rollover\n");
newRain = value;
}
// There is no need to fill up the table if we didn't get any data
if ( newRain != 0 )
{
rainAccum[now->unixtime()] = newRain;
}
for ( rainAccumulation_t::iterator iter = rainAccum.begin();
iter != rainAccum.end(); )
{
if ( iter->first <= now->unixtime() - MAX_RAIN_DURATION )
{
rainAccum.erase(iter++);
}
else
{
++iter;
}
}
time_t timeSinceLastRain = now->unixtime()-lastRainTime;
if ( timeSinceLastRain < 60*ONE_MINUTE )
{
if ( newRain )
{
dbg.printf(DEBUG, "newRain: %d\n", newRain );
dbg.printf(DEBUG, "timeSinceLastRain: %d\n", timeSinceLastRain );
// Only compute new amount if we have new rain
instantRain = (int)(newRain / (timeSinceLastRain/3600.0)); // convert to hours
dbg.printf(DEBUG, "instantRain: %d\n", instantRain );
}
else // If we didn't get new rain, we need to compute the current rain
// rate as if the next round will have rain.
{
int newInstantRain = (int)(1 / ( timeSinceLastRain/3600.0));
if ( newInstantRain < instantRain )
{
dbg.printf(DEBUG, "Rain rate decay algorithm activated. New rain rate = %d\n", newInstantRain );
instantRain = newInstantRain;
}
}
}
else
{
instantRain = 0;
}
if ( instantRain < 0 )
instantRain = 0;
if ( newRain )
lastRainTime = now->unixtime();
oldDayRain = value;
}
unsigned int Rain::getSum( time_t duration )
{
unsigned int rval = 0;
int maxRateAge = now->unixtime() - duration;
for ( rainAccumulation_t::iterator iter = rainAccum.begin();
iter != rainAccum.end(); ++iter )
{
if ( iter->first > maxRateAge )
{
rval += iter->second;
}
}
return rval;
}