|
1 <?php |
|
2 /***************************************************************************** |
|
3 * Copyright (C) 2014 |
|
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['CoolerTemp'] The simulator cold temp |
|
82 * @param string $_POST['CoolerTime'] The simulator time to reach this |
|
83 * @param string $_POST['CoolerSize'] The simulator plate size |
|
84 * @param string $_POST['HeaterTemp'] The simulator heater temp |
|
85 * @param string $_POST['HeaterTime'] The simulator time to reach this |
|
86 * @param string $_POST['HeaterSize'] The simulator plate size |
|
87 * @param string $_POST['FrigoIsolation'] The simulator isolation |
|
88 * @param string $_POST['key'] The button pressed. |
|
89 */ |
|
90 function simulator_update() { |
|
91 /* |
|
92 * Build the update command |
|
93 */ |
|
94 if ($_POST['key'] == 'Delete') { |
|
95 send_cmd("DEVICE DEL ".$_POST['UUID']); |
|
96 } |
|
97 |
|
98 if ($_POST['key'] == 'Save') { |
|
99 $cmd = array("DEVICE PUT ".$_POST['UUID']); |
|
100 $cmd[] = "NAME,".$_POST['Name']; |
|
101 $cmd[] = "VOLUME_AIR,".$_POST['VolumeAir']; |
|
102 $cmd[] = "VOLUME_BEER,".$_POST['VolumeBeer']; |
|
103 $cmd[] = "ROOM_TEMPERATURE,".$_POST['RoomTemperature']; |
|
104 $cmd[] = "COOLER_TEMP,".$_POST['CoolerTemp']; |
|
105 $cmd[] = "COOLER_TIME,".$_POST['CoolerTime']; |
|
106 $cmd[] = "COOLER_SIZE,".$_POST['CoolerSize']; |
|
107 $cmd[] = "HEATER_TEMP,".$_POST['HeaterTemp']; |
|
108 $cmd[] = "HEATER_TIME,".$_POST['HeaterTime']; |
|
109 $cmd[] = "HEATER_SIZE,".$_POST['HeaterSize']; |
|
110 $cmd[] = "FRIGO_ISOLATION,".$_POST['FrigoIsolation']; |
|
111 $cmd[] = "."; |
|
112 send_array($cmd); |
|
113 } |
|
114 |
|
115 unset($_POST['UUID']); |
|
116 unset($_POST['Name']); |
|
117 unset($_POST['VolumeAir']); |
|
118 unset($_POST['VolumeBeer']); |
|
119 unset($_POST['RoomTemperature']); |
|
120 unset($_POST['CoolerTemp']); |
|
121 unset($_POST['CoolerTime']); |
|
122 unset($_POST['CoolerSize']); |
|
123 unset($_POST['HeaterTemp']); |
|
124 unset($_POST['HeaterTime']); |
|
125 unset($_POST['HeaterSize']); |
|
126 unset($_POST['FrigoIsolation']); |
|
127 unset($_POST['key']); |
|
128 unset($_POST['command']); |
|
129 load('simulator.php'); |
|
130 } |
|
131 |
|
132 |
|
133 |
|
134 /* |
|
135 * Test input of a modified or new simulator. |
|
136 * |
|
137 * @param string $_POST['UUID'] Unique record UUID |
|
138 * @param string $_POST['Name'] The simulator name |
|
139 * @param string $_POST['VolumeAir'] The simulator Air volume |
|
140 * @param string $_POST['VolumeBeer'] The simulator Beer volume |
|
141 * @param string $_POST['RoomTemperature'] The simulator room temp |
|
142 * @param string $_POST['CoolerTemp'] The simulator cold temp |
|
143 * @param string $_POST['CoolerTime'] The simulator time to reach this |
|
144 * @param string $_POST['CoolerSize'] The simulator plate size |
|
145 * @param string $_POST['HeaterTemp'] The simulator heater temp |
|
146 * @param string $_POST['HeaterTime'] The simulator time to reach this |
|
147 * @param string $_POST['HeaterSize'] The simulator plate size |
|
148 * @param string $_POST['FrigoIsolation'] The simulator isolation |
|
149 * @param string $_POST['key'] Key choice, Save or Cancel |
|
150 * @param string $_POST['command'] Command used, 'add' or 'update' |
|
151 * |
|
152 * Return: 0 = Ok |
|
153 * 1 = Missing data |
|
154 * 2 = Name too short |
|
155 * 3 = Name already in use |
|
156 * 99 = Cancel key |
|
157 */ |
|
158 function test_thedata() { |
|
159 |
|
160 global $arr; |
|
161 |
|
162 if (isset($_POST['UUID']) && isset($_POST['Name']) && isset($_POST['VolumeAir']) && isset($_POST['VolumeBeer']) && |
|
163 isset($_POST['RoomTemperature']) && isset($_POST['CoolerTemp']) && isset($_POST['CoolerTime']) && |
|
164 isset($_POST['CoolerSize']) && isset($_POST['HeaterTemp']) && isset($_POST['HeaterTime']) && isset($_POST['HeaterSize']) && |
|
165 isset($_POST['FrigoIsolation']) && isset($_POST['key']) && isset($_POST['command'])) { |
|
166 |
|
167 if ($_POST['key'] == 'Cancel') |
|
168 return 99; |
|
169 |
|
170 if (strlen($_POST['Name']) < 2) |
|
171 return 2; |
|
172 |
|
173 if (startsWith($arr[0], "212")) { |
|
174 $j = 1; |
|
175 while (1) { |
|
176 if (strcmp($arr[$j], ".") == 0) |
|
177 break; |
|
178 $f = explode(",", $arr[$j]); |
|
179 if (strcmp($f[0], $_POST['UUID']) && ($f[1] == $_POST['Name'])) { |
|
180 return 3; |
|
181 } |
|
182 $j++; |
|
183 } |
|
184 } |
|
185 |
|
186 } else { |
|
187 return 1; |
|
188 } |
|
189 |
|
190 return 0; |
|
191 } |
|
192 |
|
193 |
|
194 |
|
195 /* |
|
196 * Test result from edit screen and do next action |
|
197 */ |
|
198 function testdata() { |
|
199 |
|
200 $result = test_thedata(); |
|
201 $error = ''; |
|
202 |
|
203 switch ($result) { |
|
204 case 0: if ($_POST['command'] == 'add') { |
|
205 simulator_add(); |
|
206 return; |
|
207 } else if ($_POST['command'] == 'update') { |
|
208 simulator_update(); |
|
209 return; |
|
210 } |
|
211 break; |
|
212 case 1: $error = 'Missing data'; |
|
213 break; |
|
214 case 2: $error = 'The Name is too short'; |
|
215 break; |
|
216 case 3: $error = 'The Name is already in use, choose another one'; |
|
217 break; |
|
218 case 99: |
|
219 load('simulator.php'); |
|
220 break; |
|
221 } |
|
222 |
|
223 if ($_POST['command'] == 'add') { |
|
224 $heading = 'ThermFerm - Add Simulator'; |
|
225 } else { |
|
226 $heading = 'ThermFerm - Edit Simulator'; |
|
227 } |
|
228 |
|
229 edit_screen($_POST['UUID'], $_POST['command'], $heading, $error); |
|
230 } |
|
231 |
|
232 |
|
233 |
|
234 /* |
|
235 * Simulator edit screen. Used by simulator_edit(), simulator_add() and testdata() |
|
236 * |
|
237 * @param string $UUID The record UUID (fixed). |
|
238 * @param string $command 'add' or 'update' |
|
239 * @param string $heading Page heading title. |
|
240 * @Param string $error_message Blank or previous error. |
|
241 */ |
|
242 function edit_screen($UUID, $command, $heading, $error_message) { |
|
243 |
|
244 $answer = send_cmd("SIMULATOR GET ".$UUID); |
|
245 $reply = explode("\r\n", $answer); |
|
246 |
|
247 $outstr = build_header($heading); |
|
248 $outstr .= ' <div id="errors">'.PHP_EOL; |
|
249 $outstr .= ' '.$error_message.PHP_EOL; |
|
250 $outstr .= ' </div> <!-- errors -->'.PHP_EOL; |
|
251 $outstr .= ' <div id="etable">'.PHP_EOL; |
|
252 $outstr .= ' <form method="POST" action="simulator.php">'.PHP_EOL; |
|
253 $outstr .= ' <table class="editor">'.PHP_EOL; |
|
254 |
|
255 if (startsWith($reply[0], "213")) { |
|
256 $j = 1; |
|
257 while (1) { |
|
258 if (strcmp($reply[$j], ".") == 0) |
|
259 break; |
|
260 $f = explode(",", $reply[$j]); |
|
261 if ($f[0] == "NAME") { |
|
262 $outstr .= ' <tr class="editor">'.PHP_EOL; |
|
263 $outstr .= ' <td class="editname">Name</td>'.PHP_EOL; |
|
264 $outstr .= ' <td class="editfield"><input type="text" name="Name" size="50" value="'.$f[1].'"></td>'.PHP_EOL; |
|
265 $outstr .= ' </tr>'.PHP_EOL; |
|
266 } |
|
267 if ($f[0] == "VOLUME_AIR") { |
|
268 $outstr .= ' <tr class="editor">'.PHP_EOL; |
|
269 $outstr .= ' <td class="editname">Volume Air</td>'.PHP_EOL; |
|
270 $outstr .= ' <td class="editfield"><input type="text" name="VolumeAir" size="5" value="'.$f[1].'"></td>'.PHP_EOL; |
|
271 $outstr .= ' </tr>'.PHP_EOL; |
|
272 } |
|
273 if ($f[0] == "VOLUME_BEER") { |
|
274 $outstr .= ' <tr class="editor">'.PHP_EOL; |
|
275 $outstr .= ' <td class="editname">Volume Beer</td>'.PHP_EOL; |
|
276 $outstr .= ' <td class="editfield"><input type="text" name="VolumeBeer" size="5" value="'.$f[1].'"></td>'.PHP_EOL; |
|
277 $outstr .= ' </tr>'.PHP_EOL; |
|
278 } |
|
279 if ($f[0] == "ROOM_TEMPERATURE") { |
|
280 $outstr .= ' <tr class="editor">'.PHP_EOL; |
|
281 $outstr .= ' <td class="editname">Room Temperature</td>'.PHP_EOL; |
|
282 $outstr .= ' <td class="editfield"><input type="text" name="RoomTemperature" size="5" value="'.$f[1].'"></td>'.PHP_EOL; |
|
283 $outstr .= ' </tr>'.PHP_EOL; |
|
284 } |
|
285 if ($f[0] == "COOLER_TEMP") { |
|
286 $outstr .= ' <tr class="editor">'.PHP_EOL; |
|
287 $outstr .= ' <td class="editname">Cooler Temperature</td>'.PHP_EOL; |
|
288 $outstr .= ' <td class="editfield"><input type="text" name="CoolerTemp" size="5" value="'.$f[1].'"></td>'.PHP_EOL; |
|
289 $outstr .= ' </tr>'.PHP_EOL; |
|
290 } |
|
291 if ($f[0] == "COOLER_TIME") { |
|
292 $outstr .= ' <tr class="editor">'.PHP_EOL; |
|
293 $outstr .= ' <td class="editname">Cooler Time</td>'.PHP_EOL; |
|
294 $outstr .= ' <td class="editfield"><input type="text" name="CoolerTime" size="5" value="'.$f[1].'"></td>'.PHP_EOL; |
|
295 $outstr .= ' </tr>'.PHP_EOL; |
|
296 } |
|
297 if ($f[0] == "COOLER_SIZE") { |
|
298 $outstr .= ' <tr class="editor">'.PHP_EOL; |
|
299 $outstr .= ' <td class="editname">Cooler Size</td>'.PHP_EOL; |
|
300 $outstr .= ' <td class="editfield"><input type="text" name="CoolerSize" size="5" value="'.$f[1].'"></td>'.PHP_EOL; |
|
301 $outstr .= ' </tr>'.PHP_EOL; |
|
302 } |
|
303 if ($f[0] == "HEATER_TEMP") { |
|
304 $outstr .= ' <tr class="editor">'.PHP_EOL; |
|
305 $outstr .= ' <td class="editname">Heater Temperature</td>'.PHP_EOL; |
|
306 $outstr .= ' <td class="editfield"><input type="text" name="HeaterTemp" size="5" value="'.$f[1].'"></td>'.PHP_EOL; |
|
307 $outstr .= ' </tr>'.PHP_EOL; |
|
308 } |
|
309 if ($f[0] == "HEATER_TIME") { |
|
310 $outstr .= ' <tr class="editor">'.PHP_EOL; |
|
311 $outstr .= ' <td class="editname">Heater Time</td>'.PHP_EOL; |
|
312 $outstr .= ' <td class="editfield"><input type="text" name="HeaterTime" size="5" value="'.$f[1].'"></td>'.PHP_EOL; |
|
313 $outstr .= ' </tr>'.PHP_EOL; |
|
314 } |
|
315 if ($f[0] == "HEATER_SIZE") { |
|
316 $outstr .= ' <tr class="editor">'.PHP_EOL; |
|
317 $outstr .= ' <td class="editname">Heater Size</td>'.PHP_EOL; |
|
318 $outstr .= ' <td class="editfield"><input type="text" name="HeaterSize" size="5" value="'.$f[1].'"></td>'.PHP_EOL; |
|
319 $outstr .= ' </tr>'.PHP_EOL; |
|
320 } |
|
321 if ($f[0] == "FRIGO_ISOLATION") { |
|
322 $outstr .= ' <tr class="editor">'.PHP_EOL; |
|
323 $outstr .= ' <td class="editname">Frigo Isolation</td>'.PHP_EOL; |
|
324 $outstr .= ' <td class="editfield"><input type="text" name="FrigoIsolation" size="5" value="'.$f[1].'"></td>'.PHP_EOL; |
|
325 $outstr .= ' </tr>'.PHP_EOL; |
|
326 } |
|
327 $j++; |
|
328 } |
|
329 } |
|
330 |
|
331 $outstr .= ' <tr class="editor">'.PHP_EOL; |
|
332 $outstr .= ' <td class="editname"><input type="submit" value="Save" name="key"></td>'.PHP_EOL; |
|
333 $outstr .= ' <td class="editfield"><input type="submit" value="Cancel" name="key">'; |
|
334 $outstr .= '<input type="submit" value="Delete" name="key" style="margin-left: 100px;">'; |
|
335 $outstr .= '<input type="hidden" value="testdata" name="action">'; |
|
336 $outstr .= '<input type="hidden" value="'.$command.'" name="command">'; |
|
337 $outstr .= '<input type="hidden" value="'.$UUID.'" name="UUID"></td>'.PHP_EOL; |
|
338 $outstr .= ' </tr>'.PHP_EOL; |
|
339 $outstr .= ' </table>'.PHP_EOL; |
|
340 $outstr .= ' </form>'.PHP_EOL; |
|
341 $outstr .= ' </div> <!-- etable -->'.PHP_EOL; |
|
342 $outstr .= build_footer(); |
|
343 echo $outstr; |
|
344 } |
|
345 |
|
346 |
|
347 |
|
348 /* |
|
349 * Edit a Simulator. Fetches the record data and shows the edit screen. |
|
350 * |
|
351 * @param string $_GET['action'] Must be 'edit'. |
|
352 * @param string $_GET['UUID'] The UUID of the Unit. |
|
353 */ |
|
354 function simulator_edit() { |
|
355 if ($_GET['action'] == 'edit') { |
|
356 edit_screen($_GET['UUID'], 'update', 'ThermFerm - Edit Simulator', ''); |
|
357 return; |
|
358 } else { |
|
359 load('simulator.php'); |
|
360 } |
|
361 } |
|
362 |
|
363 |
|
364 |
|
365 /* |
|
366 * @link edit simulator |
|
367 * @link add simulator |
|
368 */ |
|
369 function simulator_list() { |
|
370 global $arr; |
|
371 |
|
372 $outstr = build_header("ThermFerm - Simulator Setup"); |
|
373 $outstr .= ' <div id="errors">'.PHP_EOL; |
|
374 $outstr .= ' </div> <!-- errors -->'.PHP_EOL; |
|
375 $outstr .= ' <div id="etable">'.PHP_EOL; |
|
376 $outstr .= ' <table class="setup">'.PHP_EOL; |
|
377 $outstr .= ' <tr class="trhead">'.PHP_EOL; |
|
378 $outstr .= ' <td class="setup" style="width: 300px;">UUID</td>'.PHP_EOL; |
|
379 $outstr .= ' <td class="setup" style="width: 300px;">Name</td>'.PHP_EOL; |
|
380 $outstr .= ' <td class="setup" style="width: 35px;">Edit</td>'.PHP_EOL; |
|
381 $outstr .= ' </tr>'.PHP_EOL; |
|
382 |
|
383 if (startsWith($arr[0], "212")) { |
|
384 $j = 1; |
|
385 while (1) { |
|
386 if (strcmp($arr[$j], ".") == 0) |
|
387 break; |
|
388 $f = explode(",", $arr[$j]); |
|
389 $outstr .= ' <tr class="setup">'.PHP_EOL; |
|
390 $outstr .= ' <td class="setup">'.$f[0].'</td>'.PHP_EOL; |
|
391 $outstr .= ' <td class="setup">'.$f[1].'</td>'.PHP_EOL; |
|
392 $outstr .= ' <td class="setup"><a href="simulator.php?action=edit&UUID='.$f[0].'">Edit</a></td>'.PHP_EOL; |
|
393 $outstr .= ' </tr>'.PHP_EOL; |
|
394 $j++; |
|
395 } |
|
396 } |
|
397 |
|
398 $outstr .= ' </table>'.PHP_EOL; |
|
399 $outstr .= ' </div> <!-- etable -->'.PHP_EOL; |
|
400 |
|
401 $outstr .= ' <div id="atable">'.PHP_EOL; |
|
402 $outstr .= ' <form method="POST" action="simulator.php">'.PHP_EOL; |
|
403 $outstr .= ' <table class="editor">'.PHP_EOL; |
|
404 $outstr .= ' <tr class="trhead"><td colspan="3">Add new Simulator</td></tr>'.PHP_EOL; |
|
405 $outstr .= ' <tr class="editor">'.PHP_EOL; |
|
406 $outstr .= ' <td class="editname">Simulator Name</td>'.PHP_EOL; |
|
407 $outstr .= ' <td class="editfield"><input type="text" name="Name" size="50" value=""></td>'.PHP_EOL; |
|
408 $outstr .= ' <td class="editsub"><input type="submit" value="Add" name="key"></td>'.PHP_EOL; |
|
409 $outstr .= '<input type="hidden" value="testdata" name="action">'; |
|
410 $outstr .= '<input type="hidden" value="add" name="command">'; |
|
411 $outstr .= '<input type="hidden" value="00000000-0000-0000-0000-000000000000" name="UUID">'; |
|
412 $outstr .= '<input type="hidden" value="150" name="VolumeAir">'; |
|
413 $outstr .= '<input type="hidden" value="50" name="VolumeBeer">'; |
|
414 $outstr .= '<input type="hidden" value="20.0" name="RoomTemperature">'; |
|
415 $outstr .= '<input type="hidden" value="-3.0" name="CoolerTemp">'; |
|
416 $outstr .= '<input type="hidden" value="720" name="CoolerTime">'; |
|
417 $outstr .= '<input type="hidden" value="0.8" name="CoolerSize">'; |
|
418 $outstr .= '<input type="hidden" value="150" name="HeaterTemp">'; |
|
419 $outstr .= '<input type="hidden" value="12" name="HeaterTime">'; |
|
420 $outstr .= '<input type="hidden" value="0.01" name="HeaterSize">'; |
|
421 $outstr .= '<input type="hidden" value="0.04" name="FrigoIsolation">'; |
|
422 $outstr .= ' </tr>'.PHP_EOL; |
|
423 $outstr .= ' </table>'.PHP_EOL; |
|
424 $outstr .= ' </form>'.PHP_EOL; |
|
425 $outstr .= ' </div> <!-- atable -->'.PHP_EOL; |
|
426 |
|
427 $outstr .= build_footer(); |
|
428 |
|
429 echo $outstr; |
|
430 } |
|
431 |