🚧 Add cool-down command

This commit is contained in:
Dan Jones 2024-10-25 15:52:26 -05:00
commit 2d70d8994e
3 changed files with 77 additions and 0 deletions

47
chill/chill.go Normal file
View file

@ -0,0 +1,47 @@
package chill
import (
"bytes"
"context"
"errors"
"os"
"strconv"
"time"
)
var TempFile string = "/sys/class/thermal/thermal_zone2/temp"
var MaxTemp int = 72500
var Sleep = time.Second * 10
var ErrChilledOut = errors.New("temperature dropped below threshold")
func Chill(baseCtx context.Context) context.Context {
ctx, cancel := context.WithCancelCause(baseCtx)
go func() {
var by []byte
var err error
current := MaxTemp + 1
for current >= MaxTemp {
by, err = os.ReadFile(TempFile)
if err != nil {
cancel(err)
return
}
by = bytes.TrimSpace(by)
current, err = strconv.Atoi(string(by))
if err != nil {
cancel(err)
return
}
if current < MaxTemp {
break
}
time.Sleep(Sleep)
}
cancel(ErrChilledOut)
}()
return ctx
}