🚧 Add cool-down command
This commit is contained in:
parent
2552adbd3e
commit
2d70d8994e
3 changed files with 77 additions and 0 deletions
47
chill/chill.go
Normal file
47
chill/chill.go
Normal 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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue