Add MDNS discovery

This commit is contained in:
fvanroie 2024-02-25 20:36:08 +01:00
parent ac6796b19d
commit e37d38e399
3 changed files with 34 additions and 12 deletions

View File

@ -1295,13 +1295,8 @@ void dispatch_queue_discovery(const char*, const char*, uint8_t source)
dispatchSecondsToNextDiscovery = seconds; dispatchSecondsToNextDiscovery = seconds;
} }
// Periodically publish a JSON string facilitating plate discovery void dispatch_get_discovery_data(JsonDocument& doc)
void dispatch_send_discovery(const char*, const char*, uint8_t source)
{ {
#if HASP_USE_MQTT > 0
StaticJsonDocument<1024> doc;
char data[1024];
char buffer[64]; char buffer[64];
doc[F("node")] = haspDevice.get_hostname(); doc[F("node")] = haspDevice.get_hostname();
@ -1326,7 +1321,17 @@ void dispatch_send_discovery(const char*, const char*, uint8_t source)
#if HASP_USE_GPIO > 0 #if HASP_USE_GPIO > 0
gpio_discovery(input, relay, led, dimmer); gpio_discovery(input, relay, led, dimmer);
#endif #endif
}
// Periodically publish a JSON string facilitating plate discovery
void dispatch_send_discovery(const char*, const char*, uint8_t source)
{
#if HASP_USE_MQTT > 0
StaticJsonDocument<1024> doc;
char data[1024];
char buffer[64];
dispatch_get_discovery_data(doc);
size_t len = serializeJson(doc, data); size_t len = serializeJson(doc, data);
switch(mqtt_send_discovery(data, len)) { switch(mqtt_send_discovery(data, len)) {

View File

@ -100,6 +100,7 @@ void dispatch_state_val(const char* topic, hasp_event_t eventid, int32_t val);
void dispatch_state_antiburn(hasp_event_t eventid); void dispatch_state_antiburn(hasp_event_t eventid);
/* ===== Getter and Setter Functions ===== */ /* ===== Getter and Setter Functions ===== */
void dispatch_get_discovery_data(JsonDocument& doc);
/* ===== Read/Write Configuration ===== */ /* ===== Read/Write Configuration ===== */

View File

@ -51,20 +51,36 @@ void mdnsStart()
};*/ };*/
if(MDNS.begin(haspDevice.get_hostname())) { if(MDNS.begin(haspDevice.get_hostname())) {
char value[32]; char value[1024]; // 32
char service[12]; char service[12];
char key[12]; char key[12];
char proto[4]; char proto[4];
sprintf_P(proto, PSTR("tcp")); sprintf_P(proto, PSTR("tcp"));
strcpy_P(service, PSTR("http")); // strcpy_P(service, PSTR("http"));
// MDNS.addService(service, proto, 80);
// strcpy_P(key, PSTR("app_version"));
// MDNS.addServiceTxt(service, proto, key, haspDevice.get_version());
// strcpy_P(key, PSTR("app_name"));
// strcpy_P(value, PSTR(D_MANUFACTURER));
// MDNS.addServiceTxt(service, proto, key, value);
strcpy_P(service, PSTR("openhasp"));
MDNS.addService(service, proto, 80); MDNS.addService(service, proto, 80);
strcpy_P(key, PSTR("app_version")); strcpy_P(key, PSTR("version"));
MDNS.addServiceTxt(service, proto, key, haspDevice.get_version()); MDNS.addServiceTxt(service, proto, key, haspDevice.get_version());
strcpy_P(key, PSTR("app_name")); // strcpy_P(key, PSTR("app_name"));
strcpy_P(value, PSTR(D_MANUFACTURER)); // strcpy_P(value, PSTR(D_MANUFACTURER));
MDNS.addServiceTxt(service, proto, key, value);
strcpy_P(key, PSTR("discovery"));
StaticJsonDocument<1024> doc;
dispatch_get_discovery_data(doc);
size_t len = serializeJson(doc, value);
MDNS.addServiceTxt(service, proto, key, value); MDNS.addServiceTxt(service, proto, key, value);
// if(debugTelnetEnabled) { // if(debugTelnetEnabled) {
@ -85,7 +101,7 @@ bool mdns_remove_service(char* service, char* proto)
#endif #endif
#if ESP8266 #if ESP8266
return MDNS.removeService(haspDevice.get_hostname(),"_arduino", "_tcp"); return MDNS.removeService(haspDevice.get_hostname(), "_arduino", "_tcp");
#endif #endif
} }