Skip to content

Commit

Permalink
some features from burlizzi's repo
Browse files Browse the repository at this point in the history
1. onGetState
2. dimmable light
3. unique username and light id from mac address
https://github.com/burlizzi/fauxmoESP/tree/c5b24ab648b7e5372de762942f8b9a24584831bd/src
  • Loading branch information
MrGreensWorkshop committed Oct 8, 2022
1 parent 0df8089 commit 4175492
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 29 deletions.
50 changes: 29 additions & 21 deletions library/FauxmoPhilipsLight/src/fauxmoESP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ String fauxmoESP::_deviceJson(unsigned char id, bool all = true) {

if (all)
{
if (_getCallback) {
_getCallback(id, _devices[id].name, _devices[id].state, _devices[id].value);
}

snprintf_P(
buffer, sizeof(buffer),
FAUXMO_DEVICE_JSON_TEMPLATE,
Expand Down Expand Up @@ -207,7 +211,7 @@ bool fauxmoESP::_onTCPList(AsyncClient *client, String url, String body) {
if (-1 == pos) return false;

// Get the id
unsigned char id = url.substring(pos+7).toInt();
unsigned char id = url.substring(pos+24).toInt();

// This will hold the response string
String response;
Expand All @@ -218,7 +222,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, false); // send short template
response += "\"" + String(WiFi.macAddress()) + String(i+1) + "\":" + _deviceJson(i, false); // send short template
}
response += "}";

Expand All @@ -238,49 +242,53 @@ bool fauxmoESP::_onTCPControl(AsyncClient *client, String url, String body) {
// "devicetype" request
if (body.indexOf("devicetype") > 0) {
DEBUG_MSG_FAUXMO("[FAUXMO] Handling devicetype request\n");
_sendTCPResponse(client, "200 OK", (char *) "[{\"success\":{\"username\": \"2WLEDHardQrI3WHYTHoMcXHgEspsM8ZZRpSKtBQr\"}}]", "application/json");
String username = String("[{\"success\":{\"username\": \"")+WiFi.macAddress()+"\"}}]";
_sendTCPResponse(client, "200 OK", (char *) username.c_str(), "application/json");
return true;
}

// "state" request
if ((url.indexOf("state") > 0) && (body.length() > 0)) {

int state_pos=url.indexOf("state");
if ( state_pos> 0) {
// Get the index
int pos = url.indexOf("lights");
if (-1 == pos) return false;

DEBUG_MSG_FAUXMO("[FAUXMO] Handling state request\n");

// Get the index
unsigned char id = url.substring(pos+7).toInt();
unsigned char id = url.substring(pos+24,state_pos-1).toInt();
if (id > 0) {

--id;

// Brightness
pos = body.indexOf("bri");
if (pos > 0) {
unsigned char value = body.substring(pos+5).toInt();
_devices[id].value = value;
_devices[id].state = (value > 0);
} else if (body.indexOf("false") > 0) {
_devices[id].state = false;
} else {
_devices[id].state = true;
if (0 == _devices[id].value) _devices[id].value = 255;
if(body.length() > 0)
{
// Brightness
pos = body.indexOf("bri");
if (pos > 0) {
unsigned char value = body.substring(pos+5).toInt();
_devices[id].value = value;
_devices[id].state = (value > 0);
} else if (body.indexOf("false") > 0) {
_devices[id].state = false;
} else {
_devices[id].state = true;
if (0 == _devices[id].value) _devices[id].value = 255;
}
if (_setCallback) {
_setCallback(id, _devices[id].name, _devices[id].state, _devices[id].value);
}
}

char response[strlen_P(FAUXMO_TCP_STATE_RESPONSE)+10];

snprintf_P(
response, sizeof(response),
FAUXMO_TCP_STATE_RESPONSE,
id+1, _devices[id].state ? "true" : "false", id+1, _devices[id].value
);
_sendTCPResponse(client, "200 OK", response, "text/xml");

if (_setCallback) {
_setCallback(id, _devices[id].name, _devices[id].state, _devices[id].value);
}

return true;

Expand Down
3 changes: 3 additions & 0 deletions library/FauxmoPhilipsLight/src/fauxmoESP.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ THE SOFTWARE.
#include "templates.h"

typedef std::function<void(unsigned char, const char *, bool, unsigned char)> TSetStateCallback;
typedef std::function<void(unsigned char, const char *, bool&, unsigned char&)> TGetStateCallback;

typedef struct {
char * name;
Expand All @@ -96,6 +97,7 @@ class fauxmoESP {
int getDeviceId(const char * device_name);
void setDeviceUniqueId(unsigned char id, const char *uniqueid);
void onSetState(TSetStateCallback fn) { _setCallback = fn; }
void onGetState(TGetStateCallback fn) { _getCallback = fn; }
bool setState(unsigned char id, bool state, unsigned char value);
bool setState(const char * device_name, bool state, unsigned char value);
bool process(AsyncClient *client, bool isGet, String url, String body);
Expand All @@ -117,6 +119,7 @@ class fauxmoESP {
WiFiUDP _udp;
AsyncClient * _tcpClients[FAUXMO_TCP_MAX_CLIENTS];
TSetStateCallback _setCallback = NULL;
TGetStateCallback _getCallback = NULL;

String _deviceJson(unsigned char id, bool all); // all = true means we are listing all devices so use full description template

Expand Down
10 changes: 2 additions & 8 deletions library/FauxmoPhilipsLight/src/templates.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ PROGMEM const char FAUXMO_TCP_STATE_RESPONSE[] = "["

// Working with gen1 and gen3, ON/OFF/%, gen3 requires TCP port 80
PROGMEM const char FAUXMO_DEVICE_JSON_TEMPLATE[] = "{"
"\"type\": \"Extended color light\","
"\"type\": \"Dimmable light\","
"\"name\": \"%s\","
"\"uniqueid\": \"%s\","
"\"modelid\": \"LCT015\","
Expand All @@ -50,12 +50,6 @@ PROGMEM const char FAUXMO_DEVICE_JSON_TEMPLATE[] = "{"
"\"state\":{"
"\"on\": %s,"
"\"bri\": %d,"
"\"xy\": [0,0],"
"\"hue\": 0,"
"\"sat\": 0,"
"\"effect\": \"none\","
"\"colormode\": \"xy\","
"\"ct\": 500,"
"\"mode\": \"homeautomation\","
"\"reachable\": true"
"},"
Expand All @@ -68,7 +62,7 @@ PROGMEM const char FAUXMO_DEVICE_JSON_TEMPLATE[] = "{"

// Use shorter description template when listing all devices
PROGMEM const char FAUXMO_DEVICE_JSON_TEMPLATE_SHORT[] = "{"
"\"type\": \"Extended color light\","
"\"type\": \"Dimmable light\","
"\"name\": \"%s\","
"\"uniqueid\": \"%s\""

Expand Down

0 comments on commit 4175492

Please sign in to comment.