-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfetcher.php
76 lines (53 loc) · 2.21 KB
/
fetcher.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
<?php
require "db.php";
if(isset($_GET["year"]) && isset($_GET["month"])) {
$year = $_GET["year"];
$month = $_GET["month"];
if(is_numeric($year) && is_numeric($month)) {
$days_in_month = cal_days_in_month(CAL_GREGORIAN, $month, $year);
} else {
die();
}
$url = "https://monitoringapi.solaredge.com/site/319863/powerDetails.json?startTime=" .$year. "-" .$month. "-1%200:0:0&endTime=" .$year. "-" .$month. "-" .$days_in_month. "%200:0:0&api_key=" .$key;
} else {
$today = date("Y-n-j");
$lastHour = date("G") - 1;
$url = "https://monitoringapi.solaredge.com/site/319863/powerDetails.json?startTime=" .$today. "%20" .$lastHour. ":0:0&endTime=" .$today. "%20" .$lastHour. ":59:0&api_key=" .$key;
}
$arrContextOptions = [
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false
]
];
$data = file_get_contents($url, false, stream_context_create($arrContextOptions));
$decoded_data = json_decode($data, true);
$data = $decoded_data["powerDetails"]["meters"];
$conn = new mysqli($host, $user, $pw, $db);
$arrayPosition = 0;
$query = "INSERT INTO `data` (`ts`, `production`, `purchased`, `feedin`, `consumption`, `selfconsumption`) VALUES";
foreach($data[0]["values"] as $selfConsumption) {
$selfConsumptionValue = $selfConsumption["value"];
$consumptionValue = $data[1]["values"][$arrayPosition]["value"];
$purchasedValue = $data[2]["values"][$arrayPosition]["value"];
$productionValue = $data[3]["values"][$arrayPosition]["value"];
$feedInValue = $data[4]["values"][$arrayPosition]["value"];
$d = new DateTime($selfConsumption["date"], new DateTimeZone('Europe/Rome'));
$ts = $d->getTimestamp();
if(gettype($productionValue) == "NULL") {
$productionValue = 0;
}
if(gettype($selfConsumptionValue) == "NULL") {
$selfConsumptionValue = 0;
}
$query .= "(" .$ts. ", " .$productionValue. ", " .$purchasedValue. ", " .$feedInValue. ", " .$consumptionValue. ", " .$selfConsumptionValue. "), ";
$arrayPosition++;
}
$query = substr($query, 0, -2);
$conn->query($query);
if(isset($_GET["year"]) && isset($_GET["month"])) {
echo "done with " .$year. "-" .$month;
} else {
echo "done with " .$today;
}
?>