Implemented parameters, made it granular

This commit is contained in:
TraYali 2024-03-20 14:40:02 +01:00
parent 70b27a0c07
commit 2c27810dfe
7 changed files with 84 additions and 123 deletions

View file

@ -4,22 +4,14 @@
#include <vector> #include <vector>
#include <memory> #include <memory>
#include <string> #include <string>
// #include <mutex>
#include "microinverter.h" #include "microinverter.h"
#include "modbus.h" #include "modbus.h"
// struct _modbus;
// typedef _modbus modbus_t;
class Dtu { class Dtu {
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;
std::vector<Microinverter> microinverters; std::vector<Microinverter> microinverters;
bool connected; bool connected;
@ -33,13 +25,13 @@ class Dtu {
bool isConnected(); bool isConnected();
void updateMicroinverters(); // void updateMicroinverters();
void updateMicroinverters(std::vector<std::string> &parametersToGet); void updateMicroinverters(std::vector<std::string> &parametersToGet, bool allParameters, std::vector<long> &microinvertersToGet);
void printMicroinverters(); // void printMicroinverters();
void printMicroinverters(std::vector<std::string> &parametersToGet); void printMicroinverters(std::vector<std::string> &parametersToGet, bool allParameters, std::vector<long> &microinvertersToGet);
~Dtu(); ~Dtu();
}; };

View file

@ -30,17 +30,17 @@ class Microinverter {
std::vector<Port> ports; std::vector<Port> ports;
void updatePorts(); // void updatePorts();
void updatePorts(std::vector<std::string> &parametersToGet); void updatePorts(std::vector<std::string> &parametersToGet, bool allParameters);
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); void printPorts(std::vector<std::string> &parametersToGet, bool allParameters);
}; };
#endif #endif

View file

@ -26,13 +26,13 @@ class Port {
std::vector<std::shared_ptr<PortParameter>> parameters; std::vector<std::shared_ptr<PortParameter>> parameters;
void updateParameters(); // void updateParameters();
void updateParameters(std::vector<std::string> &parametersToGet); void updateParameters(std::vector<std::string> &parametersToGet, bool allParameters);
void printParameters(); // void printParameters();
void printParameters(std::vector<std::string> &parametersToGet); void printParameters(std::vector<std::string> &parametersToGet, bool allParameters);
}; };
#endif #endif

View file

