Added CLI

This commit is contained in:
TraYali 2024-03-20 11:28:43 +01:00
parent b63bd9ae42
commit 70b27a0c07
8 changed files with 151 additions and 32 deletions

View file

@ -3,6 +3,7 @@
#include <vector> #include <vector>
#include <memory> #include <memory>
#include <string>
// #include <mutex> // #include <mutex>
#include "microinverter.h" #include "microinverter.h"
@ -34,8 +35,12 @@ class Dtu {
void updateMicroinverters(); void updateMicroinverters();
void updateMicroinverters(std::vector<std::string> &parametersToGet);
void printMicroinverters(); void printMicroinverters();
void printMicroinverters(std::vector<std::string> &parametersToGet);
~Dtu(); ~Dtu();
}; };

View file

@ -3,6 +3,7 @@
#include <vector> #include <vector>
#include <memory> #include <memory>
#include <string>
// #include <mutex> // #include <mutex>
#include "port.h" #include "port.h"
@ -31,11 +32,15 @@ class Microinverter {
void updatePorts(); void updatePorts();
void updatePorts(std::vector<std::string> &parametersToGet);
void updatePort(int i); void updatePort(int i);
Port getPort(int i); Port getPort(int i);
void printPorts(); void printPorts();
void printPorts(std::vector<std::string> &parametersToGet);
}; };
#endif #endif

View file

