www-thermferm/global.php

changeset 207
8d2ba4126519
parent 173
7259ee8778e9
child 253
7b6abc0dd1c6
equal deleted inserted replaced
206:78fb6f99e473 207:8d2ba4126519
21 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. 21 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 *****************************************************************************/ 22 *****************************************************************************/
23 23
24 require_once('utilities.php'); 24 require_once('utilities.php');
25 25
26 $outstr = build_header("ThermFerm - Global Setup"); 26
27 $outstr .= ' <div id="errors">'.PHP_EOL; 27 if (isset($_POST['action'])) {
28 $outstr .= ' </div> <!-- errors -->'.PHP_EOL; 28 if ($_POST['action'] == "testdata")
29 $outstr .= ' <div id="etable">'.PHP_EOL; 29 testdata();
30 30 } else {
31 $outstr .= ' </div> <!-- etable -->'.PHP_EOL; 31 edit_screen("");
32 $outstr .= build_footer(); 32 }
33 33
34 echo $outstr; 34
35 35 exit;
36
37
38 /*****************************************************************************
39 *
40 */
41
42
43 function global_update() {
44
45 if ($_POST['key'] == 'Save') {
46 $sock = open_socket();
47 if ($sock != false) {
48 /*
49 * Send command and absorb the result.
50 */
51 socket_write($sock, "GLOBAL PUT", 4096);
52 usleep(20000);
53 socket_write($sock, "NAME,".$_POST['Name'], 4096);
54 usleep(20000);
55 socket_write($sock, "PORT,".$_POST['Port'], 4096);
56 usleep(20000);
57 socket_write($sock, "TEMP_ADDRESS,".$_POST['TempAddress'], 4096);
58 usleep(20000);
59 socket_write($sock, "HUM_ADDRESS,".$_POST['HumAddress'], 4096);
60 usleep(20000);
61 if (isset($_POST['LCDcols'])) {
62 socket_write($sock, "LCD_COLS,".$_POST['LCDcols'], 4096);
63 usleep(20000);
64 }
65 if (isset($_POST['LCDrows'])) {
66 socket_write($sock, "LCD_ROWS,".$_POST['LCDrows'], 4096);
67 usleep(20000);
68 }
69 socket_write($sock, ".", 4096);
70 while (1) {
71 $line = socket_read($sock, 4096);
72 if ($line === '')
73 break;
74 }
75 socket_close($sock);
76 }
77 }
78
79 unset($_POST['Name']);
80 unset($_POST['Port']);
81 unset($_POST['TempAddress']);
82 unset($_POST['HumAddress']);
83 unset($_POST['LCDcols']);
84 unset($_POST['LCDrows']);
85 unset($_POST['key']);
86 load('global.php');
87 }
88
89
90
91 function test_thedata() {
92
93 if (isset($_POST['Name']) && isset($_POST['Port']) &&
94 isset($_POST['TempAddress']) && isset($_POST['HumAddress']) &&
95 isset($_POST['key'])) {
96
97 if ($_POST['key'] == 'Cancel')
98 return 99;
99
100 if (isset($_POST['LCDcols']) && (($_POST['LCDcols'] != 16) && ($_POST['LCDcols'] != 20)))
101 return 2;
102
103 if (isset($_POST['LCDrows']) && (($_POST['LCDrows'] != 2) && ($_POST['LCDrows'] != 4)))
104 return 3;
105
106 } else {
107 return 1;
108 }
109
110 return 0;
111 }
112
113
114
115 function testdata()
116 {
117 $result = test_thedata();
118 $error = '';
119
120 switch ($result) {
121 case 0: global_update();
122 return;
123 break;
124 case 1: $error = 'Missing data';
125 break;
126 case 2: $error = 'LCD columns must be 16 or 20';
127 break;
128 case 3: $error = 'LCD rows must be 2 or 4';
129 break;
130 case 99:
131 load('global.php');
132 break;
133 }
134
135 edit_screen($error);
136 }
137
138
139
140 function edit_screen($error_message)
141 {
142 /*
143 * Get list of devices, we need it later
144 */
145 $answer = send_cmd("DEVICE LIST");
146 $devices = explode("\r\n", $answer);
147
148 /*
149 * Get current global data
150 */
151 $answer = send_cmd("GLOBAL GET");
152 $reply = explode("\r\n", $answer);
153
154 $outstr = build_header("ThermFerm - Global Setup");
155 $outstr .= ' <div id="errors">'.PHP_EOL;
156 $outstr .= ' '.$error_message.PHP_EOL;
157 $outstr .= ' </div> <!-- errors -->'.PHP_EOL;
158 $outstr .= ' <div id="etable">'.PHP_EOL;
159 $outstr .= ' <form method="POST" action="global.php">'.PHP_EOL;
160 $outstr .= ' <table class="editor">'.PHP_EOL;
161 $outstr .= ' <tr class="trhead">'.PHP_EOL;
162 $outstr .= ' <td class="setup" colspan="2" style="text-align: center; height: 20px; padding-top: 5px;">Global Setup</td>'.PHP_EOL;
163 $outstr .= ' </tr>'.PHP_EOL;
164
165 if (startsWith($reply[0], "213")) {
166 $i = 1;
167 while (1) {
168 if (strcmp($reply[$i], ".") == 0)
169 break;
170 $f = explode(",", $reply[$i]);
171
172 if ($f[0] == "NAME") {
173 $outstr .= ' <tr class="editor">'.PHP_EOL;
174 $outstr .= ' <td class="editname">System Name</td>'.PHP_EOL;
175 $outstr .= ' <td class="editfield"><input type="text" name="Name" size="50" value="'.$f[1].'"></td>'.PHP_EOL;
176 $outstr .= ' </tr>'.PHP_EOL;
177 }
178 if ($f[0] == "PORT") {
179 $outstr .= ' <tr class="editor">'.PHP_EOL;
180 $outstr .= ' <td class="editname">Telnet port</td>'.PHP_EOL;
181 $outstr .= ' <td class="editfield"><input type="text" name="Port" size="5" value="'.$f[1].'"></td>'.PHP_EOL;
182 $outstr .= ' </tr>'.PHP_EOL;
183 }
184 if ($f[0] == "TEMP_ADDRESS") {
185 $outstr .= ' <tr class="editor">'.PHP_EOL;
186 $outstr .= ' <td class="editname">Room temperature sensor</td>'.PHP_EOL;
187 $outstr .= ' <td class="editfield"><select name="TempAddress">'.PHP_EOL;
188 $outstr .= ' <option value="">Not Assigned</option>'.PHP_EOL;
189 if (startsWith($devices[0], "212")) {
190 $j = 1;
191 while (1) {
192 if (strcmp($devices[$j], ".") == 0)
193 break;
194 $g = explode(",", $devices[$j]);
195 if ($g[5] == "IN_ANALOG") {
196 ($f[1] == $g[0]) ? $se = " selected" : $se = "";
197 $outstr .= ' <option value="'.$g[0].'"'.$se.'>'.$g[1].' '.$g[4].'</option>'.PHP_EOL;
198 }
199 $j++;
200 }
201 }
202 $outstr .= ' </select></td>'.PHP_EOL;
203 $outstr .= ' </tr>'.PHP_EOL;
204 }
205 if ($f[0] == "HUM_ADDRESS") {
206 $outstr .= ' <tr class="editor">'.PHP_EOL;
207 $outstr .= ' <td class="editname">Room humidity sensor</td>'.PHP_EOL;
208 $outstr .= ' <td class="editfield"><select name="HumAddress">'.PHP_EOL;
209 $outstr .= ' <option value="">Not Assigned</option>'.PHP_EOL;
210 if (startsWith($devices[0], "212")) {
211 $j = 1;
212 while (1) {
213 if (strcmp($devices[$j], ".") == 0)
214 break;
215 $g = explode(",", $devices[$j]);
216 if ($g[5] == "IN_ANALOG") {
217 ($f[1] == $g[0]) ? $se = " selected" : $se = "";
218 $outstr .= ' <option value="'.$g[0].'"'.$se.'>'.$g[1].' '.$g[4].'</option>'.PHP_EOL;
219 }
220 $j++;
221 }
222 }
223 $outstr .= ' </select></td>'.PHP_EOL;
224 $outstr .= ' </tr>'.PHP_EOL;
225 }
226 if ($f[0] == "LCD_COLS") {
227 $outstr .= ' <tr class="editor">'.PHP_EOL;
228 $outstr .= ' <td class="editname">LCD columns</td>'.PHP_EOL;
229 $outstr .= ' <td class="editfield"><input type="text" name="LCDcols" size="5" value="'.$f[1].'"></td>'.PHP_EOL;
230 $outstr .= ' </tr>'.PHP_EOL;
231 }
232 if ($f[0] == "LCD_ROWS") {
233 $outstr .= ' <tr class="editor">'.PHP_EOL;
234 $outstr .= ' <td class="editname">LCD rows</td>'.PHP_EOL;
235 $outstr .= ' <td class="editfield"><input type="text" name="LCDrows" size="5" value="'.$f[1].'"></td>'.PHP_EOL;
236 $outstr .= ' </tr>'.PHP_EOL;
237 }
238 $i++;
239 }
240 }
241
242 $outstr .= ' <tr class="editor">'.PHP_EOL;
243 $outstr .= ' <td class="editname"><input type="submit" value="Save" name="key"></td>'.PHP_EOL;
244 $outstr .= ' <td class="editfield"><input type="submit" value="Cancel" name="key">';
245 $outstr .= '<input type="hidden" value="testdata" name="action">';
246 $outstr .= ' </tr>'.PHP_EOL;
247 $outstr .= ' </table>'.PHP_EOL;
248 $outstr .= ' </form>'.PHP_EOL;
249 $outstr .= ' </div> <!-- etable -->'.PHP_EOL;
250 $outstr .= build_footer();
251
252 echo $outstr;
253 }
254
255

mercurial