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