Оптимизиуем Pzem, нет ошибки при удалении UART

This commit is contained in:
Mit4el
2023-04-03 21:44:07 +03:00
parent ac77b8fb4a
commit 471a9d94bf
2 changed files with 75 additions and 52 deletions

View File

@@ -30,9 +30,10 @@ class PZEMSensor {
bool search();
// Get most up to date values from device registers and cache them
bool refresh();
void updateSerial(Stream *serial) {_serial = serial;}
private:
void init(void);
void init(void);
private:
PZEM_Info _values; // Measured values

View File

@@ -1,23 +1,25 @@
#include "Global.h"
#include "classes/IoTItem.h"
#include "PZEMSensor.h"
#include "modules/sensors/UART/Uart.h"
#include <map>
// глобальные списки необходимы для хранения объектов об экземплярах Pzem . Ключ - адрес
std::map<String, PZEMSensor*> pzemSensorArray;
//PZEMContainer _pzemCntr;
class Pzem004v : public IoTItem {
private:
String addr;
PZEMSensor* pzem;
PZEMSensor* pzem = nullptr;
public:
Pzem004v(String parameters) : IoTItem(parameters) {
addr = jsonReadStr(parameters, "addr");
if (myUART) {
pzem = new PZEMSensor(myUART, hexStringToUint8(addr));
// раскомментируйте эту строку если нужно поменять адрес pzem
// SerialPrint("i", "Pzem", String(pzem->setAddress(0x03)));
}
Pzem004v(PZEMSensor* _pzem, String parameters) : IoTItem(parameters) {
pzem = _pzem;
}
void doByInterval() {
@@ -30,6 +32,9 @@ class Pzem004v : public IoTItem {
regEvent(NAN, "Pzem V");
SerialPrint("E", "Pzem", "V error", _id);
}
} else {
regEvent(NAN, "Pzem V");
SerialPrint("E", "Pzem", "V error", _id);
}
}
@@ -39,14 +44,11 @@ class Pzem004v : public IoTItem {
class Pzem004a : public IoTItem {
private:
String addr;
PZEMSensor* pzem;
PZEMSensor* pzem = nullptr;
public:
Pzem004a(String parameters) : IoTItem(parameters) {
addr = jsonReadStr(parameters, "addr");
if (myUART) {
pzem = new PZEMSensor(myUART, hexStringToUint8(addr));
}
Pzem004a(PZEMSensor* _pzem, String parameters) : IoTItem(parameters) {
pzem = _pzem;
}
void doByInterval() {
@@ -59,6 +61,9 @@ class Pzem004a : public IoTItem {
regEvent(NAN, "Pzem A");
SerialPrint("E", "Pzem", "A error", _id);
}
} else {
regEvent(NAN, "Pzem V");
SerialPrint("E", "Pzem", "V error", _id);
}
}
@@ -68,14 +73,11 @@ class Pzem004a : public IoTItem {
class Pzem004w : public IoTItem {
private:
String addr;
PZEMSensor* pzem;
PZEMSensor* pzem = nullptr;
public:
Pzem004w(String parameters) : IoTItem(parameters) {
addr = jsonReadStr(parameters, "addr");
if (myUART) {
pzem = new PZEMSensor(myUART, hexStringToUint8(addr));
}
Pzem004w(PZEMSensor* _pzem, String parameters) : IoTItem(parameters) {
pzem = _pzem;
}
void doByInterval() {
@@ -88,6 +90,9 @@ class Pzem004w : public IoTItem {
regEvent(NAN, "Pzem W");
SerialPrint("E", "Pzem", "W error", _id);
}
} else {
regEvent(NAN, "Pzem V");
SerialPrint("E", "Pzem", "V error", _id);
}
}
@@ -97,14 +102,11 @@ class Pzem004w : public IoTItem {
class Pzem004wh : public IoTItem {
private:
String addr;
PZEMSensor* pzem;
PZEMSensor* pzem = nullptr;
public:
Pzem004wh(String parameters) : IoTItem(parameters) {
addr = jsonReadStr(parameters, "addr");
if (myUART) {
pzem = new PZEMSensor(myUART, hexStringToUint8(addr));
}
Pzem004wh(PZEMSensor* _pzem, String parameters) : IoTItem(parameters) {
pzem = _pzem;
}
void doByInterval() {
@@ -117,6 +119,9 @@ class Pzem004wh : public IoTItem {
regEvent(NAN, "Pzem Wh");
SerialPrint("E", "Pzem", "Wh error", _id);
}
} else {
regEvent(NAN, "Pzem V");
SerialPrint("E", "Pzem", "V error", _id);
}
}
@@ -126,14 +131,11 @@ class Pzem004wh : public IoTItem {
class Pzem004hz : public IoTItem {
private:
String addr;
PZEMSensor* pzem;
PZEMSensor* pzem = nullptr;
public:
Pzem004hz(String parameters) : IoTItem(parameters) {
addr = jsonReadStr(parameters, "addr");
if (myUART) {
pzem = new PZEMSensor(myUART, hexStringToUint8(addr));
}
Pzem004hz(PZEMSensor* _pzem, String parameters) : IoTItem(parameters) {
pzem = _pzem;
}
void doByInterval() {
@@ -146,6 +148,9 @@ class Pzem004hz : public IoTItem {
regEvent(NAN, "Pzem Hz");
SerialPrint("E", "Pzem", "Hz error", _id);
}
} else {
regEvent(NAN, "Pzem V");
SerialPrint("E", "Pzem", "V error", _id);
}
}
@@ -155,14 +160,11 @@ class Pzem004hz : public IoTItem {
class Pzem004pf : public IoTItem {
private:
String addr;
PZEMSensor* pzem;
PZEMSensor* pzem = nullptr;
public:
Pzem004pf(String parameters) : IoTItem(parameters) {
addr = jsonReadStr(parameters, "addr");
if (myUART) {
pzem = new PZEMSensor(myUART, hexStringToUint8(addr));
}
Pzem004pf(PZEMSensor* _pzem, String parameters) : IoTItem(parameters) {
pzem = _pzem;
}
void doByInterval() {
@@ -175,6 +177,9 @@ class Pzem004pf : public IoTItem {
regEvent(NAN, "Pzem Pf");
SerialPrint("E", "Pzem", "Pf error", _id);
}
} else {
regEvent(NAN, "Pzem V");
SerialPrint("E", "Pzem", "V error", _id);
}
}
@@ -187,17 +192,17 @@ class Pzem004cmd : public IoTItem {
int changeaddr;
String setaddr;
int reset;
PZEMSensor* pzem;
PZEMSensor* pzem = nullptr;
public:
Pzem004cmd(String parameters) : IoTItem(parameters) {
Pzem004cmd(PZEMSensor* _pzem, String parameters) : IoTItem(parameters) {
jsonRead(parameters, F("addr"), addr);
jsonRead(parameters, F("changeaddr"), changeaddr);
jsonRead(parameters, F("setaddr"), setaddr);
jsonRead(parameters, F("reset"), reset);
if (myUART) {
pzem = new PZEMSensor(myUART, hexStringToUint8(addr));
pzem = _pzem;
if (pzem && myUART) {
if (changeaddr == 1) {
if (pzem->setAddress(hexStringToUint8(setaddr))) {
SerialPrint("i", "Pzem", "address set: " + setaddr);
@@ -212,7 +217,7 @@ class Pzem004cmd : public IoTItem {
SerialPrint("i", "Pzem", "reset error");
}
}
}
} else SerialPrint("i", "Pzem", "Pzem command error");
}
void doByInterval() {
@@ -224,20 +229,37 @@ class Pzem004cmd : public IoTItem {
};
void* getAPI_Pzem004(String subtype, String param) {
PZEMSensor *pzem = nullptr;
if (myUART) {
if (subtype == F("Pzem004v") || subtype == F("Pzem004a") || subtype == F("Pzem004w")
|| subtype == F("Pzem004wh") || subtype == F("Pzem004hz") || subtype == F("Pzem004pf")
|| subtype == F("Pzem004cmd") ) {
String addr;
jsonRead(param, "addr", addr);
if (pzemSensorArray.find(addr) == pzemSensorArray.end()) {
// pzem = new PZEMSensor(myUART, hexStringToUint8(addr));
pzemSensorArray[addr] = new PZEMSensor(myUART, hexStringToUint8(addr));
} else {
pzemSensorArray[addr]->updateSerial(myUART);
}
pzem = pzemSensorArray[addr];
}
}
if (subtype == F("Pzem004v")) {
return new Pzem004v(param);
return new Pzem004v(pzem, param);
} else if (subtype == F("Pzem004a")) {
return new Pzem004a(param);
return new Pzem004a(pzem, param);
} else if (subtype == F("Pzem004w")) {
return new Pzem004w(param);
return new Pzem004w(pzem, param);
} else if (subtype == F("Pzem004wh")) {
return new Pzem004wh(param);
return new Pzem004wh(pzem, param);
} else if (subtype == F("Pzem004hz")) {
return new Pzem004hz(param);
return new Pzem004hz(pzem, param);
} else if (subtype == F("Pzem004pf")) {
return new Pzem004pf(param);
return new Pzem004pf(pzem, param);
} else if (subtype == F("Pzem004cmd")) {
return new Pzem004cmd(param);
return new Pzem004cmd(pzem, param);
} else {
return nullptr;
}