mirror of
https://github.com/EyeTrackVR/OpenIris.git
synced 2025-11-04 15:39:42 +08:00
update
- begin AutoDiscovery class
This commit is contained in:
parent
fc7d1e022b
commit
8597e066d4
@ -42,7 +42,7 @@ void ProjectConfig::initConfig()
|
||||
}
|
||||
this->config.mdns = {
|
||||
_mdnsName,
|
||||
"",
|
||||
"openiristracker",
|
||||
};
|
||||
|
||||
log_i("MDNS name: %s", _mdnsName.c_str());
|
||||
|
||||
87
ESP/lib/src/network/mDNS/auto/AutoDiscovery.cpp
Normal file
87
ESP/lib/src/network/mDNS/auto/AutoDiscovery.cpp
Normal file
@ -0,0 +1,87 @@
|
||||
#include "AutoDiscovery.hpp"
|
||||
|
||||
AutoDiscovery::AutoDiscovery(StateManager<MDNSState_e> *stateManager,
|
||||
ProjectConfig *configManager) : stateManager(stateManager),
|
||||
configManager(configManager),
|
||||
mdnsConfig(NULL),
|
||||
callback(NULL)
|
||||
{
|
||||
if (mdns_init() != ESP_OK)
|
||||
{
|
||||
stateManager->setState(MDNSState_e::MDNSState_Error);
|
||||
return;
|
||||
}
|
||||
mdnsConfig = configManager->getMDNSConfig();
|
||||
}
|
||||
|
||||
AutoDiscovery::~AutoDiscovery()
|
||||
{
|
||||
stop();
|
||||
}
|
||||
|
||||
void AutoDiscovery::start()
|
||||
{
|
||||
stateManager->setState(MDNSState_e::MDNSState_QueryStarted);
|
||||
// run the queryService function every 5 seconds
|
||||
// this will query for the service and print the results to the serial monitor
|
||||
// the queryService function will also set the hostname for mDNS
|
||||
int services = queryService(mdnsConfig->hostname, "tcp");
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Query for the number of services on the network
|
||||
*
|
||||
* @param service string - the number of services found
|
||||
* @param protocol string - the protocol of the service
|
||||
* @return int - the number of services found
|
||||
*/
|
||||
int AutoDiscovery::queryService(const std::string &service, const std::string &protocol)
|
||||
{
|
||||
int n = MDNS.queryService(service.c_str(), protocol.c_str());
|
||||
if (n == 0)
|
||||
{
|
||||
log_w("no services found");
|
||||
// set the hostname for mDNS - this is what we will need to connect to
|
||||
if (mdnsConfig->hostname != "openiristracker")
|
||||
{
|
||||
log_w("No hostname set, using default");
|
||||
configManager->setMDNSConfig(MDNS_HOSTNAME, mdnsConfig->hostname.c_str(), true);
|
||||
return;
|
||||
}
|
||||
log_i("Setting hostname to %s", mdnsConfig->hostname.c_str());
|
||||
configManager->setMDNSConfig(mdnsConfig->hostname.c_str(), mdnsConfig->service.c_str(), true);
|
||||
return;
|
||||
}
|
||||
log_i("%d services found", n);
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
// print details for each service found
|
||||
log_i("service #%d: %s - %s:%d", i + 1, MDNS.hostname(i).c_str(), MDNS.IP(i).toString().c_str(), MDNS.port(i));
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
void AutoDiscovery::stop()
|
||||
{
|
||||
stateManager->setState(MDNSState_e::MDNSState_QueryComplete);
|
||||
}
|
||||
|
||||
void AutoDiscovery::setCallback(void (*callback)(const char *))
|
||||
{
|
||||
this->callback = callback;
|
||||
}
|
||||
|
||||
void AutoDiscovery::update(ObserverEvent::Event event)
|
||||
{
|
||||
switch (event)
|
||||
{
|
||||
case ObserverEvent::Event::mdnsConfigUpdated:
|
||||
MDNS.end();
|
||||
start();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Path: lib\src\network\mDNS\auto\AutoDiscovery.hpp
|
||||
30
ESP/lib/src/network/mDNS/auto/AutoDiscovery.hpp
Normal file
30
ESP/lib/src/network/mDNS/auto/AutoDiscovery.hpp
Normal file
@ -0,0 +1,30 @@
|
||||
#ifndef AutoDiscovery_hpp
|
||||
#define AutoDiscovery_hpp
|
||||
|
||||
#include <ESPmDNS.h>
|
||||
#include <WiFi.h>
|
||||
#include "data/StateManager/StateManager.hpp"
|
||||
#include "data/utilities/Observer.hpp"
|
||||
#include "data/utilities/helpers.hpp"
|
||||
#include "data/config/project_config.hpp"
|
||||
|
||||
class AutoDiscovery : public IObserver
|
||||
{
|
||||
private:
|
||||
StateManager<MDNSState_e> *stateManager;
|
||||
ProjectConfig *configManager;
|
||||
ProjectConfig::MDNSConfig_t *mdnsConfig;
|
||||
void (*callback)(const char *);
|
||||
|
||||
public:
|
||||
AutoDiscovery(StateManager<MDNSState_e> *stateManager,
|
||||
ProjectConfig *configManager);
|
||||
~AutoDiscovery();
|
||||
void start();
|
||||
int queryService(const std::string &service, const std::string &protocol);
|
||||
void stop();
|
||||
void setCallback(void (*callback)(const char *));
|
||||
void update(ObserverEvent::Event event);
|
||||
};
|
||||
|
||||
#endif // AutoDiscovery_hpp
|
||||
@ -1 +1 @@
|
||||
524
|
||||
526
|
||||
Loading…
Reference in New Issue
Block a user