@ -60,9 +60,7 @@ void Dtu::populateMicroinverters() {
portStartAddress += 0x0028; portStartAddress += 0x0028;
// this->modbus_context_mutex.lock();
registerCount = this->modbus.get()->modbus_read_holding_registers(portStartAddress + 0x0021, 1, readArray); registerCount = this->modbus.get()->modbus_read_holding_registers(portStartAddress + 0x0021, 1, readArray);
// this->modbus_context_mutex.unlock();
} }
} }
@ -79,46 +77,40 @@ std::pair<bool, Microinverter*> Dtu::getMicroinverterBySerialNumber(long serialN
return std::pair<bool, Microinverter*>(false, &*microinvertersIterator); return std::pair<bool, Microinverter*>(false, &*microinvertersIterator);
} }
void Dtu::updateMicroinverters() { void Dtu::updateMicroinverters(std::vector<std::string> &parametersToGet, bool allParameters, std::vector<long> &microinvertersToGet) {
// std::vector<std::thread> updateThreads; if(microinvertersToGet.empty()) {
std::vector<Microinverter>::iterator microinvertersIterator = this->microinverters.begin();
while (microinvertersIterator != this->microinverters.end()) {
// updateThreads.push_back(std::thread(&Microinverter::updatePorts, microinvertersIterator));
microinvertersIterator->updatePorts();
microinvertersIterator++;
}
// std::vector<std::thread>::iterator updateThreadsIterator = updateThreads.begin();
// while(updateThreadsIterator != updateThreads.end()) {
// updateThreadsIterator->join(); updateThreadsIterator++;
// }
// std::cout << std::endl;
}
void Dtu::updateMicroinverters(std::vector<std::string> &parametersToGet) {
std::vector<Microinverter>::iterator microinvertersIterator = this->microinverters.begin(); std::vector<Microinverter>::iterator microinvertersIterator = this->microinverters.begin();
while(microinvertersIterator != this->microinverters.end()) { while(microinvertersIterator != this->microinverters.end()) {
microinvertersIterator->updatePorts(parametersToGet); microinvertersToGet.push_back(microinvertersIterator->serialNumber);
microinvertersIterator++; microinvertersIterator++;
} }
}
std::vector<long>::iterator microinvertersToGetIterator = microinvertersToGet.begin();
while(microinvertersToGetIterator != microinvertersToGet.end()) {
std::pair<bool, Microinverter*> microinverterPair = this->getMicroinverterBySerialNumber(*microinvertersToGetIterator);
if(microinverterPair.first) {
microinverterPair.second->updatePorts(parametersToGet, allParameters);
}
microinvertersToGetIterator++;
}
} }
void Dtu::printMicroinverters() { void Dtu::printMicroinverters(std::vector<std::string> &parametersToGet, bool allParameters, std::vector<long> &microinvertersToGet) {
std::cout << "DTU:" << std::endl; if(microinvertersToGet.empty()) {
std::vector<Microinverter>::iterator microinvertersIterator = this->microinverters.begin(); std::vector<Microinverter>::iterator microinvertersIterator = this->microinverters.begin();
while(microinvertersIterator != this->microinverters.end()) { while(microinvertersIterator != this->microinverters.end()) {
microinvertersIterator->printPorts(); microinvertersToGet.push_back(microinvertersIterator->serialNumber);
std::cout << std::endl;
microinvertersIterator++; microinvertersIterator++;
} }
} }
void Dtu::printMicroinverters(std::vector<std::string> &parametersToGet) { std::vector<long>::iterator microinvertersToGetIterator = microinvertersToGet.begin();
std::cout << "DTU:" << std::endl; while(microinvertersToGetIterator != microinvertersToGet.end()) {
std::vector<Microinverter>::iterator microinvertersIterator = this->microinverters.begin(); std::pair<bool, Microinverter*> microinverterPair = this->getMicroinverterBySerialNumber(*microinvertersToGetIterator);
while(microinvertersIterator != this->microinverters.end()) { if(microinverterPair.first) {
microinvertersIterator->printPorts(parametersToGet); microinverterPair.second->printPorts(parametersToGet, allParameters);
microinvertersIterator++; }
microinvertersToGetIterator++;
} }
} }

View file

