Rewritten the backend #14
5 changed files with 21 additions and 15 deletions
|
|
@ -14,8 +14,6 @@ class Dtu {
|
||||||
|
|
||||||
std::vector<Microinverter> microinverters;
|
std::vector<Microinverter> microinverters;
|
||||||
|
|
||||||
bool connected;
|
|
||||||
|
|
||||||
void populateMicroinverters();
|
void populateMicroinverters();
|
||||||
|
|
||||||
std::pair<Microinverter *, bool> getMicroinverterBySerialNumber(long long serialNumber);
|
std::pair<Microinverter *, bool> getMicroinverterBySerialNumber(long long serialNumber);
|
||||||
|
|
@ -25,6 +23,10 @@ class Dtu {
|
||||||
|
|
||||||
bool isConnected();
|
bool isConnected();
|
||||||
|
|
||||||
|
bool modbusError();
|
||||||
|
|
||||||
|
std::string modbusErrorMessage();
|
||||||
|
|
||||||
// void updateMicroinverters();
|
// void updateMicroinverters();
|
||||||
|
|
||||||
void updateMicroinverters(std::vector<std::string> ¶metersToGet, bool allParameters, std::vector<long long> µinvertersToGet);
|
void updateMicroinverters(std::vector<std::string> ¶metersToGet, bool allParameters, std::vector<long long> µinvertersToGet);
|
||||||
|
|
|
||||||
|
|
@ -20,9 +20,9 @@ class Port {
|
||||||
void increaseParametersAge();
|
void increaseParametersAge();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Port(std::shared_ptr<class modbus> modbus, uint16_t portStartAddress);
|
Port(std::shared_ptr<class modbus> modbus, int portStartAddress);
|
||||||
|
|
||||||
uint16_t portStartAddress;
|
int portStartAddress;
|
||||||
|
|
||||||
std::vector<std::shared_ptr<PortParameter>> parameters;
|
std::vector<std::shared_ptr<PortParameter>> parameters;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -16,27 +16,28 @@ Dtu::Dtu(const char *ip_address, int port) {
|
||||||
this->modbus = std::make_shared<class modbus>(modbus);
|
this->modbus = std::make_shared<class modbus>(modbus);
|
||||||
|
|
||||||
if (!this->modbus.get()->modbus_connect()) {
|
if (!this->modbus.get()->modbus_connect()) {
|
||||||
std::cerr << "conn_error" << std::endl;
|
std::cerr << "NOT CONNECTED" << std::endl;
|
||||||
this->connected = false;
|
|
||||||
} else {
|
|
||||||
this->connected = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this->connected) {
|
if (this->modbus.get()->is_connected()) {
|
||||||
this->populateMicroinverters();
|
this->populateMicroinverters();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Dtu::isConnected() { return this->connected; }
|
bool Dtu::isConnected() { return this->modbus.get()->is_connected(); }
|
||||||
|
|
||||||
|
bool Dtu::modbusError() { return this->modbus.get()->err; }
|
||||||
|
|
||||||
|
std::string Dtu::modbusErrorMessage() { return this->modbus.get()->error_msg; }
|
||||||
|
|
||||||
Dtu::~Dtu() { this->modbus.get()->modbus_close(); }
|
Dtu::~Dtu() { this->modbus.get()->modbus_close(); }
|
||||||
|
|
||||||
void Dtu::populateMicroinverters() {
|
void Dtu::populateMicroinverters() {
|
||||||
uint16_t portStartAddress = 0x1000;
|
int portStartAddress = 0x1000;
|
||||||
uint16_t readArray[1];
|
uint16_t readArray[20];
|
||||||
|
|
||||||
int registerCount;
|
int registerCount;
|
||||||
registerCount = this->modbus.get()->modbus_read_holding_registers(portStartAddress + 0x0021, 1, readArray);
|
registerCount = this->modbus.get()->modbus_read_holding_registers(portStartAddress + 0x0001, 1, readArray);
|
||||||
while (registerCount != -1 && readArray[0] == 0x700) {
|
while (registerCount != -1 && readArray[0] == 0x700) {
|
||||||
Port port{this->modbus, portStartAddress};
|
Port port{this->modbus, portStartAddress};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@
|
||||||
#include "port.h"
|
#include "port.h"
|
||||||
#include "portParameters.h"
|
#include "portParameters.h"
|
||||||
|
|
||||||
Port::Port(std::shared_ptr<class modbus> modbus, uint16_t portStartAddress) {
|
Port::Port(std::shared_ptr<class modbus> modbus, int portStartAddress) {
|
||||||
this->modbus = modbus;
|
this->modbus = modbus;
|
||||||
|
|
||||||
this->portStartAddress = portStartAddress;
|
this->portStartAddress = portStartAddress;
|
||||||
|
|
|
||||||
|
|
@ -73,7 +73,7 @@ int main(int argc, char **argv) {
|
||||||
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) && (!parametersToGet.empty() || allParameters)) {
|
while (!dtu.modbusError() && (dtu.isConnected() || ignoreNotConnected) && (!parametersToGet.empty() || allParameters)) {
|
||||||
startTime = std::chrono::high_resolution_clock::now();
|
startTime = std::chrono::high_resolution_clock::now();
|
||||||
dtu.updateMicroinverters(parametersToGet, allParameters, microinvertersToGet);
|
dtu.updateMicroinverters(parametersToGet, allParameters, microinvertersToGet);
|
||||||
endTime = std::chrono::high_resolution_clock::now();
|
endTime = std::chrono::high_resolution_clock::now();
|
||||||
|
|
@ -82,6 +82,9 @@ int main(int argc, char **argv) {
|
||||||
dtu.printMicroinverters(parametersToGet, allParameters, microinvertersToGet, shortNames, microinvertersGetTodayProduction, microinvertersGetTotalProduction);
|
dtu.printMicroinverters(parametersToGet, allParameters, microinvertersToGet, shortNames, microinvertersGetTodayProduction, microinvertersGetTotalProduction);
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
}
|
}
|
||||||
|
if(dtu.modbusError()) {
|
||||||
|
std::cerr << dtu.modbusErrorMessage() << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue