www-thermferm/getstate.php

changeset 171
15f92e5eecef
child 175
b73490398368
equal deleted inserted replaced
170:3cb99272b84b 171:15f92e5eecef
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 if (isset($_GET["uuid"]))
27 $uuid = $_GET["uuid"];
28 else
29 $uuid = "0e261929-486d-4117-897a-2b23509157c6";
30
31 $sock = open_socket();
32 if ($sock == false) {
33 echo "";
34 return;
35 }
36
37 socket_write($sock, 'UNIT '.$uuid, 4096);
38 $answer = "";
39 while (1) {
40 $line = socket_read($sock, 4096);
41 if ($line === '')
42 break;
43 $answer .= $line;
44 }
45 socket_close($sock);
46
47 $sock = open_socket();
48 if ($sock == false) {
49 echo "";
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
63 $arr = explode("\r\n", $answer);
64
65 $air_temperature = "NA";
66 $air_state = "NA";
67 $beer_temperature = "NA";
68 $beer_state = "NA";
69 $target_temperature = "NA";
70 $led1 = $led2 = $led3 = $sw1 = $sw2 = $sw3 = 0;
71 $fridge_set = "NA";
72 $beer_set = "NA";
73
74 if (startsWith($arr[0], "213")) {
75 $j = 1;
76 while (1) {
77 if (strcmp($arr[$j], ".") == 0)
78 break;
79 $f = explode(",", $arr[$j]);
80
81 if ($f[0] == "AIR_STATE")
82 $air_state = $f[1];
83 if (($f[0] == "AIR_TEMPERATURE") && ($air_state == "OK"))
84 $air_temperature = $f[1];
85 if ($f[0] == "BEER_STATE")
86 $beer_state = $f[1];
87 if (($f[0] == "BEER_TEMPERATURE") && ($beer_state == "OK"))
88 $beer_temperature = $f[1];
89 if ($f[0] == "MODE")
90 $mode = $f[1];
91 if ($f[0] == "COOLER_STATE")
92 $led1 = $f[1];
93 if ($f[0] == "HEATER_STATE")
94 $led2 = $f[1];
95 if ($f[0] == "FAN_STATE")
96 $led3 = $f[1];
97 if (($f[0] == "BEER_SET") && ($mode == "BEER"))
98 $target_temperature = $f[1];
99 if (($f[0] == "FRIDGE_SET") && ($mode == "FRIDGE"))
100 $target_temperature = $f[1];
101 if (($f[0] == "PROF_TARGET") && ($mode == "PROFILE"))
102 $target_temperature = $f[1];
103 $j++;
104 }
105 }
106
107
108 $reply = array (
109 'air_temperature' => $air_temperature,
110 'beer_temperature' => $beer_temperature,
111 'target_temperature' => $target_temperature,
112 'mode' => $mode,
113 'led1' => $led1,
114 'led2' => $led2,
115 'led3' => $led3,
116 'sw1' => $sw1,
117 'sw2' => $sw2,
118 'sw3' => $sw3
119 );
120
121
122 echo json_encode($reply);
123 ?>

mercurial