SinricPro Library
Loading...
Searching...
No Matches
SinricProMDNS.h
1/*
2 * Copyright (c) 2019 Sinric. All rights reserved.
3 * Licensed under Creative Commons Attribution-Share Alike (CC BY-SA)
4 *
5 * This file is part of the Sinric Pro (https://github.com/sinricpro/)
6 */
7
8#pragma once
9
10#include "SinricProConfig.h"
11
12#ifdef SINRICPRO_MDNS_ENABLED
13
14#if defined(ESP8266)
15 #include <ESP8266WiFi.h>
16 #include <ESP8266mDNS.h>
17 #define SINRICPRO_MDNS_LEA
18#elif defined(ESP32)
19 #include <WiFi.h>
20 #include <ESPmDNS.h>
21 #define SINRICPRO_MDNS_ESP32
22#elif defined(ARDUINO_ARCH_RP2040) && !defined(ARDUINO_ARCH_MBED)
23 // arduino-pico ships LEAmDNS as a ESP8266mDNS replacement.
24 #include <WiFi.h>
25 #include <ESP8266mDNS.h>
26 #define SINRICPRO_MDNS_LEA
27#endif
28
29#if defined(SINRICPRO_MDNS_LEA) || defined(SINRICPRO_MDNS_ESP32)
30 #define SINRICPRO_MDNS_AVAILABLE
31#endif
32
33// Retry interval for starting the responder when it was not up yet
34#ifndef SINRICPRO_MDNS_RETRY_MS
35#define SINRICPRO_MDNS_RETRY_MS 5000
36#endif
37
38#include "SinricProVersion.h"
39#include "SinricProNamespace.h"
40#include "SinricProDebug.h"
41
42namespace SINRICPRO_NAMESPACE {
43
60 public:
61 void begin(const String& hostName, const String& deviceIds);
62 void update(const String& deviceIds);
63 void handle();
64 private:
65 bool _started = false;
66 bool _announced = false;
67 unsigned long _lastStartTry = 0;
68 String _hostName;
69 String _deviceIds;
70
71 bool start();
72 void announce();
73};
74
75bool SinricProMDNS::start() {
76#ifdef SINRICPRO_MDNS_AVAILABLE
77 _lastStartTry = millis();
78 _started = MDNS.begin(_hostName.c_str());
79 if (!_started) {
80 DEBUG_SINRIC("[SinricPro:mDNS]: responder not started yet, will retry\r\n");
81 }
82#endif
83 return _started;
84}
85
86void SinricProMDNS::announce() {
87#ifdef SINRICPRO_MDNS_AVAILABLE
88 if (!_announced) {
89 MDNS.addService("sinricpro", "udp", UDP_MULTICAST_PORT);
90 MDNS.addServiceTxt("sinricpro", "udp", "sdk", SINRICPRO_VERSION);
91 MDNS.addServiceTxt("sinricpro", "udp", "udp", "1");
92 _announced = true;
93 }
94
95 MDNS.addServiceTxt("sinricpro", "udp", "deviceIds", _deviceIds.c_str());
96 DEBUG_SINRIC("[SinricPro:mDNS]: announced _sinricpro._udp.local. port=%d deviceIds=%s\r\n",
97 UDP_MULTICAST_PORT, _deviceIds.c_str());
98#endif
99}
100
101void SinricProMDNS::begin(const String& hostName, const String& deviceIds) {
102 _hostName = hostName;
103 _deviceIds = deviceIds;
104 if (start()) announce();
105}
106
107void SinricProMDNS::update(const String& deviceIds) {
108 if (_announced && _deviceIds == deviceIds) return;
109 _deviceIds = deviceIds;
110 if (_started) announce();
111}
112
113void SinricProMDNS::handle() {
114#ifdef SINRICPRO_MDNS_AVAILABLE
115 if (!_started) {
116 if (millis() - _lastStartTry < SINRICPRO_MDNS_RETRY_MS) return;
117 if (start()) announce();
118 return;
119 }
120#ifdef SINRICPRO_MDNS_LEA
121 MDNS.update();
122#endif
123 // On ESP32 the mDNS stack runs in a FreeRTOS task; no polling needed.
124#endif
125}
126
127} // SINRICPRO_NAMESPACE
128
129#endif // SINRICPRO_MDNS_ENABLED
AirQuality.
Definition AirQualitySensor.h:19
Announces this device on the LAN via mDNS.
Definition SinricProMDNS.h:59