From 76f7c78c620490ee5e4313060f41e03698267107 Mon Sep 17 00:00:00 2001 From: Paul Vint Date: Sat, 3 Apr 2021 09:36:11 -0400 Subject: [PATCH] Send abbreviated description when listing all devices (see #166 - thanks to @b1gmans) --- src/fauxmoESP.cpp | 35 +++++++++++++++++++++++------------ src/fauxmoESP.h | 2 +- src/templates.h | 9 +++++++++ 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/src/fauxmoESP.cpp b/src/fauxmoESP.cpp index 4ace7de..39186dc 100644 --- a/src/fauxmoESP.cpp +++ b/src/fauxmoESP.cpp @@ -111,24 +111,35 @@ void fauxmoESP::_sendTCPResponse(AsyncClient *client, const char * code, char * } -String fauxmoESP::_deviceJson(unsigned char id) { +String fauxmoESP::_deviceJson(unsigned char id, bool all = true) { if (id >= _devices.size()) return "{}"; fauxmoesp_device_t device = _devices[id]; - DEBUG_MSG_FAUXMO("[FAUXMO] Sending device info for \"%s\", uniqueID = \"%s\"\n", device.name, device.uniqueid); - char buffer[strlen_P(FAUXMO_DEVICE_JSON_TEMPLATE) + 64]; - snprintf_P( - buffer, sizeof(buffer), - FAUXMO_DEVICE_JSON_TEMPLATE, - device.name, device.uniqueid, - device.state ? "true": "false", - device.value - ); + DEBUG_MSG_FAUXMO("[FAUXMO] Sending device info for \"%s\", uniqueID = \"%s\"\n", device.name, device.uniqueid); + char buffer[strlen_P(FAUXMO_DEVICE_JSON_TEMPLATE) + 64]; + + if (all) + { + snprintf_P( + buffer, sizeof(buffer), + FAUXMO_DEVICE_JSON_TEMPLATE, + device.name, device.uniqueid, + device.state ? "true": "false", + device.value + ); + } + else + { + snprintf_P( + buffer, sizeof(buffer), + FAUXMO_DEVICE_JSON_TEMPLATE_SHORT, + device.name, device.uniqueid + ); + } return String(buffer); - } String fauxmoESP::_byte2hex(uint8_t zahl) @@ -207,7 +218,7 @@ bool fauxmoESP::_onTCPList(AsyncClient *client, String url, String body) { response += "{"; for (unsigned char i=0; i< _devices.size(); i++) { if (i>0) response += ","; - response += "\"" + String(i+1) + "\":" + _deviceJson(i); + response += "\"" + String(i+1) + "\":" + _deviceJson(i, false); // send short template } response += "}"; diff --git a/src/fauxmoESP.h b/src/fauxmoESP.h index d98d199..ab1b97b 100644 --- a/src/fauxmoESP.h +++ b/src/fauxmoESP.h @@ -118,7 +118,7 @@ class fauxmoESP { AsyncClient * _tcpClients[FAUXMO_TCP_MAX_CLIENTS]; TSetStateCallback _setCallback = NULL; - String _deviceJson(unsigned char id); + String _deviceJson(unsigned char id, bool all); // all = true means we are listing all devices so use full description template void _handleUDP(); void _onUDPData(const IPAddress remoteIP, unsigned int remotePort, void *data, size_t len); diff --git a/src/templates.h b/src/templates.h index ce5fe5c..d16c3b0 100644 --- a/src/templates.h +++ b/src/templates.h @@ -66,6 +66,15 @@ PROGMEM const char FAUXMO_DEVICE_JSON_TEMPLATE[] = "{" "\"swversion\": \"5.105.0.21169\"" "}"; +// Use shorter description template when listing all devices +PROGMEM const char FAUXMO_DEVICE_JSON_TEMPLATE_SHORT[] = "{" + "\"type\": \"Extended color light\"," + "\"name\": \"%s\"," + "\"uniqueid\": \"%s\"" + +"}"; + + PROGMEM const char FAUXMO_DESCRIPTION_TEMPLATE[] = "" ""