source: trunk/Modules/Measure/Page.php@ 84

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