thermferm/server.c

changeset 506
cdcd07bbee30
parent 504
862de87f9f89
child 510
2da3d3340403
equal deleted inserted replaced
504:862de87f9f89 506:cdcd07bbee30
1 /***************************************************************************** 1 /*****************************************************************************
2 * Copyright (C) 2008-2015 2 * Copyright (C) 2008-2017
3 * 3 *
4 * Michiel Broek <mbroek at mbse dot eu> 4 * Michiel Broek <mbroek at mbse dot eu>
5 * 5 *
6 * This file is part of the mbsePi-apps 6 * This file is part of the mbsePi-apps
7 * 7 *
2164 } 2164 }
2165 2165
2166 for (unit = Config.units ; unit; unit = unit->next) { 2166 for (unit = Config.units ; unit; unit = unit->next) {
2167 if (strcmp(unit->uuid, param) == 0) { 2167 if (strcmp(unit->uuid, param) == 0) {
2168 while (1) { 2168 while (1) {
2169 unit->mqtt_flag = 0;
2169 rlen = srv_recv(ibuf); 2170 rlen = srv_recv(ibuf);
2170 if (rlen == -1) { 2171 if (rlen == -1) {
2171 run_pause = FALSE; 2172 run_pause = FALSE;
2172 return 0; 2173 return 0;
2173 } 2174 }
2178 return 1; 2179 return 1;
2179 } 2180 }
2180 kwd = strtok(ibuf, ",\0"); 2181 kwd = strtok(ibuf, ",\0");
2181 val = strtok(NULL, "\0"); 2182 val = strtok(NULL, "\0");
2182 if (kwd) { 2183 if (kwd) {
2184 unit->mqtt_flag = 0;
2183 /* 2185 /*
2184 * Accept writable data. The client can sent just one line, 2186 * Accept writable data. The client can sent just one line,
2185 * but may also sent everything. Simply ignore things we 2187 * but may also sent everything. Simply ignore things we
2186 * don't understand. 2188 * don't understand.
2187 */ 2189 */
2188 if (val && (strcmp(kwd, (char *)"NAME") == 0)) { 2190 if (val && (strcmp(kwd, (char *)"NAME") == 0)) {
2189 if (unit->name) { 2191 if (unit->name) {
2190 if (strcmp(unit->name, val)) { 2192 if (strcmp(unit->name, val)) {
2191 syslog(LOG_NOTICE, "Fermenter unit %s name `%s' to `%s'", unit->uuid, unit->name, val); 2193 syslog(LOG_NOTICE, "Fermenter unit %s name `%s' to `%s'", unit->uuid, unit->name, val);
2192 mqtt_publish_str(unit->alias, (char *)"name", val);
2193 } 2194 }
2194 free(unit->name); 2195 free(unit->name);
2195 } 2196 }
2196 unit->name = xstrcpy(val); 2197 unit->name = xstrcpy(val);
2198 unit->mqtt_flag |= (MQTT_FLAG_MODE);
2197 2199
2198 } else if (val && (strcmp(kwd, (char *)"VOLUME") == 0)) { 2200 } else if (val && (strcmp(kwd, (char *)"VOLUME") == 0)) {
2199 if (sscanf(val, "%f", &fval) == 1) { 2201 if (sscanf(val, "%f", &fval) == 1) {
2200 if (unit->volume != fval) 2202 if (unit->volume != fval)
2201 syslog(LOG_NOTICE, "Fermenter unit %s volume %.3f to %.3f", unit->uuid, unit->volume, fval); 2203 syslog(LOG_NOTICE, "Fermenter unit %s volume %.3f to %.3f", unit->uuid, unit->volume, fval);
2212 if (val) { 2214 if (val) {
2213 unit->air_address = xstrcpy(val); 2215 unit->air_address = xstrcpy(val);
2214 device_count(TRUE, unit->air_address); 2216 device_count(TRUE, unit->air_address);
2215 } else 2217 } else
2216 unit->air_address = NULL; 2218 unit->air_address = NULL;
2219 unit->mqtt_flag |= MQTT_FLAG_AIR;
2217 2220
2218 } else if (strcmp(kwd, (char *)"BEER_ADDRESS") == 0) { 2221 } else if (strcmp(kwd, (char *)"BEER_ADDRESS") == 0) {
2219 if (val && unit->beer_address && (strcmp(val, unit->beer_address))) 2222 if (val && unit->beer_address && (strcmp(val, unit->beer_address)))
2220 syslog(LOG_NOTICE, "Fermenter unit %s beer address `%s' to `%s'", unit->uuid, unit->beer_address, val); 2223 syslog(LOG_NOTICE, "Fermenter unit %s beer address `%s' to `%s'", unit->uuid, unit->beer_address, val);
2221 if (unit->beer_address) { 2224 if (unit->beer_address) {
2225 if (val) { 2228 if (val) {
2226 unit->beer_address = xstrcpy(val); 2229 unit->beer_address = xstrcpy(val);
2227 device_count(TRUE, unit->beer_address); 2230 device_count(TRUE, unit->beer_address);
2228 } else 2231 } else
2229 unit->beer_address = NULL; 2232 unit->beer_address = NULL;
2233 unit->mqtt_flag |= MQTT_FLAG_BEER;
2230 2234
2231 } else if (strcmp(kwd, (char *)"HEATER_ADDRESS") == 0) { 2235 } else if (strcmp(kwd, (char *)"HEATER_ADDRESS") == 0) {
2232 if (val && unit->heater_address && (strcmp(val, unit->heater_address))) 2236 if (val && unit->heater_address && (strcmp(val, unit->heater_address)))
2233 syslog(LOG_NOTICE, "Fermenter unit %s heater address `%s' to `%s'", unit->uuid, unit->heater_address, val); 2237 syslog(LOG_NOTICE, "Fermenter unit %s heater address `%s' to `%s'", unit->uuid, unit->heater_address, val);
2234 if (unit->heater_address) { 2238 if (unit->heater_address) {
2238 if (val) { 2242 if (val) {
2239 unit->heater_address = xstrcpy(val); 2243 unit->heater_address = xstrcpy(val);
2240 device_count(TRUE, unit->heater_address); 2244 device_count(TRUE, unit->heater_address);
2241 } else 2245 } else
2242 unit->heater_address = NULL; 2246 unit->heater_address = NULL;
2247 unit->mqtt_flag |= MQTT_FLAG_HEATER;
2243 2248
2244 } else if (val && (strcmp(kwd, (char *)"HEATER_STATE") == 0)) { 2249 } else if (val && (strcmp(kwd, (char *)"HEATER_STATE") == 0)) {
2245 if ((sscanf(val, "%d", &ival) == 1) && ((ival == 0) || (ival == 100))) { 2250 if ((sscanf(val, "%d", &ival) == 1) && ((ival == 0) || (ival == 100))) {
2246 if (unit->heater_state != ival) 2251 if (unit->heater_state != ival)
2247 syslog(LOG_NOTICE, "Fermenter unit %s heater state %d to %d", unit->uuid, unit->heater_state, ival); 2252 syslog(LOG_NOTICE, "Fermenter unit %s heater state %d to %d", unit->uuid, unit->heater_state, ival);
2248 unit->heater_state = ival; 2253 unit->heater_state = ival;
2254 unit->mqtt_flag |= MQTT_FLAG_HEATER;
2249 } 2255 }
2250 2256
2251 } else if (val && (strcmp(kwd, (char *)"HEATER_DELAY") == 0)) { 2257 } else if (val && (strcmp(kwd, (char *)"HEATER_DELAY") == 0)) {
2252 if (sscanf(val, "%d", &ival) == 1) { 2258 if (sscanf(val, "%d", &ival) == 1) {
2253 if (unit->heater_delay != ival) 2259 if (unit->heater_delay != ival)
2265 if (val) { 2271 if (val) {
2266 unit->cooler_address = xstrcpy(val); 2272 unit->cooler_address = xstrcpy(val);
2267 device_count(TRUE, unit->cooler_address); 2273 device_count(TRUE, unit->cooler_address);
2268 } else 2274 } else
2269 unit->cooler_address = NULL; 2275 unit->cooler_address = NULL;
2276 unit->mqtt_flag |= MQTT_FLAG_COOLER;
2270 2277
2271 } else if (val && (strcmp(kwd, (char *)"COOLER_STATE") == 0)) { 2278 } else if (val && (strcmp(kwd, (char *)"COOLER_STATE") == 0)) {
2272 if ((sscanf(val, "%d", &ival) == 1) && ((ival == 0) || (ival == 100))) { 2279 if ((sscanf(val, "%d", &ival) == 1) && ((ival == 0) || (ival == 100))) {
2273 if (unit->cooler_state != ival) 2280 if (unit->cooler_state != ival)
2274 syslog(LOG_NOTICE, "Fermenter unit %s cooler state %d to %d", unit->uuid, unit->cooler_state, ival); 2281 syslog(LOG_NOTICE, "Fermenter unit %s cooler state %d to %d", unit->uuid, unit->cooler_state, ival);
2275 unit->cooler_state = ival; 2282 unit->cooler_state = ival;
2283 unit->mqtt_flag |= MQTT_FLAG_COOLER;
2276 } 2284 }
2277 2285
2278 } else if (val && (strcmp(kwd, (char *)"COOLER_DELAY") == 0)) { 2286 } else if (val && (strcmp(kwd, (char *)"COOLER_DELAY") == 0)) {
2279 if (sscanf(val, "%d", &ival) == 1) { 2287 if (sscanf(val, "%d", &ival) == 1) {
2280 if (unit->cooler_delay != ival) 2288 if (unit->cooler_delay != ival)
2292 if (val) { 2300 if (val) {
2293 unit->fan_address = xstrcpy(val); 2301 unit->fan_address = xstrcpy(val);
2294 device_count(TRUE, unit->fan_address); 2302 device_count(TRUE, unit->fan_address);
2295 } else 2303 } else
2296 unit->fan_address = NULL; 2304 unit->fan_address = NULL;
2305 unit->mqtt_flag |= MQTT_FLAG_FAN;
2297 2306
2298 } else if (val && (strcmp(kwd, (char *)"FAN_STATE") == 0)) { 2307 } else if (val && (strcmp(kwd, (char *)"FAN_STATE") == 0)) {
2299 if ((sscanf(val, "%d", &ival) == 1) && ((ival == 0) || (ival == 100))) { 2308 if ((sscanf(val, "%d", &ival) == 1) && ((ival == 0) || (ival == 100))) {
2300 if (unit->fan_state != ival) 2309 if (unit->fan_state != ival)
2301 syslog(LOG_NOTICE, "Fermenter unit %s fan state %d to %d", unit->uuid, unit->fan_state, ival); 2310 syslog(LOG_NOTICE, "Fermenter unit %s fan state %d to %d", unit->uuid, unit->fan_state, ival);
2302 unit->fan_state = ival; 2311 unit->fan_state = ival;
2312 unit->mqtt_flag |= MQTT_FLAG_FAN;
2303 } 2313 }
2304 2314
2305 } else if (val && (strcmp(kwd, (char *)"FAN_DELAY") == 0)) { 2315 } else if (val && (strcmp(kwd, (char *)"FAN_DELAY") == 0)) {
2306 if (sscanf(val, "%d", &ival) == 1) { 2316 if (sscanf(val, "%d", &ival) == 1) {
2307 if (unit->fan_delay != ival) 2317 if (unit->fan_delay != ival)
2319 if (val) { 2329 if (val) {
2320 unit->light_address = xstrcpy(val); 2330 unit->light_address = xstrcpy(val);
2321 device_count(TRUE, unit->light_address); 2331 device_count(TRUE, unit->light_address);
2322 } else 2332 } else
2323 unit->light_address = NULL; 2333 unit->light_address = NULL;
2334 unit->mqtt_flag |= MQTT_FLAG_LIGHT;
2324 2335
2325 } else if (val && (strcmp(kwd, (char *)"LIGHT_STATE") == 0)) { 2336 } else if (val && (strcmp(kwd, (char *)"LIGHT_STATE") == 0)) {
2326 if ((sscanf(val, "%d", &ival) == 1) && ((ival == 0) || (ival == 100))) { 2337 if ((sscanf(val, "%d", &ival) == 1) && ((ival == 0) || (ival == 100))) {
2327 if (unit->light_state != ival) 2338 if (unit->light_state != ival)
2328 syslog(LOG_NOTICE, "Fermenter unit %s light state %d to %d", unit->uuid, unit->light_state, ival); 2339 syslog(LOG_NOTICE, "Fermenter unit %s light state %d to %d", unit->uuid, unit->light_state, ival);
2329 unit->light_state = ival; 2340 unit->light_state = ival;
2341 unit->mqtt_flag |= MQTT_FLAG_LIGHT;
2330 } 2342 }
2331 2343
2332 } else if (val && (strcmp(kwd, (char *)"LIGHT_DELAY") == 0)) { 2344 } else if (val && (strcmp(kwd, (char *)"LIGHT_DELAY") == 0)) {
2333 if (sscanf(val, "%d", &ival) == 1) { 2345 if (sscanf(val, "%d", &ival) == 1) {
2334 if (unit->light_delay != ival) 2346 if (unit->light_delay != ival)
2359 if (val) { 2371 if (val) {
2360 unit->psu_address = xstrcpy(val); 2372 unit->psu_address = xstrcpy(val);
2361 device_count(TRUE, unit->psu_address); 2373 device_count(TRUE, unit->psu_address);
2362 } else 2374 } else
2363 unit->psu_address = NULL; 2375 unit->psu_address = NULL;
2376 unit->mqtt_flag |= MQTT_FLAG_PSU;
2364 2377
2365 } else if (val && (strcmp(kwd, (char *)"MODE") == 0)) { 2378 } else if (val && (strcmp(kwd, (char *)"MODE") == 0)) {
2366 for (i = 0; i < 5; i++) { 2379 for (i = 0; i < 5; i++) {
2367 if (strcmp(val, UNITMODE[i]) == 0) { 2380 if (strcmp(val, UNITMODE[i]) == 0) {
2368 /* Initialize log if the unit is turned on */ 2381 /* Initialize log if the unit is turned on */
2390 if (unit->profile) { 2403 if (unit->profile) {
2391 unit->mqtt_flag |= MQTT_FLAG_PROFILE; 2404 unit->mqtt_flag |= MQTT_FLAG_PROFILE;
2392 unit->mqtt_flag |= MQTT_FLAG_SP; 2405 unit->mqtt_flag |= MQTT_FLAG_SP;
2393 } 2406 }
2394 } 2407 }
2395 if (unit->heater_address)
2396 mqtt_publish_int(unit->alias, (char *)"heater", 0);
2397 else
2398 mqtt_publish_clear(unit->alias, (char *)"heater");
2399 if (unit->cooler_address)
2400 mqtt_publish_int(unit->alias, (char *)"cooler", 0);
2401 else
2402 mqtt_publish_clear(unit->alias, (char *)"cooler");
2403 if (unit->fan_address)
2404 mqtt_publish_int(unit->alias, (char *)"fan", 0);
2405 else
2406 mqtt_publish_clear(unit->alias, (char *)"fan");
2407 break; 2408 break;
2408 } 2409 }
2409 } 2410 }
2410 2411
2411 } else if (val && (strcmp(kwd, (char *)"FRIDGE_SET") == 0)) { 2412 } else if (val && (strcmp(kwd, (char *)"FRIDGE_SET") == 0)) {
2517 unit->heater_wait = unit->cooler_wait = unit->fan_wait = unit->light_wait = 0; 2518 unit->heater_wait = unit->cooler_wait = unit->fan_wait = unit->light_wait = 0;
2518 device_out(unit->heater_address, unit->heater_state); 2519 device_out(unit->heater_address, unit->heater_state);
2519 device_out(unit->cooler_address, unit->cooler_state); 2520 device_out(unit->cooler_address, unit->cooler_state);
2520 device_out(unit->fan_address, unit->fan_state); 2521 device_out(unit->fan_address, unit->fan_state);
2521 device_out(unit->light_address, unit->light_state); 2522 device_out(unit->light_address, unit->light_state);
2522 if (unit->heater_address) 2523 unit->mqtt_flag |= (MQTT_FLAG_PROFILE | MQTT_FLAG_SP | MQTT_FLAG_HEATER | MQTT_FLAG_COOLER | MQTT_FLAG_FAN);
2523 mqtt_publish_int(unit->alias, (char *)"heater", 0);
2524 else
2525 mqtt_publish_clear(unit->alias, (char *)"heater");
2526 if (unit->cooler_address)
2527 mqtt_publish_int(unit->alias, (char *)"cooler", 0);
2528 else
2529 mqtt_publish_clear(unit->alias, (char *)"cooler");
2530 if (unit->fan_address)
2531 mqtt_publish_int(unit->alias, (char *)"fan", 0);
2532 else
2533 mqtt_publish_clear(unit->alias, (char *)"fan");
2534 unit->mqtt_flag |= (MQTT_FLAG_PROFILE | MQTT_FLAG_SP);
2535 } 2524 }
2536 2525
2537 } else if (val && (strcmp(kwd, (char *)"PROF_STATE") == 0)) { 2526 } else if (val && (strcmp(kwd, (char *)"PROF_STATE") == 0)) {
2538 for (i = 0; i < 5; i++) { 2527 for (i = 0; i < 5; i++) {
2539 if (strcmp(val, PROFSTATE[i]) == 0) { 2528 if (strcmp(val, PROFSTATE[i]) == 0) {
2540 switch (i) { 2529 switch (i) {
2541 case PROFILE_OFF: if (unit->prof_state == PROFILE_DONE) { 2530 case PROFILE_OFF: if (unit->prof_state == PROFILE_DONE) {
2542 unit->prof_state = PROFILE_OFF; 2531 unit->prof_state = PROFILE_OFF;
2543 syslog(LOG_NOTICE, "Fermenter unit %s profile to OFF", unit->uuid); 2532 syslog(LOG_NOTICE, "Fermenter unit %s profile to OFF", unit->uuid);
2544 mqtt_publish_str(unit->alias, (char *)"profile/state", (char *)PROFSTATE[i]);
2545 } 2533 }
2546 break; 2534 break;
2547 case PROFILE_PAUSE: if (unit->prof_state == PROFILE_RUN) { 2535 case PROFILE_PAUSE: if (unit->prof_state == PROFILE_RUN) {
2548 unit->prof_state = PROFILE_PAUSE; 2536 unit->prof_state = PROFILE_PAUSE;
2549 syslog(LOG_NOTICE, "Fermenter unit %s profile PAUSE", unit->uuid); 2537 syslog(LOG_NOTICE, "Fermenter unit %s profile PAUSE", unit->uuid);
2550 mqtt_publish_str(unit->alias, (char *)"profile/state", (char *)PROFSTATE[i]);
2551 } else if (unit->prof_state == PROFILE_PAUSE) { 2538 } else if (unit->prof_state == PROFILE_PAUSE) {
2552 unit->prof_state = PROFILE_RUN; 2539 unit->prof_state = PROFILE_RUN;
2553 syslog(LOG_NOTICE, "Fermenter unit %s profile RESUME", unit->uuid); 2540 syslog(LOG_NOTICE, "Fermenter unit %s profile RESUME", unit->uuid);
2554 mqtt_publish_str(unit->alias, (char *)"profile/state", (char *)PROFSTATE[PROFILE_RUN]);
2555 } 2541 }
2556 break; 2542 break;
2557 case PROFILE_RUN: if (unit->prof_state == PROFILE_OFF) { 2543 case PROFILE_RUN: if (unit->prof_state == PROFILE_OFF) {
2558 unit->prof_state = PROFILE_RUN; 2544 unit->prof_state = PROFILE_RUN;
2559 unit->prof_started = time(NULL); 2545 unit->prof_started = time(NULL);
2560 unit->prof_paused = unit->prof_primary_done = 0; 2546 unit->prof_paused = unit->prof_primary_done = 0;
2561 unit->prof_peak_abs = unit->prof_peak_rel = 0.0; 2547 unit->prof_peak_abs = unit->prof_peak_rel = 0.0;
2562 syslog(LOG_NOTICE, "Fermenter unit %s profile to RUN", unit->uuid); 2548 syslog(LOG_NOTICE, "Fermenter unit %s profile to RUN", unit->uuid);
2563 mqtt_publish_str(unit->alias, (char *)"profile/state", (char *)PROFSTATE[i]);
2564 } 2549 }
2565 break; 2550 break;
2566 case PROFILE_DONE: break; /* Command is illegal */ 2551 case PROFILE_DONE: break; /* Command is illegal */
2567 case PROFILE_ABORT: if ((unit->prof_state == PROFILE_RUN) || (unit->prof_state == PROFILE_PAUSE)) { 2552 case PROFILE_ABORT: if ((unit->prof_state == PROFILE_RUN) || (unit->prof_state == PROFILE_PAUSE)) {
2568 unit->prof_state = PROFILE_OFF; 2553 unit->prof_state = PROFILE_OFF;
2569 unit->prof_started = 0; 2554 unit->prof_started = 0;
2570 syslog(LOG_NOTICE, "Fermenter unit %s profile ABORT", unit->uuid); 2555 syslog(LOG_NOTICE, "Fermenter unit %s profile ABORT", unit->uuid);
2571 mqtt_publish_str(unit->alias, (char *)"profile/state", (char *)PROFSTATE[i]);
2572 } 2556 }
2573 break; 2557 break;
2574 } 2558 }
2575 unit->mqtt_flag |= (MQTT_FLAG_SP | MQTT_FLAG_PROFILE | MQTT_FLAG_PERCENT); 2559 unit->mqtt_flag |= (MQTT_FLAG_SP | MQTT_FLAG_PROFILE | MQTT_FLAG_PERCENT);
2576 break; 2560 break;
2590 syslog(LOG_NOTICE, "Fermenter unit %s temperature set maximum %.1f to %.1f", unit->uuid, unit->temp_set_max, fval); 2574 syslog(LOG_NOTICE, "Fermenter unit %s temperature set maximum %.1f to %.1f", unit->uuid, unit->temp_set_max, fval);
2591 unit->temp_set_max = fval; 2575 unit->temp_set_max = fval;
2592 } 2576 }
2593 2577
2594 } 2578 }
2579 if (unit->mqtt_flag)
2580 publishDData(unit);
2595 } 2581 }
2596 } 2582 }
2597 } 2583 }
2598 } 2584 }
2599 } 2585 }

mercurial