T-DEV-811/IOT/lib/API/src/API.cpp

120 lines
3.6 KiB
C++
Raw Normal View History

2023-03-27 17:42:17 +02:00
#include "../include/API.h"
2023-04-03 16:07:57 +02:00
API::API(String user, String password, String host, bool https){
2023-03-28 16:05:36 +02:00
this->user = user;
this->password = password;
this->serveurHost = host;
this->https = https;
this->token = "";
this->client = new WiFiEspClient();
}
bool API::wifiBegin(String wifiId, String wifiPass, Stream* espSerial){
WiFi.init(espSerial);
// check for the presence of the shield
if (WiFi.status() == WL_NO_SHIELD) {
2023-04-03 16:07:57 +02:00
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) {
status = WiFi.begin(wifiId.c_str(), wifiPass.c_str());
}
return true;
2023-03-27 17:42:17 +02:00
}
bool API::connect(){
2023-04-03 16:07:57 +02:00
this->client->stop();
bool sortie = false;
JSONVar data;
data["identity"] = this->user;
data["password"] = this->password;
2023-04-03 16:07:57 +02:00
String strData = JSONVar::stringify(data);
if(this->https){
if (!this->client->connectSSL(this->serveurHost.c_str(), 443)) {
Serial.println("NOT Connected to server");
2023-04-03 12:06:39 +02:00
return sortie;
}
2023-04-03 12:06:39 +02:00
}else{
2023-04-03 16:07:57 +02:00
if (!this->client->connectSSL(("http://" + this->serveurHost).c_str(), 80)) {
Serial.println("NOT Connected to server");
return sortie;
}
}
2023-04-03 16:07:57 +02:00
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();
2023-04-03 12:06:39 +02:00
2023-04-03 16:07:57 +02:00
while (!client->available()) {}
2023-04-03 12:06:39 +02:00
String responce = "";
while (client->available()) {
responce += (char)client->read();
}
String str = responce.substring(responce.indexOf("{"),responce.lastIndexOf("}")+1);
JSONVar jsonRes = new JSONVar(JSONVar::parse(str));
2023-04-03 12:06:39 +02:00
this->token = JSONVar::stringify(jsonRes["token"]);
2023-04-03 12:06:39 +02:00
this->token = this->token.substring(1,this->token.length()-1);
2023-04-03 16:07:57 +02:00
this->client->stop();
sortie = true;
2023-03-27 17:42:17 +02:00
return sortie;
2023-04-03 16:07:57 +02:00
}
2023-04-04 10:45:44 +02:00
bool API::sendValue(String val, String poubelleID, String valUnit, bool full){
2023-04-03 16:07:57 +02:00
JSONVar data;
data["value"] = val;
data["trash_id"] = poubelleID;
data["unit"] = valUnit;
data["status"] = full;
String strData = JSONVar::stringify(data);
if(this->token == ""){
bool connected = this->connect();
if(!connected)return false;
2023-04-03 16:07:57 +02:00
}
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;
2023-03-27 17:42:17 +02:00
}