mirror of
https://github.com/Lemonochrme/clover.git
synced 2025-06-08 08:40:50 +02:00
DataHandler and ServerHandler are now single instances following the Singleton pattern.
This commit is contained in:
parent
0b63d99510
commit
c49073ece1
5 changed files with 37 additions and 11 deletions
|
@ -2,6 +2,8 @@
|
||||||
|
|
||||||
DataHandler::DataHandler() {}
|
DataHandler::DataHandler() {}
|
||||||
|
|
||||||
|
DataHandler::~DataHandler() {}
|
||||||
|
|
||||||
void DataHandler::updateTemperatureData(float temp) {
|
void DataHandler::updateTemperatureData(float temp) {
|
||||||
temperature = temp;
|
temperature = temp;
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,16 +5,25 @@
|
||||||
|
|
||||||
class DataHandler {
|
class DataHandler {
|
||||||
public:
|
public:
|
||||||
DataHandler();
|
// Singleton
|
||||||
|
static DataHandler& GetInstance()
|
||||||
|
{
|
||||||
|
static DataHandler instance;
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
// Public functions
|
||||||
String getJsonData();
|
String getJsonData();
|
||||||
|
|
||||||
void updateTemperatureData(float temperature);
|
void updateTemperatureData(float temperature);
|
||||||
void updateHumidityData(float humidity);
|
void updateHumidityData(float humidity);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
// Singleton
|
||||||
|
DataHandler();
|
||||||
|
~DataHandler();
|
||||||
|
DataHandler(const DataHandler&) = delete;
|
||||||
|
DataHandler& operator=(const DataHandler&) = delete;
|
||||||
float temperature;
|
float temperature;
|
||||||
float humidity;
|
float humidity;
|
||||||
|
|
||||||
String buildJson();
|
String buildJson();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
#include "ServerHandler.hpp"
|
#include "ServerHandler.hpp"
|
||||||
|
|
||||||
ServerHandler::ServerHandler(DataHandler * dataHandler) : server(80), dataHandler(dataHandler) {
|
ServerHandler::ServerHandler() : server(80) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ServerHandler::~ServerHandler() {}
|
||||||
|
|
||||||
void ServerHandler::setup(const char* ssid, const char* password) { // On utilise les scope resolution operator pour définir les méthodes la classe ServerHandle qui elle est dans hpp
|
void ServerHandler::setup(const char* ssid, const char* password) { // On utilise les scope resolution operator pour définir les méthodes la classe ServerHandle qui elle est dans hpp
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
WiFi.begin(ssid, password);
|
WiFi.begin(ssid, password);
|
||||||
|
@ -25,6 +27,7 @@ void ServerHandler::loop() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void ServerHandler::handleRoot() {
|
void ServerHandler::handleRoot() {
|
||||||
String jsonFormattedData = dataHandler->getJsonData();
|
auto& dataHandler = DataHandler::GetInstance();
|
||||||
|
String jsonFormattedData = dataHandler.getJsonData();
|
||||||
server.send(200, "application/json", jsonFormattedData);
|
server.send(200, "application/json", jsonFormattedData);
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,14 +8,25 @@
|
||||||
|
|
||||||
class ServerHandler {
|
class ServerHandler {
|
||||||
public:
|
public:
|
||||||
ServerHandler(DataHandler * dataHandler);
|
// Singleton
|
||||||
|
static ServerHandler& GetInstance()
|
||||||
|
{
|
||||||
|
static ServerHandler instance;
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
// Public functions
|
||||||
void setup(const char* ssid, const char* password);
|
void setup(const char* ssid, const char* password);
|
||||||
void loop();
|
void loop();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ESP8266WebServer server;
|
// Singleton
|
||||||
|
ServerHandler();
|
||||||
|
~ServerHandler();
|
||||||
|
ServerHandler(const ServerHandler&) = delete;
|
||||||
|
ServerHandler& operator=(const ServerHandler&) = delete;
|
||||||
|
// Private variables/functions
|
||||||
void handleRoot();
|
void handleRoot();
|
||||||
DataHandler * dataHandler; // Pointeur vers dataHandler
|
ESP8266WebServer server;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -3,7 +3,6 @@
|
||||||
#include <ESP8266WebServer.h>
|
#include <ESP8266WebServer.h>
|
||||||
|
|
||||||
#include "ServerHandler.hpp"
|
#include "ServerHandler.hpp"
|
||||||
#include "DataHandler.hpp"
|
|
||||||
#include "Component.hpp"
|
#include "Component.hpp"
|
||||||
#include "Display.hpp"
|
#include "Display.hpp"
|
||||||
|
|
||||||
|
@ -14,19 +13,21 @@
|
||||||
const char* pswd = PSWD_CLOVER;
|
const char* pswd = PSWD_CLOVER;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
DataHandler dataHandler;
|
|
||||||
ServerHandler serverHandler(&dataHandler); // Référence à dataHandler
|
|
||||||
Component humidity(ComponentType::Analog, PIN_A0);
|
Component humidity(ComponentType::Analog, PIN_A0);
|
||||||
Display screen;
|
Display screen;
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
|
auto& serverHandler = ServerHandler::GetInstance();
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
serverHandler.setup(ssid, pswd);
|
serverHandler.setup(ssid, pswd);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
|
auto& serverHandler = ServerHandler::GetInstance();
|
||||||
|
auto& dataHandler = DataHandler::GetInstance();
|
||||||
|
|
||||||
screen.loop();
|
screen.loop();
|
||||||
dataHandler.updateTemperatureData(random(1800, 2200) / 100.0);
|
dataHandler.updateTemperatureData(random(1800, 2200) / 100.0);
|
||||||
// 0 -> air(0), 0-300 -> dry(20), 300-700 -> humid (580), 700-950 -> water(940)
|
// 0 -> air(0), 0-300 -> dry(20), 300-700 -> humid (580), 700-950 -> water(940)
|
||||||
|
|
Loading…
Add table
Reference in a new issue