1515#include " SinricProQueue.h"
1616#include " SinricProSignature.h"
1717#include " SinricProStrings.h"
18- #include " SinricProUDP.h"
1918#include " SinricProWebsocket.h"
2019#include " Timestamp.h"
2120#include " EventLimiter.h"
21+ #include " SinricProUDP.h"
22+ #include " SinricProMDNS.h"
2223namespace SINRICPRO_NAMESPACE {
2324
2425/* *
@@ -144,6 +145,8 @@ class SinricProClass : public SinricProInterface {
144145
145146 SinricProDeviceInterface* getDevice (String deviceId);
146147
148+ String joinDeviceIds (char separator);
149+
147150 template <typename DeviceType>
148151 DeviceType& getDeviceInstance (String deviceId);
149152
@@ -155,6 +158,7 @@ class SinricProClass : public SinricProInterface {
155158
156159 WebsocketListener _websocketListener;
157160 UdpListener _udpListener;
161+ SinricProMDNS _mdnsListener;
158162 SinricProQueue_t receiveQueue;
159163 SinricProQueue_t sendQueue;
160164
@@ -262,6 +266,14 @@ void SinricProClass::begin(String appKey, String appSecret, String serverURL) {
262266 this ->serverURL = serverURL;
263267 _begin = true ;
264268 _udpListener.begin (&receiveQueue);
269+ #ifndef SINRICPRO_NOMDNS
270+ {
271+ String hostName = " sinricpro-" + WiFi.macAddress ();
272+ hostName.replace (" :" , " " );
273+ hostName.toLowerCase ();
274+ _mdnsListener.begin (hostName, joinDeviceIds (' ,' )); // mDNS TXT uses ',' (CSV).
275+ }
276+ #endif
265277}
266278
267279template <typename DeviceType>
@@ -322,6 +334,9 @@ void SinricProClass::handle() {
322334 if (!isConnected ()) connect ();
323335 _websocketListener.handle ();
324336 _udpListener.handle ();
337+ #ifndef SINRICPRO_NOMDNS
338+ _mdnsListener.handle ();
339+ #endif
325340
326341 handleReceiveQueue ();
327342 handleSendQueue ();
@@ -484,7 +499,6 @@ void SinricProClass::handleInvalidSignatureRequest(JsonDocument& requestMessage,
484499}
485500
486501void SinricProClass::handleSendQueue () {
487- if (!isConnected ()) return ;
488502 if (!timestamp.getTimestamp ()) return ;
489503 while (sendQueue.size () > 0 ) {
490504 DEBUG_SINRIC (" [SinricPro:handleSendQueue()]: %i message(s) in sendQueue\r\n " , sendQueue.size ());
@@ -508,8 +522,12 @@ void SinricProClass::handleSendQueue() {
508522
509523 switch (rawMessage->getInterface ()) {
510524 case IF_WEBSOCKET :
511- DEBUG_SINRIC (" [SinricPro:handleSendQueue]: Sending to websocket\r\n " );
512- _websocketListener.sendMessage (messageStr);
525+ if (isConnected ()) {
526+ DEBUG_SINRIC (" [SinricPro:handleSendQueue]: Sending to websocket\r\n " );
527+ _websocketListener.sendMessage (messageStr);
528+ } else {
529+ DEBUG_SINRIC (" [SinricPro:handleSendQueue]: Dropping WS message — not connected\r\n " );
530+ }
513531 break ;
514532 case IF_UDP :
515533 DEBUG_SINRIC (" [SinricPro:handleSendQueue]: Sending to UDP\r\n " );
@@ -523,17 +541,24 @@ void SinricProClass::handleSendQueue() {
523541 }
524542}
525543
526- void SinricProClass::connect ( ) {
527- String deviceList ;
528- int i = 0 ;
544+ String SinricProClass::joinDeviceIds ( char separator ) {
545+ String out ;
546+ int i = 0 ;
529547 for (auto & device : devices) {
530- String deviceId = device->getDeviceId ();
531- if (i > 0 ) deviceList += ' ;' ;
532- deviceList += device->getDeviceId ();
533- i++;
548+ if (i++ > 0 ) out += separator;
549+ out += device->getDeviceId ();
534550 }
551+ return out;
552+ }
535553
536- _websocketListener.begin (serverURL, appKey, deviceList, &receiveQueue);
554+ void SinricProClass::connect () {
555+ _websocketListener.begin (serverURL, appKey, joinDeviceIds (' ;' ), &receiveQueue);
556+
557+ #ifndef SINRICPRO_NOMDNS
558+ // Refresh mDNS TXT record on each (re-)connect so additions between
559+ // begin() and connect() are reflected. mDNS TXT uses ',' (CSV).
560+ _mdnsListener.update (joinDeviceIds (' ,' ));
561+ #endif
537562}
538563
539564void SinricProClass::stop () {
0 commit comments