source: trunk/Modules/Measure/Page.php

Last change on this file was 95, checked in by chronos, 2 years ago
  • Modified: Updated Common package.
  • Added: Explicit types for better type checking.
  • Fixed: Support for php 8.0.
File size: 13.5 KB
Line 
1<?php
2
3class PageMain extends Page
4{
5 var $Months;
6 var $GraphTimeRanges;
7
8 function __construct(System $System)
9 {
10 parent::__construct($System);
11 $this->Months = array('', 'Leden', 'Únor', 'Březen', 'Duben', 'Květen', 'Červen', 'Červenec', 'Srpen', 'Září',
12 'Říjen', 'Listopad', 'Prosinec');
13
14 $this->GraphTimeRanges = array(
15 'hour' => array(
16 'caption' => 'Hodina',
17 'period' => 3600,
18 ),
19 'day' => array(
20 'caption' => 'Den',
21 'period' => 3600 * 24,
22 ),
23 'week' => array(
24 'caption' => 'Týden',
25 'period' => 3600 * 24 * 7,
26 ),
27 'month' => array(
28 'caption' => 'Měsíc',
29 'period' => 3600 * 24 * 30,
30 ),
31 'year' => array(
32 'caption' => 'Rok',
33 'period' => 3600 * 24 * 365,
34 ),
35 'years' => array(
36 'caption' => 'Desetiletí',
37 'period' => 3600 * 24 * 365 * 10,
38 ),
39 'all' => array(
40 'caption' => 'Vše',
41 'period' => -1,
42 ),
43 );
44 $this->Time = time();
45 }
46
47 function GetTimeRange(): string
48 {
49 if (!array_key_exists($_SESSION['Period'], $this->GraphTimeRanges))
50 $_SESSION['Period'] = 'day';
51
52 $Result = $this->GraphTimeRanges[$_SESSION['Period']]['period'];
53 if ($Result == -1)
54 {
55 $Measure = $this->LoadMeasure($_SESSION['Measure']);
56 $FirstValue = $this->GetFirstMeasure($Measure);
57 $LastValue = $this->GetLastMeasure($Measure);
58 $Result = $LastValue['Time'] - $FirstValue['Time'];
59 }
60 return $Result;
61 }
62
63 function EditTime($Time): string
64 {
65 $Output = '<form style="display: inline;" action="?Operation=SetTime&amp;Time='.$Time.'" method="post">';
66 $TimeParts = getdate($_SESSION[$Time]);
67
68 // Day selection
69 $Output .= '<select name="Day">';
70 for ($I = 1; $I < 32; $I++)
71 {
72 if ($I == $TimeParts['mday']) $Selected = ' selected="1"'; else $Selected = '';
73 $Output .= '<option value="'.$I.'"'.$Selected.'>'.$I.'</option>';
74 }
75 $Output .= '</select>. ';
76
77 // Month selection
78 $Output .= '<select name="Month">';
79 foreach ($this->Months as $Index => $Month)
80 {
81 if ($Index == $TimeParts['mon']) $Selected = ' selected="1"'; else $Selected = '';
82 if ($Index > 0) $Output .= '<option value="'.$Index.'"'.$Selected.'>'.$Month.'</option>';
83 }
84 $Output .= '</select>. ';
85
86 // Year selection
87 $Output .= '<select name="Year">';
88 for ($I = 2000; $I <= date("Y"); $I++)
89 {
90 if ($I == $TimeParts['year']) $Selected = ' selected="1"'; else $Selected = '';
91 $Output .= '<option value="'.$I.'"'.$Selected.'>'.$I.'</option>';
92 }
93 $Output .= '</select> &nbsp;&nbsp; ';
94
95 // Hour selection
96 $Output .= '<select name="Hour">';
97 for ($I = 0; $I < 24; $I++)
98 {
99 if ($I == $TimeParts['hours']) $Selected = ' selected="1"'; else $Selected = '';
100 $Output .= '<option value="'.$I.'"'.$Selected.'>'.$I.'</option>';
101 }
102 $Output .= '</select> : ';
103
104 // Minute selection
105 $Output .= '<select name="Minute">';
106 for ($I = 0; $I < 60; $I++)
107 {
108 if ($I == $TimeParts['minutes']) $Selected = ' selected="1"'; else $Selected = '';
109 $Output .= '<option value="'.$I.'"'.$Selected.'>'.$I.'</option>';
110 }
111 $Output .= '</select> ';
112
113 $Output .= '<input type="submit" value="Nastavit">';
114 $Output .= '</form>';
115 $Output .= ' <form style="display: inline;" action="?Operation=SetTimeNow&amp;Time='.$Time.'" method="post">';
116 $Output .= '<input type="submit" value="Aktuální čas">';
117 $Output .= '</form>';
118
119 return $Output;
120 }
121
122 function GetFirstMeasure($Measure): array
123 {
124 $Result2 = $this->Database->select($Measure['DataTable'], '`Time`, `Avg`', '(`Measure`='.$Measure['Id'].') AND (`Level`=0) ORDER BY `Time` ASC LIMIT 1');
125 if ($Result2->num_rows > 0)
126 {
127 $Row = $Result2->fetch_array();
128 $LastMeasureTime = MysqlDateTimeToTime($Row['Time']);
129 $LastMeasureValue = $Row['Avg'];
130 } else {
131 $LastMeasureTime = $this->Time;
132 $LastMeasureValue = 0;
133 }
134 return array('Time' => $LastMeasureTime, 'Value' => $LastMeasureValue);
135 }
136
137 function GetLastMeasure($Measure): array
138 {
139 $Result2 = $this->Database->select($Measure['DataTable'], '`Time`, `Avg`', '(`Measure`='.$Measure['Id'].') AND (`Level`=0) ORDER BY `Time` DESC LIMIT 1');
140 if ($Result2->num_rows > 0)
141 {
142 $Row = $Result2->fetch_array();
143 $LastMeasureTime = MysqlDateTimeToTime($Row['Time']);
144 $LastMeasureValue = $Row['Avg'];
145 } else {
146 $LastMeasureTime = $this->Time;
147 $LastMeasureValue = 0;
148 }
149 return array('Time' => $LastMeasureTime, 'Value' => $LastMeasureValue);
150 }
151
152 function LoadMeasure($Id): array
153 {
154 $DbResult = $this->Database->select('Measure', '*', '( `Enabled`=1) AND (`Id`='.$Id.') AND ((`PermissionView`="all") OR (`PermissionView`="'.
155 gethostbyaddr($_SERVER['REMOTE_ADDR']).'"))');
156 $DbRow = $DbResult->fetch_array();
157 return $DbRow;
158 }
159
160 /* Produce table with available measures */
161 function ShowMeasureTable(): string
162 {
163 $PrefixMultiplier = new PrefixMultiplier();
164 $Output = '<table border="1" cellspacing="0" cellpadding="2" style="font-size: small; margin: 0px auto;">';
165 $Output .= '<tr><th>Měřená veličina</th><th>Poslední hodnota</th><th>Čas posledního měření</th><th>Interpolace</th><th>Poznámky</th>';
166 if (array_key_exists('Debug', $_GET)) $Output .= '<th>Počet položek</th><th>Čas vykonání</th>';
167 $Output .= '</tr>';
168 $Result = $this->Database->select('Measure', '*', '( `Enabled`=1) AND ((`PermissionView`="all") OR (`PermissionView`="'.
169 gethostbyaddr($_SERVER['REMOTE_ADDR']).'")) ORDER BY `Description`');
170 while ($Measure = $Result->fetch_array())
171 {
172 $StopWatchStart = GetMicrotime();
173 if (array_key_exists('Debug', $_GET))
174 {
175 $DbResult = $this->Database->select($Measure['DataTable'], 'COUNT(*)', '`Measure`='.$Measure['Id']);
176 $RowCount = $DbResult->fetch_array();
177 $RowCount = $RowCount[0];
178 }
179 $Result2 = $this->Database->select($Measure['DataTable'], '`Time`, `Avg`', '(`Measure`='.
180 $Measure['Id'].') AND (`Level`=0) ORDER BY `Time` DESC LIMIT 1');
181 if ($Result2->num_rows > 0)
182 {
183 $Row = $Result2->fetch_array();
184 $LastMeasureTime = date('j.n.Y G:i:s', MysqlDateTimeToTime($Row['Time']));
185 $LastMeasureValue = $PrefixMultiplier->Add($Row['Avg'], $Measure['Unit']);
186 } else {
187 $LastMeasureTime = '&nbsp;';
188 $LastMeasureValue = '&nbsp;';
189 }
190 if ($Measure['Continuity'] == 1) $Interpolate = 'Ano'; else $Interpolate = 'Ne';
191 if ($Measure['Info'] == '') $Measure['Info'] = '&nbsp;';
192 $GenerationTime = floor((GetMicrotime() - $StopWatchStart) * 1000 ) / 1000;
193 $Output .= '<tr><td style="text-align: left"><a href="?Measure='.$Measure['Id'].
194 '&amp;Differential=0">'.$Measure['Description'].'</a></td><td align="center">'.
195 $LastMeasureValue.'</td><td align="center">'.$LastMeasureTime.'</td><td align="center">'.
196 $Interpolate.'</td><td>'.$Measure['Info'].'</td>';
197 if (array_key_exists('Debug', $_GET))
198 $Output .= '<td>'.$RowCount.'</td><td>'.$GenerationTime.'</td>';
199 $Output .= '</tr>';
200 }
201 $Output .= '</table>';
202 return $Output;
203 }
204
205 function ShowGraph(): string
206 {
207 $Output = '<strong>Graf:</strong><br>';
208 $Output .= '<img alt="Graf" src="Graph.php?Measure='.$_SESSION['Measure'].'&amp;From='.
209 $_SESSION['TimeStart'].'&amp;To='.$_SESSION['TimeEnd'].
210 '&amp;Width=750&amp;Height=200&amp;Differential='.
211 $_SESSION['Differential'].'" width="750" height="200"><br>';
212 $Output .= '<a href="?Measure='.$_SESSION['Measure'].'&amp;TimeStart='.
213 $_SESSION['TimeStart'].'&amp;TimeEnd='.$_SESSION['TimeEnd'].
214 '&amp;TimeSpecify=1&amp;Differential='.$_SESSION['Differential'].
215 '">Odkaz na vybraný graf</a><br/>';
216 return $Output;
217 }
218
219 function ShowTimeRange(): string
220 {
221 $Output = '';
222
223 if (!array_key_exists('Operation', $_GET)) $_GET['Operation'] = '';
224 switch ($_GET['Operation'])
225 {
226 case 'SetTime':
227 if (array_key_exists('Time', $_GET) and array_key_exists('Month', $_POST) and array_key_exists('Day', $_POST) and
228 array_key_exists('Year', $_POST) and array_key_exists('Hour', $_POST) and array_key_exists('Minute', $_POST))
229 {
230 if (($_GET['Time'] == 'TimeStart') or ($_GET['Time'] == 'TimeEnd'))
231 {
232 $_SESSION[$_GET['Time']] = mktime($_POST['Hour'], $_POST['Minute'], 0, $_POST['Month'],
233 $_POST['Day'], $_POST['Year']);
234 }
235 }
236 break;
237 case 'SetTimeNow':
238 if (array_key_exists('Time', $_GET))
239 {
240 if (($_GET['Time'] == 'TimeStart') or ($_GET['Time'] == 'TimeEnd'))
241 {
242 $_SESSION[$_GET['Time']] = $this->Time;
243 }
244 }
245 break;
246
247 }
248 $Output .= '<strong>Časový úsek:</strong><br>';
249
250 // Show graph time range menu
251 if ($_SESSION['TimeSpecify'] == 0)
252 {
253 $Output .= 'Délka úseku: ';
254 foreach ($this->GraphTimeRanges as $Index => $Item)
255 $Output .= '<a href="?Period='.$Index.'">'.$Item['caption'].'</a>&nbsp;';
256 $Output .= '<br>';
257 $Output .= '<a href="?TimeSpecify=1">Přesnější nastavení...</a><br>';
258 } else {
259 $Output .= '<table cellspacing="0" cellpadding="2" border="0" style="margin: 0px auto;">';
260 $Output .= '<tr><td>Počátek:</td><td>'.$this->EditTime('TimeStart').'</td></tr>';
261 $Output .= '<tr><td>Konec:</td><td>'.$this->EditTime('TimeEnd').'</td></tr>';
262 $Output .= '</table>';
263 $Output .= '<a href="?TimeSpecify=0">Jednoduché nastavení...</a><br>';
264 }
265 $Output .= 'Posun: <a href="?Move=LeftEnd">|&lt;</a> <a href="?Move=Left">&lt;</a> '.
266 '<a href="?Move=Right">&gt;</a> <a href="?Move=RightEnd">&gt;|</a> <a href="?Move=Now">Nyní</a><br>';
267 $Output .= '<br/>';
268 return $Output;
269
270 }
271
272 function HandleURL(): void
273 {
274 foreach ($this->System->Config['DefaultVariables'] as $Index => $Variable)
275 {
276 if (!array_key_exists($Index, $_SESSION)) $_SESSION[$Index] = $Variable;
277 //if (array_key_exists($Index, $_GET)) $_SESSION[$Index] = $_GET[$Index];
278 //if (array_key_exists($Index, $_POST)) $_SESSION[$Index] = $_POST[$Index];
279 //$$Index = $_SESSION[$Index];
280 }
281 if (array_key_exists('Period', $_GET))
282 {
283 $_SESSION['Period'] = $_GET['Period'];
284 // Update time start according time period
285 if ($_SESSION['Period'] == 'all')
286 {
287 $Measure = $this->LoadMeasure($_SESSION['Measure']);
288 $FirstValue = $this->GetFirstMeasure($Measure);
289 $LastValue = $this->GetLastMeasure($Measure);
290 $_SESSION['TimeStart'] = $FirstValue['Time'];
291 $_SESSION['TimeEnd'] = $LastValue['Time'];
292 } else
293 {
294 $_SESSION['TimeStart'] = $_SESSION['TimeEnd'] - $this->GetTimeRange();
295 }
296 }
297 if (array_key_exists('TimeStart', $_GET))
298 {
299 $_SESSION['TimeStart'] = $_GET['TimeStart'];
300 }
301 if (array_key_exists('TimeEnd', $_GET))
302 {
303 $_SESSION['TimeStart'] = $_GET['TimeEnd'];
304 }
305 if (array_key_exists('Differential', $_GET))
306 {
307 $_SESSION['Differential'] = $_GET['Differential'];
308 }
309 if (array_key_exists('Measure', $_GET))
310 {
311 $_SESSION['Measure'] = $_GET['Measure'];
312 }
313 if (array_key_exists('TimeSpecify', $_GET))
314 {
315 if (($_SESSION['TimeSpecify'] == 1) and ($_GET['TimeSpecify'] == 0))
316 {
317 $_SESSION['TimeEnd'] = $this->Time - 60;
318 $_SESSION['TimeStart'] = $_SESSION['TimeEnd'] - $this->GetTimeRange();
319 }
320 $_SESSION['TimeSpecify'] = $_GET['TimeSpecify'];
321 }
322
323 if (array_key_exists('Move', $_GET))
324 {
325 $Move = $_GET['Move'];
326 if ($Move == 'Left')
327 {
328 $_SESSION['TimeStart'] = $_SESSION['TimeStart'] - $this->GetTimeRange();
329 $_SESSION['TimeEnd'] = $_SESSION['TimeEnd'] - $this->GetTimeRange();
330 } else
331 if ($Move == 'Right')
332 {
333 $_SESSION['TimeStart'] = $_SESSION['TimeStart'] + $this->GetTimeRange();
334 $_SESSION['TimeEnd'] = $_SESSION['TimeEnd'] + $this->GetTimeRange();
335 } else
336 if ($Move == 'Now')
337 {
338 $_SESSION['TimeEnd'] = $this->Time - 60;
339 $_SESSION['TimeStart'] = $_SESSION['TimeEnd'] - $this->GetTimeRange();
340 } else
341 if ($Move == 'LeftEnd')
342 {
343 $Measure = $this->LoadMeasure($_SESSION['Measure']);
344 $FirstValue = $this->GetFirstMeasure($Measure);
345 $_SESSION['TimeStart'] = $FirstValue['Time'];
346 $_SESSION['TimeEnd'] = $_SESSION['TimeStart'] + $this->GetTimeRange();
347 } else
348 if ($Move == 'RightEnd')
349 {
350 $Measure = $this->LoadMeasure($_SESSION['Measure']);
351 $LastValue = $this->GetLastMeasure($Measure);
352 $_SESSION['TimeEnd'] = $LastValue['Time'];
353 $_SESSION['TimeStart'] = $_SESSION['TimeEnd'] - $this->GetTimeRange();
354 }
355 }
356 }
357
358 function Show(): string
359 {
360 global $Config;
361
362 $Debug = 0;
363 $this->HandleURL();
364
365 $Output = '<div style="text-align: center"><div class="Title">'.$Config['Web']['Title'].'</div>';
366
367 $Result = $this->Database->select('Measure', 'Id', '(`Enabled`=1) AND '.
368 '((`PermissionView`="all") OR (`PermissionView`="'.gethostbyaddr($_SERVER['REMOTE_ADDR']).'")) '.
369 'AND (`Id`='.($_SESSION['Measure'] * 1).')');
370 if ($Result->num_rows == 0)
371 $_SESSION['Measure'] = $Config['DefaultVariables']['Measure'];
372
373 $Output .= $this->ShowTimeRange();
374 $Output .= $this->ShowGraph();
375 $Output .= '<br/>';
376 $Output .= $this->ShowMeasureTable();
377 $Output .= '</div>';
378 return $Output;
379 }
380}
Note: See TracBrowser for help on using the repository browser.