Removed multithreading for now, corrected connected

This commit is contained in:
TraYali 2024-03-19 12:54:05 +01:00
parent 089c7a77b5
commit e0135b6753
4 changed files with 34 additions and 20 deletions

View file

@ -17,6 +17,8 @@ class Dtu {
std::vector<Microinverter> microinverters; std::vector<Microinverter> microinverters;
bool connected;
void populateMicroinverters(); void populateMicroinverters();
std::pair<bool, Microinverter*> getMicroinverterBySerialNumber(long serialNumber); std::pair<bool, Microinverter*> getMicroinverterBySerialNumber(long serialNumber);
@ -24,6 +26,8 @@ class Dtu {
public: public:
Dtu(const char *ip_address, int port); Dtu(const char *ip_address, int port);
bool isConnected();
void updateMicroinverters(); void updateMicroinverters();
void printMicroinverters(); void printMicroinverters();

View file

@ -1,6 +1,6 @@
#include <vector> #include <vector>
#include <iostream> #include <iostream>
#include <thread> // #include <thread>
#include "modbus.h" #include "modbus.h"
@ -18,10 +18,19 @@ Dtu::Dtu(const char *ip_address, int port) {
if (modbus_connect(*this->modbus_context.get()) == -1) { if (modbus_connect(*this->modbus_context.get()) == -1) {
std::cerr << "conn_error"; std::cerr << "conn_error";
modbus_free(*this->modbus_context.get()); modbus_free(*this->modbus_context.get());
abort(); this->connected = false;
}
else {
this->connected = true;
} }
if(this->connected) {
this->populateMicroinverters(); this->populateMicroinverters();
}
}
bool Dtu::isConnected() {
return this->connected;
} }
Dtu::~Dtu() { Dtu::~Dtu() {
@ -74,19 +83,20 @@ std::pair<bool, Microinverter*> Dtu::getMicroinverterBySerialNumber(long serialN
} }
void Dtu::updateMicroinverters() { void Dtu::updateMicroinverters() {
std::vector<std::thread> updateThreads; // std::vector<std::thread> updateThreads;
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)); // updateThreads.push_back(std::thread(&Microinverter::updatePorts, microinvertersIterator));
microinvertersIterator->updatePorts();
microinvertersIterator++; microinvertersIterator++;
} }
std::vector<std::thread>::iterator updateThreadsIterator = updateThreads.begin(); // std::vector<std::thread>::iterator updateThreadsIterator = updateThreads.begin();
while(updateThreadsIterator != updateThreads.end()) { // while(updateThreadsIterator != updateThreads.end()) {
updateThreadsIterator->join(); updateThreadsIterator++; // updateThreadsIterator->join(); updateThreadsIterator++;
} // }
std::cout << std::endl; // std::cout << std::endl;
} }
void Dtu::printMicroinverters() { void Dtu::printMicroinverters() {

View file

@ -1,4 +1,4 @@
#include <thread> // #include <thread>
#include <iostream> #include <iostream>
#include <memory> #include <memory>
@ -18,16 +18,16 @@ Microinverter::Microinverter(std::shared_ptr<modbus_t*> modbus_context, std::mut
} }
void Microinverter::updatePorts() { void Microinverter::updatePorts() {
std::vector<std::thread> updateThreads; // std::vector<std::thread> updateThreads;
for(Port port : this->ports){ for(Port port : this->ports){
updateThreads.push_back(std::thread(&Port::updateParameters, port)); // updateThreads.push_back(std::thread(&Port::updateParameters, port));
// port.updateParameters(); port.updateParameters();
}
std::vector<std::thread>::iterator updateThreadsIterator = updateThreads.begin();
while(updateThreadsIterator != updateThreads.end()) {
updateThreadsIterator->join();
updateThreadsIterator++;
} }
// std::vector<std::thread>::iterator updateThreadsIterator = updateThreads.begin();
// while(updateThreadsIterator != updateThreads.end()) {
// updateThreadsIterator->join();
// updateThreadsIterator++;
// }
} }
void Microinverter::printPorts() { void Microinverter::printPorts() {

View file

@ -16,7 +16,7 @@ int main(){
auto endTime = std::chrono::high_resolution_clock::now(); 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::cout << "Construction time: " << std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).count() << "ms" << std::endl;
while(true) { while(dtu.isConnected()) {
auto startTime = std::chrono::high_resolution_clock::now(); auto startTime = std::chrono::high_resolution_clock::now();
dtu.updateMicroinverters(); dtu.updateMicroinverters();
auto endTime = std::chrono::high_resolution_clock::now(); auto endTime = std::chrono::high_resolution_clock::now();