display-epaper/main.go

63 lines
1.5 KiB
Go
Raw Normal View History

2024-09-14 21:34:02 +02:00
package main
import (
2024-09-15 10:46:07 +02:00
"context"
"github.com/Crocmagnon/display-epaper/fonts"
"github.com/Crocmagnon/display-epaper/home_assistant"
2024-09-15 23:25:17 +02:00
"github.com/Crocmagnon/display-epaper/weather"
2024-09-14 21:34:02 +02:00
"github.com/llgcode/draw2d"
_ "golang.org/x/image/bmp"
"log/slog"
2024-09-15 10:46:07 +02:00
"os"
"time"
2024-09-14 21:34:02 +02:00
)
func main() {
2024-09-15 10:46:07 +02:00
ctx := context.Background()
slog.InfoContext(ctx, "starting...")
2024-11-26 18:26:45 +01:00
fontCache, err := fonts.NewCache()
if err != nil {
slog.ErrorContext(ctx, "could not create font cache", "error", err.Error())
os.Exit(1)
}
2024-09-14 21:34:02 +02:00
draw2d.SetFontCache(fontCache)
2024-09-15 23:25:17 +02:00
weatherClient := weather.New(nil, weather.Config{
2024-09-16 00:28:43 +02:00
APIKey: os.Getenv("WEATHER_API_KEY"),
CacheLocation: os.Getenv("WEATHER_CACHE_LOCATION"),
2024-09-15 23:25:17 +02:00
})
2024-09-16 18:09:00 +02:00
const minSleep = 1 * time.Second
sleep, err := time.ParseDuration(os.Getenv("SLEEP_DURATION"))
if err != nil || sleep < minSleep {
sleep = minSleep
}
2024-09-16 18:15:42 +02:00
const minInitFastThreshold = 1 * time.Second
initFastThreshold, err := time.ParseDuration(os.Getenv("INIT_FAST_THRESHOLD"))
if err != nil || initFastThreshold < minInitFastThreshold {
initFastThreshold = minInitFastThreshold
}
slog.InfoContext(ctx, "config",
"sleep_duration", sleep,
"init_fast_threshold", initFastThreshold)
hassClient := home_assistant.New(nil, home_assistant.Config{
Token: os.Getenv("HOME_ASSISTANT_TOKEN"),
BaseURL: os.Getenv("HOME_ASSISTANT_BASE_URL"),
})
if err := run(ctx, sleep, initFastThreshold, weatherClient, hassClient); err != nil {
slog.ErrorContext(ctx, "error", "err", err)
os.Exit(1)
2024-09-14 21:34:02 +02:00
}
slog.InfoContext(ctx, "done")
2024-09-14 21:34:02 +02:00
}