@ -15,44 +15,20 @@ Microinverter::Microinverter(std::shared_ptr<class modbus> modbus, long serialNu
this->serialNumber = serialNumber; this->serialNumber = serialNumber;
} }
void Microinverter::updatePorts() { void Microinverter::updatePorts(std::vector<std::string> &parametersToGet, bool allParameters) {
// std::vector<std::thread> updateThreads;
for(Port port : this->ports){
// updateThreads.push_back(std::thread(&Port::updateParameters, port));
port.updateParameters();
}
// std::vector<std::thread>::iterator updateThreadsIterator = updateThreads.begin();
// while(updateThreadsIterator != updateThreads.end()) {
// updateThreadsIterator->join();
// updateThreadsIterator++;
// }
}
void Microinverter::updatePorts(std::vector<std::string> &parametersToGet) {
std::vector<Port>::iterator portsIterator = this->ports.begin(); std::vector<Port>::iterator portsIterator = this->ports.begin();
while(portsIterator != this->ports.end()) { while(portsIterator != this->ports.end()) {
portsIterator->updateParameters(parametersToGet); portsIterator->updateParameters(parametersToGet, allParameters);
portsIterator++; portsIterator++;
} }
} }
void Microinverter::printPorts() { void Microinverter::printPorts(std::vector<std::string> &parametersToGet, bool allParameters) {
std::cout << "Microinverter: " << this->serialNumber << std::endl; std::cout << "Microinverter: " << this->serialNumber << std::endl;
std::vector<Port>::iterator portsIterator = this->ports.begin(); std::vector<Port>::iterator portsIterator = this->ports.begin();
while(portsIterator != this->ports.end()) { while(portsIterator != this->ports.end()) {
portsIterator->printParameters(); portsIterator->printParameters(parametersToGet, allParameters);
std::cout << std::endl;
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; std::cout << std::endl;
portsIterator++; portsIterator++;
} }

View file

@ -2,6 +2,7 @@
#include <string> #include <string>
#include <iostream> #include <iostream>
#include <cmath> #include <cmath>
#include <algorithm>
#include "modbus.h" #include "modbus.h"
@ -60,6 +61,7 @@ std::pair<std::shared_ptr<PortParameter>, bool> Port::getParameterByName(std::st
result.first = *parametersIterator; result.first = *parametersIterator;
result.second = true; result.second = true;
} }
parametersIterator++;
} }
return result; return result;
@ -84,16 +86,17 @@ void Port::fixCurrent() {
this->currentFixed = true; this->currentFixed = true;
} }
void Port::updateParameters() { void Port::updateParameters(std::vector<std::string> &parametersToGet, bool allParameters) {
std::vector<std::shared_ptr<PortParameter>>::iterator parametersIterator{this->parameters.begin()}; if(allParameters && parametersToGet.size() < this->parameters.size()) {
while (parametersIterator != this->parameters.end()) { std::vector<std::shared_ptr<PortParameter>>::iterator parametersIterator = this->parameters.begin();
parametersIterator->get()->updateValue(this->modbus, this->portStartAddress); while(parametersIterator != this->parameters.end()) {
if(std::find(parametersToGet.begin(), parametersToGet.end(), parametersIterator->get()->name) == parametersToGet.end()) {
parametersToGet.push_back(parametersIterator->get()->name);
}
parametersIterator++; parametersIterator++;
} }
this->fixCurrent(); }
}
void Port::updateParameters(std::vector<std::string> &parametersToGet) {
std::vector<std::string>::iterator parametersToGetIterator = parametersToGet.begin(); std::vector<std::string>::iterator parametersToGetIterator = parametersToGet.begin();
while(parametersToGetIterator != parametersToGet.end()) { while(parametersToGetIterator != parametersToGet.end()) {
std::pair<std::shared_ptr<PortParameter>, bool> parameterPair; std::pair<std::shared_ptr<PortParameter>, bool> parameterPair;
@ -102,27 +105,22 @@ void Port::updateParameters(std::vector<std::string> &parametersToGet) {
if(parameterPair.second) { if(parameterPair.second) {
parameterPair.first->updateValue(this->modbus, this->portStartAddress); parameterPair.first->updateValue(this->modbus, this->portStartAddress);
} }
parametersToGetIterator++; parametersToGetIterator++;
} }
} }
void Port::printParameters() { void Port::printParameters(std::vector<std::string> &parametersToGet, bool allParameters) {
if(allParameters && parametersToGet.size() < this->parameters.size()) {
std::vector<std::shared_ptr<PortParameter>>::iterator parametersIterator = this->parameters.begin(); std::vector<std::shared_ptr<PortParameter>>::iterator parametersIterator = this->parameters.begin();
if(parametersIterator != this->parameters.end()) {
std::cout << "|";
}
while(parametersIterator != this->parameters.end()) { while(parametersIterator != this->parameters.end()) {
std::cout << " " << parametersIterator->get()->name << ": " << parametersIterator->get()->getOutputValue() << " |"; if(std::find(parametersToGet.begin(), parametersToGet.end(), parametersIterator->get()->name) != parametersToGet.end()) {
parametersToGet.push_back(parametersIterator->get()->name);
}
parametersIterator++; parametersIterator++;
} }
} }
void Port::printParameters(std::vector<std::string> &parametersToGet) {
std::vector<std::string>::iterator parametersToGetIterator = parametersToGet.begin(); std::vector<std::string>::iterator parametersToGetIterator = parametersToGet.begin();
if(parametersToGetIterator != parametersToGet.end()) { if(parametersToGetIterator != parametersToGet.end()) {
std::cout << "|"; std::cout << "|";
} }
@ -134,6 +132,7 @@ void Port::printParameters(std::vector<std::string> &parametersToGet) {
if(parameterPair.second) { if(parameterPair.second) {
std::cout << " " << parameterPair.first->name << ": " << parameterPair.first->getOutputValue() << " |"; std::cout << " " << parameterPair.first->name << ": " << parameterPair.first->getOutputValue() << " |";
} }
parametersToGetIterator++;
} }
} }

