Polymorphism "works"
This commit is contained in:
parent
b0e50c140e
commit
d0f8e5b885
10 changed files with 274 additions and 287 deletions
40
src/hoymiles/dtu.cpp
Normal file
40
src/hoymiles/dtu.cpp
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
#include "modbus.h"
|
||||
|
||||
#include "dtu.h"
|
||||
#include "microinverter.h"
|
||||
|
||||
struct _modbus;
|
||||
typedef _modbus modbus_t;
|
||||
|
||||
Dtu::Dtu(const char *ip_address, int port) {
|
||||
this->modbus_context = modbus_new_tcp(ip_address, port);
|
||||
if (modbus_connect(this->modbus_context) == -1) {
|
||||
std::cerr << "conn_error";
|
||||
modbus_free(this->modbus_context);
|
||||
abort();
|
||||
}
|
||||
|
||||
this->populateMicroinverters();
|
||||
}
|
||||
|
||||
Dtu::~Dtu() {
|
||||
modbus_close(this->modbus_context);
|
||||
modbus_free(this->modbus_context);
|
||||
}
|
||||
|
||||
void Dtu::populateMicroinverters() {
|
||||
uint16_t address{0x1000};
|
||||
Microinverter microinverter{this->modbus_context};
|
||||
this->microinverters.push_back(microinverter);
|
||||
}
|
||||
|
||||
void Dtu::updateMicroinverters() {
|
||||
std::vector<Microinverter>::iterator microinvertersIterator = this->microinverters.begin();
|
||||
while (microinvertersIterator != this->microinverters.end()) {
|
||||
microinvertersIterator->updatePorts();
|
||||
microinvertersIterator++;
|
||||
}
|
||||
}
|
||||
20
src/hoymiles/microinverter.cpp
Normal file
20
src/hoymiles/microinverter.cpp
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
#include "microinverter.h"
|
||||
#include "port.h"
|
||||
|
||||
Microinverter::Microinverter(modbus_t *modbus_context) {
|
||||
this->modbus_context = modbus_context;
|
||||
|
||||
this->populatePorts();
|
||||
}
|
||||
|
||||
void Microinverter::populatePorts() {
|
||||
Port port{this->modbus_context, 0x1000};
|
||||
|
||||
this->ports.push_back(port);
|
||||
}
|
||||
|
||||
void Microinverter::updatePorts() {
|
||||
for(Port port : this->ports){
|
||||
port.updateParameters();
|
||||
}
|
||||
}
|
||||
78
src/hoymiles/port.cpp
Normal file
78
src/hoymiles/port.cpp
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
#include <vector>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
#include <memory>
|
||||
|
||||
#include "modbus.h"
|
||||
|
||||
#include "port.h"
|
||||
|
||||
Port::Port(modbus_t *modbus_context, uint16_t portStartAddress) {
|
||||
this->modbus_context = modbus_context;
|
||||
this->portStartAddress = portStartAddress;
|
||||
|
||||
this->populateParameters();
|
||||
}
|
||||
|
||||
void Port::populateParameters() {
|
||||
PortParameterFloat portParameterFloat{"gridVoltage", 1, 0x001a, 2};
|
||||
this->parameters.push_back(std::make_shared<PortParameterFloat>(portParameterFloat));
|
||||
}
|
||||
|
||||
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->portStartAddress);
|
||||
parametersIterator++;
|
||||
}
|
||||
}
|
||||
|
||||
PortParameter::PortParameter(std::string name, uint16_t parameterAddressOffset, int registerSize) {
|
||||
this->name = name;
|
||||
|
||||
this->parameterAddressOffset = parameterAddressOffset;
|
||||
this->registerSize = registerSize;
|
||||
}
|
||||
|
||||
PortParameter::~PortParameter() {}
|
||||
|
||||
void PortParameter::setValueFromRegisters(uint16_t *readArray, int registerCount) {}
|
||||
|
||||
std::pair<PortParameter::PortParameterValue, PortParameter::PortParameterValueType> PortParameter::getValue() {
|
||||
return std::pair<PortParameter::PortParameterValue, PortParameter::PortParameterValueType>(this->value, this->valueType);
|
||||
}
|
||||
|
||||
std::string PortParameter::getOutputValue() {}
|
||||
|
||||
void PortParameter::updateValue(modbus_t *modbus_context, uint16_t portStartAddress) {
|
||||
uint16_t readArray[this->registerSize];
|
||||
int registerCount;
|
||||
registerCount = modbus_read_registers(modbus_context, portStartAddress + this->parameterAddressOffset, this->registerSize, readArray);
|
||||
|
||||
registerCount = 2;
|
||||
readArray[0] = 2312;
|
||||
readArray[1] = 5432;
|
||||
|
||||
if(registerCount == -1){
|
||||
std::cerr << "read_error";
|
||||
return;
|
||||
}
|
||||
else{
|
||||
this->setValueFromRegisters(readArray, registerCount);
|
||||
}
|
||||
}
|
||||
|
||||
PortParameterFloat::PortParameterFloat(std::string name, int decimalPlaces, uint16_t parameterAddressOffset, int registerSize) : PortParameter(name, parameterAddressOffset, registerSize) {
|
||||
this->decimalPlaces = decimalPlaces;
|
||||
|
||||
this->valueType = Float;
|
||||
}
|
||||
|
||||
void PortParameterFloat::setValueFromRegisters(uint16_t *readArray, int registerCount) {
|
||||
this->value.f = readArray[0] / std::pow(10, this->decimalPlaces);
|
||||
}
|
||||
|
||||
std::string PortParameterFloat::getOutputValue() {
|
||||
return std::to_string(this->value.f);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue