Skip to content

Commit

Permalink
Added setDeviceUniqueId() function (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
pvint committed Nov 17, 2020
1 parent a6c0300 commit 73ff88e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
33 changes: 20 additions & 13 deletions src/fauxmoESP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,23 +115,14 @@ String fauxmoESP::_deviceJson(unsigned char id) {

if (id >= _devices.size()) return "{}";

String mac = WiFi.macAddress();
mac.replace(":", "");
mac.toLowerCase();

fauxmoesp_device_t device = _devices[id];

// To create the uniqueID, we use the device's MAC, add the device name
// then use a truncated MD5 hash to create an ID that is unique and repeatable
mac.concat(device.name);
String hash = _makeMD5(mac).substring(0,12);

DEBUG_MSG_FAUXMO("[FAUXMO] Sending device info for \"%s\", uniqueID = \"%s\"\n", device.name, hash.c_str());
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, hash.c_str(),
device.name, device.uniqueid,
device.state ? "true": "false",
device.value
);
Expand Down Expand Up @@ -434,16 +425,32 @@ fauxmoESP::~fauxmoESP() {

}

void fauxmoESP::setDeviceUniqueId(unsigned char id, const char *uniqueid)
{
strncpy(_devices[id].uniqueid, uniqueid, FAUXMO_DEVICE_UNIQUE_ID_LENGTH);
}

unsigned char fauxmoESP::addDevice(const char * device_name) {

fauxmoesp_device_t device;
unsigned int device_id = _devices.size();

// init properties
device.name = strdup(device_name);
device.state = false;
device.value = 0;
device.state = false;
device.value = 0;

// create the uniqueid
String mac = WiFi.macAddress();
mac.replace(":", "");
mac.toLowerCase();

// To create the uniqueID, we use the device's MAC, add the device name
// then use a truncated MD5 hash to create an ID that is unique and repeatable
mac.concat(device.name);
String hash = _makeMD5(mac).substring(0,FAUXMO_DEVICE_UNIQUE_ID_LENGTH);
strcpy(device.uniqueid, hash.c_str());

// Attach
_devices.push_back(device);

Expand Down
3 changes: 3 additions & 0 deletions src/fauxmoESP.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ THE SOFTWARE.
#define FAUXMO_TCP_MAX_CLIENTS 10
#define FAUXMO_TCP_PORT 1901
#define FAUXMO_RX_TIMEOUT 3
#define FAUXMO_DEVICE_UNIQUE_ID_LENGTH 12

//#define DEBUG_FAUXMO Serial
#ifdef DEBUG_FAUXMO
Expand Down Expand Up @@ -77,6 +78,7 @@ typedef struct {
char * name;
bool state;
unsigned char value;
char uniqueid[13];
} fauxmoesp_device_t;

class fauxmoESP {
Expand All @@ -92,6 +94,7 @@ class fauxmoESP {
bool removeDevice(const char * device_name);
char * getDeviceName(unsigned char id, char * buffer, size_t len);
int getDeviceId(const char * device_name);
void setDeviceUniqueId(unsigned char id, const char *uniqueid);
void onSetState(TSetStateCallback fn) { _setCallback = fn; }
bool setState(unsigned char id, bool state, unsigned char value);
bool setState(const char * device_name, bool state, unsigned char value);
Expand Down

0 comments on commit 73ff88e

Please sign in to comment.