display-epaper/run_linux_arm64.go

185 lines
3.9 KiB
Go
Raw Normal View History

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"
"github.com/Crocmagnon/display-epaper/home_assistant"
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"
"log/slog"
"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
)
func run(
ctx context.Context,
sleep time.Duration,
2024-09-16 18:15:42 +02:00
initFastThreshold time.Duration,
weatherClient *weather.Client,
hassClient *home_assistant.Client,
) 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():
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
slog.InfoContext(ctx, "running loop")
img, err := loop(ctx, display, initFastThreshold, currentImg, weatherClient, hassClient)
2024-09-15 10:46:07 +02:00
if err != nil {
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
slog.InfoContext(ctx, "sleep", "duration", sleep)
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,
weatherClient *weather.Client,
hassClient *home_assistant.Client,
2024-09-16 18:15:42 +02:00
) (image.Image, error) {
var img image.Image = image.White
if shouldDisplay(ctx, hassClient) {
var err error
img, err = getImg(ctx, time.Now, weatherClient, hassClient)
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) {
slog.InfoContext(ctx, "Images are equal, doing nothing.")
2024-09-16 18:09:00 +02:00
return img, nil
}
2024-09-15 10:46:07 +02:00
defer func() {
if err := display.Sleep(); err != nil {
slog.ErrorContext(ctx, "error sleeping", "err", err)
2024-09-15 10:46:07 +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)
display.Refresh()
2024-09-16 18:09:00 +02:00
return img, nil
}
func shouldDisplay(ctx context.Context, hassClient *home_assistant.Client) bool {
dayNight, err := hassClient.GetState(ctx, "input_select.house_day_night")
if err != nil {
log.Printf("error getting day night: %v ; displaying anyway\n", err)
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
}
presentAway, err := hassClient.GetState(ctx, "input_select.house_present_away")
if err != nil {
log.Printf("error getting day night: %v ; displaying anyway\n", err)
return true
}
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"
slog.InfoContext(ctx, "computed should display", "should_display", res)
return res
}
const filename = "/perm/display-epaper-lastFullRefresh"
func initDisplay(ctx context.Context, display *epd.EPD, threshold time.Duration) error {
2024-09-16 18:15:42 +02:00
if canInitFast(threshold) {
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 {
return fmt.Errorf("running full init: %w", err)
2024-09-15 09:01:13 +02:00
}
markInitFull(ctx)
2024-09-15 09:01:13 +02:00
return nil
}
2024-09-16 18:15:42 +02:00
func canInitFast(threshold time.Duration) bool {
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())
}
func markInitFull(ctx context.Context) {
f, err := os.Create(filename)
if err != nil {
slog.ErrorContext(ctx, "error marking full refresh", "err", err)
}
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
}