Added CLI
This commit is contained in:
parent
b63bd9ae42
commit
70b27a0c07
8 changed files with 151 additions and 32 deletions
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
// #include <mutex>
|
||||
|
||||
#include "microinverter.h"
|
||||
|
|
@ -34,8 +35,12 @@ class Dtu {
|
|||
|
||||
void updateMicroinverters();
|
||||
|
||||
void updateMicroinverters(std::vector<std::string> ¶metersToGet);
|
||||
|
||||
void printMicroinverters();
|
||||
|
||||
void printMicroinverters(std::vector<std::string> ¶metersToGet);
|
||||
|
||||
~Dtu();
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
// #include <mutex>
|
||||
|
||||
#include "port.h"
|
||||
|
|
@ -31,11 +32,15 @@ class Microinverter {
|
|||
|
||||
void updatePorts();
|
||||
|
||||
void updatePorts(std::vector<std::string> ¶metersToGet);
|
||||
|
||||
void updatePort(int i);
|
||||
|
||||
Port getPort(int i);
|
||||
|
||||
void printPorts();
|
||||
|
||||
void printPorts(std::vector<std::string> ¶metersToGet);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -4,27 +4,20 @@
|
|||
#include <stdint.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
// #include <mutex>
|
||||
|
||||
#include "portParametersGeneric.h"
|
||||
#include "modbus.h"
|
||||
|
||||
// struct _modbus;
|
||||
// typedef _modbus modbus_t;
|
||||
|
||||
class Port {
|
||||
private:
|
||||
// std::shared_ptr<modbus_t*> modbus_context;
|
||||
|
||||
std::shared_ptr<class modbus> modbus;
|
||||
|
||||
// std::mutex *modbus_context_mutex;
|
||||
|
||||
uint16_t portStartAddress;
|
||||
|
||||
void populateParameters();
|
||||
|
||||
std::pair<std::shared_ptr<PortParameter>, bool> getParameterByName(std::string name);
|
||||
|
||||
void fixCurrent();
|
||||
bool currentFixed;
|
||||
|
||||
|
|
@ -35,7 +28,11 @@ class Port {
|
|||
|
||||
void updateParameters();
|
||||
|
||||
void updateParameters(std::vector<std::string> ¶metersToGet);
|
||||
|
||||
void printParameters();
|
||||
|
||||
void printParameters(std::vector<std::string> ¶metersToGet);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -40,7 +40,7 @@ class PortParameter {
|
|||
|
||||
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 {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
#include <vector>
|
||||
#include <iostream>
|
||||
// #include <thread>
|
||||
#include <string>
|
||||
|
||||
#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<class modbus>(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<std::string> ¶metersToGet) {
|
||||
std::vector<Microinverter>::iterator microinvertersIterator = this->microinverters.begin();
|
||||
while(microinvertersIterator != this->microinverters.end()) {
|
||||
microinvertersIterator->updatePorts(parametersToGet);
|
||||
microinvertersIterator++;
|
||||
}
|
||||
}
|
||||
|
||||
void Dtu::printMicroinverters() {
|
||||
std::cout << "DTU:" << std::endl;
|
||||
std::vector<Microinverter>::iterator microinvertersIterator = this->microinverters.begin();
|
||||
|
|
@ -107,3 +113,12 @@ void Dtu::printMicroinverters() {
|
|||
microinvertersIterator++;
|
||||
}
|
||||
}
|
||||
|
||||
void Dtu::printMicroinverters(std::vector<std::string> ¶metersToGet) {
|
||||
std::cout << "DTU:" << std::endl;
|
||||
std::vector<Microinverter>::iterator microinvertersIterator = this->microinverters.begin();
|
||||
while(microinvertersIterator != this->microinverters.end()) {
|
||||
microinvertersIterator->printPorts(parametersToGet);
|
||||
microinvertersIterator++;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
// #include <thread>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "modbus.h"
|
||||
|
||||
|
|
@ -27,6 +28,14 @@ void Microinverter::updatePorts() {
|
|||
// }
|
||||
}
|
||||
|
||||
void Microinverter::updatePorts(std::vector<std::string> ¶metersToGet) {
|
||||
std::vector<Port>::iterator portsIterator = this->ports.begin();
|
||||
while(portsIterator != this->ports.end()) {
|
||||
portsIterator->updateParameters(parametersToGet);
|
||||
portsIterator++;
|
||||
}
|
||||
}
|
||||
|
||||
void Microinverter::printPorts() {
|
||||
std::cout << "Microinverter: " << this->serialNumber << std::endl;
|
||||
|
||||
|
|
@ -37,3 +46,14 @@ void Microinverter::printPorts() {
|
|||
portsIterator++;
|
||||
}
|
||||
}
|
||||
|
||||
void Microinverter::printPorts(std::vector<std::string> ¶metersToGet) {
|
||||
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++;
|
||||
}
|
||||
}
|
||||
|
|
@ -2,7 +2,6 @@
|
|||
#include <string>
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
#include <memory>
|
||||
|
||||
#include "modbus.h"
|
||||
|
||||
|
|
@ -11,7 +10,6 @@
|
|||
|
||||
Port::Port(std::shared_ptr<class modbus> 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<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() {
|
||||
if(this->currentFixed) {
|
||||
return;
|
||||
|
|
@ -80,6 +93,20 @@ void Port::updateParameters() {
|
|||
this->fixCurrent();
|
||||
}
|
||||
|
||||
void Port::updateParameters(std::vector<std::string> ¶metersToGet) {
|
||||
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() {
|
||||
std::vector<std::shared_ptr<PortParameter>>::iterator parametersIterator = this->parameters.begin();
|
||||
|
||||
|
|
@ -92,3 +119,21 @@ void Port::printParameters() {
|
|||
parametersIterator++;
|
||||
}
|
||||
}
|
||||
|
||||
void Port::printParameters(std::vector<std::string> ¶metersToGet) {
|
||||
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() << " |";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
64
src/main.cpp
64
src/main.cpp
|
|
@ -1,28 +1,60 @@
|
|||
#include <chrono>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#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<std::chrono::milliseconds>(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<std::string>("-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<std::chrono::milliseconds>(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<int>("-p,--port", port, portHelp);
|
||||
|
||||
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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue