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-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-15 09:01:13 +02:00
|
|
|
"log"
|
|
|
|
"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 00:11:23 +02:00
|
|
|
func run(ctx context.Context, transportsClient *transports.Client, feteClient *fete.Client, weatherClient *weather.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-15 10:46:07 +02:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
log.Println("stopping because of context")
|
|
|
|
return ctx.Err()
|
|
|
|
default:
|
|
|
|
}
|
2024-09-15 09:01:13 +02:00
|
|
|
|
2024-09-15 23:55:31 +02:00
|
|
|
err = loop(
|
|
|
|
ctx,
|
|
|
|
display,
|
|
|
|
transportsClient,
|
|
|
|
feteClient,
|
|
|
|
weatherClient,
|
|
|
|
)
|
2024-09-15 10:46:07 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("error looping: %v\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("time.Sleep(30s)")
|
|
|
|
time.Sleep(30 * time.Second)
|
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-15 12:00:36 +02:00
|
|
|
func loop(
|
|
|
|
ctx context.Context,
|
|
|
|
display *epd.EPD,
|
|
|
|
transportsClient *transports.Client,
|
|
|
|
feteClient *fete.Client,
|
2024-09-15 23:25:17 +02:00
|
|
|
weatherClient *weather.Client,
|
2024-09-15 12:00:36 +02:00
|
|
|
) error {
|
2024-09-15 10:46:07 +02:00
|
|
|
defer func() {
|
|
|
|
if err := display.Sleep(); err != nil {
|
|
|
|
log.Printf("error sleeping: %v\n", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
err := display.Init()
|
2024-09-15 09:01:13 +02:00
|
|
|
if err != nil {
|
2024-09-15 10:46:07 +02:00
|
|
|
return 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-15 23:55:31 +02:00
|
|
|
black, err := getBlack(
|
|
|
|
ctx,
|
|
|
|
time.Now,
|
|
|
|
transportsClient,
|
|
|
|
feteClient,
|
|
|
|
weatherClient,
|
|
|
|
)
|
2024-09-15 10:46:07 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("getting black: %w", err)
|
2024-09-15 09:01:13 +02:00
|
|
|
}
|
|
|
|
|
2024-09-15 10:46:07 +02:00
|
|
|
display.Send(black, nil)
|
|
|
|
display.Refresh()
|
2024-09-15 09:01:13 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|