From d2dec55110737f4aeb586ad08a87529068b1ca77 Mon Sep 17 00:00:00 2001 From: Nicolas SANS Date: Fri, 9 Feb 2024 08:31:10 +0100 Subject: [PATCH] Rework de la lib dolibarr --- config.ini | 31 +++++++++-------- include/Program.h | 1 + lib/DolibarrClient/src/DolibarrClient.cpp | 42 ++--------------------- lib/DolibarrClient/src/DolibarrClient.h | 2 -- lib/GRBL/src/GRBL.cpp | 2 +- lib/M5LCD/src/M5LCD.cpp | 2 +- src/Program.cpp | 23 ++++++------- 7 files changed, 32 insertions(+), 71 deletions(-) diff --git a/config.ini b/config.ini index da29dee..09884ec 100644 --- a/config.ini +++ b/config.ini @@ -15,25 +15,26 @@ build_flags = ; DO NOT TOUCH --- END -D WAITING_WIFI_DELAY=1000 -D TIMEZONE=\"Europe/Paris\" - ;;;;;;;;;;;;;;;;;;;;;; - ;;; stepper config ;;; - ;;;;;;;;;;;;;;;;;;;;;; +;;;;;;;;;;;;;;;;;;;;;; +;;; stepper config ;;; +;;;;;;;;;;;;;;;;;;;;;; -D STEPMOTOR_I2C_ADDR=0x70 -D STEPER_ACC=200 - ;-D STEPER_PAS=755.906 ; = 65mm +;-D STEPER_PAS=755.906 ; = 65mm -D STEPER_PAS=58 ; = 5mm - -D STEPER_SPEED=1000 ; 12000 - ;;;;;;;;;;;;;;;;;;;;;; - ;;; App config ;;; - ;;;;;;;;;;;;;;;;;;;;;; + -D STEPER_SPEED=2700 ;1000 ; 2500 +;;;;;;;;;;;;;;;;;;;;;; +;;; App config ;;; +;;;;;;;;;;;;;;;;;;;;;; -D APP_TITLE=\"Warehouse\" -D APP_VERSION=\"1.0\" - ; nfc addr +; nfc addr -D NFC_ADDR=0x28 - ;;;;;;;;;;;;;;;;;;;;;; - ;;; Servo config ;;; - ;;;;;;;;;;;;;;;;;;;;;; - -D RIGHT_POS=18 - -D MIDDLE_POS=32 +;;;;;;;;;;;;;;;;;;;;;; +;;; Servo config ;;; +;;;;;;;;;;;;;;;;;;;;;; + -D RIGHT_POS=14 ;18 + -D MIDDLE_POS=30 ;32 -D LEFT_POS=52 - -D CONVOYER_LEN=150 ;mm + + -D CONVOYER_LEN=80 ;mm \ No newline at end of file diff --git a/include/Program.h b/include/Program.h index 4d632c5..cced5e5 100644 --- a/include/Program.h +++ b/include/Program.h @@ -21,6 +21,7 @@ public: void checkNfc(); void checkServo(); void checkWifi(); + void moveStepper(); private: DolibarrClient *client; ServoMotorComponent *servo; diff --git a/lib/DolibarrClient/src/DolibarrClient.cpp b/lib/DolibarrClient/src/DolibarrClient.cpp index 30b3ef2..34246bd 100644 --- a/lib/DolibarrClient/src/DolibarrClient.cpp +++ b/lib/DolibarrClient/src/DolibarrClient.cpp @@ -4,6 +4,8 @@ #include #include "M5LCD.h" +HTTPClient *client = new HTTPClient(); + DolibarrClient::DolibarrClient(struct DolibarrConfig dolibarr_config) : dolibarr(dolibarr_config) { #if defined(DEBUG) Serial.println(" --- Dolibarr configuration --- "); @@ -14,7 +16,6 @@ DolibarrClient::DolibarrClient(struct DolibarrConfig dolibarr_config) : dolibarr } 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"); @@ -25,40 +26,12 @@ HTTPClient *DolibarrClient::build_url(const String& url) const { return client; } - -int DolibarrClient::login() const { - HTTPClient *client = this->build_url(API_LOGIN_URL); - int httpResponseCode = client->GET(); - if (httpResponseCode > 0) { - DynamicJsonDocument doc(384); - DeserializationError error = deserializeJson(doc, client->getString().c_str()); - if (error) { - Serial.println("ERROR: "); - Serial.println(error.c_str()); - 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 *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_factory_id(const char* factory_id) const { HTTPClient *client = this->build_url(replace_id("/products?sortorder=ASC&limit=1&sqlfilters=(t.accountancy_code_sell:like:'{id}')", factory_id).c_str()); if (client->GET() == HTTP_CODE_OK) { @@ -67,7 +40,6 @@ models::Product DolibarrClient::get_product_by_factory_id(const char* factory_id if (error) { Serial.println("ERROR: "); Serial.println(error.c_str()); - delete client; return {}; } for (auto obj : doc.as()) { @@ -80,13 +52,10 @@ models::Product DolibarrClient::get_product_by_factory_id(const char* factory_id product.accountancy_code_sell = obj["accountancy_code_sell"].as(); product.accountancy_code_sell_export = obj["accountancy_code_sell_export"].as(); product.fk_default_warehouse = obj["fk_default_warehouse"].as(); - delete client; return product; } - delete client; return {}; } - delete client; return {}; } @@ -96,7 +65,6 @@ models::Product *DolibarrClient::get_product_by_id(const char* id_product) const DynamicJsonDocument doc(6144); DeserializationError error = deserializeJson(doc, client->getString().c_str()); if (error) { - delete client; return nullptr; } auto *product = new models::Product(); @@ -107,10 +75,8 @@ models::Product *DolibarrClient::get_product_by_id(const char* id_product) const product->label = doc["label"].as(); product->accountancy_code_sell = doc["accountancy_code_sell"].as(); product->accountancy_code_sell_export = doc["accountancy_code_sell_export"].as(); - delete client; return product; } - delete client; return nullptr; } @@ -122,7 +88,6 @@ std::vector *DolibarrClient::list_warehouse() const { if (error) { Serial.println("ERROR: "); Serial.println(error.c_str()); - delete client; return nullptr; } auto *warehouses = new std::vector(); @@ -132,10 +97,8 @@ std::vector *DolibarrClient::list_warehouse() const { warehouse.label = obj["label"].as(); warehouses->push_back(warehouse); } - delete client; return warehouses; } - delete client; return nullptr; } @@ -151,7 +114,6 @@ int DolibarrClient::create_movement(models::CreateProductStock stock) const { serializeJson(doc, result); Serial.println(result.c_str()); if (client->POST(result.c_str()) == HTTP_CODE_OK) { - delete client; return 0; } return -1; diff --git a/lib/DolibarrClient/src/DolibarrClient.h b/lib/DolibarrClient/src/DolibarrClient.h index 748d7aa..98d47ee 100644 --- a/lib/DolibarrClient/src/DolibarrClient.h +++ b/lib/DolibarrClient/src/DolibarrClient.h @@ -20,9 +20,7 @@ class DolibarrClient { public: DolibarrClient(DolibarrConfig dolibarr_config); ~DolibarrClient() = default; - int login() const; std::vector *list_warehouse() const; - std::vector *list_products() const; models::Product *get_product_by_id(const char* id_product) const; models::Product get_product_by_factory_id(const char* uuid) const; int create_movement(models::CreateProductStock stock) const; diff --git a/lib/GRBL/src/GRBL.cpp b/lib/GRBL/src/GRBL.cpp index a674180..a0469f6 100644 --- a/lib/GRBL/src/GRBL.cpp +++ b/lib/GRBL/src/GRBL.cpp @@ -36,7 +36,7 @@ void GRBL::mouveForward(int mm){ bool GRBL::isIddle(){ bool sortie = false; - if(this->grbl->readStatus().indexOf("IDLE") != -1){ + if(this->grbl->readStatus().indexOf("IDLE") != -1) { sortie = true; } diff --git a/lib/M5LCD/src/M5LCD.cpp b/lib/M5LCD/src/M5LCD.cpp index 59096c3..685dee1 100644 --- a/lib/M5LCD/src/M5LCD.cpp +++ b/lib/M5LCD/src/M5LCD.cpp @@ -4,7 +4,7 @@ * M5LCD classe */ -M5LCD::M5LCD() : _current_page(0), _logs() , _debug_loc_y(0), _components_status({COMPONENT_KO, COMPONENT_KO, COMPONENT_KO, COMPONENT_KO, COMPONENT_KO}) { +M5LCD::M5LCD() : _current_page(0), _logs() , _debug_loc_y(0), _components_status({COMPONENT_KO, COMPONENT_KO, COMPONENT_KO, COMPONENT_KO, COMPONENT_OK}) { this->_product_id = std::string(""); this->_product_label = std::string(""); this->_last_nfc = std::string(""); diff --git a/src/Program.cpp b/src/Program.cpp index 531af4e..9b86ac1 100644 --- a/src/Program.cpp +++ b/src/Program.cpp @@ -6,6 +6,8 @@ #include "NfcReader.h" #include +TaskHandle_t task1; + uint32_t derniereExecution = 0; const uint32_t intervalle = 1000; std::vector *warehouses; @@ -78,6 +80,7 @@ Program::Program() { lcdScreen->add_log("Initialize M5LCD component...."); this->nfcReader = new NfcReader(NFC_ADDR); this->servo = new ServoMotorComponent(2, 1, 1); + this->servo->setDesiredPosition(MIDDLE); this->grbl = new GRBL(STEPMOTOR_I2C_ADDR); Wire.begin(21, 22); grbl->init(STEPER_SPEED, STEPER_PAS, STEPER_ACC); @@ -85,6 +88,11 @@ Program::Program() { initialize_wifi(wifi_c); } +void Program::moveStepper() { + Serial.printf("moveStepper\n"); + this->grbl->mouveForward(5); +} + void Program::loop() { lcdScreen->update(); @@ -96,18 +104,13 @@ void Program::loop() { derniereExecution = maintenant; } this->servo->refresh(); - String nfcId = this->nfcReader->ReadNfc(); - //si qqc if(nfcId != "0"){ if (lcdScreen->get_components().wifi != COMPONENT_OK) { lcdScreen->add_log("Wifi not connected !"); lcdScreen->set_nfc_message("Cannot send wifi request !"); return; } - //j'arrète le stepper - //this->grbl->stop(); //FIXME: implemente - //j'affiche le tag lue Serial.print("new colis in comming : "); lcdScreen->add_log("new colis detected: "); lcdScreen->add_log(nfcId.c_str()); @@ -116,10 +119,6 @@ void Program::loop() { Serial.printf("Product: %s\n%s", product.label.c_str(), product.accountancy_code_sell_export.c_str()); lcdScreen->set_product_label(product.label); lcdScreen->set_product_id(product.id); - /*if (product.fk_default_warehouse == product.accountancy_code_sell_export) { - Serial.printf("Product already in the good warehouse !\n"); - return; - }*/ auto ware = std::find_if(warehouses->begin(), warehouses->end(), [&product](models::Warehouse w) {return w.id == product.accountancy_code_sell_export;}); if (ware.base() == nullptr) { Serial.printf("Warehouse not found !\n"); @@ -150,10 +149,10 @@ void Program::loop() { this->servo->setDesiredPosition(Position::MIDDLE); lcdScreen->set_servo_message("MIDDLE"); } - this->grbl->mouveForward(CONVOYER_LEN); + //this->grbl->mouveForward(CONVOYER_LEN); } else { - if(this->grbl->isIddle()){ + /*if(this->grbl->isIddle()){ this->grbl->mouveForward(5); - } + }*/ } } \ No newline at end of file