@@ -116,24 +116,24 @@ class Espalexa {
116
116
return " " ;
117
117
}
118
118
119
- String encodeLightId (uint8_t idx)
119
+ void encodeLightId (uint8_t idx, char * out )
120
120
{
121
- String mac = WiFi.macAddress ();
122
- mac.replace (" :" ," " );
123
- mac.toLowerCase ();
124
-
125
121
// Unique id must be 12 character len
126
- // use the first part of the MAC followed by the device id in hex value
122
+ // use the last 10 characters of the MAC followed by the device id in hex value
127
123
// uniqueId: aabbccddeeii
128
- String uniqueId = mac.substring (0 , DEVICE_UNIQUE_ID_LENGTH - 2 );
129
- char bufIdx[3 ];
130
- snprintf (bufIdx, sizeof (bufIdx), " %.*x" , 2 , idx);
131
- uniqueId.concat (bufIdx, strlen (bufIdx));
132
- return uniqueId;
133
- }
134
124
135
- uint32_t decodeLightId (uint32_t id) {
136
- return id & 0xF ;
125
+ uint8_t mac[6 ];
126
+ WiFi.macAddress (mac);
127
+
128
+ // shift the mac address to the left (discard first byte)
129
+ for (uint8_t i = 0 ; i < 5 ; i++) {
130
+ mac[i] = mac[i+1 ];
131
+ }
132
+ mac[5 ] = idx;
133
+
134
+ for (uint8_t i = 0 ; i < 6 ; i++) {
135
+ sprintf (out + i*2 , " %.2x" , mac[i]);
136
+ }
137
137
}
138
138
139
139
// device JSON string: color+temperature device emulates LCT015, dimmable device LWB010, (TODO: on/off Plug 01, color temperature device LWT010, color device LST001)
@@ -143,10 +143,8 @@ class Espalexa {
143
143
if (deviceId >= currentDeviceCount) {strcpy (buf," {}" ); return ;} // error
144
144
EspalexaDevice* dev = devices[deviceId];
145
145
146
- // char buf_bri[12] = "";
147
- // brightness support, add "bri" to JSON
148
- // if (dev->getType() != EspalexaDeviceType::onoff)
149
- // sprintf(buf_bri,",\"bri\":%u", dev->getLastValue()-1);
146
+ char buf_lightid[13 ];
147
+ encodeLightId (deviceId + 1 , buf_lightid);
150
148
151
149
char buf_col[80 ] = " " ;
152
150
// color support
@@ -168,7 +166,7 @@ class Espalexa {
168
166
" \" ,\" uniqueid\" :\" %s\" ,\" swversion\" :\" espalexa-2.5.0\" }" )
169
167
170
168
, (dev->getValue ())?" true" :" false" , dev->getLastValue ()-1 , buf_col, buf_ct, buf_cm, typeString (dev->getType ()),
171
- dev->getName ().c_str (), modelidString (dev->getType ()), static_cast <uint8_t >(dev->getType ()), encodeLightId (deviceId+ 1 ). c_str () );
169
+ dev->getName ().c_str (), modelidString (dev->getType ()), static_cast <uint8_t >(dev->getType ()), buf_lightid );
172
170
}
173
171
174
172
// Espalexa status page /espalexa
@@ -226,7 +224,7 @@ class Espalexa {
226
224
" <URLBase>http://%s:80/</URLBase>"
227
225
" <device>"
228
226
" <deviceType>urn:schemas-upnp-org:device:Basic:1</deviceType>"
229
- " <friendlyName>Philips hue (%s:80)</friendlyName>"
227
+ " <friendlyName>Espalexa (%s:80)</friendlyName>"
230
228
" <manufacturer>Royal Philips Electronics</manufacturer>"
231
229
" <manufacturerURL>http://www.philips.com</manufacturerURL>"
232
230
" <modelDescription>Philips hue Personal Wireless Lighting</modelDescription>"
@@ -363,30 +361,28 @@ class Espalexa {
363
361
364
362
if (!udpConnected) return ;
365
363
int packetSize = espalexaUdp.parsePacket ();
366
- if (! packetSize) return ; // no new udp packet
364
+ if (packetSize < 1 ) return ; // no new udp packet
367
365
368
366
EA_DEBUGLN (" Got UDP!" );
369
- if (packetSize > 0 ) {
370
- unsigned char packetBuffer[packetSize+1 ]; // buffer to hold incoming udp packet
371
- int len = espalexaUdp.read (packetBuffer, packetSize+1 );
372
- packetBuffer[packetSize] = 0 ;
373
-
374
- espalexaUdp.flush ();
375
- if (!discoverable) return ; // do not reply to M-SEARCH if not discoverable
376
-
377
- String request = (const char *) packetBuffer;
378
- if (request.indexOf (" M-SEARCH" ) >= 0 ) {
379
- EA_DEBUGLN (request);
380
- if (request.indexOf (" ssdp:discover" ) > 0 &&
381
- (request.indexOf (" upnp:rootdevice" ) > 0 ||
382
- // request.indexOf("asic:1") > 0 ||
383
- request.indexOf (" ssdp:all" ) > 0 ||
384
- request.indexOf (" device:basic:1" ) > 0 ))
385
- {
386
- EA_DEBUGLN (" Responding search req..." );
387
- respondToSearch ();
388
- }
389
- }
367
+
368
+ unsigned char packetBuffer[packetSize+1 ]; // buffer to hold incoming udp packet
369
+ espalexaUdp.read (packetBuffer, packetSize);
370
+ packetBuffer[packetSize] = 0 ;
371
+
372
+ espalexaUdp.flush ();
373
+ if (!discoverable) return ; // do not reply to M-SEARCH if not discoverable
374
+
375
+ const char * request = (const char *) packetBuffer;
376
+ if (strstr (request, " M-SEARCH" ) == nullptr ) return ;
377
+
378
+ EA_DEBUGLN (request);
379
+ if (strstr (request, " ssdp:disc" ) != nullptr && // short for "ssdp:discover"
380
+ (strstr (request, " upnp:rootd" ) != nullptr || // short for "upnp:rootdevice"
381
+ strstr (request, " ssdp:all" ) != nullptr ||
382
+ strstr (request, " asic:1" ) != nullptr )) // short for "device:basic:1"
383
+ {
384
+ EA_DEBUGLN (" Responding search req..." );
385
+ respondToSearch ();
390
386
}
391
387
}
392
388
@@ -594,7 +590,7 @@ class Espalexa {
594
590
return perc / 255 ;
595
591
}
596
592
597
- ~Espalexa (){delete devices; } // note: Espalexa is NOT meant to be destructed
593
+ ~Espalexa (){} // note: Espalexa is NOT meant to be destructed
598
594
};
599
595
600
596
#endif
0 commit comments