Mon, 26 May 2014 23:25:48 +0200
More configuration changes. Writing settings implemented.
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 | |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
25 | extern bool my_shutdown; |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
26 | extern bool debug; |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
27 | extern int lcdHandle; |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
28 | extern unsigned char lcdbuf[MAX_LCDS][20][4]; |
43 | 29 | extern sys_config Config; |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
30 | |
43 | 31 | int s; /* connected socket */ |
32 | int ls; /* listen socket */ | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
33 | |
43 | 34 | struct sockaddr_in myaddr_in; /* for local socket address */ |
35 | struct sockaddr_in peeraddr_in; /* for peer socket address */ | |
36 | ||
37 | struct hostent *hp; | |
38 | ||
39 | #define SS_BUFSIZE 1024 | |
40 | #define SS_TIMEOUT 300 | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
41 | |
48 | 42 | |
43 | float cv_beerDiff = 0.0; | |
44 | ||
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
45 | |
46
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
46 | |
49
92a9ca143677
Fixed temperature logging, added some commands
Michiel Broek <mbroek@mbse.eu>
parents:
48
diff
changeset
|
47 | void defaultControlSettings(void) |
46
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
48 | { |
53 | 49 | Config.cs_mode = 'o'; /* o = Off, f = fridge, b = beer, p = profile-run */ |
50 | Config.cs_beerSet = 20.0; | |
51 | Config.cs_fridgeSet = 20.0; | |
52 | Config.cs_heatEstimator = 0.2; | |
53 | Config.cs_coolEstimator = 5; | |
46
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
54 | } |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
55 | |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
56 | |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
57 | |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
58 | void defaultControlConstants(void) |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
59 | { |
54
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
60 | Config.cc_tempFormat = 'C'; |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
61 | Config.cc_tempSetMin = 1.0; |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
62 | Config.cc_tempSetMax = 30.0; |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
63 | Config.cc_idleRangeH = 1.000; |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
64 | Config.cc_idleRangeL = -1.000; |
46
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
65 | } |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
66 | |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
67 | |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
68 | |
43 | 69 | /* |
70 | * Send message to client | |
71 | */ | |
72 | int srv_send(const char *format, ...) | |
73 | { | |
74 | char out[SS_BUFSIZE]; | |
75 | va_list va_ptr; | |
76 | ||
77 | if (s == -1) | |
78 | return -1; | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
79 | |
43 | 80 | va_start(va_ptr, format); |
81 | vsnprintf(out, SS_BUFSIZE-1, format, va_ptr); | |
82 | va_end(va_ptr); | |
83 | ||
48 | 84 | if (debug) { |
85 | syslog(LOG_NOTICE, "send: \"%s\"", out); | |
45
053c4657105f
Mirror LCD display on the webpage works
Michiel Broek <mbroek@mbse.eu>
parents:
44
diff
changeset
|
86 | fprintf(stdout, "send: \"%s\"\n", out); |
48 | 87 | } |
44 | 88 | |
43 | 89 | if (send(s, out, strlen(out), 0) != strlen(out)) { |
90 | syslog(LOG_NOTICE, "srv_send failed"); | |
91 | return -1; | |
92 | } | |
93 | ||
94 | if (send(s, (char *)"\r\n", 2, 0) != 2) { | |
95 | syslog(LOG_NOTICE, "srv_send failed"); | |
96 | return -1; | |
97 | } | |
98 | ||
99 | return 0; | |
100 | } | |
101 | ||
102 | ||
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
103 | |
43 | 104 | void cmd_die(int onsig) |
105 | { | |
106 | syslog(LOG_NOTICE, "Server process die on signal %d", onsig); | |
107 | close(s); | |
108 | exit(0); | |
109 | } | |
110 | ||
111 | ||
112 | ||
113 | void cmd_server(void) | |
114 | { | |
54
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
115 | char *inp, *p, *q, buf[SS_BUFSIZE], obuf[SS_BUFSIZE]; |
48 | 116 | int i, rc, rlen; |
43 | 117 | socklen_t fromlen; |
46
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
118 | float newtemp; |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
119 | |
51
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
120 | // if (debug) { |
52 | 121 | // char *hostname = inet_ntoa(peeraddr_in.sin_addr); |
51
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
122 | // syslog(LOG_NOTICE, "Start new client 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
|
123 | // fprintf(stdout, "Start new client 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
|
124 | // } |
43 | 125 | |
48 | 126 | memset((char *)&buf, 0, SS_BUFSIZE); |
127 | fromlen = sizeof(peeraddr_in); | |
128 | rlen = recvfrom(s, buf, sizeof(buf) -1, 0, (struct sockaddr *)&peeraddr_in, &fromlen); | |
129 | if (rlen == -1) { | |
130 | syslog(LOG_NOTICE, "recvfrom(): %s", strerror(errno)); | |
131 | } else { | |
132 | for (i = 0; i < strlen(buf); i++) { | |
133 | if (buf[i] == '\n') | |
134 | buf[i] = '\0'; | |
135 | if (buf[i] == '\r') | |
136 | buf[i] = '\0'; | |
137 | } | |
138 | if (strlen(buf)) { | |
139 | if (debug) { | |
140 | syslog(LOG_NOTICE, "recv: \"%s\"", buf); | |
141 | fprintf(stdout, "recv: \"%s\"\n", buf); | |
43 | 142 | } |
143 | ||
144 | /* | |
48 | 145 | * Process commands from the client |
43 | 146 | */ |
48 | 147 | if (strncmp(buf, "ack", 3) == 0) { |
148 | srv_send((char *)"ack"); | |
149 | } else if (strncmp(buf, "lcd", 3) == 0) { | |
150 | sprintf(obuf, "[\" \", \" \", \" \", \" \"]"); | |
151 | for (i = 0; i < 20; i++) { | |
152 | obuf[i+2] = lcdbuf[lcdHandle][i][0]; | |
153 | obuf[i+26] = lcdbuf[lcdHandle][i][1]; | |
154 | obuf[i+50] = lcdbuf[lcdHandle][i][2]; | |
155 | obuf[i+74] = lcdbuf[lcdHandle][i][3]; | |
156 | } | |
157 | srv_send(obuf); | |
158 | } else if (strncmp(buf, "getMode", 7) == 0) { | |
53 | 159 | srv_send("%c", Config.cs_mode); |
48 | 160 | } else if (strncmp(buf, "getFridge", 9) == 0) { |
53 | 161 | srv_send("%.1f", Config.cs_fridgeSet); |
48 | 162 | } else if (strncmp(buf, "getBeer", 7) == 0) { |
53 | 163 | srv_send("%.1f", Config.cs_beerSet); |
48 | 164 | } else if (strncmp(buf, "getControlConstants", 19) == 0) { |
165 | srv_send("{ \"tempFormat\":\"%c\", \"tempSetMin\":%.1f, \"tempSetMax\":%.1f, \"idleRangeH\":%.3f, \"idleRangeL\":%.3f }", | |
54
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
166 | Config.cc_tempFormat, Config.cc_tempSetMin, Config.cc_tempSetMax, Config.cc_idleRangeH, Config.cc_idleRangeL ); |
48 | 167 | } else if (strncmp(buf, "getControlSettings", 18) == 0) { |
168 | srv_send("{ \"mode\":\"%c\", \"beerSet\":%.1f, \"fridgeSet\":%.1f, \"heatEstimator\":%.1f, \"coolEstimator\":%.1f }", | |
53 | 169 | Config.cs_mode, Config.cs_beerSet, Config.cs_fridgeSet, Config.cs_heatEstimator, Config.cs_coolEstimator); |
48 | 170 | } else if (strncmp(buf, "getControlVariables", 19) == 0) { |
171 | srv_send("{ \"beerDiff\":%.2f }", cv_beerDiff); | |
49
92a9ca143677
Fixed temperature logging, added some commands
Michiel Broek <mbroek@mbse.eu>
parents:
48
diff
changeset
|
172 | } else if (strncmp(buf, "loadDefaultControlSettings", 26) == 0) { |
92a9ca143677
Fixed temperature logging, added some commands
Michiel Broek <mbroek@mbse.eu>
parents:
48
diff
changeset
|
173 | defaultControlSettings(); |
92a9ca143677
Fixed temperature logging, added some commands
Michiel Broek <mbroek@mbse.eu>
parents:
48
diff
changeset
|
174 | srv_send("ack"); |
92a9ca143677
Fixed temperature logging, added some commands
Michiel Broek <mbroek@mbse.eu>
parents:
48
diff
changeset
|
175 | } else if (strncmp(buf, "loadDefaultControlConstants", 27) == 0) { |
92a9ca143677
Fixed temperature logging, added some commands
Michiel Broek <mbroek@mbse.eu>
parents:
48
diff
changeset
|
176 | defaultControlConstants(); |
92a9ca143677
Fixed temperature logging, added some commands
Michiel Broek <mbroek@mbse.eu>
parents:
48
diff
changeset
|
177 | srv_send("ack"); |
48 | 178 | } else if (strncmp(buf, "setBeer=", 8) == 0) { |
179 | inp = xstrcpy(buf+8); | |
180 | rc = sscanf(inp, "%f", &newtemp); | |
181 | if (debug) | |
182 | fprintf(stdout, "new temp from %s, %.1f, rc=%d\n", inp, newtemp, rc); | |
183 | if (rc == 1) { | |
54
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
184 | 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
|
185 | 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
|
186 | srv_send("ack"); |
53 | 187 | Config.cs_mode = 'b'; |
188 | Config.cs_beerSet = newtemp; | |
51
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
189 | } else { |
54
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
190 | 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
|
191 | srv_send("err"); |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
192 | } |
48 | 193 | } else { |
51
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
194 | syslog(LOG_NOTICE, "Cannot convert temperature '%s' to float", inp); |
48 | 195 | srv_send("err"); |
196 | } | |
197 | free(inp); | |
198 | } else if (strncmp(buf, "setFridge=", 10) == 0) { | |
199 | inp = xstrcpy(buf+10); | |
200 | rc = sscanf(inp, "%f", &newtemp); | |
201 | if (debug) | |
202 | fprintf(stdout, "new temp from %s, %.1f, rc=%d\n", inp, newtemp, rc); | |
203 | if (rc == 1) { | |
54
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
204 | 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
|
205 | 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
|
206 | srv_send("ack"); |
53 | 207 | Config.cs_mode = 'f'; |
208 | Config.cs_fridgeSet = newtemp; | |
51
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
209 | } else { |
54
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
210 | 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
|
211 | srv_send("err"); |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
212 | } |
48 | 213 | } else { |
51
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
214 | syslog(LOG_NOTICE, "Cannot convert temperature '%s' to float", inp); |
48 | 215 | srv_send("err"); |
216 | } | |
217 | free(inp); | |
49
92a9ca143677
Fixed temperature logging, added some commands
Michiel Broek <mbroek@mbse.eu>
parents:
48
diff
changeset
|
218 | } else if (strncmp(buf, "setOff", 6) == 0) { |
92a9ca143677
Fixed temperature logging, added some commands
Michiel Broek <mbroek@mbse.eu>
parents:
48
diff
changeset
|
219 | if (debug) |
92a9ca143677
Fixed temperature logging, added some commands
Michiel Broek <mbroek@mbse.eu>
parents:
48
diff
changeset
|
220 | 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
|
221 | syslog(LOG_NOTICE, "Notification: Temperature control disabled"); |
53 | 222 | Config.cs_mode = 'o'; |
49
92a9ca143677
Fixed temperature logging, added some commands
Michiel Broek <mbroek@mbse.eu>
parents:
48
diff
changeset
|
223 | srv_send("ack"); |
54
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
224 | } else if (strncmp(buf, "setParameters=", 14) == 0) { |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
225 | 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
|
226 | if (debug) |
54
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
227 | fprintf(stdout, "setParameters: %s\n", inp); |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
228 | strtok(inp, (char *)"\""); |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
229 | p = strtok(NULL, (char *)"\""); |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
230 | q = strtok(NULL, (char *)":,}"); |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
231 | if (strcmp(p, (char *)"tempSetMin") == 0) { |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
232 | rc = sscanf(q, "%f", &newtemp); |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
233 | if (rc == 1) { |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
234 | syslog(LOG_NOTICE, "cc_tempSetMin = %1.f", newtemp); |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
235 | Config.cc_tempSetMin = newtemp; |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
236 | srv_send("ack"); |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
237 | } else { |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
238 | srv_send("ERR"); |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
239 | } |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
240 | } else if (strcmp(p, (char *)"tempSetMax") == 0) { |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
241 | rc = sscanf(q, "%f", &newtemp); |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
242 | if (rc == 1) { |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
243 | syslog(LOG_NOTICE, "cc_tempSetMax = %1.f", newtemp); |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
244 | Config.cc_tempSetMax = newtemp; |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
245 | srv_send("ack"); |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
246 | } else { |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
247 | srv_send("ERR"); |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
248 | } |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
249 | } else { |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
250 | 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
|
251 | srv_send("ERR"); |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
252 | } |
51
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
253 | // stopScript |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
254 | // quit |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
255 | // eraseLogs |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
256 | // interval |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
257 | // startNewBrew |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
258 | // pauseLogging |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
259 | // stopLogging |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
260 | // resumeLogging |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
261 | // dateTimeFormatDisplay |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
262 | } 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
|
263 | 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
|
264 | // programArduino |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
265 | // refreshDeviceList |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
266 | // getDeviceList |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
267 | // applyDevice |
43 | 268 | } else { |
48 | 269 | if (debug) |
270 | fprintf(stdout, "unknown command \"%s\"\n", buf); | |
271 | srv_send((char *)"ERR"); | |
43 | 272 | } |
273 | } | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
274 | } |
44 | 275 | |
51
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
276 | // if (debug) { |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
277 | // 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
|
278 | // 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
|
279 | // } |
48 | 280 | |
44 | 281 | close(s); |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
282 | } |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
283 | |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
284 | |
43 | 285 | |
286 | PI_THREAD (my_server_loop) | |
287 | { | |
288 | socklen_t addrlen; | |
44 | 289 | int optval = 1; |
43 | 290 | |
291 | syslog(LOG_NOTICE, "Thread my_server_loop started"); | |
292 | if (debug) | |
293 | fprintf(stdout, "Thread my_server_loop started\n"); | |
294 | ||
44 | 295 | memset((char *)&myaddr_in, 0, sizeof(struct sockaddr_in)); |
296 | memset((char *)&peeraddr_in, 0, sizeof(struct sockaddr_in)); | |
297 | myaddr_in.sin_family = AF_INET; | |
298 | myaddr_in.sin_addr.s_addr = INADDR_ANY; | |
299 | myaddr_in.sin_port = htons(Config.my_port); | |
300 | ||
301 | ls = socket(AF_INET, SOCK_STREAM, 0); | |
302 | if (ls == -1) { | |
303 | syslog(LOG_NOTICE, "Can't create listen socket: %s", strerror(errno)); | |
304 | fprintf(stderr, "Can't create listen socket: %s\n", strerror(errno)); | |
305 | return 0; | |
306 | } | |
307 | ||
308 | if (setsockopt(ls, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof(optval)) == -1) { | |
309 | syslog(LOG_NOTICE, "Can't setsockopt SO_KEEPALIVE socket: %s", strerror(errno)); | |
310 | close(ls); | |
311 | return 0; | |
312 | } | |
313 | ||
314 | if (setsockopt(ls, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) == -1) { | |
315 | syslog(LOG_NOTICE, "Can't setsockopt SO_REUSEADDR socket: %s", strerror(errno)); | |
316 | close(ls); | |
317 | return NULL; | |
318 | } | |
319 | ||
320 | if (bind(ls, (struct sockaddr *)&myaddr_in, sizeof(struct sockaddr_in)) == -1) { | |
321 | syslog(LOG_NOTICE, "Can't bind to listen socket: %s", strerror(errno)); | |
322 | close(ls); | |
323 | return NULL; | |
324 | } | |
325 | ||
326 | if (listen(ls, 5) == -1) { | |
327 | syslog(LOG_NOTICE, "Can't listen on listen socket: %s", strerror(errno)); | |
328 | close(ls); | |
329 | return NULL; | |
330 | } | |
331 | ||
332 | syslog(LOG_NOTICE, "listen socket created %d", ls); | |
333 | if (debug) | |
334 | fprintf(stdout, "listen socket created %d\n", ls); | |
335 | ||
336 | ||
43 | 337 | /* |
338 | * Loop forever until the external shutdown variable is set. | |
339 | */ | |
340 | for (;;) { | |
341 | ||
342 | addrlen = sizeof(struct sockaddr_in); | |
343 | /* | |
344 | * This call will block until a new connection | |
345 | * arrives. Then it will return the address of | |
346 | * the connecting peer, and a new socket | |
347 | * descriptor, s, for that connection. | |
348 | */ | |
349 | s = accept(ls, (struct sockaddr *)&peeraddr_in, &addrlen); | |
350 | if (s == -1) { | |
351 | syslog(LOG_NOTICE, "my_server_loop accept failed %s", strerror(errno)); | |
352 | if (debug) | |
353 | fprintf(stdout, "my_server_loop accept failed %s\n", strerror(errno)); | |
44 | 354 | return 0; |
43 | 355 | } |
356 | ||
48 | 357 | cmd_server(); |
43 | 358 | |
359 | if (my_shutdown) { | |
360 | syslog(LOG_NOTICE, "Thread my_server_loop stopped"); | |
361 | if (debug) | |
362 | fprintf(stdout, "Thread my_server_loop stopped\n"); | |
363 | return 0; | |
364 | } | |
365 | ||
366 | } | |
367 | } | |
368 | ||
369 | ||
370 |