Repository navigation
Expand file tree
/
Copy pathdate.php
More file actions
152 lines (135 loc) · 5.86 KB
/
Copy pathdate.php
File metadata and controls
152 lines (135 loc) · 5.86 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
<?php
error_reporting(E_ALL);
/** script version v1.0 **/
/************ FILL IN YOUR MyHomeEnergy DETAILS HERE **********/
$aspxauthtxtfile = 'aspxauth.txt'; //paste the alphanumeric ASPXAUTH cookie data into this file
$nmi='';
/**************************************************************/
/************ FILL IN YOUR PVOutput DETAILS HERE **************/
$PVOutput_APIKey = '';
$PVOutput_SystemID = '';
/**************************************************************/
/* example usage:
upload_date.php?date=20200131 - uploads MyHomeEnergy data from 31 Jan 2020 to PVOutput
upload_date.php?date=20200131&autoupload=45 - uploads MyHomeEnergy data from 31 Jan 2020 to PVOutput, then progresses on to upload the next day's data 45s later (spaced out to avoid using up all your PVOutput API calls for the hour)
*/
$htmlheader = '<!doctype html>
<html>
<head>
<title>Meter data for '.$_GET['date'].'</title>
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
<meta name="viewport" content="width=280">';
$htmlheadermeta = '';
$htmlheaderend = '</head>
<body>';
$htmloutput = '';
$htmlfooter = '
</body>
</html>';
$fd=$_GET['date'];
$td=$_GET['date'];
$r=makeRequest($fd,$td,$nmi,$aspxauthtxtfile);
$o=json_decode($r, true);
$data=$o['Series'];
$imported_peak=0;
$imported_offpeak=0;
$exported=0;
for($i=0;$i<count($data);$i++)
{
$amt = $data[$i]['data'][0];
if ($data[$i]['name']=='Export Solar') {
$exported=$amt*-1000;
} else if ($data[$i]['name']=='Consumption Peak') {
$imported_peak=$amt*1000;
} else if ($data[$i]['name']=='Consumption Off Peak') {
$imported_offpeak=$amt*1000;
}
}
$htmloutput .= 'Date: '.date('Y-m-d', strtotime($_GET['date'])).'<br/>';
$htmloutput .= 'E: '.$exported.' P: '.$imported_peak.' O: '.$imported_offpeak.'<br />';
if ($exported==0 && $imported_offpeak==0 && $imported_peak==0) {
$htmloutput .= 'No values returned from MyHomeEnergy';
printhtml($htmlheader, $htmlheadermeta, $htmlheaderend, $htmloutput, $htmlfooter);
return;
}
$pvouploadres = uploadDaily($PVOutput_APIKey, $PVOutput_SystemID, $_GET['date'], $exported, $imported_peak, $imported_offpeak);
$htmloutput .= $pvouploadres;
if (isset($_GET['autoupload']) && substr($pvouploadres,0,2)=='OK' && strtotime($_GET['date'])<strtotime('yesterday')) {
$htmlheadermeta .= '<meta http-equiv="refresh" content="'.$_GET['autoupload'].'; url=upload_date.php?autoupload='.$_GET['autoupload'].'&date='.date('Ymd', strtotime($_GET['date'].' + 1 day')).'">';
}
$htmloutput .= '<br /><br /><a href="?date='.date('Ymd', strtotime($_GET['date'].' - 1 day')).'">Previous</a> | <a href="?date='.date('Ymd', strtotime($_GET['date'].' + 1 day')).'">Next</a>';
printhtml($htmlheader, $htmlheadermeta, $htmlheaderend, $htmloutput, $htmlfooter);
function uploadDaily($apiKey, $systemID, $date, $exported, $imported_peak, $imported_offpeak) {
$url = 'http://pvoutput.org/service/r2/addoutput.jsp';
$fields = array(
'd' => urlencode(date('Ymd', strtotime($date))),
'g' => urlencode($exported), //required if output does not already exist
'e' => urlencode($exported),
'cm' => urlencode('Imported from MyHomeEnergy'),
'ip' => urlencode($imported_peak),
'io' => urlencode($imported_offpeak)
);
$fields_string = '';
foreach($fields as $key=>$value)
$fields_string .= $key.'='.$value.'&';
rtrim($fields_string, '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,array(
'Content-Type: application/x-www-form-urlencoded',
'X-Pvoutput-Apikey: '.$apiKey,
'X-Pvoutput-SystemId: '.$systemID
));
// Set so curl_exec returns the result instead of outputting it.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
// Get the response and close the channel.
$response = curl_exec($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
if(curl_errno($ch))
echo 'err: '.curl_error($ch);
curl_close($ch);
return $body;
}
function makeRequest($startDate, $endDate, $nmi, $aspxauthtxtfile) {
$aspxauth=file_get_contents($aspxauthtxtfile);
$url = 'https://www.ausnetservices.com.au/api/Sitecore/Dashboard/GetDataForChart?customerNMI='.$nmi.'&startdate='.$startDate.'&enddate='.$endDate.'&chartView=W&typeOfData=netUsage&timeSlicesArray=[{%22START_DT%22%3A%222017-07-31%22%2C%22END_DT%22%3A%22Current%22}]&rateConFlat=0';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_REFERER, 'https://www.ausnetservices.com.au/en/myHomeEnergy/Dashboard');
curl_setopt($ch,CURLOPT_COOKIE, '.ASPXAUTH='.$aspxauth.';');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Set so curl_exec returns the result instead of outputting it.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Get the response and close the channel.
$response = curl_exec($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
//echo 'header: '.$header;
if(curl_errno($ch))
echo 'err: '.curl_error($ch);
//check if we were given a new .ASPXAUTH cookie
preg_match_all("/^Set-Cookie:\s+.ASPXAUTH=(.*);/mU", $header, $cookieMatchArray);
if (count($cookieMatchArray)>1 && count($cookieMatchArray[1])>0)
{
//save the new cookie for the next request
file_put_contents($aspxauthtxtfile, $cookieMatchArray[1]);
}
curl_close($ch);
return $body;
}
function printhtml($header, $meta, $headerend, $content, $footer) {
echo $header;
echo $meta;
echo $headerend;
echo $content;
echo $footer;
}