Implemented parameters, made it granular
This commit is contained in:
parent
70b27a0c07
commit
2c27810dfe
7 changed files with 84 additions and 123 deletions
|
|
@ -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> ¶metersToGet);
|
void updateMicroinverters(std::vector<std::string> ¶metersToGet, bool allParameters, std::vector<long> µinvertersToGet);
|
||||||
|
|
||||||
void printMicroinverters();
|
// void printMicroinverters();
|
||||||
|
|
||||||
void printMicroinverters(std::vector<std::string> ¶metersToGet);
|
void printMicroinverters(std::vector<std::string> ¶metersToGet, bool allParameters, std::vector<long> µinvertersToGet);
|
||||||
|
|
||||||
~Dtu();
|
~Dtu();
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -30,17 +30,17 @@ class Microinverter {
|
||||||
|
|
||||||
std::vector<Port> ports;
|
std::vector<Port> ports;
|
||||||
|
|
||||||
void updatePorts();
|
// void updatePorts();
|
||||||
|
|
||||||
void updatePorts(std::vector<std::string> ¶metersToGet);
|
void updatePorts(std::vector<std::string> ¶metersToGet, 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> ¶metersToGet);
|
void printPorts(std::vector<std::string> ¶metersToGet, bool allParameters);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
@ -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> ¶metersToGet);
|
void updateParameters(std::vector<std::string> ¶metersToGet, bool allParameters);
|
||||||
|
|
||||||
void printParameters();
|
// void printParameters();
|
||||||
|
|
||||||
void printParameters(std::vector<std::string> ¶metersToGet);
|
void printParameters(std::vector<std::string> ¶metersToGet, bool allParameters);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
@ -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> ¶metersToGet, bool allParameters, std::vector<long> µinvertersToGet) {
|
||||||
// std::vector<std::thread> updateThreads;
|
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()) {
|
||||||
// updateThreads.push_back(std::thread(&Microinverter::updatePorts, microinvertersIterator));
|
microinvertersToGet.push_back(microinvertersIterator->serialNumber);
|
||||||
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> ¶metersToGet) {
|
|
||||||
std::vector<Microinverter>::iterator microinvertersIterator = this->microinverters.begin();
|
|
||||||
while(microinvertersIterator != this->microinverters.end()) {
|
|
||||||
microinvertersIterator->updatePorts(parametersToGet);
|
|
||||||
microinvertersIterator++;
|
microinvertersIterator++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Dtu::printMicroinverters() {
|
std::vector<long>::iterator microinvertersToGetIterator = microinvertersToGet.begin();
|
||||||
std::cout << "DTU:" << std::endl;
|
while(microinvertersToGetIterator != microinvertersToGet.end()) {
|
||||||
|
std::pair<bool, Microinverter*> microinverterPair = this->getMicroinverterBySerialNumber(*microinvertersToGetIterator);
|
||||||
|
if(microinverterPair.first) {
|
||||||
|
microinverterPair.second->updatePorts(parametersToGet, allParameters);
|
||||||
|
}
|
||||||
|
microinvertersToGetIterator++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Dtu::printMicroinverters(std::vector<std::string> ¶metersToGet, bool allParameters, std::vector<long> µinvertersToGet) {
|
||||||
|
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> ¶metersToGet) {
|
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++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -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> ¶metersToGet, 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> ¶metersToGet) {
|
|
||||||
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> ¶metersToGet, 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> ¶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;
|
std::cout << std::endl;
|
||||||
portsIterator++;
|
portsIterator++;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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> ¶metersToGet, bool allParameters) {
|
||||||
std::vector<std::shared_ptr<PortParameter>>::iterator parametersIterator{this->parameters.begin()};
|
if(allParameters && parametersToGet.size() < this->parameters.size()) {
|
||||||
|
std::vector<std::shared_ptr<PortParameter>>::iterator parametersIterator = this->parameters.begin();
|
||||||
while(parametersIterator != this->parameters.end()) {
|
while(parametersIterator != this->parameters.end()) {
|
||||||
parametersIterator->get()->updateValue(this->modbus, this->portStartAddress);
|
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> ¶metersToGet) {
|
|
||||||
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> ¶metersToGet) {
|
||||||
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> ¶metersToGet, 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> ¶metersToGet) {
|
|
||||||
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> ¶metersToGet) {
|
||||||
if(parameterPair.second) {
|
if(parameterPair.second) {
|
||||||
std::cout << " " << parameterPair.first->name << ": " << parameterPair.first->getOutputValue() << " |";
|
std::cout << " " << parameterPair.first->name << ": " << parameterPair.first->getOutputValue() << " |";
|
||||||
}
|
}
|
||||||
|
parametersToGetIterator++;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
30
src/main.cpp
30
src/main.cpp
|
|
@ -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;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue