Initial lib commit with cmake

This commit is contained in:
TraYali 2024-04-10 18:25:36 +02:00
commit e39d6cda18
31 changed files with 7172 additions and 0 deletions

41
inc/dtu.h Normal file
View file

@ -0,0 +1,41 @@
#ifndef DTU_H
#define DTU_H
#include <memory>
#include <string>
#include <vector>
#include "microinverter.h"
#include "modbus.h"
class Dtu {
private:
modbus_t *modbus;
std::vector<Microinverter> microinverters;
bool connected;
void populateMicroinverters();
public:
Dtu(const char *address, int id, bool rtu, bool tcp);
std::pair<Microinverter *, bool> getMicroinverterBySerialNumber(long long serialNumber);
bool isConnected();
void updateMicroinverters(std::vector<std::string> &parametersToGet, bool allParameters, std::vector<long long> &microinvertersToGet);
void printMicroinverters(std::vector<std::string> &parametersToGet, bool allParameters, std::vector<long long> &microinvertersToGet, bool shortNames, bool printTodayProduction, bool printTotalProduction);
void setStatusMicroinverters(uint16_t value, std::string statusName, std::vector<long long>& microinvertersToSet);
bool empty();
void listOfMicroinverters();
~Dtu();
};
#endif

48
inc/microinverter.h Normal file
View file

@ -0,0 +1,48 @@
#ifndef MICROINVERTER_H
#define MICROINVERTER_H
#include <memory>
#include <string>
#include <vector>
#include "modbus.h"
#include "port.h"
#include "sunspec.h"
class Microinverter {
private:
modbus_t *modbus;
Sunspec sunspec;
int startAddress;
int statusStartAddress;
public:
Microinverter(modbus_t *modbus, int startAddress, long long serialNumber);
long long serialNumber;
int age;
std::vector<Port> ports;
// void updatePorts(std::vector<std::string> &parametersToGet, bool allParameters);
void updateParameters(std::vector<std::string> &parametersToGet, bool allParameters);
void updateStatusParameters();
void printPorts(std::vector<std::string> &parametersToGet, bool allParameters, bool shortNames);
long long getTodayProduction();
long long getTotalProduction();
void setStatus(std::vector<std::pair<int, uint16_t>> portsToSet, std::string statusName);
void setStatusWholeMicroinverter(uint16_t value, std::string statusName);
};
#endif

49
inc/port.h Normal file
View file

@ -0,0 +1,49 @@
#ifndef PORT_H
#define PORT_H
#include <stdint.h>
#include <string>
#include <vector>
#include "portParametersGeneric.h"
#include "portParameters.h"
#include "modbus.h"
class Port {
private:
void populateParameters();
void fixCurrent();
bool currentFixed;
// void increaseParametersAge();
public:
Port(int portStartAddress);
int portStartAddress;
int statusPortStartAddress;
std::vector<std::shared_ptr<PortParameter>> parameters;
std::vector<std::shared_ptr<PortParameter>> statusParameters;
std::pair<std::shared_ptr<PortParameter>, bool> getParameterByName(std::string name);
std::pair<std::shared_ptr<PortParameter>, bool> getStatusByName(std::string name);
// void updateParameters(std::vector<std::string> &parametersToGet, bool allParameters);
void setParametersFromMicroinverterArray(uint16_t *registers, int addressOffset);
void setStatusesFromMicroinverterArray(uint16_t *registers, int addressOffset);
void printParameters(std::vector<std::string> &parametersToGet, bool allParameters, bool shortNames);
void turnOff(modbus_t *modbus);
bool isOff(modbus_t *modbus);
};
#endif

View file

