Rework & clean de la lib DolibarrClient
This commit is contained in:
parent
1828d7a961
commit
3d94d4d803
@ -10,11 +10,7 @@ monitor_speed = 115200
|
|||||||
; notworthy ones:
|
; notworthy ones:
|
||||||
; __PLATFORMIO_BUILD_DEBUG__ = debug mode
|
; __PLATFORMIO_BUILD_DEBUG__ = debug mode
|
||||||
build_flags =
|
build_flags =
|
||||||
-std=c++17
|
|
||||||
; DO NOT TOUCH --- START
|
; DO NOT TOUCH --- START
|
||||||
-D MONITOR_SPEED=${config.monitor_speed}
|
-D MONITOR_SPEED=${config.monitor_speed}
|
||||||
; DO NOT TOUCH --- END
|
; DO NOT TOUCH --- END
|
||||||
|
-D WAITING_WIFI_DELAY=1000
|
||||||
-D EXAMPLE_NUMBER=69
|
|
||||||
|
|
||||||
-D EXAMPLE_STRING=\"Pouet\"
|
|
||||||
|
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
|
#ifndef PROGRAM_H
|
||||||
#define PROGRAM_H
|
#define PROGRAM_H
|
||||||
|
|
||||||
#define DEFAULT_BAUD_RATE 115200
|
|
||||||
|
|
||||||
#include "Arduino.h"
|
#include "Arduino.h"
|
||||||
#include "DolibarrClient.h"
|
#include "DolibarrClient.h"
|
||||||
|
#include <M5Stack.h>
|
||||||
|
|
||||||
class Program {
|
class Program {
|
||||||
public:
|
public:
|
||||||
|
@ -3,18 +3,12 @@
|
|||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#define WAITING_WIFI_DELAY 1000
|
DolibarrClient::DolibarrClient(struct DolibarrConfig dolibarr_config) : dolibarr(dolibarr_config) {
|
||||||
|
|
||||||
DolibarrClient::DolibarrClient(struct WifiConfig wifi_config, struct DolibarrConfig dolibarr_config) : wifi(wifi_config), dolibarr(dolibarr_config) {
|
|
||||||
#if defined(DEBUG)
|
#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(" --- Dolibarr configuration --- ");
|
||||||
Serial.println((std::string("Base URL: ") + std::string(dolibarr_config.url)).c_str());
|
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());
|
Serial.println((std::string("Token: ") + std::string(dolibarr_config.api_key)).c_str());
|
||||||
#endif
|
#endif
|
||||||
this->initialize_wifi();
|
|
||||||
this->initialize_http_client();
|
this->initialize_http_client();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -32,10 +26,10 @@ HTTPClient *DolibarrClient::build_url(const String& url) const {
|
|||||||
|
|
||||||
|
|
||||||
int DolibarrClient::login() const {
|
int DolibarrClient::login() const {
|
||||||
HTTPClient *client = this->build_url(LOGIN_URL);
|
HTTPClient *client = this->build_url(API_LOGIN_URL);
|
||||||
int httpResponseCode = client->GET();
|
int httpResponseCode = client->GET();
|
||||||
if (httpResponseCode > 0) {
|
if (httpResponseCode > 0) {
|
||||||
StaticJsonDocument<LOGIN_JSON_SIZE> doc;
|
StaticJsonDocument<API_MAX_JSON_SIZE> doc;
|
||||||
DeserializationError error = deserializeJson(doc, client->getString().c_str());
|
DeserializationError error = deserializeJson(doc, client->getString().c_str());
|
||||||
if (error) {
|
if (error) {
|
||||||
delete client;
|
delete client;
|
||||||
@ -48,24 +42,24 @@ int DolibarrClient::login() const {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string replace_id(const char *str, const char *id) {
|
String replace_id(const char *str, const char *id) {
|
||||||
std::string url(str);
|
String url(str);
|
||||||
url.replace(url.find("{id}"), 4, id);
|
url.replace("{id}", id);
|
||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<models::Product> *DolibarrClient::list_products() const {
|
std::vector<models::Product> *DolibarrClient::list_products() const {
|
||||||
HTTPClient *client = this->build_url(LIST_PRODUCT_URL);
|
HTTPClient *client = this->build_url(API_LIST_PRODUCT_URL);
|
||||||
if (client->GET() == OK_RESPONSE) {
|
if (client->GET() == HTTP_CODE_OK) {
|
||||||
|
|
||||||
}
|
}
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
models::Product *DolibarrClient::get_product_by_id(const char* id_product) const {
|
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());
|
HTTPClient *client = this->build_url(replace_id(API_GET_PRODUCT_URL, id_product).c_str());
|
||||||
if (client->GET() == OK_RESPONSE) {
|
if (client->GET() == HTTP_CODE_OK) {
|
||||||
StaticJsonDocument<GET_PRODUCT_JSON_SIZE> doc;
|
StaticJsonDocument<API_MAX_JSON_SIZE> doc;
|
||||||
DeserializationError error = deserializeJson(doc, client->getString().c_str());
|
DeserializationError error = deserializeJson(doc, client->getString().c_str());
|
||||||
if (error) {
|
if (error) {
|
||||||
Serial.println("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 {
|
std::vector<models::Warehouse> *DolibarrClient::list_warehouse() const {
|
||||||
HTTPClient *client = this->build_url(LIST_WAREHOUSE_URL);
|
HTTPClient *client = this->build_url(API_LIST_WAREHOUSE_URL);
|
||||||
if (client->GET() == OK_RESPONSE) {
|
if (client->GET() == HTTP_CODE_OK) {
|
||||||
StaticJsonDocument<LIST_WAREHOUSE_JSON_SIZE> doc;
|
StaticJsonDocument<API_MAX_JSON_SIZE> doc;
|
||||||
DeserializationError error = deserializeJson(doc, client->getString().c_str());
|
DeserializationError error = deserializeJson(doc, client->getString().c_str());
|
||||||
if (error) {
|
if (error) {
|
||||||
Serial.println("ERROR: ");
|
Serial.println("ERROR: ");
|
||||||
@ -111,34 +105,21 @@ std::vector<models::Warehouse> *DolibarrClient::list_warehouse() const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int DolibarrClient::create_movement(models::CreateProductStock &stock) const {
|
int DolibarrClient::create_movement(models::CreateProductStock &stock) const {
|
||||||
HTTPClient *client = this->build_url(CREATE_STOCKS_MOVEMENTS_URL);
|
HTTPClient *client = this->build_url("API_CREATE_STOCKS_MOVEMENTS_URL");
|
||||||
StaticJsonDocument<CREATE_STOCKS_MOVEMENTS_JSON_SIZE> doc;
|
StaticJsonDocument<API_MAX_JSON_SIZE> doc;
|
||||||
std::string result;
|
std::string result;
|
||||||
doc["product_id"] = stock.product_id;
|
doc["product_id"] = stock.product_id;
|
||||||
doc["warehouse_id"] = stock.warehouse_id;
|
doc["warehouse_id"] = stock.warehouse_id;
|
||||||
doc["qty"] = stock.qty;
|
doc["qty"] = stock.qty;
|
||||||
serializeJson(doc, result);
|
serializeJson(doc, result);
|
||||||
Serial.println(result.c_str());
|
Serial.println(result.c_str());
|
||||||
if (client->POST(result.c_str()) == OK_RESPONSE) {
|
if (client->POST(result.c_str()) == HTTP_CODE_OK) {
|
||||||
delete client;
|
delete client;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
return -1;
|
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() {
|
int DolibarrClient::initialize_http_client() {
|
||||||
this->httpClient = new HTTPClient();
|
this->httpClient = new HTTPClient();
|
||||||
if (this->login() == 0) {
|
if (this->login() == 0) {
|
||||||
|
@ -6,25 +6,6 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include "DolibarrModels.h"
|
#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 {
|
struct WifiConfig {
|
||||||
const char* ssid;
|
const char* ssid;
|
||||||
const char* password;
|
const char* password;
|
||||||
@ -37,7 +18,7 @@ struct DolibarrConfig {
|
|||||||
|
|
||||||
class DolibarrClient {
|
class DolibarrClient {
|
||||||
public:
|
public:
|
||||||
DolibarrClient(WifiConfig wifi_config, DolibarrConfig dolibarr_config);
|
DolibarrClient(DolibarrConfig dolibarr_config);
|
||||||
~DolibarrClient() = default;
|
~DolibarrClient() = default;
|
||||||
int login() const;
|
int login() const;
|
||||||
std::vector<models::Warehouse> *list_warehouse() const;
|
std::vector<models::Warehouse> *list_warehouse() const;
|
||||||
@ -46,9 +27,7 @@ public:
|
|||||||
int create_movement(models::CreateProductStock &stock) const;
|
int create_movement(models::CreateProductStock &stock) const;
|
||||||
private:
|
private:
|
||||||
HTTPClient* httpClient{};
|
HTTPClient* httpClient{};
|
||||||
struct WifiConfig wifi;
|
|
||||||
struct DolibarrConfig dolibarr;
|
struct DolibarrConfig dolibarr;
|
||||||
int initialize_wifi() const;
|
|
||||||
int initialize_http_client();
|
int initialize_http_client();
|
||||||
HTTPClient *build_url(const String& url) const;
|
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
|
secrets.ini
|
||||||
config.ini
|
config.ini
|
||||||
envs.ini
|
envs.ini
|
||||||
|
config_api.ini
|
||||||
|
|
||||||
; Cache folder
|
; Cache folder
|
||||||
build_cache_dir = ./.pio/cache
|
build_cache_dir = ./.pio/cache
|
||||||
|
|
||||||
[env]
|
[env]
|
||||||
; build Envs
|
; 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
|
; Add scripts for more functionnalities
|
||||||
; see individual scripts for more informations
|
; see individual scripts for more informations
|
||||||
@ -50,9 +51,9 @@ upload_speed = 921600
|
|||||||
|
|
||||||
; librairies (make sure to fix versions where possible!)
|
; librairies (make sure to fix versions where possible!)
|
||||||
lib_deps =
|
lib_deps =
|
||||||
bblanchon/ArduinoJson@^6.21.3
|
bblanchon/ArduinoJson@^6.21.3 ; JSON serializer et deserializer
|
||||||
m5stack/M5Stack@^0.4.5
|
m5stack/M5Stack@^0.4.5 ; M5 Lib
|
||||||
m5stack/M5GFX@^0.1.9
|
m5stack/M5GFX@^0.1.9 ; M5 Lib pour le LCD
|
||||||
; example:
|
; example:
|
||||||
; erropix/ESP32 AnalogWrite@0.2
|
; erropix/ESP32 AnalogWrite@0.2
|
||||||
|
|
||||||
|
@ -2,11 +2,28 @@
|
|||||||
#include "Arduino.h"
|
#include "Arduino.h"
|
||||||
#include "DolibarrClient.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() {
|
Program::Program() {
|
||||||
|
Serial.begin(MONITOR_SPEED);
|
||||||
struct WifiConfig wifi_c = {WIFI_SSID, WIFI_PASSWORD};
|
struct WifiConfig wifi_c = {WIFI_SSID, WIFI_PASSWORD};
|
||||||
struct DolibarrConfig dolibarr = {DOLIBARR_URL, DOLIBARR_API_TOKEN};
|
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() {
|
void Program::loop() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
11
src/main.cpp
11
src/main.cpp
@ -1,19 +1,8 @@
|
|||||||
#include "Program.h"
|
#include "Program.h"
|
||||||
#include "Arduino.h"
|
|
||||||
#include "WarehouseGUI.h"
|
|
||||||
#include <M5Stack.h>
|
|
||||||
|
|
||||||
Program* program;
|
Program* program;
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
|
|
||||||
Serial.begin(DEFAULT_BAUD_RATE);
|
|
||||||
program = new Program();
|
program = new Program();
|
||||||
|
|
||||||
#if defined(DEBUG)
|
|
||||||
Serial.println("Hello World");
|
|
||||||
#endif
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user