89 lines
2.1 KiB
C++
89 lines
2.1 KiB
C++
#ifndef RELAY_H
|
|
#define RELAY_H
|
|
|
|
#include "config.h"
|
|
#include "Output.h"
|
|
#include "mqtt.h"
|
|
|
|
class Relay final : public Output {
|
|
|
|
String nameFallback;
|
|
|
|
String topicFallback;
|
|
|
|
String topic;
|
|
|
|
const uint8_t index;
|
|
|
|
public:
|
|
|
|
Relay(const uint8_t index, const String &topic, const char *name, const uint8_t pin, const bool inverted, const bool logState) : Output(name, pin, inverted, logState), nameFallback(name), topicFallback(topic), topic(topic), index(index) {
|
|
//
|
|
}
|
|
|
|
void setup() override {
|
|
Output::setup();
|
|
Output::setName(configRead(path("name"), nameFallback));
|
|
topic = configRead(path("topic"), topicFallback);
|
|
Output::setInitial(configRead(path("initial"), INITIAL_OFF));
|
|
Output::setOnMillis(configRead(path("onMillis"), 0L));
|
|
Output::setOffMillis(configRead(path("offMillis"), 0L));
|
|
_applyInitial();
|
|
}
|
|
|
|
void setName(const String &value) override {
|
|
Output::setName(value);
|
|
configWrite(path("name"), nameFallback, value);
|
|
}
|
|
|
|
void setTopic(const String &value) {
|
|
topic = value;
|
|
configWrite(path("topic"), topicFallback, value);
|
|
}
|
|
|
|
void setInitial(const Initial value) override {
|
|
Output::setInitial(value);
|
|
configWrite(path("initial"), INITIAL_OFF, value);
|
|
}
|
|
|
|
void setOnMillis(const unsigned long value) override {
|
|
Output::setOnMillis(value);
|
|
configWrite(path("onMillis"), 0L, value);
|
|
}
|
|
|
|
void setOffMillis(const unsigned long value) override {
|
|
Output::setOffMillis(value);
|
|
configWrite(path("offMillis"), 0L, value);
|
|
}
|
|
|
|
void json(const JsonObject json) const {
|
|
json["name"] = name;
|
|
json["topic"] = topic;
|
|
json["state"] = get();
|
|
json["stateAgeMillis"] = millis() - stateMillis;
|
|
json["initial"] = initialToString(initial);
|
|
json["onCount"] = onCount;
|
|
json["onMillis"] = onMillis;
|
|
json["offMillis"] = offMillis;
|
|
}
|
|
|
|
private:
|
|
|
|
String path(const char *name) const {
|
|
char path[64];
|
|
snprintf(path, sizeof(path), "/relay%d/%s", index, name);
|
|
return String(path);
|
|
}
|
|
|
|
protected:
|
|
|
|
void publish() override {
|
|
JsonDocument doc;
|
|
json(doc.to<JsonObject>());
|
|
mqttPublish(topic, doc);
|
|
}
|
|
|
|
};
|
|
|
|
#endif
|