From 59728b9694227ae98caf1ff47bcfb930b7c89374 Mon Sep 17 00:00:00 2001 From: Clement Date: Mon, 3 Apr 2023 16:07:57 +0200 Subject: [PATCH] feat(iot): API Client OK --- IOT/lib/API/include/API.h | 3 +- IOT/lib/API/src/API.cpp | 75 +++++++++++++++++++++++++++++---------- 2 files changed, 58 insertions(+), 20 deletions(-) diff --git a/IOT/lib/API/include/API.h b/IOT/lib/API/include/API.h index bccd4d3..b4ff31c 100644 --- a/IOT/lib/API/include/API.h +++ b/IOT/lib/API/include/API.h @@ -13,7 +13,7 @@ public: bool wifiBegin(String wifiId, String wifiPass, Stream* espSerial); - bool sendValute(String val, String poubelleID); + bool sendValute(String val, String poubelleID, String unit, bool full); JSONVar* connect(); @@ -25,6 +25,7 @@ private: bool https; WiFiEspClient* client; + String token; diff --git a/IOT/lib/API/src/API.cpp b/IOT/lib/API/src/API.cpp index 36eda3b..f458068 100644 --- a/IOT/lib/API/src/API.cpp +++ b/IOT/lib/API/src/API.cpp @@ -1,7 +1,7 @@ #include "../include/API.h" -API::API(String user, String password, String host, bool https = true){ +API::API(String user, String password, String host, bool https){ this->user = user; this->password = password; this->serveurHost = host; @@ -16,21 +16,20 @@ bool API::wifiBegin(String wifiId, String wifiPass, Stream* espSerial){ // check for the presence of the shield if (WiFi.status() == WL_NO_SHIELD) { - Serial.println("WiFi shield not present");//FIXME: rm debug + Serial.println("WiFi shield not present"); // FIXME: rm debug return false; } // attempt to connect to WiFi network int status = WL_IDLE_STATUS; // the Wifi radio's status while (status != WL_CONNECTED) { - Serial.print("Attempting to connect to WPA SSID: ");//FIXME: rm debug - Serial.println(wifiId);//FIXME: rm debug status = WiFi.begin(wifiId.c_str(), wifiPass.c_str()); } return true; } JSONVar* API::connect(){ + this->client->stop(); JSONVar* sortie =nullptr; JSONVar data; @@ -38,29 +37,30 @@ JSONVar* API::connect(){ data["identity"] = this->user; data["password"] = this->password; - String StrData = JSONVar::stringify(data); + String strData = JSONVar::stringify(data); if(this->https){ if (!this->client->connectSSL(this->serveurHost.c_str(), 443)) { Serial.println("NOT Connected to server"); return sortie; } - this->client->println("POST /api/collections/users/auth-with-password HTTP/1.1"); - this->client->println("Host: iot.epi.cb85.software"); - this->client->println("Content-Type: application/json"); - this->client->println("Content-Length: " + String(StrData.length())); - this->client->println(); - this->client->println(StrData); - this->client->println("Connection: close"); - this->client->println(); }else{ - //TODO: implement http (no 's') request + if (!this->client->connectSSL(("http://" + this->serveurHost).c_str(), 80)) { + Serial.println("NOT Connected to server"); + return sortie; + } } + this->client->println("POST /api/collections/users/auth-with-password HTTP/1.1"); + this->client->println("Host: " + this->serveurHost); + this->client->println("Content-Type: application/json"); + this->client->println("Content-Length: " + String(strData.length())); + this->client->println(); + this->client->println(strData); + this->client->println("Connection: close"); + this->client->println(); - while (!client->available()) { - - } + while (!client->available()) {} String responce = ""; while (client->available()) { @@ -75,7 +75,44 @@ JSONVar* API::connect(){ this->token = this->token.substring(1,this->token.length()-1); - Serial.println(this->token); - + this->client->stop(); + return sortie; +} + +bool API::sendValute(String val, String poubelleID, String valUnit, bool full){ + JSONVar data; + data["value"] = val; + data["trash_id"] = poubelleID; + data["unit"] = valUnit; + data["status"] = full; + + String strData = JSONVar::stringify(data); + + if(this->token == ""){ + this->connect(); + } + if(this->https){ + if (!this->client->connectSSL(("https://" + this->serveurHost).c_str(), 443)) { + Serial.println("NOT Connected to server"); + return false; + } + }else{ + if (!this->client->connectSSL(("http://" + this->serveurHost).c_str(), 80)) { + Serial.println("NOT Connected to server"); + return false; + } + } + this->client->println("POST /api/collections/data/records HTTP/1.1"); + this->client->println("Host: " + this->serveurHost); + this->client->println("Content-Type: application/json"); + this->client->println("Content-Length: " + String(strData.length())); + this->client->println("Authorization: Bearer " + this->token); + this->client->println(); + this->client->print(strData); + this->client->println("Connection: close"); + this->client->println(); + + this->token = "";//XXX: on rm le token a chaque fois car on sais pas si la requet a bien aboutie et donc si il y est encore bon + return true; } \ No newline at end of file