@ -4,27 +4,20 @@
#include <stdint.h> #include <stdint.h>
#include <string> #include <string>
#include <vector> #include <vector>
#include <memory>
// #include <mutex>
#include "portParametersGeneric.h" #include "portParametersGeneric.h"
#include "modbus.h" #include "modbus.h"
// struct _modbus;
// typedef _modbus modbus_t;
class Port { class Port {
private: private:
// std::shared_ptr<modbus_t*> modbus_context;
std::shared_ptr<class modbus> modbus; std::shared_ptr<class modbus> modbus;
// std::mutex *modbus_context_mutex;
uint16_t portStartAddress; uint16_t portStartAddress;
void populateParameters(); void populateParameters();
std::pair<std::shared_ptr<PortParameter>, bool> getParameterByName(std::string name);
void fixCurrent(); void fixCurrent();
bool currentFixed; bool currentFixed;
@ -35,7 +28,11 @@ class Port {
void updateParameters(); void updateParameters();
void updateParameters(std::vector<std::string> &parametersToGet);
void printParameters(); void printParameters();
void printParameters(std::vector<std::string> &parametersToGet);
}; };
#endif #endif

View file

@ -40,7 +40,7 @@ class PortParameter {
virtual std::string getOutputValue(); virtual std::string getOutputValue();
void updateValue(std::shared_ptr<class modbus> modbus_context, uint16_t portStartAddress); void updateValue(std::shared_ptr<class modbus> modubs, uint16_t portStartAddress);
}; };
class PortParameterFloat : virtual public PortParameter { class PortParameterFloat : virtual public PortParameter {

View file

@ -1,6 +1,6 @@
#include <vector> #include <vector>
#include <iostream> #include <iostream>
// #include <thread> #include <string>
#include "modbus.h" #include "modbus.h"
@ -9,15 +9,13 @@
#include "portParameters.h" #include "portParameters.h"
// struct _modbus;
// typedef _modbus modbus_t;
Dtu::Dtu(const char *ip_address, int port) { Dtu::Dtu(const char *ip_address, int port) {
class modbus modbus{ip_address, (uint16_t) port}; class modbus modbus{ip_address, (uint16_t) port};
this->modbus = std::make_shared<class modbus>(modbus); this->modbus = std::make_shared<class modbus>(modbus);
if (!this->modbus.get()->modbus_connect()) { if (!this->modbus.get()->modbus_connect()) {
std::cerr << "conn_error"; std::cerr << "conn_error" << std::endl;
this->connected = false; this->connected = false;
} }
else { else {
@ -98,6 +96,14 @@ void Dtu::updateMicroinverters() {
// std::cout << std::endl; // std::cout << std::endl;
} }
void Dtu::updateMicroinverters(std::vector<std::string> &parametersToGet) {
std::vector<Microinverter>::iterator microinvertersIterator = this->microinverters.begin();
while(microinvertersIterator != this->microinverters.end()) {
microinvertersIterator->updatePorts(parametersToGet);
microinvertersIterator++;
}
}
void Dtu::printMicroinverters() { void Dtu::printMicroinverters() {
std::cout << "DTU:" << std::endl; std::cout << "DTU:" << std::endl;
std::vector<Microinverter>::iterator microinvertersIterator = this->microinverters.begin(); std::vector<Microinverter>::iterator microinvertersIterator = this->microinverters.begin();
@ -106,4 +112,13 @@ void Dtu::printMicroinverters() {
std::cout << std::endl; std::cout << std::endl;
microinvertersIterator++; microinvertersIterator++;
} }
}
void Dtu::printMicroinverters(std::vector<std::string> &parametersToGet) {
std::cout << "DTU:" << std::endl;
std::vector<Microinverter>::iterator microinvertersIterator = this->microinverters.begin();
while(microinvertersIterator != this->microinverters.end()) {
microinvertersIterator->printPorts(parametersToGet);
microinvertersIterator++;
}
} }

View file

@ -1,6 +1,7 @@
// #include <thread> // #include <thread>
#include <iostream> #include <iostream>
#include <memory> #include <memory>
#include <string>
#include "modbus.h" #include "modbus.h"
@ -27,6 +28,14 @@ void Microinverter::updatePorts() {
// } // }
} }
void Microinverter::updatePorts(std::vector<std::string> &parametersToGet) {
std::vector<Port>::iterator portsIterator = this->ports.begin();
while(portsIterator != this->ports.end()) {
portsIterator->updateParameters(parametersToGet);
portsIterator++;
}
}
void Microinverter::printPorts() { void Microinverter::printPorts() {
std::cout << "Microinverter: " << this->serialNumber << std::endl; std::cout << "Microinverter: " << this->serialNumber << std::endl;
@ -36,4 +45,15 @@ void Microinverter::printPorts() {
std::cout << std::endl; std::cout << std::endl;
portsIterator++; portsIterator++;
} }
}
void Microinverter::printPorts(std::vector<std::string> &parametersToGet) {
std::cout << "Microinverter: " << this->serialNumber << std::endl;
std::vector<Port>::iterator portsIterator = this->ports.begin();
while(portsIterator != this->ports.end()) {
portsIterator->printParameters(parametersToGet);
std::cout << std::endl;
portsIterator++;
}
} }

View file

@ -2,7 +2,6 @@
#include <string> #include <string>
#include <iostream> #include <iostream>
#include <cmath> #include <cmath>
#include <memory>
#include "modbus.h" #include "modbus.h"
@ -11,7 +10,6 @@
Port::Port(std::shared_ptr<class modbus> modbus, uint16_t portStartAddress) { Port::Port(std::shared_ptr<class modbus> modbus, uint16_t portStartAddress) {
this->modbus = modbus; this->modbus = modbus;
// this->modbus_context_mutex = modbus_context_mutex;
this->portStartAddress = portStartAddress; this->portStartAddress = portStartAddress;
@ -52,6 +50,21 @@ void Port::populateParameters() {
this->parameters.push_back(std::make_shared<PortParameterLinkStatus>()); this->parameters.push_back(std::make_shared<PortParameterLinkStatus>());
} }
std::pair<std::shared_ptr<PortParameter>, bool> Port::getParameterByName(std::string name) {
std::pair<std::shared_ptr<PortParameter>, bool> result;
result.second = false;
std::vector<std::shared_ptr<PortParameter>>::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() { void Port::fixCurrent() {
if(this->currentFixed) { if(this->currentFixed) {
return; return;
@ -80,6 +93,20 @@ void Port::updateParameters() {
this->fixCurrent(); this->fixCurrent();
} }
void Port::updateParameters(std::vector<std::string> &parametersToGet) {
std::vector<std::string>::iterator parametersToGetIterator = parametersToGet.begin();
while(parametersToGetIterator != parametersToGet.end()) {
std::pair<std::shared_ptr<PortParameter>, bool> parameterPair;
parameterPair = this->getParameterByName(*parametersToGetIterator);
if(parameterPair.second) {
parameterPair.first->updateValue(this->modbus, this->portStartAddress);
}
parametersToGetIterator++;
}
}
void Port::printParameters() { void Port::printParameters() {
std::vector<std::shared_ptr<PortParameter>>::iterator parametersIterator = this->parameters.begin(); std::vector<std::shared_ptr<PortParameter>>::iterator parametersIterator = this->parameters.begin();
@ -91,4 +118,22 @@ void Port::printParameters() {
std::cout << " " << parametersIterator->get()->name << ": " << parametersIterator->get()->getOutputValue() << " |"; std::cout << " " << parametersIterator->get()->name << ": " << parametersIterator->get()->getOutputValue() << " |";
parametersIterator++; parametersIterator++;
} }
}
void Port::printParameters(std::vector<std::string> &parametersToGet) {
std::vector<std::string>::iterator parametersToGetIterator = parametersToGet.begin();
if(parametersToGetIterator != parametersToGet.end()) {
std::cout << "|";
}
while(parametersToGetIterator != parametersToGet.end()) {
std::pair<std::shared_ptr<PortParameter>, bool> parameterPair;
parameterPair = this->getParameterByName(*parametersToGetIterator);
if(parameterPair.second) {
std::cout << " " << parameterPair.first->name << ": " << parameterPair.first->getOutputValue() << " |";
}
}
} }

View file

@ -1,28 +1,60 @@
#include <chrono>
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <chrono>
#include <thread> #include <thread>
#include <vector>
#include "CLI11.hpp"
#include "modbus.h" #include "modbus.h"
#include "dtu.h" #include "dtu.h"
int main(){ int main(int argc, char **argv) {
std::string ip_address {"192.168.31.136"}; CLI::App hoymilesClient{"Client for DTU-Pro/DTU-ProS"};
int port {502};
auto startTime = std::chrono::high_resolution_clock::now(); std::string ipAddress{"127.0.0.1"};
Dtu dtu {ip_address.c_str(), port}; std::string ipAddressHelp{"ipv4 address of DTU {default: " + ipAddress + "}"};
auto endTime = std::chrono::high_resolution_clock::now(); hoymilesClient.add_option<std::string>("-i,--ip_address", ipAddress, ipAddressHelp);
std::cout << "Construction time: " << std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).count() << "ms" << std::endl;
while(dtu.isConnected()) { int port{502};
auto startTime = std::chrono::high_resolution_clock::now(); std::string portHelp{"Port of DTU {default: " + std::to_string(port) + "}"};
dtu.updateMicroinverters(); hoymilesClient.add_option<int>("-p,--port", port, portHelp);
auto endTime = std::chrono::high_resolution_clock::now();
std::cout << "Update time: " << std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).count() << "ms" << std::endl;
dtu.printMicroinverters();
}
return 0; std::vector<std::string> parametersToGet{};
std::string parametersToGetHelp{"List of parameters to fetch, delimited by ',', example[par1,par2,par3]"};
hoymilesClient.add_option<std::vector<std::string>>("-P,--parameters", parametersToGet, parametersToGetHelp)->delimiter(',');
bool allParameters = false;
std::string allParametersHelp{"Fetch all parameters"};
hoymilesClient.add_flag<bool>("-a,--all_parameters", allParameters, allParametersHelp);
bool ignoreNotConnected = false;
std::string ignoreNotConnectedHelp{"Ignore connection errors"};
hoymilesClient.add_flag<bool>("-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<std::chrono::milliseconds>(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<std::chrono::milliseconds>(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<std::chrono::milliseconds>(endTime - startTime).count() << "ms" << std::endl;
dtu.printMicroinverters(parametersToGet);
}
}
return 0;
} }