57 lines
		
	
	
	
		
			955 B
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			57 lines
		
	
	
	
		
			955 B
		
	
	
	
		
			Go
		
	
	
	
	
	
| 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 getCurrentTemp() (o int, err error) {
 | |
| 	var by []byte
 | |
| 	by, err = os.ReadFile(TempFile)
 | |
| 	if err != nil {
 | |
| 		return
 | |
| 	}
 | |
| 	by = bytes.TrimSpace(by)
 | |
| 	o, err = strconv.Atoi(string(by))
 | |
| 	return
 | |
| }
 | |
| 
 | |
| func Chill(baseCtx context.Context) context.Context {
 | |
| 	ctx, cancel := context.WithCancelCause(baseCtx)
 | |
| 
 | |
| 	ret := func(e error) context.Context {
 | |
| 		cancel(e)
 | |
| 		return ctx
 | |
| 	}
 | |
| 
 | |
| 	current, err := getCurrentTemp()
 | |
| 	if err != nil {
 | |
| 		return ret(err)
 | |
| 	}
 | |
| 
 | |
| 	if current < MaxTemp {
 | |
| 		return ret(ErrChilledOut)
 | |
| 	}
 | |
| 
 | |
| 	go func() {
 | |
| 		for current >= MaxTemp && err == nil {
 | |
| 			time.Sleep(Sleep)
 | |
| 			current, err = getCurrentTemp()
 | |
| 		}
 | |
| 		if err == nil {
 | |
| 			err = ErrChilledOut
 | |
| 		}
 | |
| 		cancel(err)
 | |
| 	}()
 | |
| 
 | |
| 	return ctx
 | |
| }
 |