View file

@ -10,12 +10,11 @@
#include "dtu.h" #include "dtu.h"
int main(int argc, char **argv) { int main(int argc, char **argv) {
CLI::App hoymilesClient{"Client for DTU-Pro/DTU-ProS"}; CLI::App hoymilesClient{"Client for DTU-Pro/DTU-ProS"};
std::string ipAddress{"127.0.0.1"}; std::string ipAddress{"127.0.0.1"};
std::string ipAddressHelp{"ipv4 address of DTU {default: " + ipAddress + "}"}; std::string ipAddressHelp{"ipv4 address of DTU {default: " + ipAddress + "}"};
hoymilesClient.add_option<std::string>("-i,--ip_address", ipAddress, ipAddressHelp); hoymilesClient.add_option<std::string>("-i,--ip_address", ipAddress, ipAddressHelp)->required();
int port{502}; int port{502};
std::string portHelp{"Port of DTU {default: " + std::to_string(port) + "}"}; std::string portHelp{"Port of DTU {default: " + std::to_string(port) + "}"};
@ -33,27 +32,30 @@ int main(int argc, char **argv) {
std::string ignoreNotConnectedHelp{"Ignore connection errors"}; std::string ignoreNotConnectedHelp{"Ignore connection errors"};
hoymilesClient.add_flag<bool>("-I,--ignore_conn_error", ignoreNotConnected, ignoreNotConnectedHelp); hoymilesClient.add_flag<bool>("-I,--ignore_conn_error", ignoreNotConnected, ignoreNotConnectedHelp);
std::vector<long> microinvertersToGet{};
std::string microinvertersToGetHelp{"List of microinverters to fetch, if omitted all are fetched, delimited by ','"};
hoymilesClient.add_option<std::vector<long>>("-m,--microinverters", microinvertersToGet, microinvertersToGetHelp)->delimiter(',');
try {
hoymilesClient.parse(argc, argv);
} catch (const CLI::ParseError &e) {
return hoymilesClient.exit(e);
}
std::cout << "Mapping out DTU" << std::endl;
auto startTime = std::chrono::high_resolution_clock::now(); auto startTime = std::chrono::high_resolution_clock::now();
Dtu dtu{ipAddress.c_str(), port}; Dtu dtu{ipAddress.c_str(), port};
auto endTime = std::chrono::high_resolution_clock::now(); 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; std::cout << "DTU construction time: " << std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).count() << "ms" << std::endl;
while (dtu.isConnected() || ignoreNotConnected) { while ((dtu.isConnected() || ignoreNotConnected) && (!parametersToGet.empty() || allParameters)) {
if (allParameters) {
startTime = std::chrono::high_resolution_clock::now(); startTime = std::chrono::high_resolution_clock::now();
dtu.updateMicroinverters(); dtu.updateMicroinverters(parametersToGet, allParameters, microinvertersToGet);
endTime = std::chrono::high_resolution_clock::now(); 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; std::cout << "DTU update time: " << std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).count() << "ms" << std::endl;
dtu.printMicroinverters(); dtu.printMicroinverters(parametersToGet, allParameters, microinvertersToGet);
} else { std::cout << std::endl;
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; return 0;