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 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();
|
JSONVar* connect();
|
||||||
|
|
||||||
@ -25,6 +25,7 @@ private:
|
|||||||
bool https;
|
bool https;
|
||||||
|
|
||||||
WiFiEspClient* client;
|
WiFiEspClient* client;
|
||||||
|
|
||||||
String token;
|
String token;
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
#include "../include/API.h"
|
#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->user = user;
|
||||||
this->password = password;
|
this->password = password;
|
||||||
this->serveurHost = host;
|
this->serveurHost = host;
|
||||||
@ -16,21 +16,20 @@ bool API::wifiBegin(String wifiId, String wifiPass, Stream* espSerial){
|
|||||||
|
|
||||||
// check for the presence of the shield
|
// check for the presence of the shield
|
||||||
if (WiFi.status() == WL_NO_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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// attempt to connect to WiFi network
|
// attempt to connect to WiFi network
|
||||||
int status = WL_IDLE_STATUS; // the Wifi radio's status
|
int status = WL_IDLE_STATUS; // the Wifi radio's status
|
||||||
while (status != WL_CONNECTED) {
|
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());
|
status = WiFi.begin(wifiId.c_str(), wifiPass.c_str());
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
JSONVar* API::connect(){
|
JSONVar* API::connect(){
|
||||||
|
this->client->stop();
|
||||||
JSONVar* sortie =nullptr;
|
JSONVar* sortie =nullptr;
|
||||||
|
|
||||||
JSONVar data;
|
JSONVar data;
|
||||||
@ -38,29 +37,30 @@ JSONVar* API::connect(){
|
|||||||
data["identity"] = this->user;
|
data["identity"] = this->user;
|
||||||
data["password"] = this->password;
|
data["password"] = this->password;
|
||||||
|
|
||||||
String StrData = JSONVar::stringify(data);
|
String strData = JSONVar::stringify(data);
|
||||||
|
|
||||||
if(this->https){
|
if(this->https){
|
||||||
if (!this->client->connectSSL(this->serveurHost.c_str(), 443)) {
|
if (!this->client->connectSSL(this->serveurHost.c_str(), 443)) {
|
||||||
Serial.println("NOT Connected to server");
|
Serial.println("NOT Connected to server");
|
||||||
return sortie;
|
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{
|
}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 = "";
|
String responce = "";
|
||||||
while (client->available()) {
|
while (client->available()) {
|
||||||
@ -75,7 +75,44 @@ JSONVar* API::connect(){
|
|||||||
|
|
||||||
this->token = this->token.substring(1,this->token.length()-1);
|
this->token = this->token.substring(1,this->token.length()-1);
|
||||||
|
|
||||||
Serial.println(this->token);
|
this->client->stop();
|
||||||
|
|
||||||
return sortie;
|
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