Création du module DolibarrClient avec ses différentes routes & création du module WarehouseGUI pour le LCD M5Stack (#7)
Co-authored-by: Nicolas SANS <nicolas.sansd@gmail.com> Co-authored-by: Clement <c.boesmier@aptatio.com> Co-authored-by: Clement <clement@jo85.com> Reviewed-on: #7 Co-authored-by: Nicolas <nicolas.sansd@gmail.com> Co-committed-by: Nicolas <nicolas.sansd@gmail.com>
This commit is contained in:
151
lib/DolibarrClient/src/DolibarrClient.cpp
Normal file
151
lib/DolibarrClient/src/DolibarrClient.cpp
Normal file
@ -0,0 +1,151 @@
|
||||
#include "DolibarrClient.h"
|
||||
#include <WiFi.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <iostream>
|
||||
|
||||
DolibarrClient::DolibarrClient(struct DolibarrConfig dolibarr_config) : dolibarr(dolibarr_config) {
|
||||
#if defined(DEBUG)
|
||||
Serial.println(" --- Dolibarr configuration --- ");
|
||||
Serial.println((std::string("Base URL: ") + std::string(dolibarr_config.url)).c_str());
|
||||
Serial.println((std::string("Token: ") + std::string(dolibarr_config.api_key)).c_str());
|
||||
#endif
|
||||
this->initialize_http_client();
|
||||
}
|
||||
|
||||
HTTPClient *DolibarrClient::build_url(const String& url) const {
|
||||
auto *client = new HTTPClient();
|
||||
String clientUrl = this->dolibarr.url + url;
|
||||
client->begin(clientUrl);
|
||||
client->addHeader("Content-Type", "application/json");
|
||||
client->addHeader("DOLAPIKEY", this->dolibarr.api_key);
|
||||
#if defined(DEBUG)
|
||||
Serial.println("URL Request: " + clientUrl);
|
||||
#endif
|
||||
return client;
|
||||
}
|
||||
|
||||
|
||||
int DolibarrClient::login() const {
|
||||
HTTPClient *client = this->build_url(API_LOGIN_URL);
|
||||
int httpResponseCode = client->GET();
|
||||
if (httpResponseCode > 0) {
|
||||
StaticJsonDocument<API_MAX_JSON_SIZE> doc;
|
||||
DeserializationError error = deserializeJson(doc, client->getString().c_str());
|
||||
if (error) {
|
||||
delete client;
|
||||
return -1;
|
||||
}
|
||||
delete client;
|
||||
return 0;
|
||||
}
|
||||
delete client;
|
||||
return -1;
|
||||
}
|
||||
|
||||
String replace_id(const char *str, const char *id) {
|
||||
String url(str);
|
||||
url.replace("{id}", id);
|
||||
return url;
|
||||
}
|
||||
|
||||
std::vector<models::Product> *DolibarrClient::list_products() const {
|
||||
HTTPClient *client = this->build_url(API_LIST_PRODUCT_URL);
|
||||
if (client->GET() == HTTP_CODE_OK) {
|
||||
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
models::Product *DolibarrClient::get_product_by_id(const char* id_product) const {
|
||||
HTTPClient *client = this->build_url(replace_id(API_GET_PRODUCT_URL, id_product).c_str());
|
||||
if (client->GET() == HTTP_CODE_OK) {
|
||||
StaticJsonDocument<API_MAX_JSON_SIZE> doc;
|
||||
DeserializationError error = deserializeJson(doc, client->getString().c_str());
|
||||
if (error) {
|
||||
Serial.println("ERROR: ");
|
||||
Serial.println(error.c_str());
|
||||
delete client;
|
||||
return nullptr;
|
||||
}
|
||||
auto *product = new models::Product();
|
||||
product->date_creation = doc["date_creation"];
|
||||
product->id = doc["id"];
|
||||
product->entity = doc["entity"];
|
||||
product->stock_reel = doc["stock_reel"];
|
||||
product->label = doc["label"];
|
||||
delete client;
|
||||
return product;
|
||||
}
|
||||
delete client;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::vector<models::Warehouse> *DolibarrClient::list_warehouse() const {
|
||||
HTTPClient *client = this->build_url(API_LIST_WAREHOUSE_URL);
|
||||
if (client->GET() == HTTP_CODE_OK) {
|
||||
StaticJsonDocument<API_MAX_JSON_SIZE> doc;
|
||||
DeserializationError error = deserializeJson(doc, client->getString().c_str());
|
||||
if (error) {
|
||||
Serial.println("ERROR: ");
|
||||
Serial.println(error.c_str());
|
||||
delete client;
|
||||
return nullptr;
|
||||
}
|
||||
auto *warehouses = new std::vector<models::Warehouse>();
|
||||
for (auto obj : doc.as<JsonArray>()) {
|
||||
models::Warehouse warehouse = {};
|
||||
warehouse.id = obj["id"];
|
||||
warehouses->push_back(warehouse);
|
||||
}
|
||||
delete client;
|
||||
return warehouses;
|
||||
}
|
||||
delete client;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
int DolibarrClient::create_movement(models::CreateProductStock &stock) const {
|
||||
HTTPClient *client = this->build_url("API_CREATE_STOCKS_MOVEMENTS_URL");
|
||||
StaticJsonDocument<API_MAX_JSON_SIZE> doc;
|
||||
std::string result;
|
||||
doc["product_id"] = stock.product_id;
|
||||
doc["warehouse_id"] = stock.warehouse_id;
|
||||
doc["qty"] = stock.qty;
|
||||
serializeJson(doc, result);
|
||||
Serial.println(result.c_str());
|
||||
if (client->POST(result.c_str()) == HTTP_CODE_OK) {
|
||||
delete client;
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int DolibarrClient::initialize_http_client() {
|
||||
this->httpClient = new HTTPClient();
|
||||
if (this->login() == 0) {
|
||||
auto* product = this->get_product_by_id("1");
|
||||
if (product == nullptr) {
|
||||
Serial.println("Product is nullptr !");
|
||||
return -1;
|
||||
}
|
||||
Serial.println("Product label: ");
|
||||
Serial.println(product->label);
|
||||
auto* warehouses = this->list_warehouse();
|
||||
if (warehouses == nullptr) {
|
||||
delete product;
|
||||
Serial.println("Warehouse is nullptr !");
|
||||
return -1;
|
||||
}
|
||||
Serial.println("Warehouses: ");
|
||||
models::CreateProductStock product_stock = {product->id, warehouses->at(0).id, "1"};
|
||||
this->create_movement(product_stock);
|
||||
for (auto warehouse : *warehouses) {
|
||||
Serial.println(warehouse.id);
|
||||
}
|
||||
delete product;
|
||||
delete warehouses;
|
||||
} else {
|
||||
Serial.println("An Error has occurred while trying to login");
|
||||
}
|
||||
return 0;
|
||||
}
|
35
lib/DolibarrClient/src/DolibarrClient.h
Normal file
35
lib/DolibarrClient/src/DolibarrClient.h
Normal file
@ -0,0 +1,35 @@
|
||||
#ifndef DOLIBARR_CLIENT_H
|
||||
#define DOLIBARR_CLIENT_H
|
||||
|
||||
#include <WiFi.h>
|
||||
#include <HTTPClient.h>
|
||||
#include <vector>
|
||||
#include "DolibarrModels.h"
|
||||
|
||||
struct WifiConfig {
|
||||
const char* ssid;
|
||||
const char* password;
|
||||
};
|
||||
|
||||
struct DolibarrConfig {
|
||||
const char* url;
|
||||
const char* api_key;
|
||||
};
|
||||
|
||||
class DolibarrClient {
|
||||
public:
|
||||
DolibarrClient(DolibarrConfig dolibarr_config);
|
||||
~DolibarrClient() = default;
|
||||
int login() const;
|
||||
std::vector<models::Warehouse> *list_warehouse() const;
|
||||
std::vector<models::Product> *list_products() const;
|
||||
models::Product *get_product_by_id(const char* id_product) const;
|
||||
int create_movement(models::CreateProductStock &stock) const;
|
||||
private:
|
||||
HTTPClient* httpClient{};
|
||||
struct DolibarrConfig dolibarr;
|
||||
int initialize_http_client();
|
||||
HTTPClient *build_url(const String& url) const;
|
||||
};
|
||||
|
||||
#endif //DOLIBARR_CLIENT_H
|
40
lib/DolibarrClient/src/DolibarrModels.h
Normal file
40
lib/DolibarrClient/src/DolibarrModels.h
Normal file
@ -0,0 +1,40 @@
|
||||
#ifndef T_IOT_901_CONVOYOR_DOLIBARRMODELS_H
|
||||
#define T_IOT_901_CONVOYOR_DOLIBARRMODELS_H
|
||||
|
||||
namespace models {
|
||||
|
||||
struct Product {
|
||||
const char* id;
|
||||
const char* entity;
|
||||
const char* ref;
|
||||
const char* status;
|
||||
const char* date_creation;
|
||||
const char* date_modification;
|
||||
const char* label;
|
||||
const char* description;
|
||||
const char* type;
|
||||
const char* price;
|
||||
const char* stock_reel;
|
||||
const char* seuil_stock_alerte;
|
||||
const char* desiredstock;
|
||||
};
|
||||
|
||||
struct ProductStock {
|
||||
const char* id;
|
||||
const char* product_id;
|
||||
const char* quantity;
|
||||
};
|
||||
|
||||
struct CreateProductStock {
|
||||
const char* product_id;
|
||||
const char* warehouse_id;
|
||||
const char* qty;
|
||||
};
|
||||
|
||||
struct Warehouse {
|
||||
const char* id;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //T_IOT_901_CONVOYOR_DOLIBARRMODELS_H
|
Reference in New Issue
Block a user