2024-09-14 21:34:02 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-09-15 10:46:07 +02:00
|
|
|
"context"
|
2024-09-15 12:00:36 +02:00
|
|
|
"github.com/Crocmagnon/display-epaper/fete"
|
2024-11-26 02:15:59 +01:00
|
|
|
"github.com/Crocmagnon/display-epaper/fonts"
|
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-14 21:34:02 +02:00
|
|
|
"github.com/golang/freetype/truetype"
|
|
|
|
"github.com/llgcode/draw2d"
|
|
|
|
_ "golang.org/x/image/bmp"
|
2024-10-21 23:14:55 +02:00
|
|
|
"log/slog"
|
2024-09-15 10:46:07 +02:00
|
|
|
"os"
|
2024-09-16 14:12:51 +02:00
|
|
|
"time"
|
2024-09-14 21:34:02 +02:00
|
|
|
)
|
|
|
|
|
2024-11-26 01:18:38 +01:00
|
|
|
const (
|
2024-11-26 02:15:59 +01:00
|
|
|
fontRegular = "regular"
|
|
|
|
fontBold = "bold"
|
|
|
|
fontItalic = "italic"
|
2024-11-26 02:34:53 +01:00
|
|
|
fontIcons = "icons"
|
2024-11-26 01:18:38 +01:00
|
|
|
)
|
2024-09-14 21:34:02 +02:00
|
|
|
|
|
|
|
func main() {
|
2024-09-15 10:46:07 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2024-10-21 23:14:55 +02:00
|
|
|
slog.InfoContext(ctx, "starting...")
|
2024-11-26 01:18:38 +01:00
|
|
|
fontCache := MyFontCache{}
|
2024-10-21 23:14:55 +02:00
|
|
|
|
2024-11-26 02:15:59 +01:00
|
|
|
loadFont(ctx, fontCache, fonts.Regular, fontRegular)
|
|
|
|
loadFont(ctx, fontCache, fonts.Bold, fontBold)
|
|
|
|
loadFont(ctx, fontCache, fonts.Italic, fontItalic)
|
2024-11-26 02:34:53 +01:00
|
|
|
loadFont(ctx, fontCache, fonts.Icons, fontIcons)
|
2024-10-21 23:14:55 +02:00
|
|
|
|
2024-09-14 21:34:02 +02:00
|
|
|
draw2d.SetFontCache(fontCache)
|
|
|
|
|
2024-09-15 14:19:01 +02:00
|
|
|
transportsClient := transports.New(nil, transports.Config{})
|
2024-09-15 10:46:07 +02:00
|
|
|
|
2024-09-15 12:00:36 +02:00
|
|
|
feteClient := fete.New(nil, fete.Config{
|
|
|
|
APIKey: os.Getenv("FETE_API_KEY"),
|
|
|
|
CacheLocation: os.Getenv("FETE_CACHE_LOCATION"),
|
|
|
|
})
|
|
|
|
|
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
|
2024-09-16 14:12:51 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-10-21 23:14:55 +02:00
|
|
|
slog.InfoContext(ctx, "config",
|
|
|
|
"sleep_duration", sleep,
|
|
|
|
"init_fast_threshold", initFastThreshold)
|
2024-09-16 14:12:51 +02:00
|
|
|
|
2024-09-18 02:09:25 +02:00
|
|
|
hassClient := home_assistant.New(nil, home_assistant.Config{
|
|
|
|
Token: os.Getenv("HOME_ASSISTANT_TOKEN"),
|
|
|
|
BaseURL: os.Getenv("HOME_ASSISTANT_BASE_URL"),
|
|
|
|
})
|
|
|
|
|
2024-09-16 18:15:42 +02:00
|
|
|
if err := run(
|
|
|
|
ctx,
|
|
|
|
sleep,
|
|
|
|
initFastThreshold,
|
|
|
|
transportsClient,
|
|
|
|
feteClient,
|
|
|
|
weatherClient,
|
2024-09-18 02:09:25 +02:00
|
|
|
hassClient,
|
2024-09-16 18:15:42 +02:00
|
|
|
); err != nil {
|
2024-10-21 23:14:55 +02:00
|
|
|
slog.ErrorContext(ctx, "error", "err", err)
|
|
|
|
os.Exit(1)
|
2024-09-14 21:34:02 +02:00
|
|
|
}
|
|
|
|
|
2024-10-21 23:14:55 +02:00
|
|
|
slog.InfoContext(ctx, "done")
|
2024-09-14 21:34:02 +02:00
|
|
|
}
|
2024-11-26 01:18:38 +01:00
|
|
|
|
|
|
|
func loadFont(ctx context.Context, fontCache MyFontCache, ttf []byte, name string) {
|
|
|
|
font, err := truetype.Parse(ttf)
|
|
|
|
if err != nil {
|
|
|
|
slog.ErrorContext(ctx, "error loading font", "err", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
fontCache.Store(draw2d.FontData{Name: name}, font)
|
|
|
|
}
|