Mon, 21 Jul 2014 22:19:24 +0200
Added LIST LOG command
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
1 | /***************************************************************************** |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
2 | * Copyright (C) 2008-2014 |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
3 | * |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
4 | * Michiel Broek <mbroek at mbse dot eu> |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
5 | * |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
6 | * This file is part of the mbsePi-apps |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
7 | * |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
8 | * This is free software; you can redistribute it and/or modify it |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
9 | * under the terms of the GNU General Public License as published by the |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
10 | * Free Software Foundation; either version 2, or (at your option) any |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
11 | * later version. |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
12 | * |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
13 | * mbsePi-apps is distributed in the hope that it will be useful, but |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
16 | * General Public License for more details. |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
17 | * |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
18 | * You should have received a copy of the GNU General Public License |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
19 | * along with EC-65K; see the file COPYING. If not, write to the Free |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
20 | * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
21 | *****************************************************************************/ |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
22 | |
106 | 23 | #include "rdconfig.h" |
46
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
24 | #include "thermferm.h" |
106 | 25 | #include "server.h" |
26 | #include "xutil.h" | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
27 | |
58
e8e7b46b705b
Fixed compilation without wiringpi
Michiel Broek <mbroek@mbse.eu>
parents:
54
diff
changeset
|
28 | |
75
4b976601737d
Writes a basic xml configuration next to the plain ascii config file
Michiel Broek <mbroek@mbse.eu>
parents:
74
diff
changeset
|
29 | extern int my_shutdown; |
4b976601737d
Writes a basic xml configuration next to the plain ascii config file
Michiel Broek <mbroek@mbse.eu>
parents:
74
diff
changeset
|
30 | extern int debug; |
71 | 31 | extern int current_unit; |
66
805f1d285acd
Compiles with 1-wire bus on PC hardware
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
32 | #ifdef HAVE_WIRINGPI_H |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
33 | extern int lcdHandle; |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
34 | extern unsigned char lcdbuf[MAX_LCDS][20][4]; |
66
805f1d285acd
Compiles with 1-wire bus on PC hardware
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
35 | #endif |
43 | 36 | extern sys_config Config; |
81 | 37 | extern const char UNITMODE[5][8]; |
95 | 38 | extern const char UNITmode[5]; |
101
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
100
diff
changeset
|
39 | extern const char TEMPSTATE[3][8]; |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
40 | |
43 | 41 | int s; /* connected socket */ |
42 | int ls; /* listen socket */ | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
43 | |
43 | 44 | struct sockaddr_in myaddr_in; /* for local socket address */ |
45 | struct sockaddr_in peeraddr_in; /* for peer socket address */ | |
46 | ||
47 | struct hostent *hp; | |
48 | ||
49 | #define SS_BUFSIZE 1024 | |
50 | #define SS_TIMEOUT 300 | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
51 | |
48 | 52 | |
95 | 53 | float cs_heatEstimator = 0.2; |
54 | float cs_coolEstimator = 5; | |
48 | 55 | float cv_beerDiff = 0.0; |
56 | ||
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
57 | |
46
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
58 | |
43 | 59 | /* |
60 | * Send message to client | |
61 | */ | |
62 | int srv_send(const char *format, ...) | |
63 | { | |
64 | char out[SS_BUFSIZE]; | |
65 | va_list va_ptr; | |
66 | ||
67 | if (s == -1) | |
68 | return -1; | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
69 | |
43 | 70 | va_start(va_ptr, format); |
71 | vsnprintf(out, SS_BUFSIZE-1, format, va_ptr); | |
72 | va_end(va_ptr); | |
73 | ||
48 | 74 | if (debug) { |
80
81bf78a7618e
Switched to xml configuration
Michiel Broek <mbroek@mbse.eu>
parents:
79
diff
changeset
|
75 | syslog(LOG_NOTICE, "send: \"%s\"", out); |
45
053c4657105f
Mirror LCD display on the webpage works
Michiel Broek <mbroek@mbse.eu>
parents:
44
diff
changeset
|
76 | fprintf(stdout, "send: \"%s\"\n", out); |
48 | 77 | } |
44 | 78 | |
43 | 79 | if (send(s, out, strlen(out), 0) != strlen(out)) { |
80 | syslog(LOG_NOTICE, "srv_send failed"); | |
81 | return -1; | |
82 | } | |
80
81bf78a7618e
Switched to xml configuration
Michiel Broek <mbroek@mbse.eu>
parents:
79
diff
changeset
|
83 | |
43 | 84 | if (send(s, (char *)"\r\n", 2, 0) != 2) { |
85 | syslog(LOG_NOTICE, "srv_send failed"); | |
86 | return -1; | |
87 | } | |
88 | ||
89 | return 0; | |
90 | } | |
91 | ||
92 | ||
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
93 | |
88 | 94 | /* |
92 | 95 | * ADD PROFILE name |
96 | * ADD UNIT name | |
88 | 97 | */ |
80
81bf78a7618e
Switched to xml configuration
Michiel Broek <mbroek@mbse.eu>
parents:
79
diff
changeset
|
98 | int unit_add(char *buf) |
43 | 99 | { |
92 | 100 | units_list *unit, *tmpu; |
101 | profiles_list *profile, *tmpp; | |
102 | uuid_t uu; | |
103 | char *opt, *param; | |
104 | int id = 1; | |
80
81bf78a7618e
Switched to xml configuration
Michiel Broek <mbroek@mbse.eu>
parents:
79
diff
changeset
|
105 | |
92 | 106 | opt = strtok(buf, " \0"); |
107 | opt = strtok(NULL, " \0"); | |
80
81bf78a7618e
Switched to xml configuration
Michiel Broek <mbroek@mbse.eu>
parents:
79
diff
changeset
|
108 | if (opt == NULL) { |
92 | 109 | srv_send((char *)"501 Subcommand missing"); |
110 | return 1; | |
111 | } | |
112 | ||
113 | param = strtok(NULL, "\0"); | |
114 | if (param == NULL) { | |
80
81bf78a7618e
Switched to xml configuration
Michiel Broek <mbroek@mbse.eu>
parents:
79
diff
changeset
|
115 | srv_send((char *)"501 Parameter missing"); |
81bf78a7618e
Switched to xml configuration
Michiel Broek <mbroek@mbse.eu>
parents:
79
diff
changeset
|
116 | return 1; |
81bf78a7618e
Switched to xml configuration
Michiel Broek <mbroek@mbse.eu>
parents:
79
diff
changeset
|
117 | } |
81bf78a7618e
Switched to xml configuration
Michiel Broek <mbroek@mbse.eu>
parents:
79
diff
changeset
|
118 | |
81bf78a7618e
Switched to xml configuration
Michiel Broek <mbroek@mbse.eu>
parents:
79
diff
changeset
|
119 | if (debug) |
92 | 120 | fprintf(stdout, "opt: '%s' param: '%s'\n", MBSE_SS(opt), MBSE_SS(param)); |
80
81bf78a7618e
Switched to xml configuration
Michiel Broek <mbroek@mbse.eu>
parents:
79
diff
changeset
|
121 | |
92 | 122 | if (strcmp(opt, (char *)"PROFILE") == 0) { |
123 | profile = (profiles_list *)malloc(sizeof(profiles_list)); | |
124 | profile->next = NULL; | |
125 | profile->version = 1; | |
126 | profile->uuid = malloc(37); | |
127 | uuid_generate(uu); | |
128 | uuid_unparse(uu, profile->uuid); | |
129 | profile->name = xstrcpy(param); | |
130 | profile->steps = NULL; | |
80
81bf78a7618e
Switched to xml configuration
Michiel Broek <mbroek@mbse.eu>
parents:
79
diff
changeset
|
131 | |
92 | 132 | if (Config.profiles == NULL) { |
133 | Config.profiles = profile; | |
134 | } else { | |
135 | for (tmpp = Config.profiles; tmpp; tmpp = tmpp->next) { | |
136 | if (tmpp->next == NULL) { | |
137 | tmpp->next = profile; | |
138 | break; | |
139 | } | |
80
81bf78a7618e
Switched to xml configuration
Michiel Broek <mbroek@mbse.eu>
parents:
79
diff
changeset
|
140 | } |
81bf78a7618e
Switched to xml configuration
Michiel Broek <mbroek@mbse.eu>
parents:
79
diff
changeset
|
141 | } |
92 | 142 | |
143 | syslog(LOG_NOTICE, "Profile \"%s\" with uuid %s added", param, profile->uuid); | |
144 | srv_send((char *)"211 Profile \"%s\" with uuid %s added", param, profile->uuid); | |
145 | return 0; | |
146 | ||
147 | } else if (strcmp(opt, (char *)"UNIT") == 0) { | |
148 | unit = (units_list *)malloc(sizeof(units_list)); | |
149 | unit->next = NULL; | |
150 | unit->version = 1; | |
151 | unit->uuid = malloc(37); | |
152 | uuid_generate(uu); | |
153 | uuid_unparse(uu, unit->uuid); | |
154 | unit->name = xstrcpy(param); | |
155 | unit->air_address = unit->beer_address = unit->io1_address = unit->io2_address = unit->profile = NULL; | |
156 | unit->volume = 0.0; | |
101
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
100
diff
changeset
|
157 | unit->air_state = unit->beer_state = 1; |
92 | 158 | unit->heater_available = unit->cooler_available = unit->fan_available = FALSE; |
101
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
100
diff
changeset
|
159 | unit->air_temperature = unit->beer_temperature = 20000; |
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
100
diff
changeset
|
160 | unit->beer_set = unit->fridge_set = 20.0; |
92 | 161 | unit->heater_state = unit->cooler_state = unit->fan_state = unit->door_state = unit->mode = unit->prof_state = 0; |
162 | unit->temp_set_min = 1.0; | |
163 | unit->temp_set_max = 30.0; | |
164 | unit->idle_rangeH = 1.0; | |
165 | unit->idle_rangeL = -1.0; | |
166 | unit->prof_started = (time_t)0; | |
167 | ||
168 | if (Config.units == NULL) { | |
169 | Config.units = unit; | |
170 | } else { | |
171 | for (tmpu = Config.units; tmpu; tmpu = tmpu->next) { | |
172 | id++; | |
173 | if (tmpu->next == NULL) { | |
174 | tmpu->next = unit; | |
175 | break; | |
176 | } | |
177 | } | |
178 | } | |
179 | ||
180 | syslog(LOG_NOTICE, "Unit %d with uuid %s added", id, unit->uuid); | |
181 | srv_send((char *)"211 Unit %d with uuid %s added", id, unit->uuid); | |
182 | current_unit = id; | |
183 | return 0; | |
80
81bf78a7618e
Switched to xml configuration
Michiel Broek <mbroek@mbse.eu>
parents:
79
diff
changeset
|
184 | } |
81bf78a7618e
Switched to xml configuration
Michiel Broek <mbroek@mbse.eu>
parents:
79
diff
changeset
|
185 | |
92 | 186 | srv_send((char *)"502 Unknown command option"); |
187 | return 1; | |
43 | 188 | } |
189 | ||
190 | ||
191 | ||
81 | 192 | /* |
193 | * LIST | |
84 | 194 | * LIST BUS |
120 | 195 | * LIST LOG |
84 | 196 | * LIST PROFILES |
197 | * LIST UNIT | |
81 | 198 | */ |
199 | int cmd_list(char *buf) | |
200 | { | |
120 | 201 | char *opt, *filename, *p, q[2], buffer[256]; |
84 | 202 | units_list *unit; |
92 | 203 | profiles_list *profile; |
84 | 204 | int i; |
205 | DIR *fd; | |
120 | 206 | FILE *fp; |
84 | 207 | struct dirent *de; |
81 | 208 | |
87
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
209 | opt = strtok(buf, " \0"); |
92 | 210 | opt = strtok(NULL, "\0"); |
84 | 211 | |
81 | 212 | if (opt == NULL) { |
92 | 213 | /* |
214 | * Default, list available units | |
215 | */ | |
81 | 216 | srv_send((char *)"212 Fermenter list follows:"); |
217 | i = 0; | |
218 | for (unit = Config.units; unit; unit = unit->next) { | |
219 | i++; | |
100 | 220 | srv_send((char *)"%d,%s,%s,%s", i, unit->uuid, unit->name, UNITMODE[unit->mode]); |
81 | 221 | } |
222 | srv_send((char *)"."); | |
92 | 223 | return 0; |
224 | ||
84 | 225 | } else if (strcmp(opt, (char *)"BUS") == 0) { |
86
3d7a241329e2
Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents:
85
diff
changeset
|
226 | /* |
3d7a241329e2
Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents:
85
diff
changeset
|
227 | * 1-wire bus |
3d7a241329e2
Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents:
85
diff
changeset
|
228 | */ |
84 | 229 | if ((fd = opendir((char *)"/sys/bus/w1/devices"))) { |
86
3d7a241329e2
Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents:
85
diff
changeset
|
230 | srv_send((char *)"212 1-wire bus devices:"); |
3d7a241329e2
Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents:
85
diff
changeset
|
231 | while ((de = readdir(fd))) { |
3d7a241329e2
Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents:
85
diff
changeset
|
232 | if (de->d_name[0] != '.') { |
3d7a241329e2
Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents:
85
diff
changeset
|
233 | if (strncmp(de->d_name, (char *)"w1", 2) == 0) { |
100 | 234 | srv_send((char *)"%s,N/A,System device", de->d_name); |
86
3d7a241329e2
Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents:
85
diff
changeset
|
235 | } else if (strncmp(de->d_name, (char *)"10", 2) == 0) { |
100 | 236 | srv_send((char *)"%s,18S20,Digital thermometer", de->d_name); |
86
3d7a241329e2
Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents:
85
diff
changeset
|
237 | } else if (strncmp(de->d_name, (char *)"28", 2) == 0) { |
100 | 238 | srv_send((char *)"%s,18B20,Digital thermometer", de->d_name); |
86
3d7a241329e2
Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents:
85
diff
changeset
|
239 | } else if (strncmp(de->d_name, (char *)"29", 2) == 0) { |
100 | 240 | srv_send((char *)"%s,2408,8 Channel addressable switch/LCD", de->d_name); |
86
3d7a241329e2
Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents:
85
diff
changeset
|
241 | } else if (strncmp(de->d_name, (char *)"3a", 2) == 0) { |
100 | 242 | srv_send((char *)"%s,2413,Dual channel addressable switchs", de->d_name); |
86
3d7a241329e2
Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents:
85
diff
changeset
|
243 | } else { |
100 | 244 | srv_send((char *)"%s,N/A,Unknown device", de->d_name); |
84 | 245 | } |
246 | } | |
86
3d7a241329e2
Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents:
85
diff
changeset
|
247 | } |
3d7a241329e2
Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents:
85
diff
changeset
|
248 | srv_send((char *)"."); |
84 | 249 | closedir(fd); |
92 | 250 | return 0; |
84 | 251 | } else { |
252 | srv_send((char *)"503 directory /sys/bus/w1/devices: %s", strerror(errno)); | |
92 | 253 | return 1; |
84 | 254 | } |
92 | 255 | |
120 | 256 | } else if (strcmp(opt, (char *)"LOG") == 0) { |
257 | /* | |
258 | * Get the logfile data and emit only one line per hour. | |
259 | */ | |
260 | if (current_unit == -1) { | |
261 | srv_send((char *)"401 No fermenter unit selected"); | |
262 | return 1; | |
263 | } | |
264 | ||
265 | i = 0; | |
266 | q[0] = q[1] = 'a'; | |
267 | for (unit = Config.units; unit; unit = unit->next) { | |
268 | i++; | |
269 | if (i == current_unit) | |
270 | break; | |
271 | } | |
272 | ||
273 | srv_send((char *)"212 Logfile list follows:"); | |
274 | if (getenv((char *)"USER") == NULL) { | |
275 | filename = xstrcpy((char *)"/root"); | |
276 | } else { | |
277 | filename = xstrcpy(getenv((char *)"HOME")); | |
278 | } | |
279 | filename = xstrcat(filename, (char *)"/.thermferm/log/"); | |
280 | filename = xstrcat(filename, unit->name); | |
281 | filename = xstrcat(filename, (char *)".log"); | |
282 | if ((fp = fopen(filename, "r"))) { | |
283 | while (fgets(buffer, sizeof(buffer)-1, fp) != NULL) { | |
284 | buffer[strlen(buffer) -1] = '\0'; | |
285 | p = buffer + 17; | |
286 | if (strncmp(p, (char *)"Mode", 4)) { | |
287 | if ((q[0] != buffer[11]) || (q[1] != buffer[12])) { | |
288 | q[0] = buffer[11]; | |
289 | q[1] = buffer[12]; | |
290 | srv_send(buffer); | |
291 | } | |
292 | } | |
293 | } | |
294 | } | |
295 | free(filename); | |
296 | filename = NULL; | |
297 | srv_send((char *)"."); | |
298 | return 0; | |
299 | ||
84 | 300 | } else if (strcmp(opt, (char *)"PROFILES") == 0) { |
86
3d7a241329e2
Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents:
85
diff
changeset
|
301 | /* |
92 | 302 | * Fermenting profiles |
86
3d7a241329e2
Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents:
85
diff
changeset
|
303 | */ |
92 | 304 | srv_send((char *)"212 profiles:"); |
305 | i = 0; | |
306 | for (profile = Config.profiles; profile; profile = profile->next) { | |
307 | i++; | |
100 | 308 | srv_send((char *)"%d,%s,%s", i, profile->uuid, profile->name); |
86
3d7a241329e2
Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents:
85
diff
changeset
|
309 | } |
92 | 310 | srv_send((char *)"."); |
311 | return 0; | |
312 | ||
84 | 313 | } else if (strcmp(opt, (char *)"UNIT") == 0) { |
86
3d7a241329e2
Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents:
85
diff
changeset
|
314 | /* |
3d7a241329e2
Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents:
85
diff
changeset
|
315 | * List configured and selected fermenter unit |
3d7a241329e2
Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents:
85
diff
changeset
|
316 | */ |
84 | 317 | if (current_unit == -1) { |
318 | srv_send((char *)"401 No fermenter unit selected"); | |
92 | 319 | return 1; |
320 | } | |
321 | ||
322 | srv_send((char *)"213 Unit %d listing follows:", current_unit); | |
323 | i = 0; | |
324 | for (unit = Config.units; unit; unit = unit->next) { | |
325 | i++; | |
326 | if (i == current_unit) { | |
100 | 327 | srv_send((char *)"NAME,%s", unit->name); |
328 | srv_send((char *)"UUID,%s", unit->uuid); | |
92 | 329 | if (unit->air_address) { |
100 | 330 | srv_send((char *)"AIR_ADDRESS,%s", unit->air_address); |
101
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
100
diff
changeset
|
331 | srv_send((char *)"AIR_STATE,%s", TEMPSTATE[unit->air_state]); |
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
100
diff
changeset
|
332 | srv_send((char *)"AIR_TEMPERATURE,%.3f", unit->air_temperature / 1000.0); |
92 | 333 | } |
334 | if (unit->beer_address) { | |
100 | 335 | srv_send((char *)"BEER_ADDRESS,%s", unit->beer_address); |
101
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
100
diff
changeset
|
336 | srv_send((char *)"BEER_STATE,%s", TEMPSTATE[unit->beer_state]); |
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
100
diff
changeset
|
337 | srv_send((char *)"BEER_TEMPERATURE,%.3f", unit->beer_temperature / 1000.0); |
92 | 338 | } |
339 | if (unit->io1_address) { | |
100 | 340 | srv_send((char *)"IO1_ADDRESS,%s", unit->io1_address); |
87
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
341 | } |
92 | 342 | if (unit->io2_address) { |
100 | 343 | srv_send((char *)"IO2_ADDRESS,%s", unit->io2_address); |
92 | 344 | } |
345 | if (unit->heater_available) { | |
100 | 346 | srv_send((char *)"HEATER_AVAILABLE,%s", unit->heater_available); |
92 | 347 | } |
348 | if (unit->cooler_available) { | |
100 | 349 | srv_send((char *)"COOLER_AVAILABLE,%s", unit->cooler_available); |
92 | 350 | } |
351 | if (unit->fan_available) { | |
100 | 352 | srv_send((char *)"FAN_AVAILABLE,%s", unit->fan_available); |
92 | 353 | } |
100 | 354 | srv_send((char *)"MODE,%s", UNITMODE[unit->mode]); |
355 | srv_send((char *)"FRIDGE_SET,%.1f", unit->fridge_set); | |
356 | srv_send((char *)"BEER_SET,%.1f", unit->beer_set); | |
92 | 357 | if (unit->profile) { |
100 | 358 | srv_send((char *)"PROFILE,%s", unit->profile); |
92 | 359 | } |
100 | 360 | srv_send((char *)"TEMP_SET_MIN,%.1f", unit->temp_set_min); |
361 | srv_send((char *)"TEMP_SET_MAX,%.1f", unit->temp_set_max); | |
362 | srv_send((char *)"IDLE_RANGE_L,%.1f", unit->idle_rangeL); | |
363 | srv_send((char *)"IDLE_RANGE_H,%.1f", unit->idle_rangeH); | |
87
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
364 | } |
84 | 365 | } |
92 | 366 | srv_send((char *)"."); |
367 | return 0; | |
81 | 368 | } |
369 | ||
92 | 370 | srv_send((char *)"502 Unknown command option"); |
371 | return 1; | |
81 | 372 | } |
373 | ||
374 | ||
375 | ||
87
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
376 | /* |
88 | 377 | * Set new operating mode |
378 | * MODE OFF|NONE|BEER|FRIDGE|PROFILE | |
379 | */ | |
380 | int cmd_mode(char *buf) | |
381 | { | |
382 | return 1; | |
383 | } | |
384 | ||
385 | ||
386 | ||
387 | /* | |
92 | 388 | * SET BEER float |
389 | * SET FRIDGE float | |
390 | * SET VOLUME float | |
391 | * SET IDLE_LOW float | |
392 | * SET IDLE_HIGH float | |
393 | * SET TEMP_MIN float | |
394 | * SET TEMP_MAX float | |
395 | * SET NAME string | |
396 | * SET PROFILE string | |
88 | 397 | */ |
398 | int cmd_set(char *buf) | |
399 | { | |
400 | char *opt, *param; | |
401 | units_list *unit; | |
402 | int i, rc; | |
403 | float fval; | |
404 | ||
405 | opt = strtok(buf, " \0"); | |
406 | opt = strtok(NULL, " \0"); | |
407 | if (opt == NULL) { | |
408 | srv_send((char *)"501 Subcommand missing"); | |
409 | return 1; | |
410 | } | |
411 | ||
412 | param = strtok(NULL, "\0"); | |
413 | if (param == NULL) { | |
414 | srv_send((char *)"501 Parameter missing"); | |
415 | return 1; | |
416 | } | |
417 | ||
418 | rc = sscanf(param, "%f", &fval); | |
419 | ||
420 | if (debug) | |
421 | fprintf(stdout, "opt='%s' param='%s' rc=%d fval=%.1f\n", opt, param, rc, fval); | |
422 | ||
423 | /* | |
424 | * Commands below need a selected unit | |
425 | */ | |
426 | if (current_unit == -1) { | |
427 | srv_send((char *)"401 No fermenter unit selected"); | |
428 | return 1; | |
429 | } | |
430 | ||
431 | i = 0; | |
432 | for (unit = Config.units; unit; unit = unit->next) { | |
433 | i++; | |
434 | if (i == current_unit) | |
435 | break; | |
436 | } | |
437 | ||
438 | /* | |
439 | * If a valid parameter float value | |
440 | */ | |
441 | if (rc == 1) { | |
442 | if (strcmp(opt, (char *)"BEER") == 0) { | |
443 | if ((fval >= unit->temp_set_min) && (fval <= unit->temp_set_max)) { | |
444 | unit->beer_set = fval; | |
445 | srv_send((char *)"214 Unit %d BEER set to %.1f", current_unit, fval); | |
446 | return 0; | |
447 | } else { | |
448 | srv_send((char *)"510 New temperature not between %.1f and %.1f", unit->temp_set_min, unit->temp_set_max); | |
449 | return 1; | |
450 | } | |
451 | } else if (strcmp(opt, (char *)"FRIDGE") == 0) { | |
452 | if ((fval >= unit->temp_set_min) && (fval <= unit->temp_set_max)) { | |
453 | unit->fridge_set = fval; | |
454 | srv_send((char *)"214 Unit %d BEER set to %.1f", current_unit, fval); | |
455 | return 0; | |
456 | } else { | |
457 | srv_send((char *)"510 New temperature not between %.1f and %.1f", unit->temp_set_min, unit->temp_set_max); | |
458 | return 1; | |
459 | } | |
460 | } else if (strcmp(opt, (char *)"VOLUME") == 0) { | |
461 | /* | |
462 | * Must fit in a 2 TEU container | |
463 | */ | |
464 | if ((fval >= 0.0) && (fval <= 77020.0)) { | |
465 | unit->volume = fval; | |
466 | srv_send((char *)"214 Unit %d VOLUME set to %.1f", current_unit, fval); | |
467 | return 0; | |
468 | } else { | |
469 | srv_send((char *)"510 New volume not between 0 and 77020"); | |
470 | return 1; | |
471 | } | |
472 | } else if (strcmp(opt, (char *)"IDLE_LOW") == 0) { | |
473 | if ((fval >= -5.0) && (fval <= -0.1)) { | |
474 | unit->idle_rangeL = fval; | |
475 | srv_send((char *)"214 Unit %d IDLE_LOW set to %.1f", current_unit, fval); | |
476 | return 0; | |
477 | } else { | |
478 | srv_send((char *)"510 New value not between -5.0 and -0.1"); | |
479 | return 1; | |
480 | } | |
481 | } else if (strcmp(opt, (char *)"IDLE_HIGH") == 0) { | |
482 | if ((fval >= 0.1) && (fval <= 5.0)) { | |
483 | unit->idle_rangeH = fval; | |
484 | srv_send((char *)"214 Unit %d IDLE_HIGH set to %.1f", current_unit, fval); | |
485 | return 0; | |
486 | } else { | |
487 | srv_send((char *)"510 New value not between -5.0 and -0.1"); | |
488 | return 1; | |
489 | } | |
490 | } else if (strcmp(opt, (char *)"TEMP_MIN") == 0) { | |
491 | if ((fval >= -2.0) && (fval <= 35.0) && (fval < unit->temp_set_max)) { | |
492 | unit->temp_set_min = fval; | |
493 | srv_send((char *)"214 Unit %d TEMP_MIN set to %.1f", current_unit, fval); | |
494 | return 0; | |
495 | } else { | |
496 | srv_send((char *)"510 New value not between -2.0 and 35.0 and lower then TEMP_MAX"); | |
497 | return 1; | |
498 | } | |
499 | } else if (strcmp(opt, (char *)"TEMP_MAX") == 0) { | |
500 | if ((fval >= -2.0) && (fval <= 35.0) && (fval > unit->temp_set_min)) { | |
501 | unit->temp_set_max = fval; | |
502 | srv_send((char *)"214 Unit %d TEMP_MAX set to %.1f", current_unit, fval); | |
503 | return 0; | |
504 | } else { | |
505 | srv_send((char *)"510 New value not between -2.0 and 35.0 and higher then TEMP_MIN"); | |
506 | return 1; | |
507 | } | |
508 | } | |
509 | } | |
510 | ||
511 | /* | |
512 | * Set new unit or beer name | |
513 | */ | |
514 | if (strcmp(opt, (char *)"NAME") == 0) { | |
515 | if (unit->name) | |
516 | free(unit->name); | |
517 | unit->name = xstrcpy(param); | |
518 | srv_send((char *)"214 Unit %d NAME set to '%s'", current_unit, param); | |
519 | // TODO: change logfile name | |
520 | return 0; | |
521 | } else if (strcmp(opt, (char *)"PROFILE") == 0) { | |
522 | // check profile file, excists, type, at least one profile item | |
523 | } | |
524 | ||
525 | srv_send((char *)"502 Unknown command option"); | |
526 | return 1; | |
527 | } | |
528 | ||
529 | ||
530 | ||
531 | /* | |
87
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
532 | * UNIT n |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
533 | * UNIT uuid |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
534 | */ |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
535 | int cmd_unit(char *buf) |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
536 | { |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
537 | char *opt; |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
538 | units_list *tmp; |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
539 | int i, unit_no; |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
540 | |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
541 | opt = strtok(buf, " \0"); |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
542 | opt = strtok(NULL, " \0"); |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
543 | |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
544 | if (opt == NULL) { |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
545 | srv_send((char *)"501 Parameter missing"); |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
546 | return 1; |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
547 | } |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
548 | |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
549 | i = 0; |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
550 | if (strlen(opt) == 36) { |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
551 | /* |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
552 | * Search using uuid |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
553 | */ |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
554 | for (tmp = Config.units; tmp; tmp = tmp->next) { |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
555 | i++; |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
556 | if (strcmp(opt, tmp->uuid) == 0) { |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
557 | srv_send((char *)"210 Unit %d selected", i); |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
558 | current_unit = i; |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
559 | return 0; |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
560 | } |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
561 | } |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
562 | srv_send((char *)"410 No such unit"); |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
563 | return 1; |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
564 | } |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
565 | |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
566 | if (sscanf(opt, "%d", &unit_no) == 1) { |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
567 | /* |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
568 | * We got a number, see if it is valid. |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
569 | */ |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
570 | for (tmp = Config.units; tmp; tmp = tmp->next) { |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
571 | i++; |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
572 | if (unit_no == i) { |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
573 | srv_send((char *)"210 Unit %d selected", i); |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
574 | current_unit = i; |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
575 | return 0; |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
576 | } |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
577 | } |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
578 | srv_send((char *)"410 No such unit"); |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
579 | return 1; |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
580 | } |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
581 | |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
582 | srv_send((char *)"502 Unknown command option"); |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
583 | return 1; |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
584 | } |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
585 | |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
586 | |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
587 | |
43 | 588 | void cmd_server(void) |
589 | { | |
54
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
590 | char *inp, *p, *q, buf[SS_BUFSIZE], obuf[SS_BUFSIZE]; |
48 | 591 | int i, rc, rlen; |
43 | 592 | socklen_t fromlen; |
46
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
593 | float newtemp; |
68
6dba71439cd8
Added first part of out own protocol
Michiel Broek <mbroek@mbse.eu>
parents:
66
diff
changeset
|
594 | #ifdef HAVE_WIRINGPI_H |
6dba71439cd8
Added first part of out own protocol
Michiel Broek <mbroek@mbse.eu>
parents:
66
diff
changeset
|
595 | int j; |
6dba71439cd8
Added first part of out own protocol
Michiel Broek <mbroek@mbse.eu>
parents:
66
diff
changeset
|
596 | #endif |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
597 | |
48 | 598 | memset((char *)&buf, 0, SS_BUFSIZE); |
599 | fromlen = sizeof(peeraddr_in); | |
600 | rlen = recvfrom(s, buf, sizeof(buf) -1, 0, (struct sockaddr *)&peeraddr_in, &fromlen); | |
601 | if (rlen == -1) { | |
602 | syslog(LOG_NOTICE, "recvfrom(): %s", strerror(errno)); | |
603 | } else { | |
604 | for (i = 0; i < strlen(buf); i++) { | |
605 | if (buf[i] == '\n') | |
606 | buf[i] = '\0'; | |
607 | if (buf[i] == '\r') | |
608 | buf[i] = '\0'; | |
609 | } | |
80
81bf78a7618e
Switched to xml configuration
Michiel Broek <mbroek@mbse.eu>
parents:
79
diff
changeset
|
610 | for (i = strlen(buf) -1; i > 0; i--) { |
81bf78a7618e
Switched to xml configuration
Michiel Broek <mbroek@mbse.eu>
parents:
79
diff
changeset
|
611 | if (buf[i] == ' ') |
81bf78a7618e
Switched to xml configuration
Michiel Broek <mbroek@mbse.eu>
parents:
79
diff
changeset
|
612 | buf[i] = '\0'; |
81bf78a7618e
Switched to xml configuration
Michiel Broek <mbroek@mbse.eu>
parents:
79
diff
changeset
|
613 | else |
81bf78a7618e
Switched to xml configuration
Michiel Broek <mbroek@mbse.eu>
parents:
79
diff
changeset
|
614 | break; |
81bf78a7618e
Switched to xml configuration
Michiel Broek <mbroek@mbse.eu>
parents:
79
diff
changeset
|
615 | } |
48 | 616 | if (strlen(buf)) { |
617 | if (debug) { | |
618 | syslog(LOG_NOTICE, "recv: \"%s\"", buf); | |
619 | fprintf(stdout, "recv: \"%s\"\n", buf); | |
43 | 620 | } |
621 | ||
622 | /* | |
48 | 623 | * Process commands from the client |
43 | 624 | */ |
80
81bf78a7618e
Switched to xml configuration
Michiel Broek <mbroek@mbse.eu>
parents:
79
diff
changeset
|
625 | if (strncmp(buf, "ADD", 3) == 0) { |
92 | 626 | if (unit_add(buf) == 0) |
627 | wrconfig(); | |
80
81bf78a7618e
Switched to xml configuration
Michiel Broek <mbroek@mbse.eu>
parents:
79
diff
changeset
|
628 | } else if (strncmp(buf, "HELP", 4) == 0) { |
68
6dba71439cd8
Added first part of out own protocol
Michiel Broek <mbroek@mbse.eu>
parents:
66
diff
changeset
|
629 | srv_send((char *)"100 Help text follows"); |
6dba71439cd8
Added first part of out own protocol
Michiel Broek <mbroek@mbse.eu>
parents:
66
diff
changeset
|
630 | srv_send((char *)"Recognized commands:"); |
6dba71439cd8
Added first part of out own protocol
Michiel Broek <mbroek@mbse.eu>
parents:
66
diff
changeset
|
631 | srv_send((char *)""); |
92 | 632 | srv_send((char *)"ADD PROFILE name Add a new profile with \"name\""); |
633 | srv_send((char *)"ADD UNIT name Add a new unit with \"name\""); | |
68
6dba71439cd8
Added first part of out own protocol
Michiel Broek <mbroek@mbse.eu>
parents:
66
diff
changeset
|
634 | srv_send((char *)"LCD Get LCD screen (allways 4 rows of 20 characters)"); |
81 | 635 | srv_send((char *)"LIST List all fermenter units"); |
84 | 636 | srv_send((char *)"LIST BUS List 1-wire bus"); |
120 | 637 | srv_send((char *)"LIST LOG List logfile data in 1 hour lines"); |
87
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
638 | srv_send((char *)"LIST PROFILES List available profiles"); |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
639 | srv_send((char *)"LIST UNIT List fermenter unit"); |
88 | 640 | srv_send((char *)"MODE OFF|NONE|BEER|FRIDGE|PROFILE"); |
81 | 641 | // srv_send((char *)"PROFILE Profile status"); |
642 | // srv_send((char *)"PROFILE start|stop|pause Profile start, stop or pause"); | |
88 | 643 | srv_send((char *)"SET BEER val Set beer temperature"); |
644 | srv_send((char *)"SET FRIDGE val Set fridge temperature"); | |
645 | srv_send((char *)"SET IDLE_LOW val Set idle temperature low (-5.0 .. -0.1)"); | |
646 | srv_send((char *)"SET IDLE_HIGH val Set idle temperature high (0.1 .. 5.0)"); | |
647 | srv_send((char *)"SET NAME name Set name or beername for the unit"); | |
81 | 648 | // srv_send((char *)"SET PROFILE name Set named profile"); |
88 | 649 | srv_send((char *)"SET TEMP_MIN val Set unit minimum temperature"); |
650 | srv_send((char *)"SET TEMP_MAX val Set unit maximum temperature"); | |
651 | srv_send((char *)"SET VOLUME val Set unit volume"); | |
87
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
652 | srv_send((char *)"UNIT n|uuid Select unit by number or uuid"); |
68
6dba71439cd8
Added first part of out own protocol
Michiel Broek <mbroek@mbse.eu>
parents:
66
diff
changeset
|
653 | srv_send((char *)"."); |
6dba71439cd8
Added first part of out own protocol
Michiel Broek <mbroek@mbse.eu>
parents:
66
diff
changeset
|
654 | } else if (strncmp(buf, "LCD", 3) == 0) { |
6dba71439cd8
Added first part of out own protocol
Michiel Broek <mbroek@mbse.eu>
parents:
66
diff
changeset
|
655 | #ifdef HAVE_WIRINGPI_H |
6dba71439cd8
Added first part of out own protocol
Michiel Broek <mbroek@mbse.eu>
parents:
66
diff
changeset
|
656 | srv_send((char *)"201 information follows"); |
6dba71439cd8
Added first part of out own protocol
Michiel Broek <mbroek@mbse.eu>
parents:
66
diff
changeset
|
657 | for (j = 0; j < 4; j++) { |
6dba71439cd8
Added first part of out own protocol
Michiel Broek <mbroek@mbse.eu>
parents:
66
diff
changeset
|
658 | sprintf(obuf, " "); |
70 | 659 | obuf[20] = '\0'; |
68
6dba71439cd8
Added first part of out own protocol
Michiel Broek <mbroek@mbse.eu>
parents:
66
diff
changeset
|
660 | for (i = 0; i < 20; i++) |
6dba71439cd8
Added first part of out own protocol
Michiel Broek <mbroek@mbse.eu>
parents:
66
diff
changeset
|
661 | obuf[i] = lcdbuf[lcdHandle][i][j]; |
6dba71439cd8
Added first part of out own protocol
Michiel Broek <mbroek@mbse.eu>
parents:
66
diff
changeset
|
662 | srv_send(obuf); |
6dba71439cd8
Added first part of out own protocol
Michiel Broek <mbroek@mbse.eu>
parents:
66
diff
changeset
|
663 | } |
6dba71439cd8
Added first part of out own protocol
Michiel Broek <mbroek@mbse.eu>
parents:
66
diff
changeset
|
664 | srv_send((char *)"."); |
6dba71439cd8
Added first part of out own protocol
Michiel Broek <mbroek@mbse.eu>
parents:
66
diff
changeset
|
665 | #else |
6dba71439cd8
Added first part of out own protocol
Michiel Broek <mbroek@mbse.eu>
parents:
66
diff
changeset
|
666 | srv_send((char *)"403 LCD not available"); |
6dba71439cd8
Added first part of out own protocol
Michiel Broek <mbroek@mbse.eu>
parents:
66
diff
changeset
|
667 | #endif |
81 | 668 | } else if (strncmp(buf, "LIST", 4) == 0) { |
669 | cmd_list(buf); | |
88 | 670 | } else if (strncmp(buf, "MODE", 4) == 0) { |
671 | cmd_mode(buf); | |
672 | } else if (strncmp(buf, "SET", 3) == 0) { | |
92 | 673 | if (cmd_set(buf) == 0) |
674 | wrconfig(); | |
87
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
675 | } else if (strncmp(buf, "UNIT", 4) == 0) { |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
676 | cmd_unit(buf); |
95 | 677 | |
678 | /* | |
679 | * Now the old style (brewPi) commands. These will go away. | |
680 | */ | |
68
6dba71439cd8
Added first part of out own protocol
Michiel Broek <mbroek@mbse.eu>
parents:
66
diff
changeset
|
681 | } else if (strncmp(buf, "ack", 3) == 0) { |
48 | 682 | srv_send((char *)"ack"); |
683 | } else if (strncmp(buf, "lcd", 3) == 0) { | |
684 | sprintf(obuf, "[\" \", \" \", \" \", \" \"]"); | |
66
805f1d285acd
Compiles with 1-wire bus on PC hardware
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
685 | #ifdef HAVE_WIRINGPI_H |
48 | 686 | for (i = 0; i < 20; i++) { |
687 | obuf[i+2] = lcdbuf[lcdHandle][i][0]; | |
688 | obuf[i+26] = lcdbuf[lcdHandle][i][1]; | |
689 | obuf[i+50] = lcdbuf[lcdHandle][i][2]; | |
690 | obuf[i+74] = lcdbuf[lcdHandle][i][3]; | |
691 | } | |
66
805f1d285acd
Compiles with 1-wire bus on PC hardware
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
692 | #endif |
48 | 693 | srv_send(obuf); |
694 | } else if (strncmp(buf, "getMode", 7) == 0) { | |
95 | 695 | srv_send("%c", UNITmode[Config.units->mode]); |
48 | 696 | } else if (strncmp(buf, "getFridge", 9) == 0) { |
95 | 697 | srv_send("%.1f", Config.units->fridge_set); |
48 | 698 | } else if (strncmp(buf, "getBeer", 7) == 0) { |
95 | 699 | srv_send("%.1f", Config.units->beer_set); |
48 | 700 | } else if (strncmp(buf, "getControlConstants", 19) == 0) { |
701 | srv_send("{ \"tempFormat\":\"%c\", \"tempSetMin\":%.1f, \"tempSetMax\":%.1f, \"idleRangeH\":%.3f, \"idleRangeL\":%.3f }", | |
95 | 702 | Config.tempFormat, Config.units->temp_set_min, Config.units->temp_set_max, Config.units->idle_rangeH, Config.units->idle_rangeL); |
48 | 703 | } else if (strncmp(buf, "getControlSettings", 18) == 0) { |
704 | srv_send("{ \"mode\":\"%c\", \"beerSet\":%.1f, \"fridgeSet\":%.1f, \"heatEstimator\":%.1f, \"coolEstimator\":%.1f }", | |
95 | 705 | UNITmode[Config.units->mode], Config.units->beer_set, Config.units->fridge_set, cs_heatEstimator, cs_coolEstimator); |
48 | 706 | } else if (strncmp(buf, "getControlVariables", 19) == 0) { |
707 | srv_send("{ \"beerDiff\":%.2f }", cv_beerDiff); | |
49
92a9ca143677
Fixed temperature logging, added some commands
Michiel Broek <mbroek@mbse.eu>
parents:
48
diff
changeset
|
708 | } else if (strncmp(buf, "loadDefaultControlSettings", 26) == 0) { |
95 | 709 | // Does nothing anymore |
49
92a9ca143677
Fixed temperature logging, added some commands
Michiel Broek <mbroek@mbse.eu>
parents:
48
diff
changeset
|
710 | srv_send("ack"); |
92a9ca143677
Fixed temperature logging, added some commands
Michiel Broek <mbroek@mbse.eu>
parents:
48
diff
changeset
|
711 | } else if (strncmp(buf, "loadDefaultControlConstants", 27) == 0) { |
95 | 712 | // Does nothing anymore |
49
92a9ca143677
Fixed temperature logging, added some commands
Michiel Broek <mbroek@mbse.eu>
parents:
48
diff
changeset
|
713 | srv_send("ack"); |
48 | 714 | } else if (strncmp(buf, "setBeer=", 8) == 0) { |
715 | inp = xstrcpy(buf+8); | |
716 | rc = sscanf(inp, "%f", &newtemp); | |
717 | if (debug) | |
718 | fprintf(stdout, "new temp from %s, %.1f, rc=%d\n", inp, newtemp, rc); | |
719 | if (rc == 1) { | |
95 | 720 | if ((Config.units->temp_set_min <= newtemp) && (newtemp <= Config.units->temp_set_max)) { |
51
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
721 | syslog(LOG_NOTICE, "Beer temperature set to %.1f degrees in web interface", newtemp); |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
722 | srv_send("ack"); |
95 | 723 | Config.units->mode = UNITMODE_BEER; |
724 | Config.units->beer_set = newtemp; | |
51
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
725 | } else { |
95 | 726 | syslog(LOG_NOTICE, "Beer temperature setting %.1f is outside of allowed range %.1f - %.1f", newtemp, Config.units->temp_set_min, Config.units->temp_set_max); |
51
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
727 | srv_send("err"); |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
728 | } |
48 | 729 | } else { |
51
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
730 | syslog(LOG_NOTICE, "Cannot convert temperature '%s' to float", inp); |
48 | 731 | srv_send("err"); |
732 | } | |
733 | free(inp); | |
734 | } else if (strncmp(buf, "setFridge=", 10) == 0) { | |
735 | inp = xstrcpy(buf+10); | |
736 | rc = sscanf(inp, "%f", &newtemp); | |
737 | if (debug) | |
738 | fprintf(stdout, "new temp from %s, %.1f, rc=%d\n", inp, newtemp, rc); | |
739 | if (rc == 1) { | |
95 | 740 | if ((Config.units->temp_set_min <= newtemp) && (newtemp <= Config.units->temp_set_max)) { |
51
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
741 | syslog(LOG_NOTICE, "Fridge temperature set to %.1f degrees in web interface", newtemp); |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
742 | srv_send("ack"); |
95 | 743 | Config.units->mode = UNITMODE_FRIDGE; |
744 | Config.units->fridge_set = newtemp; | |
51
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
745 | } else { |
95 | 746 | syslog(LOG_NOTICE, "Fridge temperature setting %.1f is outside of allowed range %.1f - %.1f", newtemp, Config.units->temp_set_min, Config.units->temp_set_max); |
51
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
747 | srv_send("err"); |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
748 | } |
48 | 749 | } else { |
51
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
750 | syslog(LOG_NOTICE, "Cannot convert temperature '%s' to float", inp); |
48 | 751 | srv_send("err"); |
752 | } | |
753 | free(inp); | |
49
92a9ca143677
Fixed temperature logging, added some commands
Michiel Broek <mbroek@mbse.eu>
parents:
48
diff
changeset
|
754 | } else if (strncmp(buf, "setOff", 6) == 0) { |
92a9ca143677
Fixed temperature logging, added some commands
Michiel Broek <mbroek@mbse.eu>
parents:
48
diff
changeset
|
755 | if (debug) |
92a9ca143677
Fixed temperature logging, added some commands
Michiel Broek <mbroek@mbse.eu>
parents:
48
diff
changeset
|
756 | fprintf(stdout, "temperature control disabled\n"); |
51
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
757 | syslog(LOG_NOTICE, "Notification: Temperature control disabled"); |
95 | 758 | Config.units->mode = UNITMODE_OFF; |
49
92a9ca143677
Fixed temperature logging, added some commands
Michiel Broek <mbroek@mbse.eu>
parents:
48
diff
changeset
|
759 | srv_send("ack"); |
54
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
760 | } else if (strncmp(buf, "setParameters=", 14) == 0) { |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
761 | inp = xstrcpy(buf+14); /* {"tempSetMax":30.5} */ |
51
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
762 | if (debug) |
54
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
763 | fprintf(stdout, "setParameters: %s\n", inp); |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
764 | strtok(inp, (char *)"\""); |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
765 | p = strtok(NULL, (char *)"\""); |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
766 | q = strtok(NULL, (char *)":,}"); |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
767 | if (strcmp(p, (char *)"tempSetMin") == 0) { |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
768 | rc = sscanf(q, "%f", &newtemp); |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
769 | if (rc == 1) { |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
770 | syslog(LOG_NOTICE, "cc_tempSetMin = %1.f", newtemp); |
95 | 771 | Config.units->temp_set_min = newtemp; |
54
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
772 | srv_send("ack"); |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
773 | } else { |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
774 | srv_send("ERR"); |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
775 | } |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
776 | } else if (strcmp(p, (char *)"tempSetMax") == 0) { |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
777 | rc = sscanf(q, "%f", &newtemp); |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
778 | if (rc == 1) { |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
779 | syslog(LOG_NOTICE, "cc_tempSetMax = %1.f", newtemp); |
95 | 780 | Config.units->temp_set_max = newtemp; |
54
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
781 | srv_send("ack"); |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
782 | } else { |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
783 | srv_send("ERR"); |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
784 | } |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
785 | } else { |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
786 | fprintf(stdout, "p='%s' q='%s' inp='%s'\n", p, q, inp); |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
787 | srv_send("ERR"); |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
788 | } |
62 | 789 | free(inp); |
51
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
790 | // stopScript |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
791 | // quit |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
792 | // eraseLogs |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
793 | // interval |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
794 | // startNewBrew |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
795 | // pauseLogging |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
796 | // stopLogging |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
797 | // resumeLogging |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
798 | // dateTimeFormatDisplay |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
799 | } else if (strncmp(buf, "setActiveProfile", 16) == 0) { |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
800 | syslog(LOG_NOTICE, "Setting profile '%s' as active profile", "undefined"); |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
801 | // programArduino |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
802 | // refreshDeviceList |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
803 | // getDeviceList |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
804 | // applyDevice |
43 | 805 | } else { |
48 | 806 | if (debug) |
807 | fprintf(stdout, "unknown command \"%s\"\n", buf); | |
808 | srv_send((char *)"ERR"); | |
43 | 809 | } |
810 | } | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
811 | } |
44 | 812 | |
813 | close(s); | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
814 | } |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
815 | |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
816 | |
66
805f1d285acd
Compiles with 1-wire bus on PC hardware
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
817 | #ifdef HAVE_WIRINGPI_H |
43 | 818 | PI_THREAD (my_server_loop) |
66
805f1d285acd
Compiles with 1-wire bus on PC hardware
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
819 | #else |
74 | 820 | void *my_server_loop(void *threadid) |
66
805f1d285acd
Compiles with 1-wire bus on PC hardware
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
821 | #endif |
43 | 822 | { |
823 | socklen_t addrlen; | |
44 | 824 | int optval = 1; |
43 | 825 | |
826 | syslog(LOG_NOTICE, "Thread my_server_loop started"); | |
827 | if (debug) | |
828 | fprintf(stdout, "Thread my_server_loop started\n"); | |
829 | ||
44 | 830 | memset((char *)&myaddr_in, 0, sizeof(struct sockaddr_in)); |
831 | memset((char *)&peeraddr_in, 0, sizeof(struct sockaddr_in)); | |
832 | myaddr_in.sin_family = AF_INET; | |
833 | myaddr_in.sin_addr.s_addr = INADDR_ANY; | |
834 | myaddr_in.sin_port = htons(Config.my_port); | |
835 | ||
836 | ls = socket(AF_INET, SOCK_STREAM, 0); | |
837 | if (ls == -1) { | |
838 | syslog(LOG_NOTICE, "Can't create listen socket: %s", strerror(errno)); | |
839 | fprintf(stderr, "Can't create listen socket: %s\n", strerror(errno)); | |
840 | return 0; | |
841 | } | |
842 | ||
843 | if (setsockopt(ls, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof(optval)) == -1) { | |
844 | syslog(LOG_NOTICE, "Can't setsockopt SO_KEEPALIVE socket: %s", strerror(errno)); | |
845 | close(ls); | |
846 | return 0; | |
847 | } | |
848 | ||
849 | if (setsockopt(ls, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) == -1) { | |
850 | syslog(LOG_NOTICE, "Can't setsockopt SO_REUSEADDR socket: %s", strerror(errno)); | |
851 | close(ls); | |
66
805f1d285acd
Compiles with 1-wire bus on PC hardware
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
852 | return 0; |
44 | 853 | } |
854 | ||
855 | if (bind(ls, (struct sockaddr *)&myaddr_in, sizeof(struct sockaddr_in)) == -1) { | |
856 | syslog(LOG_NOTICE, "Can't bind to listen socket: %s", strerror(errno)); | |
857 | close(ls); | |
66
805f1d285acd
Compiles with 1-wire bus on PC hardware
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
858 | return 0; |
44 | 859 | } |
860 | ||
861 | if (listen(ls, 5) == -1) { | |
862 | syslog(LOG_NOTICE, "Can't listen on listen socket: %s", strerror(errno)); | |
863 | close(ls); | |
66
805f1d285acd
Compiles with 1-wire bus on PC hardware
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
864 | return 0; |
44 | 865 | } |
866 | ||
867 | syslog(LOG_NOTICE, "listen socket created %d", ls); | |
868 | if (debug) | |
869 | fprintf(stdout, "listen socket created %d\n", ls); | |
870 | ||
871 | ||
43 | 872 | /* |
873 | * Loop forever until the external shutdown variable is set. | |
874 | */ | |
875 | for (;;) { | |
876 | ||
877 | addrlen = sizeof(struct sockaddr_in); | |
878 | /* | |
879 | * This call will block until a new connection | |
880 | * arrives. Then it will return the address of | |
881 | * the connecting peer, and a new socket | |
882 | * descriptor, s, for that connection. | |
883 | */ | |
884 | s = accept(ls, (struct sockaddr *)&peeraddr_in, &addrlen); | |
885 | if (s == -1) { | |
886 | syslog(LOG_NOTICE, "my_server_loop accept failed %s", strerror(errno)); | |
887 | if (debug) | |
888 | fprintf(stdout, "my_server_loop accept failed %s\n", strerror(errno)); | |
44 | 889 | return 0; |
43 | 890 | } |
891 | ||
48 | 892 | cmd_server(); |
43 | 893 | |
894 | if (my_shutdown) { | |
895 | syslog(LOG_NOTICE, "Thread my_server_loop stopped"); | |
896 | if (debug) | |
897 | fprintf(stdout, "Thread my_server_loop stopped\n"); | |
898 | return 0; | |
899 | } | |
900 | ||
901 | } | |
902 | } | |
903 | ||
904 |