@ -0,0 +1,94 @@
#ifndef PORTPARAMETERS_H
#define PORTPARAMETERS_H
#include "portParametersGeneric.h"
class PortParameterMicroinverterSerialNumber : public PortParameterInt {
private:
void getValueFromRegisters(uint16_t *registers, int addressOffset);
public:
PortParameterMicroinverterSerialNumber();
};
class PortParameterPortNumber : public PortParameterInt {
public:
PortParameterPortNumber();
};
class PortParameterPvVoltage : public PortParameterFloat {
public:
PortParameterPvVoltage();
};
class PortParameterPvCurrentMi : public PortParameterFloat {
public:
PortParameterPvCurrentMi();
};
class PortParameterPvCurrentHm : public PortParameterFloat {
public:
PortParameterPvCurrentHm();
};
class PortParameterGridVoltage : public PortParameterFloat {
public:
PortParameterGridVoltage();
};
class PortParameterGridFrequency : public PortParameterFloat {
public:
PortParameterGridFrequency();
};
class PortParameterPvPower : public PortParameterFloat {
public:
PortParameterPvPower();
};
class PortParameterTodayProduction : public PortParameterInt {
public:
PortParameterTodayProduction();
};
class PortParameterTotalProduction : public PortParameterInt {
public:
PortParameterTotalProduction();
};
class PortParameterTemperature : public PortParameterFloat {
public:
PortParameterTemperature();
};
class PortParameterOperatingStatus : public PortParameterInt {
public:
PortParameterOperatingStatus();
};
class PortParameterAlarmCode : public PortParameterInt {
public:
PortParameterAlarmCode();
};
class PortParameterAlarmCount : public PortParameterInt {
public:
PortParameterAlarmCount();
};
class PortParameterLinkStatus : public PortParameterInt {
public:
PortParameterLinkStatus();
};
class PortParameterOnOff : public PortParameterInt {
public:
PortParameterOnOff();
};
class PortParameterLimitActivePower : public PortParameterInt {
public:
PortParameterLimitActivePower();
};
#endif

View file

@ -0,0 +1,77 @@
#ifndef PORTPARAMETERSGENERIC_H
#define PORTPARAMETERSGENERIC_H
#include <memory>
#include <stdint.h>
#include <string>
struct _modbus;
typedef _modbus modbus_t;
class PortParameter {
protected:
uint16_t parameterAddressOffset;
int registerSize;
std::string unit;
bool r;
bool w;
public:
PortParameter(std::string name, std::string shortName, std::string unit, bool r, bool w, uint16_t parameterAddressOffset, int registerSize);
virtual ~PortParameter();
enum PortParameterValueType { Int, Float };
union PortParameterValue {
long long i;
float f;
};
protected:
PortParameterValueType valueType;
PortParameterValue value;
public:
std::string name;
std::string shortName;
// int age;
std::pair<PortParameterValue, PortParameterValueType> getValue();
PortParameter& writeValue(uint16_t value, modbus_t *modbus, int portStartAddress);
virtual std::string getOutputValue();
virtual void getValueFromRegisters(uint16_t *registers, int addressOffset);
// void updateValue(std::shared_ptr<class modbus> modubs, uint16_t portStartAddress);
};
class PortParameterFloat : public PortParameter {
protected:
int decimalPlaces;
public:
PortParameterFloat(std::string name, std::string shortName, std::string unit, bool r, bool w, int decimalPlaces, uint16_t parameterAddressOffset, int registerSize);
std::string getOutputValue();
virtual void getValueFromRegisters(uint16_t *registers, int addressOffset);
};
class PortParameterInt : public PortParameter {
protected:
public:
PortParameterInt(std::string name, std::string shortName, std::string unit, bool r, bool w, uint16_t parameterAddressOffset, int registerSize);
std::string getOutputValue();
virtual void getValueFromRegisters(uint16_t *registers, int addressOffset);
};
#endif

37
inc/sunspec.h Normal file
View file

@ -0,0 +1,37 @@
#ifndef SUNSPEC_H
#define SUNSPEC_H
#include <string>
#include <stdint.h>
#include <memory>
#include <vector>
#include "modbus.h"
#include "sunspecParametersGeneric.h"
class Sunspec {
protected:
modbus_t *modbus;
std::vector<std::shared_ptr<SunspecParameter>> parameters;
std::string manufacturer;
std::string model;
std::string version;
std::string serialNumber;
uint16_t deviceAddress;
int sunspecAddress;
void setValues();
void populateParameters();
public:
Sunspec(int address, modbus_t *modbus);
uint16_t getAddress();
};
#endif

