Оптимизируем Dht1122

This commit is contained in:
2022-02-02 11:05:59 +03:00
parent ad9679314c
commit b2e20f6760

View File

@@ -16,15 +16,15 @@ std::map<int, DHTesp*> dhts;
class Dht1122t : public IoTSensor { class Dht1122t : public IoTSensor {
private: private:
int _pin; DHTesp* _dht;
public: public:
Dht1122t(String parameters): IoTSensor(parameters) { Dht1122t(DHTesp* dht, String parameters): IoTSensor(parameters) {
jsonRead(parameters, "pin", _pin); _dht = dht;
} }
void doByInterval() { void doByInterval() {
float value = dhts[_pin]->getTemperature(); float value = _dht->getTemperature();
if (String(value) != "nan") regEvent(value, "Dht1122t"); if (String(value) != "nan") regEvent(value, "Dht1122t");
else SerialPrint("E", "Sensor DHTt", "Error"); else SerialPrint("E", "Sensor DHTt", "Error");
} }
@@ -35,15 +35,15 @@ class Dht1122t : public IoTSensor {
class Dht1122h : public IoTSensor { class Dht1122h : public IoTSensor {
private: private:
int _pin; DHTesp* _dht;
public: public:
Dht1122h(String parameters): IoTSensor(parameters) { Dht1122h(DHTesp* dht, String parameters): IoTSensor(parameters) {
jsonRead(parameters, "pin", _pin); _dht = dht;
} }
void doByInterval() { void doByInterval() {
float value = dhts[_pin]->getHumidity(); float value = _dht->getHumidity();
if (String(value) != "nan") regEvent(value, "Dht1122h"); if (String(value) != "nan") regEvent(value, "Dht1122h");
else SerialPrint("E", "Sensor DHTh", "Error"); else SerialPrint("E", "Sensor DHTh", "Error");
} }
@@ -59,21 +59,19 @@ void* getAPI_Dht1122(String subtype, String param) {
jsonRead(param, "senstype", senstype); jsonRead(param, "senstype", senstype);
if (dhts.find(pin) == dhts.end()) { if (dhts.find(pin) == dhts.end()) {
DHTesp* dht = new DHTesp(); dhts[pin] = new DHTesp();
if (senstype == "dht11") { if (senstype == "dht11") {
dht->setup(pin, DHTesp::DHT11); dhts[pin]->setup(pin, DHTesp::DHT11);
} else if (senstype == "dht22") { } else if (senstype == "dht22") {
dht->setup(pin, DHTesp::DHT22); dhts[pin]->setup(pin, DHTesp::DHT22);
} }
dhts[pin] = dht;
} }
if (subtype == F("Dht1122t")) { if (subtype == F("Dht1122t")) {
return new Dht1122t(param); return new Dht1122t(dhts[pin], param);
} else if (subtype == F("Dht1122h")) { } else if (subtype == F("Dht1122h")) {
return new Dht1122h(param); return new Dht1122h(dhts[pin], param);
} else { } else {
return nullptr; return nullptr;
} }