-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetLiveData.php
132 lines (106 loc) · 3.58 KB
/
getLiveData.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
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
<?php header("Content-type: text/json");
include "Parameters.php";
$USE_YOULESS=false;
$USE_INVERTOR=true;
$MOCK=false;
session_start();
$invertor_state="Unknown";
//
// Parse the Powerstocc information page
//
if ($USE_INVERTOR) {
$url="http://pvserver:".$PIKO_PASSWD."@".$PIKO_ADDRESS;
$handle=fopen($url, "r");
$teller=1;
$actual_yield= 0;
$total_yield=0;
$total_yield_today=0;
while (!feof($handle))
{
$line=fgets($handle);
if ($teller == 46) {$actual_yield = strip_tags($line);}
if ($teller == 51) {$total_yield = strip_tags($line);}
if ($teller == 65) {$total_yield_today = strip_tags($line);}
if ($teller == 74) {$invertor_state = strip_tags($line);}
if ($teller == 109) {$dc1_c = strip_tags($line);}
if ($teller == 114) {$ac1_c = strip_tags($line);}
if ($teller == 123) {$dc1_i = strip_tags($line);}
if ($teller == 128) {$ac1_p = strip_tags($line);}
if ($teller == 148) {$dc2_c = strip_tags($line);}
if ($teller == 153) {$ac2_c = strip_tags($line);}
if ($teller == 162) {$dc2_i = strip_tags($line);}
if ($teller == 167) {$ac2_p = strip_tags($line);}
if ($teller == 187) {$dc3_c = strip_tags($line);}
if ($teller == 193) {$ac3_c = strip_tags($line);}
if ($teller == 202) {$dc3_i = strip_tags($line);}
if ($teller == 208) {$ac3_p = strip_tags($line);}
if ($teller >= 208) {
break;
}
$teller++;
}
fclose($handle);
}
//
// Parse the Youless information page
//
$yl_actual_yield = 0;
if ($USE_YOULESS) {
$ckfile = tempnam ("/tmp", "CURLCOOKIE");
$curl_handle = curl_init ("http://82.95.120.155:8888/L?w=''");
curl_setopt( $curl_handle, CURLOPT_COOKIESESSION, true );
curl_setopt( $curl_handle, CURLOPT_COOKIEJAR, '/tmp/koekje' );
curl_setopt( $curl_handle, CURLOPT_COOKIEFILE, '/tmp/koekje' );
$output = curl_exec ($curl_handle);
$outputyouless = file_get_contents('http://82.95.120.155:8888/L?w=""');
$outputyouless = file_get_contents('http://82.95.120.155:8888/a?f=j');
$json = json_decode($outputyouless);
$yl_actual_yield = $json->{'pwr'};
}
// The x value is the current JavaScript time, which is the Unix time multiplied by 1000.
$x = time() * 1000;
$y1 = (int)$actual_yield;
$y2 = (int)$yl_actual_yield;
$y3 = (int)($total_yield_today*1000);
$y4 = (float)$dc1_i;
$y5 = (float)$dc2_i;
$y6 = (float)$dc3_i;
$y7 = (int)$ac1_p;
$y8 = (int)$ac2_p;
$y9 = (int)$ac3_p;
if (strpos($invertor_state,'MPP')) {
$invertor_state = "Feeding";
} else if (strpos($invertor_state, 'stationair')) {
$invertor_state = "Standby";
} else if (strpos($invertor_state, 'uit')) {
$invertor_state = "Off";
}
$real_usage = 0;
$direction = 1;
if ($USE_YOULESS && $USE_INVERTOR) {
//
// Create new value for power usage
// TODO: try to guess direction.....
//
if ($actual_yield < $yl_actual_yield) {
$real_usage = $yl_actual_yield + $actual_yield;
$direction = -1;
} else {
$real_usage = $actual_yield - $yl_actual_yield;
$direction = 1;
}
}
// Create a PHP array and echo it as JSON
$ret1 = array($x, $y1);
$ret2 = array($x, $y2);
$ret3 = array($x, $y3);
$ret4 = array($x, $y4);
$ret5 = array($x, $y5);
$ret6 = array($x, $y6);
$ret7 = array($x, $real_usage);
$ret8 = array($x, $y7);
$ret9 = array($x, $y8);
$ret10 = array($x, $y9);
$ret = array($ret1, $ret2, $ret3, $ret4, $ret5, $ret6, $invertor_state, $total_yield, $real_usage, $direction, $ret7, $ret8, $ret9, $ret10);
echo json_encode($ret);
?>