View file

@ -0,0 +1,15 @@
#ifndef SUNSPEC_PARAMETERS_H
#define SUNSPEC_PARAMETERS_H
#include "sunspecParametersGeneric.h"
class SunspecParameterManufacturer : public SunspecParameterString32 {
protected:
public:
SunspecParameterManufacturer();
void getValueFromRegisters(uint16_t *registers, int addressOffset);
};
#endif

View file

@ -0,0 +1,124 @@
#ifndef SUNSPEC_PARAMETERS_GENERIC_H
#define SUNSPEC_PARAMETERS_GENERIC_H
#include <stdint.h>
#include <string>
class SunspecParameter {
protected:
std::string name;
int registerAddressOffset;
int registerSize;
public:
SunspecParameter(std::string name, int registerAddressOffset, int registerSize);
virtual ~SunspecParameter();
enum SunspecValueType { uint32, uint16, string32, string16, sunssf, int16, acc32, float32, enum16 };
union SunspecValue {
uint32_t u32;
uint16_t u16;
uint16_t ssf;
int i16;
uint16_t a32;
float f;
uint16_t e16;
};
protected:
SunspecValueType valueType;
SunspecValue value;
public:
virtual void getValueFromRegisters(uint16_t *registers, int addressOffset);
std::pair<SunspecValue, SunspecValueType> getValue();
};
// class SunspecParameterUint32 : public SunspecParameter {
// protected:
// public:
// SunspecParameterUint32(std::string name, int registerAddressOffset, int registerSize);
// virtual void getValueFromRegisters(uint16_t *registers, int addressOffset);
// };
// class SunspecParameterUint16 : public SunspecParameter {
// protected:
// public:
// SunspecParameterUint16(std::string name, int registerAddressOffset, int registerSize);
// virtual void getValueFromRegisters(uint16_t *registers, int addressOffset);
// };
class SunspecParameterString32 : public SunspecParameter {
protected:
std::string value;
public:
SunspecParameterString32(std::string name, int registerAddressOffset, int registerSize);
virtual void getValueFromRegisters(uint16_t *registers, int addressOffset);
};
// class SunspecParameterString16 : public SunspecParameter {
// protected:
// std::string value;
// public:
// SunspecParameterString16(std::string name, int registerAddressOffset, int registerSize);
// virtual void getValueFromRegisters(uint16_t *registers, int addressOffset);
// };
// class SunspecParameterSunssf : public SunspecParameter {
// protected:
// public:
// SunspecParameterSunssf(std::string name, int registerAddressOffset, int registerSize);
// virtual void getValueFromRegisters(uint16_t *registers, int addressOffset);
// };
// class SunspecParameterInt16 : public SunspecParameter {
// protected:
// public:
// SunspecParameterInt16(std::string name, int registerAddressOffset, int registerSize);
// virtual void getValueFromRegisters(uint16_t *registers, int addressOffset);
// };
// class SunspecParameterAcc32 : public SunspecParameter {
// protected:
// public:
// SunspecParameterAcc32(std::string name, int registerAddressOffset, int registerSize);
// virtual void getValueFromRegisters(uint16_t *registers, int addressOffset);
// };
// class SunspecParameterFloat32 : public SunspecParameter {
// protected:
// public:
// SunspecParameterFloat32(std::string name, int registerAddressOffset, int registerSize);
// virtual void getValueFromRegisters(uint16_t *registers, int addressOffset);
// };
// class SunspecParameterEnum16 : public SunspecParameter {
// protected:
// public:
// SunspecParameterEnum16(std::string name, int registerAddressOffset, int registerSize);
// virtual void getValueFromRegisters(uint16_t *registers, int addressOffset);
// };
#endif