frankcohen / EleksTubeIPSHack

Hacking the Elekstube IPS ESP32 TFT based clock
GNU General Public License v3.0
69 stars 22 forks source link

Feature: Add Rest API #3

Open koshisan opened 3 years ago

koshisan commented 3 years ago

I already have a rudimentary API running in my fork - however, without rest (since it is a simple display/backlight on/off through URL requests) - however, it is a start and a good test for a pull request :)

frankcohen commented 3 years ago

Hi Koshisan, I need some help understanding this. Do you need a REST API implemented? -Frank

koshisan commented 3 years ago

Hi Frank,

This was originally something I intended to do. That's why I started looking into firmware alternatives for the device. The idea is to provide a better smarthome integration. In my specific case I keep my clock in the bedroom. The displays are to bright to sleep, so I want to turn them off when I start the "nightmode" for my smarthome. I also want the LED backlight to work together with other bedroom lights.

So far I added it to SmittyHalibut's version of the code, since it was quick and easy - I just had to register a webserver with some URL handlers:

API_serv.on("/display_off", api_displayOff);
API_serv.on("/display_on", api_displayOn);
API_serv.on("/backlight_off", api_backlightOff);
API_serv.on("/backlight_const", api_backlightConst);

And as for the implementation I could use his existing functions:


void api_displayOff() {
 tfts.disableAllDisplays(); 
 API_serv.send(200, "application/json", "{}");
 Serial.println("Displays off");
}
void api_displayOn() {
 tfts.enableAllDisplays(); 
 API_serv.send(200, "application/json", "{}");
}

void api_backlightOff() {
 backlights.setPattern(backlights.patterns::dark); 
 API_serv.send(200, "application/json", "{}");
}
void api_backlightConst() {
 backlights.setPattern(backlights.patterns::constant); 
 API_serv.send(200, "application/json", "{}");
}

Note that I managed to avoid actually using a JSON object until now ;) However, I will need one for the color defintion of the background light. Ideally the background light would be controlled by an object providing mode, brightness, red, green, blue...

Also the display_on/display_off URLs are a bit amateurish - it should either accept an JSON object with a "display" item that can contain a mode, or at least something like /api/display?mode=off (with "clock", "images", etc. as alternatives)

However - for this to be implemented in your fork #5 would need to be adressed first - testing isn't really fun if you have to join the device to your wifi by phone every reboot ;)

(I did some work on this, too - will update that ticket next)