From 70b27a0c078c659cc4bd6acb3c181e232ccdd6dc Mon Sep 17 00:00:00 2001 From: trabus322 Date: Wed, 20 Mar 2024 11:28:43 +0100 Subject: [PATCH] Added CLI --- inc/hoymiles/dtu.h | 5 ++ inc/hoymiles/microinverter.h | 5 ++ inc/hoymiles/port.h | 15 ++--- .../portParameters/portParametersGeneric.h | 2 +- src/hoymiles/dtu.cpp | 23 +++++-- src/hoymiles/microinverter.cpp | 20 ++++++ src/hoymiles/port.cpp | 49 +++++++++++++- src/main.cpp | 64 ++++++++++++++----- 8 files changed, 151 insertions(+), 32 deletions(-) diff --git a/inc/hoymiles/dtu.h b/inc/hoymiles/dtu.h index 530688a..94c4be1 100644 --- a/inc/hoymiles/dtu.h +++ b/inc/hoymiles/dtu.h @@ -3,6 +3,7 @@ #include #include +#include // #include #include "microinverter.h" @@ -34,8 +35,12 @@ class Dtu { void updateMicroinverters(); + void updateMicroinverters(std::vector ¶metersToGet); + void printMicroinverters(); + void printMicroinverters(std::vector ¶metersToGet); + ~Dtu(); }; diff --git a/inc/hoymiles/microinverter.h b/inc/hoymiles/microinverter.h index 4cf2e73..c3e9f9f 100644 --- a/inc/hoymiles/microinverter.h +++ b/inc/hoymiles/microinverter.h @@ -3,6 +3,7 @@ #include #include +#include // #include #include "port.h" @@ -31,11 +32,15 @@ class Microinverter { void updatePorts(); + void updatePorts(std::vector ¶metersToGet); + void updatePort(int i); Port getPort(int i); void printPorts(); + + void printPorts(std::vector ¶metersToGet); }; #endif \ No newline at end of file diff --git a/inc/hoymiles/port.h b/inc/hoymiles/port.h index 99d04ed..e433d5a 100644 --- a/inc/hoymiles/port.h +++ b/inc/hoymiles/port.h @@ -4,27 +4,20 @@ #include #include #include -#include -// #include #include "portParametersGeneric.h" #include "modbus.h" -// struct _modbus; -// typedef _modbus modbus_t; - class Port { private: - // std::shared_ptr modbus_context; - std::shared_ptr modbus; - // std::mutex *modbus_context_mutex; - uint16_t portStartAddress; void populateParameters(); + std::pair, bool> getParameterByName(std::string name); + void fixCurrent(); bool currentFixed; @@ -35,7 +28,11 @@ class Port { void updateParameters(); + void updateParameters(std::vector ¶metersToGet); + void printParameters(); + + void printParameters(std::vector ¶metersToGet); }; #endif \ No newline at end of file diff --git a/inc/hoymiles/portParameters/portParametersGeneric.h b/inc/hoymiles/portParameters/portParametersGeneric.h index 7fbd574..1567bb9 100644 --- a/inc/hoymiles/portParameters/portParametersGeneric.h +++ b/inc/hoymiles/portParameters/portParametersGeneric.h @@ -40,7 +40,7 @@ class PortParameter { virtual std::string getOutputValue(); - void updateValue(std::shared_ptr modbus_context, uint16_t portStartAddress); + void updateValue(std::shared_ptr modubs, uint16_t portStartAddress); }; class PortParameterFloat : virtual public PortParameter { diff --git a/src/hoymiles/dtu.cpp b/src/hoymiles/dtu.cpp index 57e5aee..30afcd3 100644 --- a/src/hoymiles/dtu.cpp +++ b/src/hoymiles/dtu.cpp @@ -1,6 +1,6 @@ #include #include -// #include +#include #include "modbus.h" @@ -9,15 +9,13 @@ #include "portParameters.h" -// struct _modbus; -// typedef _modbus modbus_t; Dtu::Dtu(const char *ip_address, int port) { class modbus modbus{ip_address, (uint16_t) port}; this->modbus = std::make_shared(modbus); if (!this->modbus.get()->modbus_connect()) { - std::cerr << "conn_error"; + std::cerr << "conn_error" << std::endl; this->connected = false; } else { @@ -98,6 +96,14 @@ void Dtu::updateMicroinverters() { // std::cout << std::endl; } +void Dtu::updateMicroinverters(std::vector ¶metersToGet) { + std::vector::iterator microinvertersIterator = this->microinverters.begin(); + while(microinvertersIterator != this->microinverters.end()) { + microinvertersIterator->updatePorts(parametersToGet); + microinvertersIterator++; + } +} + void Dtu::printMicroinverters() { std::cout << "DTU:" << std::endl; std::vector::iterator microinvertersIterator = this->microinverters.begin(); @@ -106,4 +112,13 @@ void Dtu::printMicroinverters() { std::cout << std::endl; microinvertersIterator++; } +} + +void Dtu::printMicroinverters(std::vector ¶metersToGet) { + std::cout << "DTU:" << std::endl; + std::vector::iterator microinvertersIterator = this->microinverters.begin(); + while(microinvertersIterator != this->microinverters.end()) { + microinvertersIterator->printPorts(parametersToGet); + microinvertersIterator++; + } } \ No newline at end of file diff --git a/src/hoymiles/microinverter.cpp b/src/hoymiles/microinverter.cpp index 78b2157..c86c144 100644 --- a/src/hoymiles/microinverter.cpp +++ b/src/hoymiles/microinverter.cpp @@ -1,6 +1,7 @@ // #include #include #include +#include #include "modbus.h" @@ -27,6 +28,14 @@ void Microinverter::updatePorts() { // } } +void Microinverter::updatePorts(std::vector ¶metersToGet) { + std::vector::iterator portsIterator = this->ports.begin(); + while(portsIterator != this->ports.end()) { + portsIterator->updateParameters(parametersToGet); + portsIterator++; + } +} + void Microinverter::printPorts() { std::cout << "Microinverter: " << this->serialNumber << std::endl; @@ -36,4 +45,15 @@ void Microinverter::printPorts() { std::cout << std::endl; portsIterator++; } +} + +void Microinverter::printPorts(std::vector ¶metersToGet) { + std::cout << "Microinverter: " << this->serialNumber << std::endl; + + std::vector::iterator portsIterator = this->ports.begin(); + while(portsIterator != this->ports.end()) { + portsIterator->printParameters(parametersToGet); + std::cout << std::endl; + portsIterator++; + } } \ No newline at end of file diff --git a/src/hoymiles/port.cpp b/src/hoymiles/port.cpp index 298d4ae..67182b6 100644 --- a/src/hoymiles/port.cpp +++ b/src/hoymiles/port.cpp @@ -2,7 +2,6 @@ #include #include #include -#include #include "modbus.h" @@ -11,7 +10,6 @@ Port::Port(std::shared_ptr modbus, uint16_t portStartAddress) { this->modbus = modbus; - // this->modbus_context_mutex = modbus_context_mutex; this->portStartAddress = portStartAddress; @@ -52,6 +50,21 @@ void Port::populateParameters() { this->parameters.push_back(std::make_shared()); } +std::pair, bool> Port::getParameterByName(std::string name) { + std::pair, bool> result; + result.second = false; + + std::vector>::iterator parametersIterator = this->parameters.begin(); + while(parametersIterator != this->parameters.end() && !result.second) { + if(parametersIterator->get()->name == name) { + result.first = *parametersIterator; + result.second = true; + } + } + + return result; +} + void Port::fixCurrent() { if(this->currentFixed) { return; @@ -80,6 +93,20 @@ void Port::updateParameters() { this->fixCurrent(); } +void Port::updateParameters(std::vector ¶metersToGet) { + std::vector::iterator parametersToGetIterator = parametersToGet.begin(); + while(parametersToGetIterator != parametersToGet.end()) { + std::pair, bool> parameterPair; + + parameterPair = this->getParameterByName(*parametersToGetIterator); + if(parameterPair.second) { + parameterPair.first->updateValue(this->modbus, this->portStartAddress); + } + + parametersToGetIterator++; + } +} + void Port::printParameters() { std::vector>::iterator parametersIterator = this->parameters.begin(); @@ -91,4 +118,22 @@ void Port::printParameters() { std::cout << " " << parametersIterator->get()->name << ": " << parametersIterator->get()->getOutputValue() << " |"; parametersIterator++; } +} + +void Port::printParameters(std::vector ¶metersToGet) { + std::vector::iterator parametersToGetIterator = parametersToGet.begin(); + + if(parametersToGetIterator != parametersToGet.end()) { + std::cout << "|"; + } + + while(parametersToGetIterator != parametersToGet.end()) { + std::pair, bool> parameterPair; + + parameterPair = this->getParameterByName(*parametersToGetIterator); + if(parameterPair.second) { + std::cout << " " << parameterPair.first->name << ": " << parameterPair.first->getOutputValue() << " |"; + } + } + } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 1f708f7..3e7a2d8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,28 +1,60 @@ +#include #include #include -#include #include +#include +#include "CLI11.hpp" #include "modbus.h" + #include "dtu.h" -int main(){ +int main(int argc, char **argv) { - std::string ip_address {"192.168.31.136"}; - int port {502}; + CLI::App hoymilesClient{"Client for DTU-Pro/DTU-ProS"}; - auto startTime = std::chrono::high_resolution_clock::now(); - Dtu dtu {ip_address.c_str(), port}; - auto endTime = std::chrono::high_resolution_clock::now(); - std::cout << "Construction time: " << std::chrono::duration_cast(endTime - startTime).count() << "ms" << std::endl; + std::string ipAddress{"127.0.0.1"}; + std::string ipAddressHelp{"ipv4 address of DTU {default: " + ipAddress + "}"}; + hoymilesClient.add_option("-i,--ip_address", ipAddress, ipAddressHelp); - while(dtu.isConnected()) { - auto startTime = std::chrono::high_resolution_clock::now(); - dtu.updateMicroinverters(); - auto endTime = std::chrono::high_resolution_clock::now(); - std::cout << "Update time: " << std::chrono::duration_cast(endTime - startTime).count() << "ms" << std::endl; - dtu.printMicroinverters(); - } + int port{502}; + std::string portHelp{"Port of DTU {default: " + std::to_string(port) + "}"}; + hoymilesClient.add_option("-p,--port", port, portHelp); - return 0; + std::vector parametersToGet{}; + std::string parametersToGetHelp{"List of parameters to fetch, delimited by ',', example[par1,par2,par3]"}; + hoymilesClient.add_option>("-P,--parameters", parametersToGet, parametersToGetHelp)->delimiter(','); + + bool allParameters = false; + std::string allParametersHelp{"Fetch all parameters"}; + hoymilesClient.add_flag("-a,--all_parameters", allParameters, allParametersHelp); + + bool ignoreNotConnected = false; + std::string ignoreNotConnectedHelp{"Ignore connection errors"}; + hoymilesClient.add_flag("-I,--ignore_conn_error", ignoreNotConnected, ignoreNotConnectedHelp); + + auto startTime = std::chrono::high_resolution_clock::now(); + Dtu dtu{ipAddress.c_str(), port}; + auto endTime = std::chrono::high_resolution_clock::now(); + std::cout << "DTU construction time: " << std::chrono::duration_cast(endTime - startTime).count() << "ms" << std::endl; + + while (dtu.isConnected() || ignoreNotConnected) { + if (allParameters) { + startTime = std::chrono::high_resolution_clock::now(); + dtu.updateMicroinverters(); + endTime = std::chrono::high_resolution_clock::now(); + std::cout << "DTU update time: " << std::chrono::duration_cast(endTime - startTime).count() << "ms" << std::endl; + + dtu.printMicroinverters(); + } else { + startTime = std::chrono::high_resolution_clock::now(); + dtu.updateMicroinverters(parametersToGet); + endTime = std::chrono::high_resolution_clock::now(); + std::cout << "DTU update time: " << std::chrono::duration_cast(endTime - startTime).count() << "ms" << std::endl; + + dtu.printMicroinverters(parametersToGet); + } + } + + return 0; } \ No newline at end of file