Fri, 11 Jul 2014 22:53:33 +0200
Basic web idea
100 | 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 | ||
25 | require_once('utilities.php'); | |
26 | ||
27 | ||
28 | ||
29 | function showunit($unit) | |
30 | { | |
31 | $outstr = ''; | |
32 | ||
33 | $sock = open_socket(); | |
34 | if ($sock == false) { | |
35 | return ''; | |
36 | } | |
37 | ||
38 | socket_write($sock, "UNIT " . $unit, 4096); | |
39 | $answer = ""; | |
40 | while (1) { | |
41 | $line = socket_read($sock, 4096); | |
42 | if ($line === '') | |
43 | break; | |
44 | $answer .= $line; | |
45 | } | |
46 | socket_close($sock); | |
47 | ||
48 | $sock = open_socket(); | |
49 | if ($sock == false) { | |
50 | return ''; | |
51 | } | |
52 | ||
53 | socket_write($sock, "LIST UNIT", 4096); | |
54 | $answer = ""; | |
55 | while (1) { | |
56 | $line = socket_read($sock, 4096); | |
57 | if ($line === '') | |
58 | break; | |
59 | $answer .= $line; | |
60 | } | |
61 | socket_close($sock); | |
62 | $arr = explode("\r\n", $answer); | |
63 | ||
64 | if (startsWith($arr[0], "213")) { | |
65 | ||
66 | $outstr .= ' <div id="fermentor">'.PHP_EOL; | |
67 | ||
68 | foreach($arr as $l) { | |
69 | $vals = explode(",", $l); | |
70 | if (strcmp($vals[0], "NAME") == 0) { | |
71 | /* | |
72 | * The name is allways first, so when we have it | |
73 | * emit the formatting. | |
74 | */ | |
75 | $outstr .= ' <div id="fermentor_head">'.PHP_EOL; | |
76 | $outstr .= ' ' . $vals[1] .PHP_EOL; | |
77 | $outstr .= ' </div>'.PHP_EOL; | |
78 | $outstr .= ' <div id="fermentor_pict">'.PHP_EOL; | |
79 | $outstr .= ' <img src="images/Fermenter_60l.gif">'.PHP_EOL; | |
80 | $outstr .= ' </div>'.PHP_EOL; | |
81 | $outstr .= ' <div id="fermentor_control">'.PHP_EOL; | |
82 | } | |
83 | if (strcmp($vals[0], "MODE") == 0) { | |
84 | $outstr .= ' mode ' . $vals[1] . '<br>' .PHP_EOL; | |
85 | } | |
86 | } | |
87 | $outstr .= ' temp 12.3'.PHP_EOL; | |
88 | $outstr .= ' </div>'.PHP_EOL; | |
89 | $outstr .= ' </div>'.PHP_EOL; | |
90 | } | |
91 | ||
92 | return $outstr; | |
93 | } | |
94 | ||
95 | ||
96 | ||
97 | function liveview() | |
98 | { | |
99 | $outstr = PHP_EOL; | |
100 | ||
101 | $sock = open_socket(); | |
102 | if ($sock !== false) { | |
103 | socket_write($sock, "LIST", 4096); | |
104 | ||
105 | /* | |
106 | * Multiple reads until the remote closed the connection | |
107 | */ | |
108 | $answer = ""; | |
109 | while (1) { | |
110 | $line = socket_read($sock, 4096); | |
111 | if ($line === '') | |
112 | break; | |
113 | $answer .= $line; | |
114 | } | |
115 | socket_close($sock); | |
116 | $arr = explode("\r\n", $answer); | |
117 | ||
118 | if (startsWith($arr[0], "212")) { | |
119 | $i = 1; | |
120 | while (1) { | |
121 | if (strcmp($arr[$i], ".") == 0) | |
122 | break; | |
123 | $parts = explode(",", $arr[$i]); | |
124 | $outstr .= showunit($parts[1]); | |
125 | $i++; | |
126 | } | |
127 | } | |
128 | } | |
129 | ||
130 | return $outstr; | |
131 | } | |
132 | ||
133 | ||
134 | ||
135 | ?> |