Major Update

- migrate toRepresentation methods out of header file
- optimize toRepresentation methods
- add helper for string formatting
- put proper include guards in helper and MDNSManager
- begin adding queryMDNS code
This commit is contained in:
ZanzyTHEbar 2022-11-01 20:24:50 +00:00
parent 88f5cf29b7
commit 746a0a9f9b
8 changed files with 284 additions and 227 deletions

View File

@ -179,7 +179,6 @@ void ProjectConfig::load()
this->config.ap_network.password = getString("apPass", "12345678").c_str();
this->config.ap_network.channel = getUInt("apChannel", 1);
/* Camera Config */
this->config.camera.vflip = getInt("vflip", 0);
this->config.camera.href = getInt("href", 0);
@ -274,3 +273,25 @@ void ProjectConfig::setAPWifiConfig(const std::string &ssid, const std::string &
if (shouldNotify)
this->notify(ObserverEvent::networksConfigUpdated);
}
std::string ProjectConfig::DeviceConfig_t::toRepresentation()
{
std::string json = Helpers::format_string(
"device_config: {\"name\": \"%s\", \"OTAPassword\": \"%s\", \"OTAPort\": %u}",
this->name.c_str(),
this->OTAPassword.c_str(),
this->OTAPort);
return json;
}
std::string ProjectConfig::CameraConfig_t::toRepresentation()
{
std::string json = Helpers::format_string("camera_config: {\"vflip\": %d,\"framesize\": %d,\"href\": %d,\"quality\": %d,\"brightness\": %d}",
this->vflip,
this->framesize,
this->href,
this->quality,
this->brightness);
return json;
}

View File

@ -28,15 +28,7 @@ public:
std::string OTAPassword;
int OTAPort;
const char* toRepresentation() {
char *p = (char*)"device_config: {";
p += sprintf(p, "\"name\":%s,", this->name);
p += sprintf(p, "\"OTAPassword\":%s,", this->OTAPassword);
p += sprintf(p, "\"OTAPort\":%u,", this->OTAPort);
p += sprintf(p, "},");
*p++ = 0;
return p;
}
std::string toRepresentation();
};
struct CameraConfig_t
@ -47,17 +39,7 @@ public:
uint8_t quality;
uint8_t brightness;
const char* toRepresentation() {
char *p = (char*)"camera_config: {";
p += sprintf(p, "\"vflip\":%u,", this->vflip);
p += sprintf(p, "\"href\":%u,", this->href);
p += sprintf(p, "\"framesize\":%d,", this->framesize);
p += sprintf(p, "\"quality\":%d,", this->quality);
p += sprintf(p, "\"brightness\":%d", this->brightness);
p += sprintf(p, "},");
*p++ = 0;
return p;
}
std::string toRepresentation();
};
struct WiFiConfig_t
@ -78,8 +60,9 @@ public:
uint8_t channel;
bool adhoc;
const char* toRepresentation() {
char *p = (char*)"wifi_entry: {";
const char *toRepresentation()
{
char *p = (char *)"wifi_entry: {";
p += sprintf(p, "\"name\":%s,", this->name);
p += sprintf(p, "\"ssid\":%s,", this->ssid);
p += sprintf(p, "\"password\":%s,", this->password);
@ -97,8 +80,9 @@ public:
std::string password;
uint8_t channel;
bool adhoc;
const char* toRepresentation() {
char *p = (char*)"adhoc_network: {";
const char *toRepresentation()
{
char *p = (char *)"adhoc_network: {";
p += sprintf(p, "\"ssid\":%s,", this->ssid);
p += sprintf(p, "\"password\":%s,", this->password);
p += sprintf(p, "\"channel\":%u,", this->channel);

View File

@ -1,7 +1,10 @@
#ifndef HELPERS_HPP
#define HELPERS_HPP
#include <string>
#include <sstream>
#include <vector>
#include <iostream>
#include <memory>
namespace Helpers
{
@ -9,4 +12,26 @@ namespace Helpers
void split(std::string str, std::string splitBy, std::vector<std::string> &tokens);
std::vector<std::string> split(const std::string &s, char delimiter);
void update_progress_bar(int progress, int total);
/// @brief
/// @tparam ...Args
/// @param format
/// @param ...args
/// @return
template <typename... Args>
std::string format_string(const std::string &format, Args... args)
{
int size_s = std::snprintf(nullptr, 0, format.c_str(), args...) + 1; // Extra space for '\0'
if (size_s <= 0)
{
std::cout << "Error during formatting.";
return "";
}
auto size = static_cast<size_t>(size_s);
std::unique_ptr<char[]> buf(new char[size]);
std::snprintf(buf.get(), size, format.c_str(), args...);
return std::string(buf.get(), buf.get() + size - 1); // We don't want the '\0' inside
}
}
#endif // HELPERS_HPP

View File

@ -1,4 +1,6 @@
#pragma once
#ifndef MDNSHANDLER_HPP
#define MDNSHANDLER_HPP
#include <ESPmDNS.h>
#include "data/StateManager/StateManager.hpp"
#include "data/utilities/Observer.hpp"
@ -17,3 +19,5 @@ public:
void startMDNS();
void update(ObserverEvent::Event event);
};
#endif // MDNSHANDLER_HPP

View File

View File

@ -0,0 +1,23 @@
#pragma once
#ifndef QUERYMDNSSERVICE_HPP
#define QUERYMDNSSERVICE_HPP
#include <ESPmDNS.h>
#include "data/StateManager/StateManager.hpp"
#include "data/utilities/Observer.hpp"
#include "data/utilities/helpers.hpp"
#include "data/config/project_config.hpp"
class QueryMDNSService : public IObserver
{
private:
StateManager<MDNSState_e> *stateManager;
ProjectConfig *configManager;
public:
QueryMDNSService(StateManager<MDNSState_e> *stateManager,
ProjectConfig *configManager);
void queryMDNS();
void update(ObserverEvent::Event event);
};
#endif // QUERYMDNSSERVICE_HPP

View File

@ -1 +1 @@
276
288