-
Notifications
You must be signed in to change notification settings - Fork 0
/
Amount.SolarForcast.ips.php
341 lines (332 loc) · 11.7 KB
/
Amount.SolarForcast.ips.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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<?php
declare(strict_types=1);
################################################################################
# Script: Amount.SolarForcast.ips.php
# Version: 2.2.20230326
# Author: Heiko Wilknitz (@Pitti)
#
# Script zur Abholung und Aufbereitung der prognostizierten Solorproduktion
# von solarprognose.de.
#
# API
# https://www.solarprognose.de/web/solarprediction/api/v1
# ?access-token=ACCESS-TOKEN
# &project=Hier Ihre Projekt-Website oder Ihre Kontakt-E-Mail
# &item=ITEM
# &id=ID
# &type=hourly|daily
# &_format=json|xml
# &algorithm=mosmix|own-v1|clearsky
# &day=DAY
# &start_epoch_time=START_EPOCH_TIME&end_epoch_time=END_EPOCH_TIME
# &start_day=START_DAY&end_day=END_DAY
# &snomminixml=true # für snom VoIP Telefone
#
# ------------------------------ Installation ----------------------------------
#
# Dieses Skript richtet automatisch alle nötigen Objekte bei manueller
# Ausführung ein. Eine weitere manuelle Ausführung setzt alle benötigten Objekte
# wieder auf den Ausgangszustand.
#
# - Neues Skript erstellen
# - Diesen PHP-Code hineinkopieren
# - Abschnitt 'Konfiguration' mit eigenen Werten anpassen (__WWX austauschen)
# - Skript Abspeichern
# - Skript Ausführen
# - Visualisierung per Link auf entstandene Variablen erstellen
#
# ------------------------------ Changelog -------------------------------------
#
# 08.02.2023 - Initalversion (v1.0)
# 23.02.2023 - API Doc, Type fixes (v1.1)
# 15.03.2023 - Umstellung bzw. Erweiterung für mehrere Anlagen
# und All-In-One Script (v2.0)
# 17.03.2023 - Fix für Archive Control (v2.1)
# 26.03.2023 - Fix Style & Logging (v2.2)
# 04.03.2024 - Fix für Archivdaten (v2.3)
#
# ------------------------------ Konfiguration ---------------------------------
#
# Global Debug Output Flag
$DEBUG = false;
#
# Globale Variable __WWX als Array via define() in __autoload definiert!!!
#
# Erste Anlage
$PVA['Garage'] = [
'token' => __WWX['SPD_TOKEN'], // (string) erstezen durch => '<api-token>'
'project' => __WWX['SPD_MAIL'], // (string) erstezen durch => '<mail-adresse>'
'id' => __WWX['SPD_ID'], // (int) erstezen durch => <anlagen-id>
'item' => 'location',
'format' => 'json',
'type' => 'hourly', // 'daily';
'start' => 0,
'end' => 1,
];
#
# Weitere Anlage
# $PVA['Haus'] = [
# 'token' => __WWX['SPD_TOKEN'],
# 'project' => __WWX['SPD_MAIL'],
# 'id' => __WWX['SPD_ID2'],
# 'item' => 'location',
# 'format' => 'json',
# 'type' => 'daily';
# 'start' => 0,
# 'end' => 1,
# ];
#
################################################################################
#
# Requires include of the global function script via autoload (__autoload.php)
# or direct in the script (uncomment next line)!
# require_once(IPS_GetKernelDir()."scripts".DIRECTORY_SEPARATOR.'System.Functions.ips.php');
# You can download it from here https://github.com/wilkware/ips-scripts
#
defined('WWX_FUNCTIONS') || die('Global function library not available!');
require_once IPS_GetKernelDir() . 'scripts' . DIRECTORY_SEPARATOR . 'System.QuickChart.ips.php';
// INSTALLATION
if ($_IPS['SENDER'] == 'Execute') {
// Event 1 mal am Tag
$midnight = mktime(0, 0, 15);
CreateEventByName($_IPS['SELF'], 'UpdateDaily', $midnight);
// Event aller 1 Atunde von 05:00 bis 18:00 Uhr - TODO: Timer im Sommer vielleicht anpassen
$from = mktime(5, 0, 15);
$to = mktime(18, 0, 15);
CreateEventByNameFromTo($_IPS['SELF'], 'UpdateHourly', 3, 1, $from, $to);
// Variablen pro Anlage anlegen
foreach ($PVA as $name => $plant) {
// Kategorie erzeugen
$cid = CreateCategoryByName($_IPS['SELF'], $name);
$vid = CreateVariableByName($cid, 'Daten', 3);
$vid = CreateVariableByName($cid, 'Tabellarischer Verlauf', 3, 10, 'Database', '~HTMLBox');
$vid = CreateVariableByName($cid, 'Graphischer Verlauf', 3, 11, 'Graph', '~HTMLBox');
$vid = CreateVariableByName($cid, 'Prognose Heute', 2, 20, '', '~Electricity');
// Archivierung aktivieren (Typ: Zähler)
RegisterArchive($vid, false);
$vid = CreateVariableByName($cid, 'Prognose Morgen', 2, 21, '', '~Electricity');
$vid = CreateVariableByName($cid, 'Aktuelle Stunde', 2, 22, '', '~Electricity');
}
}
// WEBFRONT
elseif ($_IPS['SENDER'] == 'WebFront') {
// Benutzer hat etwas geändert!
}
// VARIABLENAENDERUNG
elseif ($_IPS['SENDER'] == 'Variable') {
// Varaible hat sich geändert!
}
// TIMER EVENT
elseif ($_IPS['SENDER'] == 'TimerEvent') {
$event = IPS_GetName($_IPS['EVENT']);
if ($event == 'UpdateDaily') {
foreach ($PVA as $name => $plant) {
// Kategorie erzeugen oder lesen
$cid = CreateCategoryByName($_IPS['SELF'], $name);
// Mitternacht - Reset auf 0
$vid = CreateVariableByName($cid, 'Prognose Heute', 2);
SetValueFloat($vid, 0);
$vid = CreateVariableByName($cid, 'Prognose Morgen', 2);
SetValueFloat($vid, 0);
$vid = CreateVariableByName($cid, 'Aktuelle Stunde', 2);
SetValueFloat($vid, 0);
}
} elseif ($event == 'UpdateHourly') {
foreach ($PVA as $name => $plant) {
// Kategorie erzeugen oder lesen
$cid = CreateCategoryByName($_IPS['SELF'], $name);
// von 08:00 bis 18:00 - TODO: Timer im Sommer vielleicht anpassen
$data = UpdateForecast($plant);
// Save
$vid = CreateVariableByName($cid, 'Daten', 3);
$json = json_encode($data);
SetValueString($vid, $json);
// aktuellen Werte abgleichen wenn notwendig
$vid = CreateVariableByName($cid, 'Prognose Heute', 2);
if ($data['Heute'] > 0) {
$lv = GetValueFloat($vid);
if ($lv < $data['Heute']) {
SetValueFloat($vid, $data['Heute']);
} elseif ($lv == $data['Heute']) {
// Do nothing
} else {
//Den letzten Wert, der in der Datenbank gespeichert wurde, holen
$aid = IPS_GetInstanceListByModuleID(ExtractGuid('Archive Control'))[0];
$last = AC_GetLoggedValues($aid, $vid, 0, 0, 1)[0];
AC_DeleteVariableData($aid, $vid, $last['TimeStamp'], 0);
SetValueFloat($vid, $data['Heute']);
IPS_Sleep(1000);
AC_ReAggregateVariable($aid, $vid);
}
}
unset($data['Heute']);
$vid = CreateVariableByName($cid, 'Prognose Morgen', 2);
SetValueFloat($vid, $data['Morgen']);
unset($data['Morgen']);
$vid = CreateVariableByName($cid, 'Aktuelle Stunde', 2);
SetValueFloat($vid, $data['Stunde']);
unset($data['Stunde']);
// SVG Chart
$svg = DrawChart($data);
$svg = str_replace(['1024pt', '325pt'], '100%', $svg);
$vid = CreateVariableByName($cid, 'Graphischer Verlauf', 3);
SetValueString($vid, $svg);
// HTML Table
$html = BuildHtml($data);
$vid = CreateVariableByName($cid, 'Tabellarischer Verlauf', 3);
SetValueString($vid, $html);
}
}
}
#----------------------------------- Functions ---------------------------------
function UpdateForecast($plant)
{
$url = 'https://www.solarprognose.de/web/solarprediction/api/v1?access-token=' .
$plant['token'] . '&project=' .
$plant['project'] . '&item=' .
$plant['item'] . '&id=' .
$plant['id'] . '&type=' .
$plant['type'] . '&_format=' .
$plant['format'] . '&algorithm=own-v1&start_day=' .
$plant['start'] . '&end_day=' .
$plant['end'];
//EchoDebug('URL: ', $url);
$json = file_get_contents($url);
//EchoDebug('Daten :', $json);
$result = [];
if ($json !== false) {
$data = json_decode($json, true);
// Heute, Morgen und aktuelle Stunde
$ad = date('d.m.Y');
$at = date('H') . ':00';
$result['Heute'] = 0;
$result['Morgen'] = 0;
$result['Stunde'] = 0;
// Stundenwerte
foreach ($data['data'] as $key => $values) {
// Tag
$dd = date('d.m.Y', $key);
// Uhrzeit
$dt = date('H:i', $key);
// Werte hochzählen
if ($ad == $dd) { // Heute
$result['Heute'] += $values[0];
} else { // Morgen
$result['Morgen'] += $values[0];
}
if (($ad == $dd) && ($at == $dt)) {
$result['Stunde'] = $values[0];
}
// Pro Tag ein Feld
if (!isset($result[$ad])) {
$result[$ad] = [];
}
// Stundenwerte und akkumulierten Werte
$result[$dd][$dt] = $values;
}
}
return $result;
}
function BuildHtml($data)
{
// Rows
$rows = array_keys($data);
// cols
$cols = max(count($data[$rows[0]]), count($data[$rows[1]]));
// html
$html = '';
$html .= '<table class="wwx">';
foreach ($data as $day => $hours) {
$count = 0;
$th = '<thead class="orange"><th><center>' . $day . '</center></th>';
$tr1 = '<tr><td>kW/h</td>';
$tr2 = '<tr><td>∑ kWh</td>';
foreach ($hours as $hour => $values) {
$th .= '<th>' . $hour . '</th>';
$tr1 .= '<td>' . $values[0] . '</td>';
$tr2 .= '<td>' . $values[1] . '</td>';
$count++;
}
for ($i = $count; $i < $cols; $i++) {
$th .= '<th>-</th>';
$tr1 .= '<td>-</td>';
$tr2 .= '<td>-</td>';
}
$html .= $th . '</thead>';
$html .= $tr1 . '</tr>';
$html .= $tr2 . '</tr>';
}
$html .= '</table>';
return $html;
}
function DrawChart($data)
{
$axis_x = [];
$axis_y = [];
// prepeare dataset
foreach ($data as $day => $hours) {
foreach ($hours as $hour => $values) {
$axis_x[] = '\'' . $hour . '\'';
$axis_y[] = intval($values[0] * 1000);
}
}
// new chart object
$chart = new QuickChart(['width' => 1024, 'height' => 325, 'format' => 'svg']);
// chart config
$chart->setConfig("{
type: 'line',
data: {
labels: [" . implode(',', $axis_x) . "],
datasets: [
{
backgroundColor: 'rgba(255, 125, 0, 0.1)',
borderColor: 'rgb(255, 125, 0)',
borderWidth: 1,
lineTension: 0.4,
pointRadius: 1,
fill: true,
data: [" . implode(',', $axis_y) . "]
}]
},
options: {
legend: {
labels: {
fontColor: 'rgb(255, 255, 255)'
},
display: false
},
scales: {
xAxes: [{
ticks: {
fontColor: '#fff',
},
gridLines: {
color: 'rgba(211, 211, 211, 0.2)',
borderDash: [5,5]
},
scaleLabel: {
fontColor: '#fff',
display: false,
labelString: 'Uhrzeit'
}
}],
yAxes: [{
ticks: {
fontColor: '#fff'
},
gridLines: {
color: 'rgba(211, 211, 211, 0.2)',
borderDash: [5,5]
},
scaleLabel: {
fontColor: '#fff',
display: true,
labelString: 'Leistung (kW)'
}
}]
}
}
}");
return $chart->toBinary();
}
################################################################################