Rework & clean de la lib DolibarrClient
This commit is contained in:
parent
1828d7a961
commit
3d94d4d803
@ -10,11 +10,7 @@ monitor_speed = 115200
|
||||
; notworthy ones:
|
||||
; __PLATFORMIO_BUILD_DEBUG__ = debug mode
|
||||
build_flags =
|
||||
-std=c++17
|
||||
; DO NOT TOUCH --- START
|
||||
-D MONITOR_SPEED=${config.monitor_speed}
|
||||
; DO NOT TOUCH --- END
|
||||
|
||||
-D EXAMPLE_NUMBER=69
|
||||
|
||||
-D EXAMPLE_STRING=\"Pouet\"
|
||||
-D WAITING_WIFI_DELAY=1000
|
||||
|
10
config_api.ini
Normal file
10
config_api.ini
Normal file
@ -0,0 +1,10 @@
|
||||
[config_api]
|
||||
build_flags =
|
||||
-D API_LOGIN_URL=\"/users/info\"
|
||||
-D API_LIST_PRODUCT_URL=\"/products/\"
|
||||
-D API_GET_PRODUCT_URL=\"/products/{id}/\"
|
||||
-D API_GET_PRODUCT_STOCK_URL=\"/products/{id}/stock/\"
|
||||
-D API_LIST_WAREHOUSE_URL=\"/warehouses/\"
|
||||
-D API_LIST_STOCKS_MOVEMENTS_URL=\"/stockmovements/?sortfield=t.rowid\"
|
||||
-D API_CREATE_STOCKS_MOVEMENTS_URL=\"/stockmovements/\"
|
||||
-D API_MAX_JSON_SIZE=4096
|
@ -1,10 +1,9 @@
|
||||
#ifndef PROGRAM_H
|
||||
#define PROGRAM_H
|
||||
|
||||
#define DEFAULT_BAUD_RATE 115200
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "DolibarrClient.h"
|
||||
#include <M5Stack.h>
|
||||
|
||||
class Program {
|
||||
public:
|
||||
|
@ -3,18 +3,12 @@
|
||||
#include <ArduinoJson.h>
|
||||
#include <iostream>
|
||||
|
||||
#define WAITING_WIFI_DELAY 1000
|
||||
|
||||
DolibarrClient::DolibarrClient(struct WifiConfig wifi_config, struct DolibarrConfig dolibarr_config) : wifi(wifi_config), dolibarr(dolibarr_config) {
|
||||
DolibarrClient::DolibarrClient(struct DolibarrConfig dolibarr_config) : dolibarr(dolibarr_config) {
|
||||
#if defined(DEBUG)
|
||||
Serial.println(" --- Wifi configuration --- ");
|
||||
Serial.println((std::string("SSID: ") + std::string(wifi_config.ssid)).c_str());
|
||||
Serial.println((std::string("Password: ") + std::string(wifi_config.password)).c_str());
|
||||
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_wifi();
|
||||
this->initialize_http_client();
|
||||
}
|
||||
|
||||
@ -32,10 +26,10 @@ HTTPClient *DolibarrClient::build_url(const String& url) const {
|
||||
|
||||
|
||||
int DolibarrClient::login() const {
|
||||
HTTPClient *client = this->build_url(LOGIN_URL);
|
||||
HTTPClient *client = this->build_url(API_LOGIN_URL);
|
||||
int httpResponseCode = client->GET();
|
||||
if (httpResponseCode > 0) {
|
||||
StaticJsonDocument<LOGIN_JSON_SIZE> doc;
|
||||
StaticJsonDocument<API_MAX_JSON_SIZE> doc;
|
||||
DeserializationError error = deserializeJson(doc, client->getString().c_str());
|
||||
if (error) {
|
||||
delete client;
|
||||
@ -48,24 +42,24 @@ int DolibarrClient::login() const {
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::string replace_id(const char *str, const char *id) {
|
||||
std::string url(str);
|
||||
url.replace(url.find("{id}"), 4, id);
|
||||
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(LIST_PRODUCT_URL);
|
||||
if (client->GET() == OK_RESPONSE) {
|
||||
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(GET_PRODUCT_URL, id_product).c_str());
|
||||
if (client->GET() == OK_RESPONSE) {
|
||||
StaticJsonDocument<GET_PRODUCT_JSON_SIZE> doc;
|
||||
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: ");
|
||||
@ -87,9 +81,9 @@ models::Product *DolibarrClient::get_product_by_id(const char* id_product) const
|
||||
}
|
||||
|
||||
std::vector<models::Warehouse> *DolibarrClient::list_warehouse() const {
|
||||
HTTPClient *client = this->build_url(LIST_WAREHOUSE_URL);
|
||||
if (client->GET() == OK_RESPONSE) {
|
||||
StaticJsonDocument<LIST_WAREHOUSE_JSON_SIZE> doc;
|
||||
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: ");
|
||||
@ -111,34 +105,21 @@ std::vector<models::Warehouse> *DolibarrClient::list_warehouse() const {
|
||||
}
|
||||
|
||||
int DolibarrClient::create_movement(models::CreateProductStock &stock) const {
|
||||
HTTPClient *client = this->build_url(CREATE_STOCKS_MOVEMENTS_URL);
|
||||
StaticJsonDocument<CREATE_STOCKS_MOVEMENTS_JSON_SIZE> doc;
|
||||
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()) == OK_RESPONSE) {
|
||||
if (client->POST(result.c_str()) == HTTP_CODE_OK) {
|
||||
delete client;
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int DolibarrClient::initialize_wifi() const {
|
||||
WiFiClass::mode(WIFI_STA); //Optional
|
||||
WiFi.setSleep(false);
|
||||
WiFi.begin(this->wifi.ssid, this->wifi.password);
|
||||
Serial.print("Connecting ");
|
||||
while(WiFiClass::status() != WL_CONNECTED){
|
||||
delay(WAITING_WIFI_DELAY);
|
||||
Serial.print(".");
|
||||
}
|
||||
Serial.println("Connected to the WiFi network");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int DolibarrClient::initialize_http_client() {
|
||||
this->httpClient = new HTTPClient();
|
||||
if (this->login() == 0) {
|
||||
|
@ -6,25 +6,6 @@
|
||||
#include <vector>
|
||||
#include "DolibarrModels.h"
|
||||
|
||||
constexpr u_int32_t OK_RESPONSE = 200;
|
||||
constexpr u_int32_t CREATED_RESPONSE = 201;
|
||||
constexpr u_int32_t NO_CONTENT_RESPONSE = 204;
|
||||
|
||||
constexpr u_int32_t LOGIN_JSON_SIZE = 4096;
|
||||
constexpr const char* LOGIN_URL = "/users/info";
|
||||
constexpr u_int32_t LIST_PRODUCT_JSON_SIZE = 4096;
|
||||
constexpr const char* LIST_PRODUCT_URL = "/products/";
|
||||
constexpr u_int32_t GET_PRODUCT_JSON_SIZE = 4096;
|
||||
constexpr const char* GET_PRODUCT_URL = "/products/{id}/";
|
||||
constexpr u_int32_t GET_PRODUCT_STOCK_JSON_SIZE = 4096;
|
||||
constexpr const char* GET_PRODUCT_STOCK_URL = "/products/{id}/stock/";
|
||||
constexpr u_int32_t LIST_WAREHOUSE_JSON_SIZE = 4096;
|
||||
constexpr const char* LIST_WAREHOUSE_URL = "/warehouses/";
|
||||
constexpr u_int32_t LIST_STOCKS_MOVEMENTS_JSON_SIZE = 4096;
|
||||
constexpr const char* LIST_STOCKS_MOVEMENTS_URL = "/stockmovements/?sortfield=t.rowid&sortorder=ASC&limit=100";
|
||||
constexpr u_int32_t CREATE_STOCKS_MOVEMENTS_JSON_SIZE = 4096;
|
||||
constexpr const char* CREATE_STOCKS_MOVEMENTS_URL = "/stockmovements/";
|
||||
|
||||
struct WifiConfig {
|
||||
const char* ssid;
|
||||
const char* password;
|
||||
@ -37,7 +18,7 @@ struct DolibarrConfig {
|
||||
|
||||
class DolibarrClient {
|
||||
public:
|
||||
DolibarrClient(WifiConfig wifi_config, DolibarrConfig dolibarr_config);
|
||||
DolibarrClient(DolibarrConfig dolibarr_config);
|
||||
~DolibarrClient() = default;
|
||||
int login() const;
|
||||
std::vector<models::Warehouse> *list_warehouse() const;
|
||||
@ -46,9 +27,7 @@ public:
|
||||
int create_movement(models::CreateProductStock &stock) const;
|
||||
private:
|
||||
HTTPClient* httpClient{};
|
||||
struct WifiConfig wifi;
|
||||
struct DolibarrConfig dolibarr;
|
||||
int initialize_wifi() const;
|
||||
int initialize_http_client();
|
||||
HTTPClient *build_url(const String& url) const;
|
||||
};
|
||||
|
@ -1,5 +0,0 @@
|
||||
//
|
||||
// Created by nico on 15/09/23.
|
||||
//
|
||||
|
||||
#include "DolibarrModels.h"
|
@ -17,13 +17,14 @@ extra_configs =
|
||||
secrets.ini
|
||||
config.ini
|
||||
envs.ini
|
||||
config_api.ini
|
||||
|
||||
; Cache folder
|
||||
build_cache_dir = ./.pio/cache
|
||||
|
||||
[env]
|
||||
; build Envs
|
||||
build_flags = ${config.build_flags} ${secrets.build_flags}
|
||||
build_flags = ${config.build_flags} ${secrets.build_flags} ${config_api.build_flags}
|
||||
|
||||
; Add scripts for more functionnalities
|
||||
; see individual scripts for more informations
|
||||
@ -50,9 +51,9 @@ upload_speed = 921600
|
||||
|
||||
; librairies (make sure to fix versions where possible!)
|
||||
lib_deps =
|
||||
bblanchon/ArduinoJson@^6.21.3
|
||||
m5stack/M5Stack@^0.4.5
|
||||
m5stack/M5GFX@^0.1.9
|
||||
bblanchon/ArduinoJson@^6.21.3 ; JSON serializer et deserializer
|
||||
m5stack/M5Stack@^0.4.5 ; M5 Lib
|
||||
m5stack/M5GFX@^0.1.9 ; M5 Lib pour le LCD
|
||||
; example:
|
||||
; erropix/ESP32 AnalogWrite@0.2
|
||||
|
||||
|
@ -2,11 +2,28 @@
|
||||
#include "Arduino.h"
|
||||
#include "DolibarrClient.h"
|
||||
|
||||
int initialize_wifi(WifiConfig wifi) {
|
||||
WiFiClass::mode(WIFI_STA); //Optional
|
||||
WiFi.setSleep(false);
|
||||
WiFi.begin(wifi.ssid, wifi.password);
|
||||
Serial.print("Connecting ");
|
||||
while(WiFiClass::status() != WL_CONNECTED){
|
||||
delay(WAITING_WIFI_DELAY);
|
||||
Serial.print(".");
|
||||
}
|
||||
Serial.println("Connected to the WiFi network");
|
||||
return 0;
|
||||
}
|
||||
|
||||
Program::Program() {
|
||||
Serial.begin(MONITOR_SPEED);
|
||||
struct WifiConfig wifi_c = {WIFI_SSID, WIFI_PASSWORD};
|
||||
struct DolibarrConfig dolibarr = {DOLIBARR_URL, DOLIBARR_API_TOKEN};
|
||||
this->client = new DolibarrClient(wifi_c, dolibarr);
|
||||
initialize_wifi(wifi_c);
|
||||
this->client = new DolibarrClient(dolibarr);
|
||||
}
|
||||
|
||||
void Program::loop() {
|
||||
}
|
||||
|
||||
|
||||
|
11
src/main.cpp
11
src/main.cpp
@ -1,19 +1,8 @@
|
||||
#include "Program.h"
|
||||
#include "Arduino.h"
|
||||
#include "WarehouseGUI.h"
|
||||
#include <M5Stack.h>
|
||||
|
||||
Program* program;
|
||||
|
||||
void setup() {
|
||||
|
||||
Serial.begin(DEFAULT_BAUD_RATE);
|
||||
program = new Program();
|
||||
|
||||
#if defined(DEBUG)
|
||||
Serial.println("Hello World");
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user