mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-26 22:22:16 +03:00
Оптимизиуем Pzem, нет ошибки при удалении UART
This commit is contained in:
@@ -30,9 +30,10 @@ class PZEMSensor {
|
|||||||
bool search();
|
bool search();
|
||||||
// Get most up to date values from device registers and cache them
|
// Get most up to date values from device registers and cache them
|
||||||
bool refresh();
|
bool refresh();
|
||||||
|
void updateSerial(Stream *serial) {_serial = serial;}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void init(void);
|
void init(void);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
PZEM_Info _values; // Measured values
|
PZEM_Info _values; // Measured values
|
||||||
|
|||||||
@@ -1,23 +1,25 @@
|
|||||||
|
|
||||||
#include "Global.h"
|
#include "Global.h"
|
||||||
#include "classes/IoTItem.h"
|
#include "classes/IoTItem.h"
|
||||||
|
|
||||||
#include "PZEMSensor.h"
|
#include "PZEMSensor.h"
|
||||||
#include "modules/sensors/UART/Uart.h"
|
#include "modules/sensors/UART/Uart.h"
|
||||||
|
#include <map>
|
||||||
|
|
||||||
|
// глобальные списки необходимы для хранения объектов об экземплярах Pzem . Ключ - адрес
|
||||||
|
std::map<String, PZEMSensor*> pzemSensorArray;
|
||||||
|
//PZEMContainer _pzemCntr;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Pzem004v : public IoTItem {
|
class Pzem004v : public IoTItem {
|
||||||
private:
|
private:
|
||||||
String addr;
|
String addr;
|
||||||
PZEMSensor* pzem;
|
PZEMSensor* pzem = nullptr;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Pzem004v(String parameters) : IoTItem(parameters) {
|
Pzem004v(PZEMSensor* _pzem, String parameters) : IoTItem(parameters) {
|
||||||
addr = jsonReadStr(parameters, "addr");
|
pzem = _pzem;
|
||||||
if (myUART) {
|
|
||||||
pzem = new PZEMSensor(myUART, hexStringToUint8(addr));
|
|
||||||
// раскомментируйте эту строку если нужно поменять адрес pzem
|
|
||||||
// SerialPrint("i", "Pzem", String(pzem->setAddress(0x03)));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void doByInterval() {
|
void doByInterval() {
|
||||||
@@ -30,6 +32,9 @@ class Pzem004v : public IoTItem {
|
|||||||
regEvent(NAN, "Pzem V");
|
regEvent(NAN, "Pzem V");
|
||||||
SerialPrint("E", "Pzem", "V error", _id);
|
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 {
|
class Pzem004a : public IoTItem {
|
||||||
private:
|
private:
|
||||||
String addr;
|
String addr;
|
||||||
PZEMSensor* pzem;
|
PZEMSensor* pzem = nullptr;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Pzem004a(String parameters) : IoTItem(parameters) {
|
Pzem004a(PZEMSensor* _pzem, String parameters) : IoTItem(parameters) {
|
||||||
addr = jsonReadStr(parameters, "addr");
|
pzem = _pzem;
|
||||||
if (myUART) {
|
|
||||||
pzem = new PZEMSensor(myUART, hexStringToUint8(addr));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void doByInterval() {
|
void doByInterval() {
|
||||||
@@ -59,6 +61,9 @@ class Pzem004a : public IoTItem {
|
|||||||
regEvent(NAN, "Pzem A");
|
regEvent(NAN, "Pzem A");
|
||||||
SerialPrint("E", "Pzem", "A error", _id);
|
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 {
|
class Pzem004w : public IoTItem {
|
||||||
private:
|
private:
|
||||||
String addr;
|
String addr;
|
||||||
PZEMSensor* pzem;
|
PZEMSensor* pzem = nullptr;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Pzem004w(String parameters) : IoTItem(parameters) {
|
Pzem004w(PZEMSensor* _pzem, String parameters) : IoTItem(parameters) {
|
||||||
addr = jsonReadStr(parameters, "addr");
|
pzem = _pzem;
|
||||||
if (myUART) {
|
|
||||||
pzem = new PZEMSensor(myUART, hexStringToUint8(addr));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void doByInterval() {
|
void doByInterval() {
|
||||||
@@ -88,6 +90,9 @@ class Pzem004w : public IoTItem {
|
|||||||
regEvent(NAN, "Pzem W");
|
regEvent(NAN, "Pzem W");
|
||||||
SerialPrint("E", "Pzem", "W error", _id);
|
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 {
|
class Pzem004wh : public IoTItem {
|
||||||
private:
|
private:
|
||||||
String addr;
|
String addr;
|
||||||
PZEMSensor* pzem;
|
PZEMSensor* pzem = nullptr;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Pzem004wh(String parameters) : IoTItem(parameters) {
|
Pzem004wh(PZEMSensor* _pzem, String parameters) : IoTItem(parameters) {
|
||||||
addr = jsonReadStr(parameters, "addr");
|
pzem = _pzem;
|
||||||
if (myUART) {
|
|
||||||
pzem = new PZEMSensor(myUART, hexStringToUint8(addr));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void doByInterval() {
|
void doByInterval() {
|
||||||
@@ -117,6 +119,9 @@ class Pzem004wh : public IoTItem {
|
|||||||
regEvent(NAN, "Pzem Wh");
|
regEvent(NAN, "Pzem Wh");
|
||||||
SerialPrint("E", "Pzem", "Wh error", _id);
|
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 {
|
class Pzem004hz : public IoTItem {
|
||||||
private:
|
private:
|
||||||
String addr;
|
String addr;
|
||||||
PZEMSensor* pzem;
|
PZEMSensor* pzem = nullptr;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Pzem004hz(String parameters) : IoTItem(parameters) {
|
Pzem004hz(PZEMSensor* _pzem, String parameters) : IoTItem(parameters) {
|
||||||
addr = jsonReadStr(parameters, "addr");
|
pzem = _pzem;
|
||||||
if (myUART) {
|
|
||||||
pzem = new PZEMSensor(myUART, hexStringToUint8(addr));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void doByInterval() {
|
void doByInterval() {
|
||||||
@@ -146,6 +148,9 @@ class Pzem004hz : public IoTItem {
|
|||||||
regEvent(NAN, "Pzem Hz");
|
regEvent(NAN, "Pzem Hz");
|
||||||
SerialPrint("E", "Pzem", "Hz error", _id);
|
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 {
|
class Pzem004pf : public IoTItem {
|
||||||
private:
|
private:
|
||||||
String addr;
|
String addr;
|
||||||
PZEMSensor* pzem;
|
PZEMSensor* pzem = nullptr;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Pzem004pf(String parameters) : IoTItem(parameters) {
|
Pzem004pf(PZEMSensor* _pzem, String parameters) : IoTItem(parameters) {
|
||||||
addr = jsonReadStr(parameters, "addr");
|
pzem = _pzem;
|
||||||
if (myUART) {
|
|
||||||
pzem = new PZEMSensor(myUART, hexStringToUint8(addr));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void doByInterval() {
|
void doByInterval() {
|
||||||
@@ -175,6 +177,9 @@ class Pzem004pf : public IoTItem {
|
|||||||
regEvent(NAN, "Pzem Pf");
|
regEvent(NAN, "Pzem Pf");
|
||||||
SerialPrint("E", "Pzem", "Pf error", _id);
|
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;
|
int changeaddr;
|
||||||
String setaddr;
|
String setaddr;
|
||||||
int reset;
|
int reset;
|
||||||
PZEMSensor* pzem;
|
PZEMSensor* pzem = nullptr;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Pzem004cmd(String parameters) : IoTItem(parameters) {
|
Pzem004cmd(PZEMSensor* _pzem, String parameters) : IoTItem(parameters) {
|
||||||
jsonRead(parameters, F("addr"), addr);
|
jsonRead(parameters, F("addr"), addr);
|
||||||
jsonRead(parameters, F("changeaddr"), changeaddr);
|
jsonRead(parameters, F("changeaddr"), changeaddr);
|
||||||
jsonRead(parameters, F("setaddr"), setaddr);
|
jsonRead(parameters, F("setaddr"), setaddr);
|
||||||
jsonRead(parameters, F("reset"), reset);
|
jsonRead(parameters, F("reset"), reset);
|
||||||
|
|
||||||
if (myUART) {
|
pzem = _pzem;
|
||||||
pzem = new PZEMSensor(myUART, hexStringToUint8(addr));
|
if (pzem && myUART) {
|
||||||
if (changeaddr == 1) {
|
if (changeaddr == 1) {
|
||||||
if (pzem->setAddress(hexStringToUint8(setaddr))) {
|
if (pzem->setAddress(hexStringToUint8(setaddr))) {
|
||||||
SerialPrint("i", "Pzem", "address set: " + setaddr);
|
SerialPrint("i", "Pzem", "address set: " + setaddr);
|
||||||
@@ -212,7 +217,7 @@ class Pzem004cmd : public IoTItem {
|
|||||||
SerialPrint("i", "Pzem", "reset error");
|
SerialPrint("i", "Pzem", "reset error");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} else SerialPrint("i", "Pzem", "Pzem command error");
|
||||||
}
|
}
|
||||||
|
|
||||||
void doByInterval() {
|
void doByInterval() {
|
||||||
@@ -224,20 +229,37 @@ class Pzem004cmd : public IoTItem {
|
|||||||
};
|
};
|
||||||
|
|
||||||
void* getAPI_Pzem004(String subtype, String param) {
|
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")) {
|
if (subtype == F("Pzem004v")) {
|
||||||
return new Pzem004v(param);
|
return new Pzem004v(pzem, param);
|
||||||
} else if (subtype == F("Pzem004a")) {
|
} else if (subtype == F("Pzem004a")) {
|
||||||
return new Pzem004a(param);
|
return new Pzem004a(pzem, param);
|
||||||
} else if (subtype == F("Pzem004w")) {
|
} else if (subtype == F("Pzem004w")) {
|
||||||
return new Pzem004w(param);
|
return new Pzem004w(pzem, param);
|
||||||
} else if (subtype == F("Pzem004wh")) {
|
} else if (subtype == F("Pzem004wh")) {
|
||||||
return new Pzem004wh(param);
|
return new Pzem004wh(pzem, param);
|
||||||
} else if (subtype == F("Pzem004hz")) {
|
} else if (subtype == F("Pzem004hz")) {
|
||||||
return new Pzem004hz(param);
|
return new Pzem004hz(pzem, param);
|
||||||
} else if (subtype == F("Pzem004pf")) {
|
} else if (subtype == F("Pzem004pf")) {
|
||||||
return new Pzem004pf(param);
|
return new Pzem004pf(pzem, param);
|
||||||
} else if (subtype == F("Pzem004cmd")) {
|
} else if (subtype == F("Pzem004cmd")) {
|
||||||
return new Pzem004cmd(param);
|
return new Pzem004cmd(pzem, param);
|
||||||
} else {
|
} else {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user