Polymorphism "works"

This commit is contained in:
TraYali 2024-03-16 21:15:15 +01:00
parent b0e50c140e
commit d0f8e5b885
10 changed files with 274 additions and 287 deletions

View file

@ -1,132 +0,0 @@
#ifndef HOYMILES_H
#define HOYMILES_H
#include <stdint.h>
#include <string>
#include <vector>
#include <memory>
struct _modbus;
typedef _modbus modbus_t;
class PortParameter {
protected:
uint16_t addressOffset;
int registerSize;
virtual void setValue(uint16_t *readArray, int registerCount);
public:
PortParameter(std::string name, uint16_t addressOffset, int registerSize);
std::string name;
int age;
virtual void updateValue(modbus_t *modbus_context, uint16_t portStartAddress);
};
template<typename Type>
class PortParameterTemplate : virtual public PortParameter {
public:
PortParameterTemplate(std::string name, uint16_t addressOffset, int registerSize);
Type value;
Type getValue();
};
class PortParameterInt : public PortParameterTemplate<int> {
private:
void setValue(uint16_t *readArray, int registerCount);
public:
PortParameterInt();
};
class PortParameterSerialNumber : public PortParameterTemplate<std::string> {
private:
void setValue(uint16_t *readArray, int registerCount);
public:
PortParameterSerialNumber();
};
class PortParameterFloat : public PortParameterTemplate<float> {
private:
int decimalPlaces;
void setValue(uint16_t *readArray, int registerCount);
public:
PortParameterFloat(std::string name, uint16_t addressOffset, int registerSize, int decimalPlaces);
float value;
};
class Port {
private:
modbus_t *modbus_context;
uint16_t portStartAddress;
std::vector<std::shared_ptr<PortParameter>> parameters;
void populateParameters();
public:
Port(modbus_t *modbus_context, uint16_t portStartAddress);
void updateParameters();
std::shared_ptr<PortParameter> getParameterById(int i);
std::shared_ptr<PortParameter> getParameterByName(std::string name);
};
class Microinverter {
private:
modbus_t *modbus_context;
std::vector<Port> ports;
void populatePorts();
public:
Microinverter(modbus_t *modbus_t);
void updatePorts();
void updatePort(int i);
Port getPort(int i);
};
class Dtu {
private:
modbus_t *modbus_context;
std::vector<Microinverter> microinverters;
void populateMicroinverters();
public:
Dtu(const char *ip_address, int port);
void updateMicroinverters();
~Dtu();
};
#endif

27
inc/hoymiles/dtu.h Normal file
View file

@ -0,0 +1,27 @@
#ifndef DTU_H
#define DTU_H
#include <vector>
#include "microinverter.h"
struct _modbus;
typedef _modbus modbus_t;
class Dtu {
private:
modbus_t *modbus_context;
std::vector<Microinverter> microinverters;
void populateMicroinverters();
public:
Dtu(const char *ip_address, int port);
void updateMicroinverters();
~Dtu();
};
#endif

View file

@ -0,0 +1,29 @@
#ifndef MICROINVERTER_H
#define MICROINVERTER_H
#include <vector>
#include "port.h"
struct _modbus;
typedef _modbus modbus_t;
class Microinverter {
private:
modbus_t *modbus_context;
std::vector<Port> ports;
void populatePorts();
public:
Microinverter(modbus_t *modbus_t);
void updatePorts();
void updatePort(int i);
Port getPort(int i);
};
#endif

77
inc/hoymiles/port.h Normal file
View file

@ -0,0 +1,77 @@
#ifndef PORT_H
#define PORT_H
#include <stdint.h>
#include <string>
#include <vector>
#include <memory>
struct _modbus;
typedef _modbus modbus_t;
class PortParameter {
protected:
uint16_t parameterAddressOffset;
int registerSize;
virtual void setValueFromRegisters(uint16_t *readArray, int registerCount);
public:
PortParameter(std::string name, uint16_t parameterAddressOffset, int registerSize);
virtual ~PortParameter();
enum PortParameterValueType { Int, Float };
union PortParameterValue {
int i;
float f;
};
protected:
PortParameterValueType valueType;
PortParameterValue value;
public:
std::string name;
std::pair<PortParameterValue, PortParameterValueType> getValue();
virtual std::string getOutputValue();
void updateValue(modbus_t *modbus_context, uint16_t portStartAddress);
};
class PortParameterFloat : public PortParameter {
protected:
int decimalPlaces;
void setValueFromRegisters(uint16_t *readArray, int registerCount);
public:
PortParameterFloat(std::string name, int decimalPlaces, uint16_t parameterAddressOffset, int registerSize);
virtual std::string getOutputValue();
};
class PortParameterInt : public PortParameter {
protected:
void setValueFromRegisters()
};
class Port {
private:
modbus_t *modbus_context;
uint16_t portStartAddress;
std::vector<std::shared_ptr<PortParameter>> parameters;
void populateParameters();
public:
Port(modbus_t *modbus_context, uint16_t portStartAddress);
void updateParameters();
};
#endif