52 lines
1,006 B
C++
52 lines
1,006 B
C++
#include "LED.cpp"
|
|
#include "TimerManager.cpp"
|
|
#include <map>
|
|
#include <ArduinoJson.h>
|
|
|
|
class JsonLightControl{
|
|
std::map<std::string, LED*> _leds;
|
|
TimerManager *_timers;
|
|
|
|
public:
|
|
JsonLightControl(TimerManager *timers){
|
|
_timers = timers;
|
|
}
|
|
|
|
void registerLEDs(LED *leds){
|
|
_leds[leds->getName()] = leds;
|
|
}
|
|
|
|
void action(std::string input){
|
|
if (input.length() <= 0)
|
|
return;
|
|
|
|
StaticJsonDocument<256> document;
|
|
deserializeJson(document, input.c_str());
|
|
|
|
auto lights = document.as<JsonArray>();
|
|
|
|
for (int i = 0; i < lights.size(); i++)
|
|
{
|
|
auto light = lights[i];
|
|
|
|
auto name = light["name"].as<std::string>();
|
|
if (!_leds.count(name))
|
|
continue;
|
|
|
|
auto led = _leds.find(name)->second;
|
|
|
|
auto brightness = light["brightness"].as<float>();
|
|
led->setBrightness(brightness);
|
|
|
|
auto on = light["on"].as<bool>();
|
|
if (on)
|
|
led->on();
|
|
else
|
|
led->off();
|
|
|
|
auto timer = light["timer"].as<unsigned long>();
|
|
if (timer > 0)
|
|
_timers->reset(name, timer);
|
|
}
|
|
}
|
|
};
|