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 <memory>
|
||||
#include <string>
|
||||
// #include <mutex>
|
||||
|
||||
#include "microinverter.h"
|
||||
#include "modbus.h"
|
||||
|
||||
// struct _modbus;
|
||||
// typedef _modbus modbus_t;
|
||||
|
||||
class Dtu {
|
||||
private:
|
||||
// std::shared_ptr<modbus_t*> modbus_context;
|
||||
|
||||
std::shared_ptr<class modbus> modbus;
|
||||
|
||||
// std::mutex modbus_context_mutex;
|
||||
|
||||
std::vector<Microinverter> microinverters;
|
||||
|
||||
bool connected;
|
||||
|
|
@ -33,13 +25,13 @@ class Dtu {
|
|||
|
||||
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();
|
||||
};
|
||||
|
|
|
|||
|
|
@ -30,17 +30,17 @@ class Microinverter {
|
|||
|
||||
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);
|
||||
|
||||
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
|
||||
|
|
@ -26,13 +26,13 @@ class Port {
|
|||
|
||||
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
|
||||
|
|
@ -60,9 +60,7 @@ void Dtu::populateMicroinverters() {
|
|||
|
||||
portStartAddress += 0x0028;
|
||||
|
||||
// this->modbus_context_mutex.lock();
|
||||
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);
|
||||
}
|
||||
|
||||
void Dtu::updateMicroinverters() {
|
||||
// std::vector<std::thread> updateThreads;
|
||||
|
||||
std::vector<Microinverter>::iterator microinvertersIterator = this->microinverters.begin();
|
||||
while (microinvertersIterator != this->microinverters.end()) {
|
||||
// updateThreads.push_back(std::thread(&Microinverter::updatePorts, microinvertersIterator));
|
||||
microinvertersIterator->updatePorts();
|
||||
microinvertersIterator++;
|
||||
void Dtu::updateMicroinverters(std::vector<std::string> ¶metersToGet, bool allParameters, std::vector<long> µinvertersToGet) {
|
||||
if(microinvertersToGet.empty()) {
|
||||
std::vector<Microinverter>::iterator microinvertersIterator = this->microinverters.begin();
|
||||
while(microinvertersIterator != this->microinverters.end()) {
|
||||
microinvertersToGet.push_back(microinvertersIterator->serialNumber);
|
||||
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++;
|
||||
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() {
|
||||
std::cout << "DTU:" << std::endl;
|
||||
std::vector<Microinverter>::iterator microinvertersIterator = this->microinverters.begin();
|
||||
while(microinvertersIterator != this->microinverters.end()) {
|
||||
microinvertersIterator->printPorts();
|
||||
std::cout << std::endl;
|
||||
microinvertersIterator++;
|
||||
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();
|
||||
while(microinvertersIterator != this->microinverters.end()) {
|
||||
microinvertersToGet.push_back(microinvertersIterator->serialNumber);
|
||||
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++;
|
||||
std::vector<long>::iterator microinvertersToGetIterator = microinvertersToGet.begin();
|
||||
while(microinvertersToGetIterator != microinvertersToGet.end()) {
|
||||
std::pair<bool, Microinverter*> microinverterPair = this->getMicroinverterBySerialNumber(*microinvertersToGetIterator);
|
||||
if(microinverterPair.first) {
|
||||
microinverterPair.second->printPorts(parametersToGet, allParameters);
|
||||
}
|
||||
microinvertersToGetIterator++;
|
||||
}
|
||||
}
|
||||
|
|
@ -15,44 +15,20 @@ Microinverter::Microinverter(std::shared_ptr<class modbus> modbus, long serialNu
|
|||
this->serialNumber = serialNumber;
|
||||
}
|
||||
|
||||
void Microinverter::updatePorts() {
|
||||
// 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) {
|
||||
void Microinverter::updatePorts(std::vector<std::string> ¶metersToGet, bool allParameters) {
|
||||
std::vector<Port>::iterator portsIterator = this->ports.begin();
|
||||
while(portsIterator != this->ports.end()) {
|
||||
portsIterator->updateParameters(parametersToGet);
|
||||
portsIterator->updateParameters(parametersToGet, allParameters);
|
||||
portsIterator++;
|
||||
}
|
||||
}
|
||||
|
||||
void Microinverter::printPorts() {
|
||||
void Microinverter::printPorts(std::vector<std::string> ¶metersToGet, bool allParameters) {
|
||||
std::cout << "Microinverter: " << this->serialNumber << std::endl;
|
||||
|
||||
std::vector<Port>::iterator portsIterator = this->ports.begin();
|
||||
while(portsIterator != this->ports.end()) {
|
||||
portsIterator->printParameters();
|
||||
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);
|
||||
portsIterator->printParameters(parametersToGet, allParameters);
|
||||
std::cout << std::endl;
|
||||
portsIterator++;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
#include <string>
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
#include <algorithm>
|
||||
|
||||
#include "modbus.h"
|
||||
|
||||
|
|
@ -60,6 +61,7 @@ std::pair<std::shared_ptr<PortParameter>, bool> Port::getParameterByName(std::st
|
|||
result.first = *parametersIterator;
|
||||
result.second = true;
|
||||
}
|
||||
parametersIterator++;
|
||||
}
|
||||
|
||||
return result;
|
||||
|
|
@ -84,16 +86,17 @@ void Port::fixCurrent() {
|
|||
this->currentFixed = true;
|
||||
}
|
||||
|
||||
void Port::updateParameters() {
|
||||
std::vector<std::shared_ptr<PortParameter>>::iterator parametersIterator{this->parameters.begin()};
|
||||
while (parametersIterator != this->parameters.end()) {
|
||||
parametersIterator->get()->updateValue(this->modbus, this->portStartAddress);
|
||||
parametersIterator++;
|
||||
void Port::updateParameters(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();
|
||||
while(parametersIterator != this->parameters.end()) {
|
||||
if(std::find(parametersToGet.begin(), parametersToGet.end(), parametersIterator->get()->name) == parametersToGet.end()) {
|
||||
parametersToGet.push_back(parametersIterator->get()->name);
|
||||
}
|
||||
parametersIterator++;
|
||||
}
|
||||
}
|
||||
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;
|
||||
|
|
@ -102,27 +105,22 @@ void Port::updateParameters(std::vector<std::string> ¶metersToGet) {
|
|||
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();
|
||||
|
||||
if(parametersIterator != this->parameters.end()) {
|
||||
std::cout << "|";
|
||||
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();
|
||||
while(parametersIterator != this->parameters.end()) {
|
||||
if(std::find(parametersToGet.begin(), parametersToGet.end(), parametersIterator->get()->name) != parametersToGet.end()) {
|
||||
parametersToGet.push_back(parametersIterator->get()->name);
|
||||
}
|
||||
parametersIterator++;
|
||||
}
|
||||
}
|
||||
|
||||
while(parametersIterator != this->parameters.end()) {
|
||||
std::cout << " " << parametersIterator->get()->name << ": " << parametersIterator->get()->getOutputValue() << " |";
|
||||
parametersIterator++;
|
||||
}
|
||||
}
|
||||
|
||||
void Port::printParameters(std::vector<std::string> ¶metersToGet) {
|
||||
std::vector<std::string>::iterator parametersToGetIterator = parametersToGet.begin();
|
||||
|
||||
if(parametersToGetIterator != parametersToGet.end()) {
|
||||
std::cout << "|";
|
||||
}
|
||||
|
|
@ -134,6 +132,7 @@ void Port::printParameters(std::vector<std::string> ¶metersToGet) {
|
|||
if(parameterPair.second) {
|
||||
std::cout << " " << parameterPair.first->name << ": " << parameterPair.first->getOutputValue() << " |";
|
||||
}
|
||||
parametersToGetIterator++;
|
||||
}
|
||||
|
||||
}
|
||||
36
src/main.cpp
36
src/main.cpp
|
|
@ -10,12 +10,11 @@
|
|||
#include "dtu.h"
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
|
||||
CLI::App hoymilesClient{"Client for DTU-Pro/DTU-ProS"};
|
||||
|
||||
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);
|
||||
hoymilesClient.add_option<std::string>("-i,--ip_address", ipAddress, ipAddressHelp)->required();
|
||||
|
||||
int port{502};
|
||||
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"};
|
||||
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();
|
||||
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;
|
||||
while ((dtu.isConnected() || ignoreNotConnected) && (!parametersToGet.empty() || allParameters)) {
|
||||
startTime = std::chrono::high_resolution_clock::now();
|
||||
dtu.updateMicroinverters(parametersToGet, allParameters, microinvertersToGet);
|
||||
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);
|
||||
}
|
||||
dtu.printMicroinverters(parametersToGet, allParameters, microinvertersToGet);
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue