hoymilesClient/src/hoymiles/port.cpp

91 lines
2.8 KiB
C++
Raw Normal View History

#include <vector>
#include <string>
#include <iostream>
#include <cmath>
2024-03-16 21:15:15 +01:00
#include <memory>
#include "modbus.h"
#include "port.h"
#include "portParameters.h"
2024-03-16 21:15:15 +01:00
Port::Port(std::shared_ptr<modbus_t*> modbus_context, std::mutex *modbus_context_mutex, uint16_t portStartAddress) {
2024-03-16 21:15:15 +01:00
this->modbus_context = modbus_context;
this->modbus_context_mutex = modbus_context_mutex;
2024-03-16 21:15:15 +01:00
this->portStartAddress = portStartAddress;
2024-03-19 09:22:21 +01:00
this->currentFixed = false;
2024-03-16 21:15:15 +01:00
this->populateParameters();
}
void Port::populateParameters() {
this->parameters.push_back(std::make_shared<PortParameterMicroinverterSerialNumber>());
2024-03-16 21:15:15 +01:00
this->parameters.push_back(std::make_shared<PortParameterPortNumber>());
2024-03-16 21:15:15 +01:00
this->parameters.push_back(std::make_shared<PortParameterPvVoltage>());
2024-03-16 21:15:15 +01:00
this->parameters.push_back(std::make_shared<PortParameterPvCurrentMi>());
2024-03-16 21:15:15 +01:00
this->parameters.push_back(std::make_shared<PortParameterPvCurrentHm>());
2024-03-16 21:15:15 +01:00
this->parameters.push_back(std::make_shared<PortParameterGridVoltage>());
2024-03-16 21:15:15 +01:00
this->parameters.push_back(std::make_shared<PortParameterGridFrequency>());
2024-03-16 21:15:15 +01:00
this->parameters.push_back(std::make_shared<PortParameterPvPower>());
2024-03-16 21:15:15 +01:00
this->parameters.push_back(std::make_shared<PortParameterTodayProduction>());
2024-03-16 21:15:15 +01:00
this->parameters.push_back(std::make_shared<PortParameterTotalProduction>());
2024-03-16 21:15:15 +01:00
this->parameters.push_back(std::make_shared<PortParameterTemperature>());
2024-03-16 21:15:15 +01:00
this->parameters.push_back(std::make_shared<PortParameterOperatingStatus>());
2024-03-16 21:15:15 +01:00
this->parameters.push_back(std::make_shared<PortParameterAlarmCode>());
2024-03-16 21:15:15 +01:00
this->parameters.push_back(std::make_shared<PortParameterAlarmCount>());
this->parameters.push_back(std::make_shared<PortParameterLinkStatus>());
2024-03-16 21:15:15 +01:00
}
2024-03-19 09:22:21 +01:00
void Port::fixCurrent() {
if(this->currentFixed) {
return;
}
if(this->parameters.at(7).get()->getValue().first.f == 0) {
return;
}
if(this->parameters.at(2).get()->getValue().first.f * this->parameters.at(4).get()->getValue().first.f < this->parameters.at(7).get()->getValue().first.f) {
this->parameters.erase(this->parameters.begin() + 4);
}
else {
this->parameters.erase(this->parameters.begin() + 3);
}
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_context, this->modbus_context_mutex, this->portStartAddress);
parametersIterator++;
}
2024-03-19 09:22:21 +01:00
this->fixCurrent();
}
void Port::printParameters() {
std::vector<std::shared_ptr<PortParameter>>::iterator parametersIterator = this->parameters.begin();
if(parametersIterator != this->parameters.end()) {
std::cout << "|";
}
while(parametersIterator != this->parameters.end()) {
std::cout << " " << parametersIterator->get()->name << ": " << parametersIterator->get()->getOutputValue() << " |";
parametersIterator++;
}
2024-03-16 21:15:15 +01:00
}