control init fast delay

This commit is contained in:
Gabriel Augendre 2024-09-16 18:15:42 +02:00
parent 2fa57a73a1
commit df0ccc536d
3 changed files with 32 additions and 7 deletions

16
main.go
View file

@ -48,9 +48,23 @@ func main() {
sleep = minSleep sleep = minSleep
} }
const minInitFastThreshold = 1 * time.Second
initFastThreshold, err := time.ParseDuration(os.Getenv("INIT_FAST_THRESHOLD"))
if err != nil || initFastThreshold < minInitFastThreshold {
initFastThreshold = minInitFastThreshold
}
log.Printf("sleep duration: %v\n", sleep) log.Printf("sleep duration: %v\n", sleep)
if err := run(ctx, sleep, transportsClient, feteClient, weatherClient); err != nil { if err := run(
ctx,
sleep,
initFastThreshold,
transportsClient,
feteClient,
weatherClient,
); err != nil {
log.Fatal("error: ", err) log.Fatal("error: ", err)
} }

View file

@ -13,6 +13,7 @@ import (
func run( func run(
ctx context.Context, ctx context.Context,
_ time.Duration, _ time.Duration,
_ time.Duration,
transportsClient *transports.Client, transportsClient *transports.Client,
feteClient *fete.Client, feteClient *fete.Client,
weatherClient *weather.Client, weatherClient *weather.Client,

View file

@ -17,6 +17,7 @@ import (
func run( func run(
ctx context.Context, ctx context.Context,
sleep time.Duration, sleep time.Duration,
initFastThreshold time.Duration,
transportsClient *transports.Client, transportsClient *transports.Client,
feteClient *fete.Client, feteClient *fete.Client,
weatherClient *weather.Client, weatherClient *weather.Client,
@ -46,6 +47,7 @@ func run(
img, err := loop( img, err := loop(
ctx, ctx,
display, display,
initFastThreshold,
currentImg, currentImg,
transportsClient, transportsClient,
feteClient, feteClient,
@ -62,7 +64,15 @@ func run(
} }
} }
func loop(ctx context.Context, display *epd.EPD, currentImg image.Image, transportsClient *transports.Client, feteClient *fete.Client, weatherClient *weather.Client) (image.Image, error) { func loop(
ctx context.Context,
display *epd.EPD,
initFastThreshold time.Duration,
currentImg image.Image,
transportsClient *transports.Client,
feteClient *fete.Client,
weatherClient *weather.Client,
) (image.Image, error) {
img, err := getImg( img, err := getImg(
ctx, ctx,
time.Now, time.Now,
@ -85,7 +95,7 @@ func loop(ctx context.Context, display *epd.EPD, currentImg image.Image, transpo
} }
}() }()
err = initDisplay(display) err = initDisplay(display, initFastThreshold)
if err != nil { if err != nil {
return nil, fmt.Errorf("initializing display: %w", err) return nil, fmt.Errorf("initializing display: %w", err)
} }
@ -100,8 +110,8 @@ func loop(ctx context.Context, display *epd.EPD, currentImg image.Image, transpo
const filename = "/perm/display-epaper-lastFullRefresh" const filename = "/perm/display-epaper-lastFullRefresh"
func initDisplay(display *epd.EPD) error { func initDisplay(display *epd.EPD, threshold time.Duration) error {
if canInitFast() { if canInitFast(threshold) {
err := display.InitFast() err := display.InitFast()
if err != nil { if err != nil {
return fmt.Errorf("running fast init: %w", err) return fmt.Errorf("running fast init: %w", err)
@ -119,13 +129,13 @@ func initDisplay(display *epd.EPD) error {
return nil return nil
} }
func canInitFast() bool { func canInitFast(threshold time.Duration) bool {
stat, err := os.Stat(filename) stat, err := os.Stat(filename)
if err != nil { if err != nil {
return false return false
} }
return stat.ModTime().Add(12 * time.Hour).After(time.Now()) return stat.ModTime().Add(threshold).After(time.Now())
} }
func markInitFull() { func markInitFull() {