--- a/thermferm/server.c Mon Apr 15 17:06:15 2024 +0200 +++ b/thermferm/server.c Tue Apr 16 16:03:47 2024 +0200 @@ -53,7 +53,7 @@ struct hostent *hp; -#define SS_BUFSIZE 1024 +#define SS_BUFSIZE 4096 #define SS_TIMEOUT 300 #define MAX_INTERVALS 10 @@ -298,8 +298,10 @@ * DEVICE ADD type * DEVICE DEL uuid * DEVICE LIST + * DEVICE JSON * DEVICE GET uuid * DEVICE PUT uuid + * DEVICE JSON uuid */ int cmd_device(int s, char *buf) { @@ -339,71 +341,162 @@ return 0; } + if (strcmp(opt, (char *)"JSON") == 0) { + char *payload = NULL, vbuf[64]; + bool comma = false; + + if (param == NULL) { + srv_send(s, (char *)"212 Devices json list follows:"); + payload = xstrcpy((char *)"["); + for (device = Config.devices; device; device = device->next) { + if (comma) + payload = xstrcat(payload, (char *)","); + payload = xstrcat(payload, (char *)"{\"uuid\":\""); + payload = xstrcat(payload, device->uuid); + payload = xstrcat(payload, (char *)"\",\"address\":\""); + payload = xstrcat(payload, device->address); + payload = xstrcat(payload, (char *)"\",\"subdevice\":"); + snprintf(vbuf, 63, "%d", device->subdevice); + payload = xstrcat(payload, vbuf); + payload = xstrcat(payload, (char *)",\"inuse\":"); + snprintf(vbuf, 63, "%d", device->inuse); + payload = xstrcat(payload, vbuf); + payload = xstrcat(payload, (char *)",\"comment\":\""); + payload = xstrcat(payload, device->comment); + payload = xstrcat(payload, (char *)"\",\"direction\":\""); + payload = xstrcat(payload, (char *)DEVDIR[device->direction]); + payload = xstrcat(payload, (char *)"\",\"value\":"); + snprintf(vbuf, 63, "%d", device->value + device->offset); + payload = xstrcat(payload, vbuf); + payload = xstrcat(payload, (char *)"}"); + comma = true; + } + payload = xstrcat(payload, (char *)"]"); + srv_send(s, payload); + srv_send(s, (char *)"."); + free(payload); + payload = NULL; + return 0; + } else { + for (device = Config.devices; device; device = device->next) { + if (strcmp(device->uuid, param) == 0) { + payload = xstrcpy((char *)"{\"type\":\"device\",\"unit\":\""); + snprintf(vbuf, 63, "%d", device->subdevice); + payload = xstrcat(payload, device->address); + payload = xstrcat(payload, (char *)"-"); + payload = xstrcat(payload, vbuf); + payload = xstrcat(payload, (char *)"\",\"metric\":{\"uuid\":\""); + payload = xstrcat(payload, device->uuid); + payload = xstrcat(payload, (char *)"\",\"type\":\""); + payload = xstrcat(payload, (char *)DEVTYPE[device->type]); + payload = xstrcat(payload, (char *)"\",\"direction\":\""); + payload = xstrcat(payload, (char *)DEVDIR[device->direction]); + payload = xstrcat(payload, (char *)"\",\"address\":\""); + payload = xstrcat(payload, device->address); + payload = xstrcat(payload, (char *)"\",\"subdevice\":"); + snprintf(vbuf, 63, "%d", device->subdevice); + payload = xstrcat(payload, vbuf); + payload = xstrcat(payload, (char *)",\"value\":"); + snprintf(vbuf, 63, "%d", device->value); + payload = xstrcat(payload, vbuf); + payload = xstrcat(payload, (char *)",\"offset\":"); + snprintf(vbuf, 63, "%d", device->offset); + payload = xstrcat(payload, vbuf); + payload = xstrcat(payload, (char *)",\"present\":\""); + payload = xstrcat(payload, (char *)DEVPRESENT[device->present]); + payload = xstrcat(payload, (char *)"\",\"gpiopin\":"); + snprintf(vbuf, 63, "%d", device->gpiopin); + payload = xstrcat(payload, vbuf); + payload = xstrcat(payload, (char *)",\"inuse\":"); + snprintf(vbuf, 63, "%d", device->inuse); + payload = xstrcat(payload, vbuf); + payload = xstrcat(payload, (char *)",\"description\":\""); + payload = xstrcat(payload, device->description); + payload = xstrcat(payload, (char *)"\",\"comment\":\""); + payload = xstrcat(payload, device->comment); + payload = xstrcat(payload, (char *)"\",\"timestamp\":"); + snprintf(vbuf, 63, "%ld", (long)device->timestamp); + payload = xstrcat(payload, vbuf); + payload = xstrcat(payload, (char *)"}}"); + srv_send(s, (char *)"213 Device json record follows:"); + srv_send(s, payload); + free(payload); + payload = NULL; + srv_send(s, (char *)"."); + return 0; + } + } + srv_send(s, (char *)"440 No such device"); + return 0; + } + } + if (param == NULL) { - srv_send(s, (char *)"502 Parameter missing"); - return 1; + srv_send(s, (char *)"502 Parameter missing"); + return 1; } if (strcmp(opt, (char *)"ADD") == 0) { - if ((strcmp(param, (char *)"RC433") == 0) || (strcmp(param, (char *)"DHT") == 0) || - (strcmp(param, (char *)"I2C") == 0) || (strcmp(param, (char *)"SPI") == 0)) { + if ((strcmp(param, (char *)"RC433") == 0) || (strcmp(param, (char *)"DHT") == 0) || + (strcmp(param, (char *)"I2C") == 0) || (strcmp(param, (char *)"SPI") == 0)) { - device = (devices_list *)malloc(sizeof(devices_list)); - device->next = NULL; - device->uuid = malloc(37); - uuid_generate(uu); - uuid_unparse(uu, device->uuid); - for (i = 0; i < 8; i++) { - if (strcmp(param, DEVTYPE[i]) == 0) { - device->type = i; - break; - } - } - device->direction = DEVDIR_UNDEF; - device->value = device->offset = device->subdevice = device->inuse = 0; - device->present = DEVPRESENT_UNDEF; - device->address = xstrcpy((char *)"Enter address here"); - device->gpiopin = -1; - device->description = xstrcpy((char *)"Describe me here"); - device->comment = xstrcpy((char *)"Comment here"); + device = (devices_list *)malloc(sizeof(devices_list)); + device->next = NULL; + device->uuid = malloc(37); + uuid_generate(uu); + uuid_unparse(uu, device->uuid); + for (i = 0; i < 8; i++) { + if (strcmp(param, DEVTYPE[i]) == 0) { + device->type = i; + break; + } + } + device->direction = DEVDIR_UNDEF; + device->value = device->offset = device->subdevice = device->inuse = 0; + device->present = DEVPRESENT_UNDEF; + device->address = xstrcpy((char *)"Enter address here"); + device->gpiopin = -1; + device->description = xstrcpy((char *)"Describe me here"); + device->comment = xstrcpy((char *)"Comment here"); - pthread_mutex_lock(&mutexes[LOCK_DEVICES]); - if (Config.devices == NULL) { - Config.devices = device; - } else { - for (tmpd = Config.devices; tmpd; tmpd = tmpd->next) { - if (tmpd->next == NULL) { - tmpd->next = device; - break; - } - } - } - pthread_mutex_unlock(&mutexes[LOCK_DEVICES]); - syslog(LOG_NOTICE, "Device %s added", device->uuid); - srv_send(s, (char *)"211 Device %s added", device->uuid); - return 1; + pthread_mutex_lock(&mutexes[LOCK_DEVICES]); + if (Config.devices == NULL) { + Config.devices = device; + } else { + for (tmpd = Config.devices; tmpd; tmpd = tmpd->next) { + if (tmpd->next == NULL) { + tmpd->next = device; + break; + } + } + } + pthread_mutex_unlock(&mutexes[LOCK_DEVICES]); + syslog(LOG_NOTICE, "Device %s added", device->uuid); + srv_send(s, (char *)"211 Device %s added", device->uuid); + return 1; - } else { - srv_send(s, (char *)"503 Parameter error"); - return 0; - } + } else { + srv_send(s, (char *)"503 Parameter error"); + return 0; + } } if (strcmp(opt, (char *)"DEL") == 0) { - - pthread_mutex_lock(&mutexes[LOCK_DEVICES]); - rc = delete_Device(param); - pthread_mutex_unlock(&mutexes[LOCK_DEVICES]); - if (rc) { - syslog(LOG_NOTICE, "Device %s deleted", param); - srv_send(s, (char *)"211 Device %s deleted", param); - return 1; - } else { - srv_send(s, (char *)"440 No such device"); - return 0; - } + + pthread_mutex_lock(&mutexes[LOCK_DEVICES]); + rc = delete_Device(param); + pthread_mutex_unlock(&mutexes[LOCK_DEVICES]); + if (rc) { + syslog(LOG_NOTICE, "Device %s deleted", param); + srv_send(s, (char *)"211 Device %s deleted", param); + return 1; + } else { + srv_send(s, (char *)"440 No such device"); + return 0; + } } + if (strcmp(opt, (char *)"GET") == 0) { for (device = Config.devices; device; device = device->next) { if (strcmp(device->uuid, param) == 0) {