thermferm/server.c

changeset 680
8b3c86124a08
parent 678
cc49115e769e
child 681
1f81e52c5abf
equal deleted inserted replaced
679:ecfcb1104b54 680:8b3c86124a08
51 struct sockaddr_in myaddr_in; /* for local socket address */ 51 struct sockaddr_in myaddr_in; /* for local socket address */
52 struct sockaddr_in peeraddr_in; /* for peer socket address */ 52 struct sockaddr_in peeraddr_in; /* for peer socket address */
53 53
54 struct hostent *hp; 54 struct hostent *hp;
55 55
56 #define SS_BUFSIZE 1024 56 #define SS_BUFSIZE 4096
57 #define SS_TIMEOUT 300 57 #define SS_TIMEOUT 300
58 58
59 #define MAX_INTERVALS 10 59 #define MAX_INTERVALS 10
60 const int GRAPH_INTERVAL[MAX_INTERVALS] = { 0, 1, 5, 15, 30, 60, 120, 240, 480, 960 }; 60 const int GRAPH_INTERVAL[MAX_INTERVALS] = { 0, 1, 5, 15, 30, 60, 120, 240, 480, 960 };
61 const int GRAPH_DATALINES[MAX_INTERVALS] = { 0, 800, 3200, 12000, 24000, 48000, 96000, 192000, 384000, 768000 }; 61 const int GRAPH_DATALINES[MAX_INTERVALS] = { 0, 800, 3200, 12000, 24000, 48000, 96000, 192000, 384000, 768000 };
296 296
297 /* 297 /*
298 * DEVICE ADD type 298 * DEVICE ADD type
299 * DEVICE DEL uuid 299 * DEVICE DEL uuid
300 * DEVICE LIST 300 * DEVICE LIST
301 * DEVICE JSON
301 * DEVICE GET uuid 302 * DEVICE GET uuid
302 * DEVICE PUT uuid 303 * DEVICE PUT uuid
304 * DEVICE JSON uuid
303 */ 305 */
304 int cmd_device(int s, char *buf) 306 int cmd_device(int s, char *buf)
305 { 307 {
306 char *opt, *param, *kwd, *val, ibuf[SS_BUFSIZE]; 308 char *opt, *param, *kwd, *val, ibuf[SS_BUFSIZE];
307 devices_list *device, *tmpd; 309 devices_list *device, *tmpd;
337 } 339 }
338 srv_send(s, (char *)"."); 340 srv_send(s, (char *)".");
339 return 0; 341 return 0;
340 } 342 }
341 343
342 if (param == NULL) { 344 if (strcmp(opt, (char *)"JSON") == 0) {
343 srv_send(s, (char *)"502 Parameter missing"); 345 char *payload = NULL, vbuf[64];
344 return 1; 346 bool comma = false;
345 } 347
346 348 if (param == NULL) {
347 if (strcmp(opt, (char *)"ADD") == 0) { 349 srv_send(s, (char *)"212 Devices json list follows:");
348 if ((strcmp(param, (char *)"RC433") == 0) || (strcmp(param, (char *)"DHT") == 0) || 350 payload = xstrcpy((char *)"[");
349 (strcmp(param, (char *)"I2C") == 0) || (strcmp(param, (char *)"SPI") == 0)) { 351 for (device = Config.devices; device; device = device->next) {
350 352 if (comma)
351 device = (devices_list *)malloc(sizeof(devices_list)); 353 payload = xstrcat(payload, (char *)",");
352 device->next = NULL; 354 payload = xstrcat(payload, (char *)"{\"uuid\":\"");
353 device->uuid = malloc(37); 355 payload = xstrcat(payload, device->uuid);
354 uuid_generate(uu); 356 payload = xstrcat(payload, (char *)"\",\"address\":\"");
355 uuid_unparse(uu, device->uuid); 357 payload = xstrcat(payload, device->address);
356 for (i = 0; i < 8; i++) { 358 payload = xstrcat(payload, (char *)"\",\"subdevice\":");
357 if (strcmp(param, DEVTYPE[i]) == 0) { 359 snprintf(vbuf, 63, "%d", device->subdevice);
358 device->type = i; 360 payload = xstrcat(payload, vbuf);
359 break; 361 payload = xstrcat(payload, (char *)",\"inuse\":");
362 snprintf(vbuf, 63, "%d", device->inuse);
363 payload = xstrcat(payload, vbuf);
364 payload = xstrcat(payload, (char *)",\"comment\":\"");
365 payload = xstrcat(payload, device->comment);
366 payload = xstrcat(payload, (char *)"\",\"direction\":\"");
367 payload = xstrcat(payload, (char *)DEVDIR[device->direction]);
368 payload = xstrcat(payload, (char *)"\",\"value\":");
369 snprintf(vbuf, 63, "%d", device->value + device->offset);
370 payload = xstrcat(payload, vbuf);
371 payload = xstrcat(payload, (char *)"}");
372 comma = true;
373 }
374 payload = xstrcat(payload, (char *)"]");
375 srv_send(s, payload);
376 srv_send(s, (char *)".");
377 free(payload);
378 payload = NULL;
379 return 0;
380 } else {
381 for (device = Config.devices; device; device = device->next) {
382 if (strcmp(device->uuid, param) == 0) {
383 payload = xstrcpy((char *)"{\"type\":\"device\",\"unit\":\"");
384 snprintf(vbuf, 63, "%d", device->subdevice);
385 payload = xstrcat(payload, device->address);
386 payload = xstrcat(payload, (char *)"-");
387 payload = xstrcat(payload, vbuf);
388 payload = xstrcat(payload, (char *)"\",\"metric\":{\"uuid\":\"");
389 payload = xstrcat(payload, device->uuid);
390 payload = xstrcat(payload, (char *)"\",\"type\":\"");
391 payload = xstrcat(payload, (char *)DEVTYPE[device->type]);
392 payload = xstrcat(payload, (char *)"\",\"direction\":\"");
393 payload = xstrcat(payload, (char *)DEVDIR[device->direction]);
394 payload = xstrcat(payload, (char *)"\",\"address\":\"");
395 payload = xstrcat(payload, device->address);
396 payload = xstrcat(payload, (char *)"\",\"subdevice\":");
397 snprintf(vbuf, 63, "%d", device->subdevice);
398 payload = xstrcat(payload, vbuf);
399 payload = xstrcat(payload, (char *)",\"value\":");
400 snprintf(vbuf, 63, "%d", device->value);
401 payload = xstrcat(payload, vbuf);
402 payload = xstrcat(payload, (char *)",\"offset\":");
403 snprintf(vbuf, 63, "%d", device->offset);
404 payload = xstrcat(payload, vbuf);
405 payload = xstrcat(payload, (char *)",\"present\":\"");
406 payload = xstrcat(payload, (char *)DEVPRESENT[device->present]);
407 payload = xstrcat(payload, (char *)"\",\"gpiopin\":");
408 snprintf(vbuf, 63, "%d", device->gpiopin);
409 payload = xstrcat(payload, vbuf);
410 payload = xstrcat(payload, (char *)",\"inuse\":");
411 snprintf(vbuf, 63, "%d", device->inuse);
412 payload = xstrcat(payload, vbuf);
413 payload = xstrcat(payload, (char *)",\"description\":\"");
414 payload = xstrcat(payload, device->description);
415 payload = xstrcat(payload, (char *)"\",\"comment\":\"");
416 payload = xstrcat(payload, device->comment);
417 payload = xstrcat(payload, (char *)"\",\"timestamp\":");
418 snprintf(vbuf, 63, "%ld", (long)device->timestamp);
419 payload = xstrcat(payload, vbuf);
420 payload = xstrcat(payload, (char *)"}}");
421 srv_send(s, (char *)"213 Device json record follows:");
422 srv_send(s, payload);
423 free(payload);
424 payload = NULL;
425 srv_send(s, (char *)".");
426 return 0;
360 } 427 }
361 } 428 }
362 device->direction = DEVDIR_UNDEF; 429 srv_send(s, (char *)"440 No such device");
363 device->value = device->offset = device->subdevice = device->inuse = 0; 430 return 0;
364 device->present = DEVPRESENT_UNDEF; 431 }
365 device->address = xstrcpy((char *)"Enter address here"); 432 }
366 device->gpiopin = -1; 433
367 device->description = xstrcpy((char *)"Describe me here"); 434 if (param == NULL) {
368 device->comment = xstrcpy((char *)"Comment here"); 435 srv_send(s, (char *)"502 Parameter missing");
369 436 return 1;
370 pthread_mutex_lock(&mutexes[LOCK_DEVICES]); 437 }
371 if (Config.devices == NULL) { 438
372 Config.devices = device; 439 if (strcmp(opt, (char *)"ADD") == 0) {
373 } else { 440 if ((strcmp(param, (char *)"RC433") == 0) || (strcmp(param, (char *)"DHT") == 0) ||
374 for (tmpd = Config.devices; tmpd; tmpd = tmpd->next) { 441 (strcmp(param, (char *)"I2C") == 0) || (strcmp(param, (char *)"SPI") == 0)) {
375 if (tmpd->next == NULL) { 442
376 tmpd->next = device; 443 device = (devices_list *)malloc(sizeof(devices_list));
377 break; 444 device->next = NULL;
378 } 445 device->uuid = malloc(37);
379 } 446 uuid_generate(uu);
380 } 447 uuid_unparse(uu, device->uuid);
381 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]); 448 for (i = 0; i < 8; i++) {
382 syslog(LOG_NOTICE, "Device %s added", device->uuid); 449 if (strcmp(param, DEVTYPE[i]) == 0) {
383 srv_send(s, (char *)"211 Device %s added", device->uuid); 450 device->type = i;
384 return 1; 451 break;
385 452 }
386 } else { 453 }
387 srv_send(s, (char *)"503 Parameter error"); 454 device->direction = DEVDIR_UNDEF;
388 return 0; 455 device->value = device->offset = device->subdevice = device->inuse = 0;
389 } 456 device->present = DEVPRESENT_UNDEF;
457 device->address = xstrcpy((char *)"Enter address here");
458 device->gpiopin = -1;
459 device->description = xstrcpy((char *)"Describe me here");
460 device->comment = xstrcpy((char *)"Comment here");
461
462 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
463 if (Config.devices == NULL) {
464 Config.devices = device;
465 } else {
466 for (tmpd = Config.devices; tmpd; tmpd = tmpd->next) {
467 if (tmpd->next == NULL) {
468 tmpd->next = device;
469 break;
470 }
471 }
472 }
473 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
474 syslog(LOG_NOTICE, "Device %s added", device->uuid);
475 srv_send(s, (char *)"211 Device %s added", device->uuid);
476 return 1;
477
478 } else {
479 srv_send(s, (char *)"503 Parameter error");
480 return 0;
481 }
390 } 482 }
391 483
392 if (strcmp(opt, (char *)"DEL") == 0) { 484 if (strcmp(opt, (char *)"DEL") == 0) {
393 485
394 pthread_mutex_lock(&mutexes[LOCK_DEVICES]); 486 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
395 rc = delete_Device(param); 487 rc = delete_Device(param);
396 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]); 488 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
397 if (rc) { 489 if (rc) {
398 syslog(LOG_NOTICE, "Device %s deleted", param); 490 syslog(LOG_NOTICE, "Device %s deleted", param);
399 srv_send(s, (char *)"211 Device %s deleted", param); 491 srv_send(s, (char *)"211 Device %s deleted", param);
400 return 1; 492 return 1;
401 } else { 493 } else {
402 srv_send(s, (char *)"440 No such device"); 494 srv_send(s, (char *)"440 No such device");
403 return 0; 495 return 0;
404 } 496 }
405 } 497 }
498
406 499
407 if (strcmp(opt, (char *)"GET") == 0) { 500 if (strcmp(opt, (char *)"GET") == 0) {
408 for (device = Config.devices; device; device = device->next) { 501 for (device = Config.devices; device; device = device->next) {
409 if (strcmp(device->uuid, param) == 0) { 502 if (strcmp(device->uuid, param) == 0) {
410 pthread_mutex_lock(&mutexes[LOCK_DEVICES]); 503 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);

mercurial