diff --git a/include/Program.h b/include/Program.h index d59e814..5209e17 100644 --- a/include/Program.h +++ b/include/Program.h @@ -2,9 +2,13 @@ #define PROGRAM_H #include +#include +#include "DiscordAPI.h" #include "Encoder.h" + + class Program { public: /** @@ -26,6 +30,8 @@ private: * Old encoder position */ long oldPosition; + + DiscordAPI* discord; }; #endif diff --git a/lib/DiscordAPI/include/DiscordAPI.h b/lib/DiscordAPI/include/DiscordAPI.h new file mode 100644 index 0000000..638e06a --- /dev/null +++ b/lib/DiscordAPI/include/DiscordAPI.h @@ -0,0 +1,55 @@ +#ifndef DISCORD_API_H +#define DISCORD_API_H + +#include +#include +#include +#include +#include + +class DiscordAPI{ +public: + /** + * @brief Construct a new Discord API object + * @param hookUrl url of the discord webhook + */ + DiscordAPI(String hookUrl); + + /** + * @brief send a message to the discord webhook + * @param trame trame a envoyer + * @return true la trame est bien envoyée + * @return false la trame n'est pas envoyée + */ + bool sendMessage(String trame); + + /** + * @brief envoie un embed discord avec les horaires d'ouverture et de fermeture du lab + * + * @param hStart heure d'ouverture + * @param hEnd heure de fermeture + * @return true la trame est bien envoyée + * @return false la trame n'est pas envoyée + */ + bool sendHeure(String hStart, String hEnd); + +private: + + /** + * @brief webhook URL + */ + String hookUrl; + + /** + * @brief http client + */ + HTTPClient* httpClient; + + /** + * @brief wifi client (for https) + */ + BearSSL::WiFiClientSecure* wifiClient; +}; + + +#endif //DISCORD_API_H diff --git a/lib/DiscordAPI/src/DiscordAPI.cpp b/lib/DiscordAPI/src/DiscordAPI.cpp new file mode 100644 index 0000000..e8dd87c --- /dev/null +++ b/lib/DiscordAPI/src/DiscordAPI.cpp @@ -0,0 +1,38 @@ +#include "../include/DiscordAPI.h" + +DiscordAPI::DiscordAPI(String hookUrl){ + this->hookUrl = hookUrl; + + this->httpClient = new HTTPClient(); + this->wifiClient = new BearSSL::WiFiClientSecure; + this->wifiClient->setInsecure(); + + randomSeed(analogRead(A0)); +} + +bool DiscordAPI::sendMessage(String trame){ + bool sortie = true; + + if(WiFi.status() != WL_CONNECTED){ + return false; + } + + this->httpClient->begin(*this->wifiClient, this->hookUrl); + this->httpClient->addHeader("Content-Type", "application/json"); + + int resp = this->httpClient->POST(trame); + if(resp != 204){ + sortie = false; + Serial.print("sending message error code : "); + Serial.println(resp); + } + this->httpClient->end(); + return sortie; +} + + +bool DiscordAPI::sendHeure(String hStart, String hEnd){ + int color = random(16777215); + String trame = "{\"embeds\": [{\"title\": \"Le Lab est ouvert !\",\"description\": \"Le Lab est ouvert de **"+ hStart +"** à **"+ hEnd +"**\",\"color\": \"" + color + "\"}]}"; + return this->sendMessage(trame); +} diff --git a/src/Program.cpp b/src/Program.cpp index 2a01622..452d653 100644 --- a/src/Program.cpp +++ b/src/Program.cpp @@ -4,6 +4,28 @@ Program::Program() { // Startup Serial.begin(MONITOR_SPEED); + WiFi.begin(WSSID, PASS); + + Serial.print("Connecting to "); + Serial.print(WSSID); Serial.println(" ..."); + + int i = 0; + while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect + delay(1000); + Serial.print(++i); Serial.print(' '); + } + + Serial.println('\n'); + Serial.println("Connection established!"); + Serial.print("IP address:\t"); + Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer + + + + this->discord = new DiscordAPI(DISCORD_HOOK); + delay(1000); + Serial.println(this->discord->sendHeure("10h", "18h")); + this->encoder = new Encoder(ENCODER_DT, ENCODER_CLK); this->oldPosition = -999;