Skip to content

Commit

Permalink
Use MAC and device name to create better uniqueid (Closes #126, ref #120
Browse files Browse the repository at this point in the history


and #127)
  • Loading branch information
pvint committed Nov 17, 2020
1 parent 6bb0c01 commit a6c0300
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
46 changes: 41 additions & 5 deletions src/fauxmoESP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,22 @@ String fauxmoESP::_deviceJson(unsigned char id) {
if (id >= _devices.size()) return "{}";

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

fauxmoesp_device_t device = _devices[id];
char buffer[strlen_P(FAUXMO_DEVICE_JSON_TEMPLATE) + 64];
snprintf_P(

// 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());
char buffer[strlen_P(FAUXMO_DEVICE_JSON_TEMPLATE) + 64];
snprintf_P(
buffer, sizeof(buffer),
FAUXMO_DEVICE_JSON_TEMPLATE,
device.name, mac.substring(6).c_str(), id,
device.name, hash.c_str(),
device.state ? "true": "false",
device.value
);
Expand All @@ -133,6 +140,35 @@ String fauxmoESP::_deviceJson(unsigned char id) {

}

String fauxmoESP::_byte2hex(uint8_t zahl)
{
String hstring = String(zahl, HEX);
if (zahl < 16)
{
hstring = "0" + hstring;
}

return hstring;
}

String fauxmoESP::_makeMD5(String text)
{
unsigned char bbuf[16];
String hash = "";
MD5Builder md5;
md5.begin();
md5.add(text);
md5.calculate();

md5.getBytes(bbuf);
for (uint8_t i = 0; i < 16; i++)
{
hash += _byte2hex(bbuf[i]);
}

return hash;
}

bool fauxmoESP::_onTCPDescription(AsyncClient *client, String url, String body) {

(void) url;
Expand Down
3 changes: 3 additions & 0 deletions src/fauxmoESP.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ THE SOFTWARE.
#include <WiFiUdp.h>
#include <functional>
#include <vector>
#include <MD5Builder.h>
#include "templates.h"

typedef std::function<void(unsigned char, const char *, bool, unsigned char)> TSetStateCallback;
Expand Down Expand Up @@ -128,4 +129,6 @@ class fauxmoESP {
bool _onTCPControl(AsyncClient *client, String url, String body);
void _sendTCPResponse(AsyncClient *client, const char * code, char * body, const char * mime);

String _byte2hex(uint8_t zahl);
String _makeMD5(String text);
};
2 changes: 1 addition & 1 deletion src/templates.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ PROGMEM const char FAUXMO_TCP_STATE_RESPONSE[] = "["
PROGMEM const char FAUXMO_DEVICE_JSON_TEMPLATE[] = "{"
"\"type\":\"Extended Color Light\","
"\"name\":\"%s\","
"\"uniqueid\":\"%s%06d\","
"\"uniqueid\":\"%s\","
"\"modelid\":\"LCT007\","
"\"state\":{"
"\"on\":%s,\"bri\":%d,\"xy\":[0,0],\"reachable\": true"
Expand Down

0 comments on commit a6c0300

Please sign in to comment.