-
Notifications
You must be signed in to change notification settings - Fork 1
/
WeatherMapDataSource_influxdb.php
68 lines (58 loc) · 1.63 KB
/
WeatherMapDataSource_influxdb.php
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
<?php
// Datasource plugin for influxdb, target is defined as:
// influxdb:host:database:username:password:seriesin:seriesout
class WeatherMapDataSource_influxdb extends WeatherMapDataSource {
private $regex_pattern = "/^influxdb:(.*):(.*):(.*):(.*):(.*):(.*)$/";
function Init(&$map)
{
return(TRUE);
}
function Recognise($targetstring)
{
if(preg_match($this->regex_pattern,$targetstring,$matches))
{
return TRUE;
}
else
{
return FALSE;
}
}
function ReadData($targetstring, &$map, &$item)
{
$data[IN] = NULL;
$data[OUT] = NULL;
$data_time = time();
if(preg_match($this->regex_pattern,$targetstring,$matches))
{
$host = $matches[1];
$database = $matches[2];
$username = urlencode($matches[3]);
$password = urlencode($matches[4]);
$seriesin = $matches[5];
$seriesout = $matches[6];
$buffer = "";
$query = urlencode("select mean(value) from $seriesin where time > now() - 3m");
$file = "http://$host:8086/db/$database/series?q=$query&u=$username&p=$password";
$fp = fopen($file,"r");
while (!feof($fp)) {
$buffer .= fgets($fp,4096);
}
fclose($fp);
$decoded = json_decode($buffer);
$data[IN] = round($decoded[0]->points[0][1]);
$buffer = "";
$query = urlencode("select mean(value) from $seriesout where time > now() - 3m");
$file = "http://$host:8086/db/$database/series?q=$query&u=$username&p=$password";
$fp = fopen($file,"r");
while (!feof($fp)) {
$buffer .= fgets($fp,4096);
}
fclose($fp);
$decoded = json_decode($buffer);
$data[OUT] = round($decoded[0]->points[0][1]);
}
return( array($data[IN], $data[OUT], $data_time) );
}
}
?>