Skip to content

Commit

Permalink
Merge pull request #176 from vintlabs/jsonInfo
Browse files Browse the repository at this point in the history
Send abbreviated description when listing all devices (see #166)
  • Loading branch information
pvint authored Apr 3, 2021
2 parents 200e4be + 76f7c78 commit 27c5531
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
35 changes: 23 additions & 12 deletions src/fauxmoESP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 += "}";

Expand Down
2 changes: 1 addition & 1 deletion src/fauxmoESP.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 9 additions & 0 deletions src/templates.h
Original file line number Diff line number Diff line change
Expand Up @@ -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[] =
"<?xml version=\"1.0\" ?>"
"<root xmlns=\"urn:schemas-upnp-org:device-1-0\">"
Expand Down

0 comments on commit 27c5531

Please sign in to comment.