-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatabase.php
More file actions
executable file
·226 lines (204 loc) · 5.83 KB
/
Copy pathdatabase.php
File metadata and controls
executable file
·226 lines (204 loc) · 5.83 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php
function createDB($file)
{
try
{
$file_db = new PDO("sqlite:$file");
$file_db->setAttribute(PDO::ATTR_ERRMODE,
PDO::ERRMODE_EXCEPTION);
$file_db->exec("CREATE TABLE IF NOT EXISTS weather (
station INTEGER ,
metric TEXT,
value INTEGER,
time INTEGER)");
logToFile("created new database ".$file,true);
}
catch(PDOException $e)
{
logToFile("create error ".$e->getMessage(),true);
}
$file_db=null;
}
function openDB($file)
{
if (!file_exists("$file")) createDB($file);
try
{
$file_db = new PDO("sqlite:$file");
$file_db->setAttribute(PDO::ATTR_ERRMODE,
PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
logToFile("open error ".$e->getMessage(),true);
}
return ($file_db);
}
function data_getval($parm)
{
$fh = fopen("weather.dat", 'r');
if ($fh)
{
while (($buffer = fgets($fh)) !== false)
{
$buffer = trim(preg_replace('/\s\s+/', ' ', $buffer));
$parts=explode(' ',$buffer);
if ("$parts[0]|$parts[1]"==="$parm") return $parts[2];
}
fclose($fh);
}
return(false);
}
function data_getuv()
{
// added to DB externally via rtl_433
$file_db=openDB("weather.db3");
try
{
$select = "select value from weather where metric=\"uv\" and
station=\"UV800\" and
time < ".(time()-1200)." order by time desc limit 1";
$stmt = $file_db->query($select);
$row =$stmt->fetchObject();
if (!$row) return false;
else return $row->value;
}
catch(PDOException $e)
{
echo("getuv error ".$e->getMessage());
}
return false;
}
function data_getrain()
{
$file_db=openDB("weather.db3");
try
{
$select = "select value from weather where metric=\"dailyrainin\" and
station=\"5N1x31\" and
time < ".(time()-1500)." order by time desc limit 1";
$stmt = $file_db->query($select);
$row =$stmt->fetchObject();
if (!$row) return false;
else return $row->value;
}
catch(PDOException $e)
{
echo("getrain error ".$e->getMessage());
}
return false;
}
function data_save($querystring,$keep,$sample,$history)
{
parse_str($querystring,$qarray);
$array = array();
$tarray = array();
$sampleOK=false;
$station=$qarray["mt"];
$sensor=$qarray["sensor"];
if ($station === "tower") $station=$station."-".$sensor;
$time=time();
$fh = fopen("weather.dat", 'r');
if ($fh)
{
while (($buffer = fgets($fh)) !== false)
{
$buffer = trim(preg_replace('/\s\s+/', ' ', $buffer));
$parts=explode(' ',$buffer);
$array["$parts[0]|$parts[1]"]=$parts[2];
if (isset($parts[3])) $tarray["$parts[0]|$parts[1]"]=$parts[3];
}
fclose($fh);
}
foreach ($qarray as $key => $value)
{
$array["$station|$key"]=$value;
$tarray["$station|$key"]=$time;
}
/*
if (!isset($qarray['windgustmph'])) // no longer being sent
{
unset($array['5N1x38|windgustmph']);
unset($array['5N1x31|windgustmph']);
unset($array['5N1x38|windgustdir']);
unset($array['5N1x31|windgustdir']); // this one never seems to exist anyway, but just in case
}
*/
$fh=fopen("weather.dat.tmp","w");
foreach ( $array as $key => $value)
{
if ($key==='ETIME|xxx')
{
if ( ($time-$value) > $sample)
{
$value=time();
$sampleOK=true;
fwrite($fh,"ETIME xxx ".$time."\n");
}
else
fwrite($fh,"ETIME xxx ".$value."\n");
continue;
}
$parts=explode('|',$key);
if (($time-$tarray[$key]) < 600) fwrite($fh,"$parts[0] $parts[1] $value $tarray[$key]\n");
}
if (!isset($array['ETIME|xxx']))
{
fwrite($fh,"ETIME xxx ".$time."\n");
}
fclose($fh);
rename("weather.dat.tmp","weather.dat");
if ($keep && $sampleOK)
{
try
{
$file_db=openDB("weather.db3");
$insert = "INSERT INTO weather (station, metric, value, time)
VALUES (:station, :metric, :value, :time)";
$stmt = $file_db->prepare($insert);
$stmt->bindParam(':station', $station);
$stmt->bindParam(':metric', $metric);
$stmt->bindParam(':value', $mvalue);
$stmt->bindParam(':time', $time);
foreach ( $array as $key => $value) //NOT qarray because its incomplete given sampling
{
if ($key === "ETIME|xxx"
|| strpos($key,"|id")
|| strpos($key,"|dateutc")
|| strpos($key,"|action")
|| strpos($key,"|realtime")
|| strpos($key,"|sensor")
|| strpos($key,"|battery")
|| strpos($key,"|rssi")
|| strpos($key,"|rtfreq")
|| strpos($key,"|ID")
|| strpos($key,"|PASSWORD")
|| strpos($key,"|mt")
|| strpos($key,"|probe")
|| strpos($key,"|check")) continue;
$parts=explode('|',$key);
$station=$parts[0];
$metric=$parts[1];
$mvalue=$value;
//$time=date("Y/m/d H:i:s", time());
$stmt->execute();
}
// try to execute once a day after 1st sample assuming acurite posts
// at least once every 42 seconds
if (time()%86400 < 43)
{
$seconds=$history*86400;
$delete = "DELETE FROM weather WHERE TIME < ".(time()-$seconds);
logToFile("Purging data",true);
$stmt = $file_db->query($delete);
}
}
catch(PDOException $e)
{
logToFile("db error ".$e->getMessage(),true);
}
}
$file_db=null;
return;
}
?>