www-thermferm/simulator.php

changeset 709
5b6d7b640e52
parent 708
13555c27b592
child 710
abe60578d695
equal deleted inserted replaced
708:13555c27b592 709:5b6d7b640e52
1 <?php
2 /*****************************************************************************
3 * Copyright (C) 2014-2018
4 *
5 * Michiel Broek <mbroek at mbse dot eu>
6 *
7 * This file is part of ThermFerm
8 *
9 * This is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2, or (at your option) any
12 * later version.
13 *
14 * ThermFerm is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with ThermFerm; see the file COPYING. If not, write to the Free
21 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 *****************************************************************************/
23
24 require_once('utilities.php');
25
26 /*
27 * $arr contains the complete reply of the SIMULATOR LIST command
28 */
29 $answer = send_cmd("SIMULATOR LIST");
30 $arr = explode("\r\n", $answer);
31
32
33 if (isset($_GET['action'])) {
34 switch ($_GET['action']) {
35 case 'edit': simulator_edit();
36 break;
37 default: break;
38 }
39 } elseif (isset($_POST['action'])) {
40 switch ($_POST['action']) {
41 case 'testdata': testdata();
42 break;
43 default: break;
44 }
45 } else {
46 simulator_list();
47 }
48
49 exit;
50
51 /****************************************************************************
52 *
53 */
54
55
56 /*
57 * Simulator add
58 *
59 * @param string $_POST['Name'] The simulator name
60 */
61 function simulator_add() {
62 if ($_POST['key'] == 'Add')
63 send_cmd("SIMULATOR ADD ".$_POST['Name']);
64 unset($_POST['UUID']);
65 unset($_POST['Name']);
66 unset($_POST['key']);
67 unset($_POST['command']);
68 load('simulator.php');
69 }
70
71
72
73 /*
74 * Simulator update
75 *
76 * @param string $_POST['UUID'] The simulator UUID
77 * @param string $_POST['Name'] The simulator name
78 * @param string $_POST['VolumeAir'] The simulator Air volume
79 * @param string $_POST['VolumeBeer'] The simulator Beer volume
80 * @param string $_POST['RoomTemperature'] The simulator room temp
81 * @param string $_POST['RoomHumidity'] The simulator room humidity
82 * @param string $_POST['CoolerTemp'] The simulator cold temp
83 * @param string $_POST['CoolerTime'] The simulator time to reach this
84 * @param string $_POST['CoolerSize'] The simulator plate size
85 * @param string $_POST['HeaterTemp'] The simulator heater temp
86 * @param string $_POST['HeaterTime'] The simulator time to reach this
87 * @param string $_POST['HeaterSize'] The simulator plate size
88 * @param string $_POST['FrigoIsolation'] The simulator isolation
89 * @param string $_POST['key'] The button pressed.
90 */
91 function simulator_update() {
92 /*
93 * Build the update command
94 */
95 if ($_POST['key'] == 'Delete') {
96 send_cmd("SIMULATOR DEL ".$_POST['UUID']);
97 }
98
99 if ($_POST['key'] == 'Save') {
100 $cmd = array("SIMULATOR PUT ".$_POST['UUID']);
101 $cmd[] = "NAME,".$_POST['Name'];
102 $cmd[] = "VOLUME_AIR,".$_POST['VolumeAir'];
103 $cmd[] = "VOLUME_BEER,".$_POST['VolumeBeer'];
104 $cmd[] = "ROOM_TEMPERATURE,".$_POST['RoomTemperature'];
105 $cmd[] = "ROOM_HUMIDITY,".$_POST['RoomHumidity'];
106 $cmd[] = "COOLER_TEMP,".$_POST['CoolerTemp'];
107 $cmd[] = "COOLER_TIME,".$_POST['CoolerTime'];
108 $cmd[] = "COOLER_SIZE,".$_POST['CoolerSize'];
109 $cmd[] = "HEATER_TEMP,".$_POST['HeaterTemp'];
110 $cmd[] = "HEATER_TIME,".$_POST['HeaterTime'];
111 $cmd[] = "HEATER_SIZE,".$_POST['HeaterSize'];
112 $cmd[] = "FRIGO_ISOLATION,".$_POST['FrigoIsolation'];
113 $cmd[] = ".";
114 send_array($cmd);
115 }
116
117 unset($_POST['UUID']);
118 unset($_POST['Name']);
119 unset($_POST['VolumeAir']);
120 unset($_POST['VolumeBeer']);
121 unset($_POST['RoomTemperature']);
122 unset($_POST['RoomHumidity']);
123 unset($_POST['CoolerTemp']);
124 unset($_POST['CoolerTime']);
125 unset($_POST['CoolerSize']);
126 unset($_POST['HeaterTemp']);
127 unset($_POST['HeaterTime']);
128 unset($_POST['HeaterSize']);
129 unset($_POST['FrigoIsolation']);
130 unset($_POST['key']);
131 unset($_POST['command']);
132 load('simulator.php');
133 }
134
135
136
137 /*
138 * Test input of a modified or new simulator.
139 *
140 * @param string $_POST['UUID'] Unique record UUID
141 * @param string $_POST['Name'] The simulator name
142 * @param string $_POST['VolumeAir'] The simulator Air volume
143 * @param string $_POST['VolumeBeer'] The simulator Beer volume
144 * @param string $_POST['RoomTemperature'] The simulator room temp
145 * @param string $_POST['RoomHumidity'] The simulator room humidity
146 * @param string $_POST['CoolerTemp'] The simulator cold temp
147 * @param string $_POST['CoolerTime'] The simulator time to reach this
148 * @param string $_POST['CoolerSize'] The simulator plate size
149 * @param string $_POST['HeaterTemp'] The simulator heater temp
150 * @param string $_POST['HeaterTime'] The simulator time to reach this
151 * @param string $_POST['HeaterSize'] The simulator plate size
152 * @param string $_POST['FrigoIsolation'] The simulator isolation
153 * @param string $_POST['key'] Key choice, Save or Cancel
154 * @param string $_POST['command'] Command used, 'add' or 'update'
155 *
156 * Return: 0 = Ok
157 * 1 = Missing data
158 * 2 = Name too short
159 * 3 = Name already in use
160 * 99 = Cancel key
161 */
162 function test_thedata() {
163
164 global $arr;
165
166 if (isset($_POST['UUID']) && isset($_POST['Name']) && isset($_POST['VolumeAir']) && isset($_POST['VolumeBeer']) &&
167 isset($_POST['RoomTemperature']) && isset($_POST['RoomHumidity']) && isset($_POST['CoolerTemp']) && isset($_POST['CoolerTime']) &&
168 isset($_POST['CoolerSize']) && isset($_POST['HeaterTemp']) && isset($_POST['HeaterTime']) && isset($_POST['HeaterSize']) &&
169 isset($_POST['FrigoIsolation']) && isset($_POST['key']) && isset($_POST['command'])) {
170
171 if ($_POST['key'] == 'Cancel')
172 return 99;
173
174 if (strlen($_POST['Name']) < 2)
175 return 2;
176
177 if (startsWith($arr[0], "212")) {
178 $j = 1;
179 while (1) {
180 if (strcmp($arr[$j], ".") == 0)
181 break;
182 $f = explode(",", $arr[$j]);
183 if (strcmp($f[0], $_POST['UUID']) && ($f[1] == $_POST['Name'])) {
184 return 3;
185 }
186 $j++;
187 }
188 }
189
190 } else {
191 return 1;
192 }
193
194 return 0;
195 }
196
197
198
199 /*
200 * Test result from edit screen and do next action
201 */
202 function testdata() {
203
204 $result = test_thedata();
205 $error = '';
206
207 switch ($result) {
208 case 0: if ($_POST['command'] == 'add') {
209 simulator_add();
210 return;
211 } else if ($_POST['command'] == 'update') {
212 simulator_update();
213 return;
214 }
215 break;
216 case 1: $error = 'Missing data';
217 break;
218 case 2: $error = 'The Name is too short';
219 break;
220 case 3: $error = 'The Name is already in use, choose another one';
221 break;
222 case 99:
223 load('simulator.php');
224 break;
225 }
226
227 if ($_POST['command'] == 'add') {
228 $heading = 'ThermFerm - Add Simulator';
229 } else {
230 $heading = 'ThermFerm - Edit Simulator';
231 }
232
233 edit_screen($_POST['UUID'], $_POST['command'], $heading, $error);
234 }
235
236
237
238 /*
239 * Simulator edit screen. Used by simulator_edit(), simulator_add() and testdata()
240 *
241 * @param string $UUID The record UUID (fixed).
242 * @param string $command 'add' or 'update'
243 * @param string $heading Page heading title.
244 * @Param string $error_message Blank or previous error.
245 */
246 function edit_screen($UUID, $command, $heading, $error_message) {
247
248 $answer = send_cmd("SIMULATOR GET ".$UUID);
249 $reply = explode("\r\n", $answer);
250
251 $outstr = build_header($heading);
252 $outstr .= ' <div id="errors">'.PHP_EOL;
253 $outstr .= ' '.$error_message.PHP_EOL;
254 $outstr .= ' </div> <!-- errors -->'.PHP_EOL;
255 $outstr .= ' <div id="etable">'.PHP_EOL;
256 $outstr .= ' <form method="POST" action="simulator.php">'.PHP_EOL;
257 $outstr .= ' <table class="editor">'.PHP_EOL;
258
259 if (startsWith($reply[0], "213")) {
260 $j = 1;
261 while (1) {
262 if (strcmp($reply[$j], ".") == 0)
263 break;
264 $f = explode(",", $reply[$j]);
265 if ($f[0] == "NAME") {
266 $outstr .= ' <tr class="editor">'.PHP_EOL;
267 $outstr .= ' <td class="editname">Name</td>'.PHP_EOL;
268 $outstr .= ' <td class="editfield"><input type="text" name="Name" size="50" value="'.$f[1].'"></td>'.PHP_EOL;
269 $outstr .= ' </tr>'.PHP_EOL;
270 }
271 if ($f[0] == "VOLUME_AIR") {
272 $outstr .= ' <tr class="editor">'.PHP_EOL;
273 $outstr .= ' <td class="editname">Volume Air</td>'.PHP_EOL;
274 $outstr .= ' <td class="editfield"><input type="text" name="VolumeAir" size="5" value="'.$f[1].'"></td>'.PHP_EOL;
275 $outstr .= ' </tr>'.PHP_EOL;
276 }
277 if ($f[0] == "VOLUME_BEER") {
278 $outstr .= ' <tr class="editor">'.PHP_EOL;
279 $outstr .= ' <td class="editname">Volume Beer</td>'.PHP_EOL;
280 $outstr .= ' <td class="editfield"><input type="text" name="VolumeBeer" size="5" value="'.$f[1].'"></td>'.PHP_EOL;
281 $outstr .= ' </tr>'.PHP_EOL;
282 }
283 if ($f[0] == "ROOM_TEMPERATURE") {
284 $outstr .= ' <tr class="editor">'.PHP_EOL;
285 $outstr .= ' <td class="editname">Room Temperature</td>'.PHP_EOL;
286 $outstr .= ' <td class="editfield"><input type="text" name="RoomTemperature" size="5" value="'.$f[1].'"></td>'.PHP_EOL;
287 $outstr .= ' </tr>'.PHP_EOL;
288 }
289 if ($f[0] == "ROOM_HUMIDITY") {
290 $outstr .= ' <tr class="editor">'.PHP_EOL;
291 $outstr .= ' <td class="editname">Room Humidity</td>'.PHP_EOL;
292 $outstr .= ' <td class="editfield"><input type="text" name="RoomHumidity" size="5" value="'.$f[1].'"></td>'.PHP_EOL;
293 $outstr .= ' </tr>'.PHP_EOL;
294 }
295 if ($f[0] == "COOLER_TEMP") {
296 $outstr .= ' <tr class="editor">'.PHP_EOL;
297 $outstr .= ' <td class="editname">Cooler Temperature</td>'.PHP_EOL;
298 $outstr .= ' <td class="editfield"><input type="text" name="CoolerTemp" size="5" value="'.$f[1].'"></td>'.PHP_EOL;
299 $outstr .= ' </tr>'.PHP_EOL;
300 }
301 if ($f[0] == "COOLER_TIME") {
302 $outstr .= ' <tr class="editor">'.PHP_EOL;
303 $outstr .= ' <td class="editname">Cooler Time</td>'.PHP_EOL;
304 $outstr .= ' <td class="editfield"><input type="text" name="CoolerTime" size="5" value="'.$f[1].'"></td>'.PHP_EOL;
305 $outstr .= ' </tr>'.PHP_EOL;
306 }
307 if ($f[0] == "COOLER_SIZE") {
308 $outstr .= ' <tr class="editor">'.PHP_EOL;
309 $outstr .= ' <td class="editname">Cooler Size</td>'.PHP_EOL;
310 $outstr .= ' <td class="editfield"><input type="text" name="CoolerSize" size="5" value="'.$f[1].'"></td>'.PHP_EOL;
311 $outstr .= ' </tr>'.PHP_EOL;
312 }
313 if ($f[0] == "HEATER_TEMP") {
314 $outstr .= ' <tr class="editor">'.PHP_EOL;
315 $outstr .= ' <td class="editname">Heater Temperature</td>'.PHP_EOL;
316 $outstr .= ' <td class="editfield"><input type="text" name="HeaterTemp" size="5" value="'.$f[1].'"></td>'.PHP_EOL;
317 $outstr .= ' </tr>'.PHP_EOL;
318 }
319 if ($f[0] == "HEATER_TIME") {
320 $outstr .= ' <tr class="editor">'.PHP_EOL;
321 $outstr .= ' <td class="editname">Heater Time</td>'.PHP_EOL;
322 $outstr .= ' <td class="editfield"><input type="text" name="HeaterTime" size="5" value="'.$f[1].'"></td>'.PHP_EOL;
323 $outstr .= ' </tr>'.PHP_EOL;
324 }
325 if ($f[0] == "HEATER_SIZE") {
326 $outstr .= ' <tr class="editor">'.PHP_EOL;
327 $outstr .= ' <td class="editname">Heater Size</td>'.PHP_EOL;
328 $outstr .= ' <td class="editfield"><input type="text" name="HeaterSize" size="5" value="'.$f[1].'"></td>'.PHP_EOL;
329 $outstr .= ' </tr>'.PHP_EOL;
330 }
331 if ($f[0] == "FRIGO_ISOLATION") {
332 $outstr .= ' <tr class="editor">'.PHP_EOL;
333 $outstr .= ' <td class="editname">Frigo Isolation</td>'.PHP_EOL;
334 $outstr .= ' <td class="editfield"><input type="text" name="FrigoIsolation" size="5" value="'.$f[1].'"></td>'.PHP_EOL;
335 $outstr .= ' </tr>'.PHP_EOL;
336 }
337 $j++;
338 }
339 }
340
341 $outstr .= ' <tr class="editor">'.PHP_EOL;
342 $outstr .= ' <td class="editname"><input type="submit" value="Save" name="key"></td>'.PHP_EOL;
343 $outstr .= ' <td class="editfield"><input type="submit" value="Cancel" name="key">';
344 $outstr .= '<input type="submit" value="Delete" name="key" style="margin-left: 100px;">';
345 $outstr .= '<input type="hidden" value="testdata" name="action">';
346 $outstr .= '<input type="hidden" value="'.$command.'" name="command">';
347 $outstr .= '<input type="hidden" value="'.$UUID.'" name="UUID"></td>'.PHP_EOL;
348 $outstr .= ' </tr>'.PHP_EOL;
349 $outstr .= ' </table>'.PHP_EOL;
350 $outstr .= ' </form>'.PHP_EOL;
351 $outstr .= ' </div> <!-- etable -->'.PHP_EOL;
352 $outstr .= ' <script type="text/javascript">'.PHP_EOL;
353 $outstr .= ' $(document).ready(function () {'.PHP_EOL;
354 $outstr .= ' $("#maintenance").jqxButton({ width: 150, height: 25, theme: \'ui-redmond\' });'.PHP_EOL;
355 $outstr .= ' });'.PHP_EOL;
356 $outstr .= ' </script>'.PHP_EOL;
357 $outstr .= build_footer();
358 echo $outstr;
359 }
360
361
362
363 /*
364 * Edit a Simulator. Fetches the record data and shows the edit screen.
365 *
366 * @param string $_GET['action'] Must be 'edit'.
367 * @param string $_GET['UUID'] The UUID of the Unit.
368 */
369 function simulator_edit() {
370 if ($_GET['action'] == 'edit') {
371 edit_screen($_GET['UUID'], 'update', 'ThermFerm - Edit Simulator', '');
372 return;
373 } else {
374 load('simulator.php');
375 }
376 }
377
378
379
380 /*
381 * @link edit simulator
382 * @link add simulator
383 */
384 function simulator_list() {
385 global $arr;
386
387 $outstr = build_header("ThermFerm - Simulator Setup");
388 $outstr .= ' <div id="errors">'.PHP_EOL;
389 $outstr .= ' </div> <!-- errors -->'.PHP_EOL;
390 $outstr .= ' <div id="etable">'.PHP_EOL;
391 $outstr .= ' <table class="setup">'.PHP_EOL;
392 $outstr .= ' <tr class="trhead">'.PHP_EOL;
393 $outstr .= ' <td class="setup" style="width: 300px;">UUID</td>'.PHP_EOL;
394 $outstr .= ' <td class="setup" style="width: 300px;">Name</td>'.PHP_EOL;
395 $outstr .= ' <td class="setup" style="width: 35px;">Edit</td>'.PHP_EOL;
396 $outstr .= ' </tr>'.PHP_EOL;
397
398 if (startsWith($arr[0], "212")) {
399 $j = 1;
400 while (1) {
401 if (strcmp($arr[$j], ".") == 0)
402 break;
403 $f = explode(",", $arr[$j]);
404 $outstr .= ' <tr class="setup">'.PHP_EOL;
405 $outstr .= ' <td class="setup">'.$f[0].'</td>'.PHP_EOL;
406 $outstr .= ' <td class="setup">'.$f[1].'</td>'.PHP_EOL;
407 $outstr .= ' <td class="setup"><a href="simulator.php?action=edit&amp;UUID='.$f[0].'">Edit</a></td>'.PHP_EOL;
408 $outstr .= ' </tr>'.PHP_EOL;
409 $j++;
410 }
411 }
412
413 $outstr .= ' </table>'.PHP_EOL;
414 $outstr .= ' </div> <!-- etable -->'.PHP_EOL;
415
416 $outstr .= ' <div id="atable">'.PHP_EOL;
417 $outstr .= ' <form method="POST" action="simulator.php">'.PHP_EOL;
418 $outstr .= ' <table class="editor">'.PHP_EOL;
419 $outstr .= ' <tr class="trhead"><td colspan="3">Add new Simulator</td></tr>'.PHP_EOL;
420 $outstr .= ' <tr class="editor">'.PHP_EOL;
421 $outstr .= ' <td class="editname">Simulator Name</td>'.PHP_EOL;
422 $outstr .= ' <td class="editfield"><input type="text" name="Name" size="50" value=""></td>'.PHP_EOL;
423 $outstr .= ' <td class="editsub"><input type="submit" value="Add" name="key"></td>'.PHP_EOL;
424 $outstr .= '<input type="hidden" value="testdata" name="action">';
425 $outstr .= '<input type="hidden" value="add" name="command">';
426 $outstr .= '<input type="hidden" value="00000000-0000-0000-0000-000000000000" name="UUID">';
427 $outstr .= '<input type="hidden" value="150" name="VolumeAir">';
428 $outstr .= '<input type="hidden" value="50" name="VolumeBeer">';
429 $outstr .= '<input type="hidden" value="20.0" name="RoomTemperature">';
430 $outstr .= '<input type="hidden" value="48.5" name="RoomHumidity">';
431 $outstr .= '<input type="hidden" value="-3.0" name="CoolerTemp">';
432 $outstr .= '<input type="hidden" value="720" name="CoolerTime">';
433 $outstr .= '<input type="hidden" value="0.8" name="CoolerSize">';
434 $outstr .= '<input type="hidden" value="150" name="HeaterTemp">';
435 $outstr .= '<input type="hidden" value="12" name="HeaterTime">';
436 $outstr .= '<input type="hidden" value="0.01" name="HeaterSize">';
437 $outstr .= '<input type="hidden" value="0.04" name="FrigoIsolation">';
438 $outstr .= ' </tr>'.PHP_EOL;
439 $outstr .= ' </table>'.PHP_EOL;
440 $outstr .= ' </form>'.PHP_EOL;
441 $outstr .= ' </div> <!-- atable -->'.PHP_EOL;
442
443 $outstr .= ' <script type="text/javascript">'.PHP_EOL;
444 $outstr .= ' $(document).ready(function () {'.PHP_EOL;
445 $outstr .= ' $("#maintenance").jqxButton({ width: 150, height: 25, theme: \'ui-redmond\' });'.PHP_EOL;
446 $outstr .= ' });'.PHP_EOL;
447 $outstr .= ' </script>'.PHP_EOL;
448 $outstr .= build_footer();
449
450 echo $outstr;
451 }
452

mercurial