2024-09-15 09:01:13 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-09-15 10:46:07 +02:00
|
|
|
"context"
|
2024-09-15 09:01:13 +02:00
|
|
|
"fmt"
|
|
|
|
"github.com/Crocmagnon/display-epaper/epd"
|
2024-09-15 12:00:36 +02:00
|
|
|
"github.com/Crocmagnon/display-epaper/fete"
|
2024-09-18 02:09:25 +02:00
|
|
|
"github.com/Crocmagnon/display-epaper/home_assistant"
|
2024-09-15 10:46:07 +02:00
|
|
|
"github.com/Crocmagnon/display-epaper/transports"
|
2024-09-15 23:25:17 +02:00
|
|
|
"github.com/Crocmagnon/display-epaper/weather"
|
2024-09-16 18:09:00 +02:00
|
|
|
"image"
|
2024-09-15 09:01:13 +02:00
|
|
|
"log"
|
2024-10-21 23:14:55 +02:00
|
|
|
"log/slog"
|
2024-09-16 14:12:51 +02:00
|
|
|
"os"
|
2024-09-15 09:01:13 +02:00
|
|
|
"periph.io/x/host/v3"
|
2024-09-15 10:46:07 +02:00
|
|
|
"time"
|
2024-09-15 09:01:13 +02:00
|
|
|
)
|
|
|
|
|
2024-09-16 14:12:51 +02:00
|
|
|
func run(
|
|
|
|
ctx context.Context,
|
|
|
|
sleep time.Duration,
|
2024-09-16 18:15:42 +02:00
|
|
|
initFastThreshold time.Duration,
|
2024-09-16 14:12:51 +02:00
|
|
|
transportsClient *transports.Client,
|
|
|
|
feteClient *fete.Client,
|
|
|
|
weatherClient *weather.Client,
|
2024-09-18 02:09:25 +02:00
|
|
|
hassClient *home_assistant.Client,
|
2024-09-16 14:12:51 +02:00
|
|
|
) error {
|
2024-09-15 09:01:13 +02:00
|
|
|
_, err := host.Init()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("initializing host: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
display, err := epd.New()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("initializing epd: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-09-16 18:09:00 +02:00
|
|
|
var currentImg image.Image
|
|
|
|
|
2024-09-15 10:46:07 +02:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2024-10-21 23:14:55 +02:00
|
|
|
slog.InfoContext(ctx, "stopping because of context")
|
2024-09-15 10:46:07 +02:00
|
|
|
return ctx.Err()
|
|
|
|
default:
|
|
|
|
}
|
2024-09-15 09:01:13 +02:00
|
|
|
|
2024-10-21 23:14:55 +02:00
|
|
|
slog.InfoContext(ctx, "running loop")
|
2024-09-16 14:12:51 +02:00
|
|
|
|
2024-09-16 18:09:00 +02:00
|
|
|
img, err := loop(
|
2024-09-15 23:55:31 +02:00
|
|
|
ctx,
|
|
|
|
display,
|
2024-09-16 18:15:42 +02:00
|
|
|
initFastThreshold,
|
2024-09-16 18:09:00 +02:00
|
|
|
currentImg,
|
2024-09-15 23:55:31 +02:00
|
|
|
transportsClient,
|
|
|
|
feteClient,
|
|
|
|
weatherClient,
|
2024-09-18 02:09:25 +02:00
|
|
|
hassClient,
|
2024-09-15 23:55:31 +02:00
|
|
|
)
|
2024-09-15 10:46:07 +02:00
|
|
|
if err != nil {
|
2024-10-21 23:14:55 +02:00
|
|
|
slog.ErrorContext(ctx, "error looping", "err", err)
|
2024-09-15 10:46:07 +02:00
|
|
|
}
|
|
|
|
|
2024-09-16 18:09:00 +02:00
|
|
|
currentImg = img
|
|
|
|
|
2024-10-21 23:14:55 +02:00
|
|
|
slog.InfoContext(ctx, "sleep", "duration", sleep)
|
2024-09-16 14:12:51 +02:00
|
|
|
time.Sleep(sleep)
|
2024-09-15 09:01:13 +02:00
|
|
|
}
|
2024-09-15 10:46:07 +02:00
|
|
|
}
|
2024-09-15 09:01:13 +02:00
|
|
|
|
2024-09-16 18:15:42 +02:00
|
|
|
func loop(
|
|
|
|
ctx context.Context,
|
|
|
|
display *epd.EPD,
|
|
|
|
initFastThreshold time.Duration,
|
|
|
|
currentImg image.Image,
|
|
|
|
transportsClient *transports.Client,
|
|
|
|
feteClient *fete.Client,
|
|
|
|
weatherClient *weather.Client,
|
2024-09-18 02:09:25 +02:00
|
|
|
hassClient *home_assistant.Client,
|
2024-09-16 18:15:42 +02:00
|
|
|
) (image.Image, error) {
|
2024-09-18 02:16:49 +02:00
|
|
|
var img image.Image = image.White
|
2024-09-18 02:09:25 +02:00
|
|
|
|
2024-09-18 02:16:49 +02:00
|
|
|
if shouldDisplay(ctx, hassClient) {
|
|
|
|
var err error
|
|
|
|
img, err = getImg(
|
|
|
|
ctx,
|
|
|
|
time.Now,
|
|
|
|
transportsClient,
|
|
|
|
feteClient,
|
|
|
|
weatherClient,
|
2024-09-18 22:26:40 +02:00
|
|
|
hassClient,
|
2024-09-18 02:16:49 +02:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("getting black: %w", err)
|
|
|
|
}
|
2024-09-16 18:09:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if imgEqual(currentImg, img, epd.Width, epd.Height) {
|
2024-10-21 23:14:55 +02:00
|
|
|
slog.InfoContext(ctx, "Images are equal, doing nothing.")
|
2024-09-16 18:09:00 +02:00
|
|
|
return img, nil
|
2024-09-16 14:12:51 +02:00
|
|
|
}
|
|
|
|
|
2024-09-15 10:46:07 +02:00
|
|
|
defer func() {
|
|
|
|
if err := display.Sleep(); err != nil {
|
2024-10-21 23:14:55 +02:00
|
|
|
slog.ErrorContext(ctx, "error sleeping", "err", err)
|
2024-09-15 10:46:07 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2024-10-21 23:14:55 +02:00
|
|
|
err := initDisplay(ctx, display, initFastThreshold)
|
2024-09-15 09:01:13 +02:00
|
|
|
if err != nil {
|
2024-09-16 18:09:00 +02:00
|
|
|
return nil, fmt.Errorf("initializing display: %w", err)
|
2024-09-15 09:01:13 +02:00
|
|
|
}
|
|
|
|
|
2024-09-15 10:46:07 +02:00
|
|
|
display.Clear()
|
2024-09-15 09:01:13 +02:00
|
|
|
|
2024-09-16 18:09:00 +02:00
|
|
|
display.Send(img)
|
2024-09-16 14:12:51 +02:00
|
|
|
display.Refresh()
|
|
|
|
|
2024-09-16 18:09:00 +02:00
|
|
|
return img, nil
|
2024-09-16 14:12:51 +02:00
|
|
|
}
|
|
|
|
|
2024-09-18 02:16:49 +02:00
|
|
|
func shouldDisplay(ctx context.Context, hassClient *home_assistant.Client) bool {
|
2024-09-18 02:09:25 +02:00
|
|
|
dayNight, err := hassClient.GetState(ctx, "input_select.house_day_night")
|
|
|
|
if err != nil {
|
2024-09-18 02:16:49 +02:00
|
|
|
log.Printf("error getting day night: %v ; displaying anyway\n", err)
|
2024-09-18 02:09:25 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2024-10-18 08:47:21 +02:00
|
|
|
hallLights, err := hassClient.GetState(ctx, "light.couloir")
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error getting hall lights: %v ; displaying anyway\n", err)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2024-09-18 02:09:25 +02:00
|
|
|
presentAway, err := hassClient.GetState(ctx, "input_select.house_present_away")
|
|
|
|
if err != nil {
|
2024-09-18 02:16:49 +02:00
|
|
|
log.Printf("error getting day night: %v ; displaying anyway\n", err)
|
2024-09-18 02:09:25 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2024-10-21 23:14:55 +02:00
|
|
|
slog.InfoContext(ctx, "home assistant states",
|
|
|
|
"hall_lights", hallLights,
|
|
|
|
"day_night", dayNight,
|
|
|
|
"present_away", presentAway)
|
2024-10-18 08:47:21 +02:00
|
|
|
|
|
|
|
res := (hallLights == "on" || dayNight == "day") && presentAway == "present"
|
2024-10-21 23:14:55 +02:00
|
|
|
slog.InfoContext(ctx, "computed should display", "should_display", res)
|
2024-09-18 02:09:25 +02:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2024-09-16 14:12:51 +02:00
|
|
|
const filename = "/perm/display-epaper-lastFullRefresh"
|
|
|
|
|
2024-10-21 23:14:55 +02:00
|
|
|
func initDisplay(ctx context.Context, display *epd.EPD, threshold time.Duration) error {
|
2024-09-16 18:15:42 +02:00
|
|
|
if canInitFast(threshold) {
|
2024-09-16 14:12:51 +02:00
|
|
|
err := display.InitFast()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("running fast init: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err := display.Init()
|
2024-09-15 10:46:07 +02:00
|
|
|
if err != nil {
|
2024-09-16 14:12:51 +02:00
|
|
|
return fmt.Errorf("running full init: %w", err)
|
2024-09-15 09:01:13 +02:00
|
|
|
}
|
|
|
|
|
2024-10-21 23:14:55 +02:00
|
|
|
markInitFull(ctx)
|
2024-09-15 09:01:13 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-09-16 14:12:51 +02:00
|
|
|
|
2024-09-16 18:15:42 +02:00
|
|
|
func canInitFast(threshold time.Duration) bool {
|
2024-09-16 14:12:51 +02:00
|
|
|
stat, err := os.Stat(filename)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-09-16 18:15:42 +02:00
|
|
|
return stat.ModTime().Add(threshold).After(time.Now())
|
2024-09-16 14:12:51 +02:00
|
|
|
}
|
|
|
|
|
2024-10-21 23:14:55 +02:00
|
|
|
func markInitFull(ctx context.Context) {
|
2024-09-16 14:12:51 +02:00
|
|
|
f, err := os.Create(filename)
|
|
|
|
if err != nil {
|
2024-10-21 23:14:55 +02:00
|
|
|
slog.ErrorContext(ctx, "error marking full refresh", "err", err)
|
2024-09-16 14:12:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
f.Close()
|
|
|
|
}
|
2024-09-16 18:09:00 +02:00
|
|
|
|
|
|
|
func imgEqual(img1, img2 image.Image, width, height int) bool {
|
|
|
|
if img1 == nil || img2 == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for y := 0; y < height; y++ {
|
|
|
|
for x := 0; x < width; x++ {
|
|
|
|
r1, g1, b1, a1 := img1.At(x, y).RGBA()
|
|
|
|
r2, g2, b2, a2 := img2.At(x, y).RGBA()
|
|
|
|
if r1 != r2 || g1 != g2 || b1 != b2 || a1 != a2 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|