Sonoff4ChPro/src/Relay.h

59 lines
1.4 KiB
C++

#ifndef RELAY_H
#define RELAY_H
#include "config.h"
#include "Output.h"
class Relay final : public Output {
String nameFallback;
const uint8_t index;
public:
Relay(const uint8_t index, const char *name, const uint8_t pin, const bool inverted, const bool logState) : Output(name, pin, inverted, logState), nameFallback(name), index(index) {
//
}
void setup() override {
Output::setup();
Output::setName(configRead(path("name"), nameFallback));
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, false);
}
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);
}
private:
String path(const char *name) const {
char path[64];
snprintf(path, sizeof(path), "/relay%d/%s", index, name);
return String(path);
}
};
#endif