Skip to content

Commit

Permalink
Add UserModMDNS (WIP) + UserModInstances bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
ewowi committed Oct 12, 2023
1 parent 8c1ffc2 commit f9581a8
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Sys/SysModWeb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ bool SysModWeb::addURL(const char * uri, const char * contentType, const char *
request->send(LittleFS, path, contentType);
}
else {
USER_PRINT_Async("Webserver: addUrl %s %s csdata", uri, contentType);
USER_PRINT_Async("Webserver: addUrl %s %s csdata %d-%d (%s)", uri, contentType, content, len, request->url().c_str());

// if (handleIfNoneMatchCacheHeader(request)) return;

Expand Down
6 changes: 6 additions & 0 deletions src/User/UserModExample.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ class UserModExample:public Module {
// Module::loop();
}

void onOffChanged() {
if (SysModModules::isConnected && isEnabled) {
} else {
}
}

};

static UserModExample *example;
8 changes: 5 additions & 3 deletions src/User/UserModInstances.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ class UserModInstances:public Module {
}

void updateNode( UDPWLEDMessage udpMessage, char *body = nullptr) {
USER_PRINTF("Instance: %d.%d.%d.%d %s\n", udpMessage.ip0, udpMessage.ip1, udpMessage.ip2, udpMessage.ip3, udpMessage.name );
USER_PRINTF("Instance: %d.%d.%d.%d n:%s b:%s\n", udpMessage.ip0, udpMessage.ip1, udpMessage.ip2, udpMessage.ip3, udpMessage.name, body );
if (body) {
// const char *bump = reinterpret_cast<const char*>(body);
USER_PRINTF("body %s\n", body);
Expand All @@ -239,8 +239,10 @@ class UserModInstances:public Module {
found = true;
node->timeStamp = millis(); //update timestamp
strncpy(node->details, udpMessage.name, sizeof(node->details)-1); //update name (in case changed)
strncat(node->details, " ", sizeof(node->details)-1);
strncat(node->details, body, sizeof(node->details)-1);
if (body) {
strncat(node->details, " ", sizeof(node->details)-1);
strncat(node->details, body, sizeof(node->details)-1);
}
}
}

Expand Down
63 changes: 63 additions & 0 deletions src/User/UserModMDNS.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
@title StarMod
@file UserModMDNS.h
@date 20230810
@repo https://github.com/ewowi/StarMod
@Authors https://github.com/ewowi/StarMod/commits/main
@Copyright (c) 2023 Github StarMod Commit Authors
@license GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007
*/

#include <ESPmDNS.h>

class UserModMDNS:public Module {

public:
String escapedMac;
char cmDNS[64] = "";

UserModMDNS() :Module("MDNS") {
USER_PRINT_FUNCTION("%s %s\n", __PRETTY_FUNCTION__, name);

USER_PRINT_FUNCTION("%s %s %s\n", __PRETTY_FUNCTION__, name, success?"success":"failed");
};

//setup filesystem
void setup() {
Module::setup();
USER_PRINT_FUNCTION("%s %s\n", __PRETTY_FUNCTION__, name);

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

sprintf(cmDNS, PSTR("wled-%*s"), 6, escapedMac.c_str() + 6);
// strncpy(cmDNS, "wled-98765", sizeof(cmDNS) -1);

USER_PRINT_FUNCTION("%s %s %s\n", __PRETTY_FUNCTION__, name, success?"success":"failed");
}

void loop(){
// Module::loop();
}

void onOffChanged() {
if (SysModModules::isConnected && isEnabled) {

// print->fFormat(cmDNS, sizeof(cmDNS)-1, "wled-%*s", WiFi.macAddress().c_str() + 6);

MDNS.end();
MDNS.begin(cmDNS);

USER_PRINTF("mDNS started %s -> %s -> %s\n", WiFi.macAddress().c_str(), escapedMac.c_str(), cmDNS);
MDNS.addService("http", "tcp", 80);
MDNS.addService("wled", "tcp", 80);
MDNS.addServiceTxt("wled", "tcp", "mac", escapedMac.c_str());
} else {
MDNS.end();
}
}

};

static UserModMDNS *mdns;
3 changes: 3 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "Sys/SysModNetwork.h"
#include "Sys/SysModPins.h"
#include "User/UserModInstances.h"
#include "User/UserModMDNS.h"
#ifdef APPMOD_LEDS
#include "App/AppModLeds.h"
#include "App/AppModLedFixGen.h"
Expand Down Expand Up @@ -56,6 +57,7 @@ void setup() {
sys = new SysModSystem();
pins = new SysModPins();
instances = new UserModInstances();
mdns = new UserModMDNS();
#ifdef APPMOD_LEDS
lds = new AppModLeds();
lfg = new AppModLedFixGen();
Expand Down Expand Up @@ -109,6 +111,7 @@ void setup() {
#ifdef USERMOD_WLEDAUDIO
mdls->add(wledAudioMod);
#endif
// mdls->add(mdns); //no ui

//do not add mdls itself as it does setup and loop for itself!!! (it is the orchestrator)
mdls->setup();
Expand Down

0 comments on commit f9581a8

Please sign in to comment.