bmsd/ispindels.c

changeset 849
16079aef4c4c
parent 843
29e7223e37ae
child 850
26cc7ea0f790
equal deleted inserted replaced
848:542bdc7f6522 849:16079aef4c4c
36 extern int debug; 36 extern int debug;
37 extern sys_config Config; 37 extern sys_config Config;
38 extern MYSQL *con; 38 extern MYSQL *con;
39 extern MYSQL_RES *res_set; 39 extern MYSQL_RES *res_set;
40 extern MYSQL_ROW row; 40 extern MYSQL_ROW row;
41
42
43
44 double plato_to_sg(double plato)
45 {
46 return 1.00001 + (0.0038661 * plato) + (1.3488e-5 * pow(plato, 2)) + (4.3074e-8 * pow(plato, 3));
47 }
48
41 49
42 50
43 void ispindel_ws_send(sys_ispindel_list *ispindel) 51 void ispindel_ws_send(sys_ispindel_list *ispindel)
44 { 52 {
45 char *msg = NULL, buf[65]; 53 char *msg = NULL, buf[65];
223 char *datetime, buf[65], *line, *logfile, *query = malloc(512); 231 char *datetime, buf[65], *line, *logfile, *query = malloc(512);
224 struct tm *mytime; 232 struct tm *mytime;
225 time_t timestamp; 233 time_t timestamp;
226 FILE *fp; 234 FILE *fp;
227 235
228 // syslog(LOG_NOTICE, "ispindel_set: %s %s", node, payload); 236 syslog(LOG_NOTICE, "ispindel_set: %s %s", node, payload);
229 237
230 /* 238 /*
231 * Search ispindel record in the memory array and use it if found. 239 * Search ispindel record in the memory array and use it if found.
232 */ 240 */
233 if (ispindels) { 241 if (ispindels) {
263 * Update only the fields that are found in the payload. 271 * Update only the fields that are found in the payload.
264 */ 272 */
265 jobj = json_tokener_parse(payload); 273 jobj = json_tokener_parse(payload);
266 274
267 if (json_object_object_get_ex(jobj, "unit", &metric)) { 275 if (json_object_object_get_ex(jobj, "unit", &metric)) {
276 ispindel->lastseen = time(NULL);
268 if (json_object_object_get_ex(metric, "uuid", &val)) { 277 if (json_object_object_get_ex(metric, "uuid", &val)) {
269 if (ispindel->uuid) 278 if (ispindel->uuid)
270 free(ispindel->uuid); 279 free(ispindel->uuid);
271 ispindel->uuid = xstrcpy((char *)json_object_get_string(val)); 280 ispindel->uuid = xstrcpy((char *)json_object_get_string(val));
272 } 281 }
283 ispindel->angle = json_object_get_double(val); 292 ispindel->angle = json_object_get_double(val);
284 if (json_object_object_get_ex(metric, "temperature", &val)) 293 if (json_object_object_get_ex(metric, "temperature", &val))
285 ispindel->temperature = json_object_get_double(val); 294 ispindel->temperature = json_object_get_double(val);
286 if (json_object_object_get_ex(metric, "battery", &val)) 295 if (json_object_object_get_ex(metric, "battery", &val))
287 ispindel->battery = json_object_get_double(val); 296 ispindel->battery = json_object_get_double(val);
288 if (json_object_object_get_ex(metric, "gravity", &val)) { 297 if (json_object_object_get_ex(metric, "gravity", &val))
289 ispindel->gravity = json_object_get_double(val); 298 ispindel->gravity = json_object_get_double(val);
290 if (ispindel->gravity > ispindel->og_gravity) 299 }
291 ispindel->og_gravity = ispindel->gravity; 300
292 } 301 //ispindel_dump(ispindel);
293 } 302
294 303 /*
295 // ispindel_dump(ispindel); 304 * If a new iSpindel, insert record.
296 305 */
297
298 if (new_ispindel) { 306 if (new_ispindel) {
299 if (ispindels == NULL) { 307 if (ispindels == NULL) {
300 ispindels = ispindel; 308 ispindels = ispindel;
301 } else { 309 } else {
302 for (tmpp = ispindels; tmpp; tmpp = tmpp->next) { 310 for (tmpp = ispindels; tmpp; tmpp = tmpp->next) {
305 break; 313 break;
306 } 314 }
307 } 315 }
308 } 316 }
309 ispindel_mysql_insert(ispindel); 317 ispindel_mysql_insert(ispindel);
310 } else { 318 }
311 ispindel_mysql_update(ispindel); 319
312 } 320 /*
321 * Calculate Plato using the calibration data.
322 * Then update again and make it available.
323 */
324 ispindel->gravity = ispindel_mysql_plato(ispindel->uuid, ispindel->angle);
325 if (ispindel->gravity > ispindel->og_gravity)
326 ispindel->og_gravity = ispindel->gravity;
327 ispindel_mysql_update(ispindel);
313 ispindel_ws_send(ispindel); 328 ispindel_ws_send(ispindel);
314 329
315 /* 330 /*
316 * The data is complete, see if we can write a log entry. 331 * The data is complete, see if we can write a log entry.
317 */ 332 */
329 line = xstrcat(line, buf); 344 line = xstrcat(line, buf);
330 line = xstrcat(line, (char *)","); 345 line = xstrcat(line, (char *)",");
331 snprintf(buf, 64, "%.5f", ispindel->gravity); 346 snprintf(buf, 64, "%.5f", ispindel->gravity);
332 line = xstrcat(line, buf); 347 line = xstrcat(line, buf);
333 line = xstrcat(line, (char *)","); 348 line = xstrcat(line, (char *)",");
334 snprintf(buf, 64, "%.5f", 1.00001 + (0.0038661 * ispindel->gravity) + (1.3488e-5 * ispindel->gravity * ispindel->gravity) + 349 snprintf(buf, 64, "%.5f", plato_to_sg(ispindel->gravity));
335 (4.3074e-8 * ispindel->gravity * ispindel->gravity * ispindel->gravity));
336 line = xstrcat(line, buf); 350 line = xstrcat(line, buf);
337 line = xstrcat(line, (char *)","); 351 line = xstrcat(line, (char *)",");
338 snprintf(buf, 64, "%.6f", ispindel->battery); 352 snprintf(buf, 64, "%.6f", ispindel->battery);
339 line = xstrcat(line, buf); 353 line = xstrcat(line, buf);
340 line = xstrcat(line, (char *)","); 354 line = xstrcat(line, (char *)",");
347 line = xstrcat(line, ispindel->uuid); 361 line = xstrcat(line, ispindel->uuid);
348 362
349 snprintf(query, 511, "INSERT IGNORE INTO log_ispindel SET code='%s', datetime='%s', " \ 363 snprintf(query, 511, "INSERT IGNORE INTO log_ispindel SET code='%s', datetime='%s', " \
350 "temperature='%.4f', plato='%.5f', sg='%.5f', battery='%.6f', " \ 364 "temperature='%.4f', plato='%.5f', sg='%.5f', battery='%.6f', " \
351 "angle='%.5f', refresh='%d', uuid='%s'", 365 "angle='%.5f', refresh='%d', uuid='%s'",
352 ispindel->beercode, datetime, ispindel->temperature, ispindel->gravity, 366 ispindel->beercode, datetime, ispindel->temperature, ispindel->gravity, plato_to_sg(ispindel->gravity),
353 1.00001 + (0.0038661 * ispindel->gravity) + (1.3488e-5 * ispindel->gravity * ispindel->gravity) +
354 (4.3074e-8 * ispindel->gravity * ispindel->gravity * ispindel->gravity),
355 ispindel->battery, ispindel->angle, ispindel->interval, ispindel->uuid); 367 ispindel->battery, ispindel->angle, ispindel->interval, ispindel->uuid);
356 //syslog(LOG_NOTICE, "%s", query); 368 //syslog(LOG_NOTICE, "%s", query);
357 bms_mysql_query(query); 369 bms_mysql_query(query);
358 370
359 /* 371 /*
414 printf("product %s / %s\n", ispindel->beercode, ispindel->beername); 426 printf("product %s / %s\n", ispindel->beercode, ispindel->beername);
415 printf("tilt %.5f\n", ispindel->angle); 427 printf("tilt %.5f\n", ispindel->angle);
416 printf("temperature %.4f\n", ispindel->temperature); 428 printf("temperature %.4f\n", ispindel->temperature);
417 printf("battery %.6f\n", ispindel->battery); 429 printf("battery %.6f\n", ispindel->battery);
418 printf("gravity %.5f\n", ispindel->gravity); 430 printf("gravity %.5f\n", ispindel->gravity);
431 printf("last seen %ld\n", ispindel->lastseen);
419 printf("interval %d\n", ispindel->interval); 432 printf("interval %d\n", ispindel->interval);
420 printf("og_gravity %.5f\n", ispindel->og_gravity); 433 printf("og_gravity %.5f\n", ispindel->og_gravity);
421 } 434 printf("yeast_lo %.1f\n", ispindel->yeast_lo);
422 } 435 printf("yeast_hi %.1f\n", ispindel->yeast_hi);
423 436 printf("calibrate %s\n", ispindel->calibrate);
424 437 }
438 }
439
440

mercurial