thermferm/server.c

changeset 554
ab9f22ab57b5
parent 553
4091d4fe217f
child 557
ae17042a6d39
equal deleted inserted replaced
553:4091d4fe217f 554:ab9f22ab57b5
90 90
91 va_start(va_ptr, format); 91 va_start(va_ptr, format);
92 vsnprintf(out, SS_BUFSIZE-1, format, va_ptr); 92 vsnprintf(out, SS_BUFSIZE-1, format, va_ptr);
93 va_end(va_ptr); 93 va_end(va_ptr);
94 94
95 if (debug) { 95 // if (debug) {
96 syslog(LOG_NOTICE, "send: \"%s\"", out); 96 // syslog(LOG_NOTICE, "send: \"%s\"", out);
97 fprintf(stdout, "send: \"%s\"\n", out); 97 // fprintf(stdout, "send: \"%s\"\n", out);
98 } 98 // }
99 99
100 if (send(s, out, strlen(out), 0) != strlen(out)) { 100 if (send(s, out, strlen(out), 0) != strlen(out)) {
101 syslog(LOG_NOTICE, "srv_send failed"); 101 syslog(LOG_NOTICE, "srv_send failed");
102 return -1; 102 return -1;
103 } 103 }
146 buffer[bytesloaded] = buf; 146 buffer[bytesloaded] = buf;
147 bytesloaded++; 147 bytesloaded++;
148 } 148 }
149 } 149 }
150 150
151 if (debug) { 151 // if (debug) {
152 syslog(LOG_NOTICE, "recv: %d `%s'", bytesloaded, buffer); 152 // syslog(LOG_NOTICE, "recv: %d `%s'", bytesloaded, buffer);
153 fprintf(stdout, "recv: %d `%s'\n", bytesloaded, buffer); 153 // fprintf(stdout, "recv: %d `%s'\n", bytesloaded, buffer);
154 } 154 // }
155 return bytesloaded; 155 return bytesloaded;
156 } 156 }
157 157
158 158
159 159
369 * Remove files from the list when they are in use 369 * Remove files from the list when they are in use
370 */ 370 */
371 found = 0; 371 found = 0;
372 for (unit = Config.units; unit; unit = unit->next) { 372 for (unit = Config.units; unit; unit = unit->next) {
373 if (unit->mode != UNITMODE_OFF) { 373 if (unit->mode != UNITMODE_OFF) {
374 logname = xstrcpy(unit->name); 374 logname = xstrcpy(unit->product_code);
375 logname = xstrcat(logname, (char *)" ");
376 logname = xstrcat(logname, unit->product_name);
375 logname = xstrcat(logname, (char *)".log"); 377 logname = xstrcat(logname, (char *)".log");
376 if (! strcmp(result->d_name, logname)) 378 if (! strcmp(result->d_name, logname))
377 found = 1; 379 found = 1;
378 free(logname); 380 free(logname);
379 logname = NULL; 381 logname = NULL;
1188 /* 1190 /*
1189 * Default, list available units 1191 * Default, list available units
1190 */ 1192 */
1191 srv_send((char *)"212 Fermenter list follows:"); 1193 srv_send((char *)"212 Fermenter list follows:");
1192 for (unit = Config.units; unit; unit = unit->next) { 1194 for (unit = Config.units; unit; unit = unit->next) {
1193 srv_send((char *)"%s,%s,%s", unit->uuid, unit->name, UNITMODE[unit->mode]); 1195 srv_send((char *)"%s,%s,%s", unit->uuid, unit->alias, UNITMODE[unit->mode]);
1194 } 1196 }
1195 srv_send((char *)"."); 1197 srv_send((char *)".");
1196 return 0; 1198 return 0;
1197 1199
1198 } else if (strcmp(opt, (char *)"HELP") == 0) { 1200 } else if (strcmp(opt, (char *)"HELP") == 0) {
1808 if (strcmp(current->uuid, uuid) == 0) { 1810 if (strcmp(current->uuid, uuid) == 0) {
1809 if (previous == NULL) { 1811 if (previous == NULL) {
1810 Config.units = current->next; 1812 Config.units = current->next;
1811 free(current->uuid); 1813 free(current->uuid);
1812 current->uuid = NULL; 1814 current->uuid = NULL;
1813 free(current->name); 1815 if (current->product_uuid)
1814 current->name = NULL; 1816 free(current->product_uuid);
1817 current->product_uuid = NULL;
1818 if (current->product_code)
1819 free(current->product_code);
1820 current->product_code = NULL;
1821 if (current->product_name)
1822 free(current->product_name);
1823 current->product_name = NULL;
1815 if (current->air_address) 1824 if (current->air_address)
1816 free(current->air_address); 1825 free(current->air_address);
1817 current->air_address = NULL; 1826 current->air_address = NULL;
1818 if (current->beer_address) 1827 if (current->beer_address)
1819 free(current->beer_address); 1828 free(current->beer_address);
1851 free(current); 1860 free(current);
1852 return 1; 1861 return 1;
1853 } else { 1862 } else {
1854 free(current->uuid); 1863 free(current->uuid);
1855 current->uuid = NULL; 1864 current->uuid = NULL;
1856 free(current->name); 1865 if (current->product_uuid)
1857 current->name = NULL; 1866 free(current->product_uuid);
1867 current->product_uuid = NULL;
1868 if (current->product_code)
1869 free(current->product_code);
1870 current->product_code = NULL;
1871 if (current->product_name)
1872 free(current->product_name);
1873 current->product_name = NULL;
1858 if (current->air_address) 1874 if (current->air_address)
1859 free(current->air_address); 1875 free(current->air_address);
1860 current->air_address = NULL; 1876 current->air_address = NULL;
1861 if (current->beer_address) 1877 if (current->beer_address)
1862 free(current->beer_address); 1878 free(current->beer_address);
1943 } 1959 }
1944 1960
1945 if ((strcmp(opt, (char *)"LIST") == 0) && (param == NULL)) { 1961 if ((strcmp(opt, (char *)"LIST") == 0) && (param == NULL)) {
1946 srv_send((char *)"212 Fermenter list follows:"); 1962 srv_send((char *)"212 Fermenter list follows:");
1947 for (unit = Config.units; unit; unit = unit->next) { 1963 for (unit = Config.units; unit; unit = unit->next) {
1948 srv_send((char *)"%s,%s,%s", unit->uuid, unit->name, UNITMODE[unit->mode]); 1964 srv_send((char *)"%s,%s,%s", unit->uuid, unit->alias, UNITMODE[unit->mode]);
1949 } 1965 }
1950 srv_send((char *)"."); 1966 srv_send((char *)".");
1951 return 0; 1967 return 0;
1952 } 1968 }
1953 1969
1965 unit->next = NULL; 1981 unit->next = NULL;
1966 unit->version = 1; 1982 unit->version = 1;
1967 unit->uuid = malloc(37); 1983 unit->uuid = malloc(37);
1968 uuid_generate(uu); 1984 uuid_generate(uu);
1969 uuid_unparse(uu, unit->uuid); 1985 uuid_unparse(uu, unit->uuid);
1970 unit->name = xstrcpy(param); 1986 unit->product_uuid = NULL;
1987 unit->product_code = xstrcpy((char *)"FAKE0000");
1988 unit->product_name = xstrcpy(param);
1971 unit->alias = xstrcpy(an); 1989 unit->alias = xstrcpy(an);
1972 unit->air_address = unit->beer_address = unit->chiller_address = unit->heater_address = unit->cooler_address = \ 1990 unit->air_address = unit->beer_address = unit->chiller_address = unit->heater_address = unit->cooler_address = \
1973 unit->fan_address = unit->door_address = unit->light_address = \ 1991 unit->fan_address = unit->door_address = unit->light_address = \
1974 unit->psu_address = unit->profile = NULL; 1992 unit->psu_address = unit->profile = NULL;
1975 unit->air_idx = unit->beer_idx = unit->chiller_idx = unit->heater_idx = unit->cooler_idx = unit->fan_idx = \ 1993 unit->air_idx = unit->beer_idx = unit->chiller_idx = unit->heater_idx = unit->cooler_idx = unit->fan_idx = \
2048 2066
2049 if (strcmp(opt, (char *)"GET") == 0) { 2067 if (strcmp(opt, (char *)"GET") == 0) {
2050 for (unit = Config.units; unit; unit = unit->next) { 2068 for (unit = Config.units; unit; unit = unit->next) {
2051 if (strcmp(param, unit->uuid) == 0) { 2069 if (strcmp(param, unit->uuid) == 0) {
2052 srv_send((char *)"213 Unit listing follows:"); 2070 srv_send((char *)"213 Unit listing follows:");
2053 srv_send((char *)"NAME,%s", unit->name);
2054 srv_send((char *)"UUID,%s", unit->uuid); 2071 srv_send((char *)"UUID,%s", unit->uuid);
2055 srv_send((char *)"ALIAS,%s", unit->alias); 2072 srv_send((char *)"ALIAS,%s", unit->alias);
2073 srv_send((char *)"PRODUCT_NAME,%s", unit->product_name);
2074 srv_send((char *)"PRODUCT_CODE,%s", unit->product_code);
2056 srv_send((char *)"MODE,%s", UNITMODE[unit->mode]); 2075 srv_send((char *)"MODE,%s", UNITMODE[unit->mode]);
2057 srv_send((char *)"VOLUME,%2f", unit->volume); 2076 srv_send((char *)"VOLUME,%2f", unit->volume);
2058 srv_send((char *)"AIR_ADDRESS,%s", unit->air_address); 2077 srv_send((char *)"AIR_ADDRESS,%s", unit->air_address);
2059 srv_send((char *)"AIR_STATE,%s", TEMPSTATE[unit->air_state]); 2078 srv_send((char *)"AIR_STATE,%s", TEMPSTATE[unit->air_state]);
2060 srv_send((char *)"AIR_TEMPERATURE,%.3f", unit->air_temperature / 1000.0); 2079 srv_send((char *)"AIR_TEMPERATURE,%.3f", unit->air_temperature / 1000.0);
2168 /* 2187 /*
2169 * Accept writable data. The client can sent just one line, 2188 * Accept writable data. The client can sent just one line,
2170 * but may also sent everything. Simply ignore things we 2189 * but may also sent everything. Simply ignore things we
2171 * don't understand. 2190 * don't understand.
2172 */ 2191 */
2173 if (val && (strcmp(kwd, (char *)"NAME") == 0)) { 2192 if (val && (strcmp(kwd, (char *)"PRODUCT_CODE") == 0)) {
2174 if (unit->name) { 2193 if (unit->product_code) {
2175 if (strcmp(unit->name, val)) { 2194 if (strcmp(unit->product_code, val)) {
2176 syslog(LOG_NOTICE, "Fermenter unit %s name `%s' to `%s'", unit->uuid, unit->name, val); 2195 syslog(LOG_NOTICE, "Fermenter unit %s name `%s' to `%s'", unit->uuid, unit->product_code, val);
2177 } 2196 }
2178 free(unit->name); 2197 free(unit->product_code);
2179 } 2198 }
2180 unit->name = xstrcpy(val); 2199 unit->product_code = xstrcpy(val);
2200 unit->mqtt_flag |= MQTT_FLAG_DATA;
2201
2202 } else if (val && (strcmp(kwd, (char *)"PRODUCT_NAME") == 0)) {
2203 if (unit->product_name) {
2204 if (strcmp(unit->product_name, val)) {
2205 syslog(LOG_NOTICE, "Fermenter unit %s name `%s' to `%s'", unit->uuid, unit->product_name, val);
2206 }
2207 free(unit->product_name);
2208 }
2209 unit->product_name = xstrcpy(val);
2181 unit->mqtt_flag |= MQTT_FLAG_DATA; 2210 unit->mqtt_flag |= MQTT_FLAG_DATA;
2182 2211
2183 } else if (val && (strcmp(kwd, (char *)"VOLUME") == 0)) { 2212 } else if (val && (strcmp(kwd, (char *)"VOLUME") == 0)) {
2184 if (sscanf(val, "%f", &fval) == 1) { 2213 if (sscanf(val, "%f", &fval) == 1) {
2185 if (unit->volume != fval) 2214 if (unit->volume != fval)
2439 for (i = 0; i < 5; i++) { 2468 for (i = 0; i < 5; i++) {
2440 if (strcmp(val, UNITMODE[i]) == 0) { 2469 if (strcmp(val, UNITMODE[i]) == 0) {
2441 unit->mqtt_flag |= MQTT_FLAG_DATA; 2470 unit->mqtt_flag |= MQTT_FLAG_DATA;
2442 /* Initialize log if the unit is turned on */ 2471 /* Initialize log if the unit is turned on */
2443 if ((unit->mode == UNITMODE_OFF) && (i != UNITMODE_OFF)) { 2472 if ((unit->mode == UNITMODE_OFF) && (i != UNITMODE_OFF)) {
2444 initlog(unit->name); 2473 initlog(unit->product_code, unit->product_name);
2445 unit->mqtt_flag |= MQTT_FLAG_BIRTH; 2474 unit->mqtt_flag |= MQTT_FLAG_BIRTH;
2446 } else if ((unit->mode != UNITMODE_OFF) && (i == UNITMODE_OFF)) { 2475 } else if ((unit->mode != UNITMODE_OFF) && (i == UNITMODE_OFF)) {
2447 unit->mqtt_flag |= MQTT_FLAG_DEATH; 2476 unit->mqtt_flag |= MQTT_FLAG_DEATH;
2448 } 2477 }
2449 syslog(LOG_NOTICE, "Fermenter unit %s mode %s to %s", unit->uuid, UNITMODE[unit->mode], UNITMODE[i]); 2478 syslog(LOG_NOTICE, "Fermenter unit %s mode %s to %s", unit->uuid, UNITMODE[unit->mode], UNITMODE[i]);
2649 publishDData(unit); 2678 publishDData(unit);
2650 } 2679 }
2651 if (unit->mqtt_flag & MQTT_FLAG_DEATH) { 2680 if (unit->mqtt_flag & MQTT_FLAG_DEATH) {
2652 publishDDeath(unit); 2681 publishDDeath(unit);
2653 } 2682 }
2683 unit->mqtt_flag |= MQTT_FLAG_DLOG; // Something to log
2654 } 2684 }
2655 #endif 2685 #endif
2656 } 2686 }
2657 } 2687 }
2658 } 2688 }

mercurial