feat(iot): API Client OK
This commit is contained in:
parent
7861cfae51
commit
59728b9694
@ -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;
|
||||
|
||||
|
||||
|
@ -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;
|
||||
@ -23,14 +23,13 @@ bool API::wifiBegin(String wifiId, String wifiPass, Stream* espSerial){
|
||||
// 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;
|
||||
}
|
||||
}else{
|
||||
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: iot.epi.cb85.software");
|
||||
this->client->println("Host: " + this->serveurHost);
|
||||
this->client->println("Content-Type: application/json");
|
||||
this->client->println("Content-Length: " + String(StrData.length()));
|
||||
this->client->println("Content-Length: " + String(strData.length()));
|
||||
this->client->println();
|
||||
this->client->println(StrData);
|
||||
this->client->println(strData);
|
||||
this->client->println("Connection: close");
|
||||
this->client->println();
|
||||
}else{
|
||||
//TODO: implement http (no 's') request
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user