mirror of
https://github.com/Lemonochrme/clover.git
synced 2025-06-08 16:50: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() {}
|
||||
|
||||
void DataHandler::updateTemperatureData(float temp) {
|
||||
temperature = temp;
|
||||
}
|
||||
|
|
|
@ -5,16 +5,25 @@
|
|||
|
||||
class DataHandler {
|
||||
public:
|
||||
DataHandler();
|
||||
// Singleton
|
||||
static DataHandler& GetInstance()
|
||||
{
|
||||
static DataHandler instance;
|
||||
return instance;
|
||||
}
|
||||
// Public functions
|
||||
String getJsonData();
|
||||
|
||||
void updateTemperatureData(float temperature);
|
||||
void updateHumidityData(float humidity);
|
||||
|
||||
private:
|
||||
// Singleton
|
||||
DataHandler();
|
||||
~DataHandler();
|
||||
DataHandler(const DataHandler&) = delete;
|
||||
DataHandler& operator=(const DataHandler&) = delete;
|
||||
float temperature;
|
||||
float humidity;
|
||||
|
||||
String buildJson();
|
||||
};
|
||||
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
#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
|
||||
Serial.begin(9600);
|
||||
WiFi.begin(ssid, password);
|
||||
|
@ -25,6 +27,7 @@ void ServerHandler::loop() {
|
|||
}
|
||||
|
||||
void ServerHandler::handleRoot() {
|
||||
String jsonFormattedData = dataHandler->getJsonData();
|
||||
auto& dataHandler = DataHandler::GetInstance();
|
||||
String jsonFormattedData = dataHandler.getJsonData();
|
||||
server.send(200, "application/json", jsonFormattedData);
|
||||
}
|
||||
|
|
|
@ -8,14 +8,25 @@
|
|||
|
||||
class ServerHandler {
|
||||
public:
|
||||
ServerHandler(DataHandler * dataHandler);
|
||||
// Singleton
|
||||
static ServerHandler& GetInstance()
|
||||
{
|
||||
static ServerHandler instance;
|
||||
return instance;
|
||||
}
|
||||
// Public functions
|
||||
void setup(const char* ssid, const char* password);
|
||||
void loop();
|
||||
|
||||
private:
|
||||
ESP8266WebServer server;
|
||||
// Singleton
|
||||
ServerHandler();
|
||||
~ServerHandler();
|
||||
ServerHandler(const ServerHandler&) = delete;
|
||||
ServerHandler& operator=(const ServerHandler&) = delete;
|
||||
// Private variables/functions
|
||||
void handleRoot();
|
||||
DataHandler * dataHandler; // Pointeur vers dataHandler
|
||||
ESP8266WebServer server;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
#include <ESP8266WebServer.h>
|
||||
|
||||
#include "ServerHandler.hpp"
|
||||
#include "DataHandler.hpp"
|
||||
#include "Component.hpp"
|
||||
#include "Display.hpp"
|
||||
|
||||
|
@ -14,19 +13,21 @@
|
|||
const char* pswd = PSWD_CLOVER;
|
||||
#endif
|
||||
|
||||
DataHandler dataHandler;
|
||||
ServerHandler serverHandler(&dataHandler); // Référence à dataHandler
|
||||
Component humidity(ComponentType::Analog, PIN_A0);
|
||||
Display screen;
|
||||
|
||||
void setup()
|
||||
{
|
||||
auto& serverHandler = ServerHandler::GetInstance();
|
||||
Serial.begin(9600);
|
||||
serverHandler.setup(ssid, pswd);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
auto& serverHandler = ServerHandler::GetInstance();
|
||||
auto& dataHandler = DataHandler::GetInstance();
|
||||
|
||||
screen.loop();
|
||||
dataHandler.updateTemperatureData(random(1800, 2200) / 100.0);
|
||||
// 0 -> air(0), 0-300 -> dry(20), 300-700 -> humid (580), 700-950 -> water(940)
|
||||
|
|
Loading…
Add table
Reference in a new issue