2021-08-30 20:46:38 +00:00
|
|
|
package temperature
|
|
|
|
|
2021-11-16 05:19:24 +00:00
|
|
|
// Kernel documentation: https://www.kernel.org/doc/html/latest/w1/slaves/w1_therm.html
|
|
|
|
|
2021-08-30 20:46:38 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2022-07-25 08:55:25 +00:00
|
|
|
"fmt"
|
2022-02-18 20:59:32 +00:00
|
|
|
"log"
|
2021-11-16 05:19:24 +00:00
|
|
|
"os"
|
2022-02-18 20:59:32 +00:00
|
|
|
"path/filepath"
|
2021-08-30 20:46:38 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2024-06-15 14:31:02 +00:00
|
|
|
"git.joco.dk/snr/fermentord/internal/configuration"
|
|
|
|
"git.joco.dk/snr/fermentord/internal/metrics"
|
2021-11-16 05:19:24 +00:00
|
|
|
"github.com/getsentry/sentry-go"
|
2022-02-18 20:59:32 +00:00
|
|
|
)
|
|
|
|
|
2022-03-15 16:09:53 +00:00
|
|
|
type BulkReadBusState int
|
|
|
|
|
2022-02-18 20:59:32 +00:00
|
|
|
const (
|
2022-03-15 20:07:53 +00:00
|
|
|
NaN = -999
|
|
|
|
|
2022-03-15 16:09:53 +00:00
|
|
|
BulkReadIdle BulkReadBusState = 0
|
|
|
|
BulkReadBusy BulkReadBusState = -1
|
|
|
|
BulkReadReady BulkReadBusState = 1
|
|
|
|
|
2022-02-18 20:59:32 +00:00
|
|
|
sensorConversionTime = 750 * time.Millisecond
|
2022-03-15 16:09:53 +00:00
|
|
|
busPollDelay = 10 * time.Millisecond
|
2021-08-30 20:46:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-11-16 05:19:24 +00:00
|
|
|
// ErrReadSensor indicates that the sensor could not be read
|
|
|
|
ErrReadSensor = errors.New("failed to read sensor")
|
|
|
|
)
|
2021-08-30 20:46:38 +00:00
|
|
|
|
2021-11-16 05:19:24 +00:00
|
|
|
var (
|
2022-03-15 20:07:53 +00:00
|
|
|
// Reading receives the temperature readings
|
|
|
|
Reading chan TemperatureReading
|
|
|
|
ConfigUpdate chan configuration.Configuration
|
2022-08-01 17:49:52 +00:00
|
|
|
RequestReset chan bool
|
2021-08-30 20:46:38 +00:00
|
|
|
|
2022-03-05 23:39:34 +00:00
|
|
|
accErrTrigger int
|
2022-03-15 20:07:53 +00:00
|
|
|
sensors []Sensor
|
2021-08-30 20:46:38 +00:00
|
|
|
)
|
|
|
|
|
2022-03-15 16:09:53 +00:00
|
|
|
func init() {
|
2022-03-15 20:07:53 +00:00
|
|
|
Reading = make(chan TemperatureReading, 30)
|
2022-07-23 23:16:40 +00:00
|
|
|
ConfigUpdate = make(chan configuration.Configuration, 1)
|
2022-08-01 17:49:52 +00:00
|
|
|
RequestReset = make(chan bool)
|
2022-03-15 20:07:53 +00:00
|
|
|
sensors = make([]Sensor, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func configure(config configuration.Configuration) {
|
|
|
|
sensors = []Sensor{
|
|
|
|
NewSensor("Ambient", config.Sensors.Ambient, config.Sensors.Weight),
|
|
|
|
NewSensor("Chamber", config.Sensors.Chamber, config.Sensors.Weight),
|
|
|
|
NewSensor("Wort", config.Sensors.Wort, config.Sensors.Weight),
|
|
|
|
}
|
2021-08-30 20:46:38 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 19:46:18 +00:00
|
|
|
func PollSensors(ctx context.Context, wg *sync.WaitGroup, readingInterval time.Duration, filterWeight float64) {
|
2022-07-25 08:55:25 +00:00
|
|
|
defer wg.Done()
|
|
|
|
|
2022-03-05 19:42:17 +00:00
|
|
|
hub := sentry.CurrentHub().Clone()
|
|
|
|
defer hub.Flush(10 * time.Second)
|
2021-08-30 20:46:38 +00:00
|
|
|
|
2022-02-18 20:59:32 +00:00
|
|
|
if readingInterval < sensorConversionTime {
|
|
|
|
log.Fatalf("Reading interval must be at least %v ms.", sensorConversionTime)
|
2021-11-16 05:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ticker := time.NewTicker(readingInterval)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
2021-08-30 20:46:38 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
2022-03-15 16:09:53 +00:00
|
|
|
poll_bus:
|
|
|
|
state, err := pollBusState()
|
|
|
|
if err != nil {
|
|
|
|
accErrTrigger++
|
|
|
|
hub.CaptureException(err)
|
|
|
|
log.Print(err)
|
2022-07-26 19:28:13 +00:00
|
|
|
break
|
2022-03-15 16:09:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch state {
|
|
|
|
case BulkReadBusy:
|
|
|
|
time.Sleep(busPollDelay)
|
|
|
|
goto poll_bus
|
|
|
|
|
2022-07-26 19:28:13 +00:00
|
|
|
case BulkReadIdle:
|
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
// Trigger a bulk read to start conversion on all sensors.
|
|
|
|
if err := triggerBulkRead(); err != nil {
|
|
|
|
accErrTrigger++
|
|
|
|
hub.CaptureException(err)
|
|
|
|
log.Print(err)
|
2022-08-01 17:49:52 +00:00
|
|
|
break
|
2022-07-26 19:28:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that we wait for sensors to convert data.
|
|
|
|
deltaSleep := sensorConversionTime - time.Since(start)
|
|
|
|
if deltaSleep > 0 {
|
|
|
|
// Wait for sensors to convert data.
|
|
|
|
time.Sleep(deltaSleep)
|
|
|
|
}
|
|
|
|
|
|
|
|
goto poll_bus
|
|
|
|
|
|
|
|
case BulkReadReady:
|
2022-08-02 03:15:03 +00:00
|
|
|
if !readSensors(hub) {
|
|
|
|
accErrTrigger++
|
|
|
|
} else {
|
2022-08-01 17:49:52 +00:00
|
|
|
accErrTrigger = 0
|
|
|
|
}
|
2022-07-26 19:28:13 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 20:07:53 +00:00
|
|
|
case c := <-ConfigUpdate:
|
|
|
|
configure(c)
|
2022-03-11 19:46:18 +00:00
|
|
|
|
2021-08-30 20:46:38 +00:00
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
2022-08-02 03:15:03 +00:00
|
|
|
|
|
|
|
if accErrTrigger > 60 {
|
|
|
|
select {
|
|
|
|
case RequestReset <- true:
|
|
|
|
log.Print("Thermal bulk read failed 60 times in a row -- bus reset")
|
|
|
|
|
|
|
|
// Reset counter to allow 60 more reads
|
|
|
|
accErrTrigger = 0
|
|
|
|
|
|
|
|
default:
|
|
|
|
log.Fatal("Thermal bulk read failed 60 times in a row -- terminating")
|
|
|
|
}
|
|
|
|
}
|
2021-08-30 20:46:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-02 03:15:03 +00:00
|
|
|
func readSensors(hub *sentry.Hub) bool {
|
2022-03-15 20:07:53 +00:00
|
|
|
r := TemperatureReading{
|
|
|
|
Time: time.Now(),
|
|
|
|
Ambient: NaN,
|
|
|
|
Chamber: NaN,
|
|
|
|
Wort: NaN,
|
|
|
|
}
|
|
|
|
|
2022-07-25 07:35:02 +00:00
|
|
|
hasReadFailure := false
|
|
|
|
|
2021-08-30 20:46:38 +00:00
|
|
|
for _, sensor := range sensors {
|
2022-07-29 17:40:24 +00:00
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
|
2022-03-15 20:07:53 +00:00
|
|
|
t, err := read(sensor.Path)
|
2021-08-30 20:46:38 +00:00
|
|
|
if err != nil {
|
2022-07-25 07:35:02 +00:00
|
|
|
hasReadFailure = true
|
2022-03-15 20:07:53 +00:00
|
|
|
sensor.Fail()
|
2022-03-05 19:42:17 +00:00
|
|
|
hub.CaptureException(err)
|
2022-07-26 11:10:17 +00:00
|
|
|
log.Printf("%v/%v", sensor.Name, err)
|
2022-03-15 20:07:53 +00:00
|
|
|
metrics.TemperatureSensorReadingStatus.WithLabelValues(sensor.Name, "failed").Inc()
|
2021-11-16 05:19:24 +00:00
|
|
|
continue
|
2021-08-30 20:46:38 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 20:07:53 +00:00
|
|
|
tw := sensor.Update(t)
|
|
|
|
|
|
|
|
switch sensor.Name {
|
|
|
|
case "Ambient":
|
|
|
|
r.Ambient = tw
|
2022-03-05 23:39:34 +00:00
|
|
|
|
2022-03-15 20:07:53 +00:00
|
|
|
case "Chamber":
|
|
|
|
r.Chamber = tw
|
|
|
|
|
|
|
|
case "Wort":
|
|
|
|
r.Wort = tw
|
2022-02-18 20:59:32 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 20:07:53 +00:00
|
|
|
// TODO Move metrics out
|
2021-08-30 20:46:38 +00:00
|
|
|
metrics.TemperatureSensorReadingDegreesCelcius.
|
2022-03-15 20:07:53 +00:00
|
|
|
WithLabelValues(sensor.Name).
|
2022-03-11 19:46:18 +00:00
|
|
|
Set(tw)
|
2021-08-30 20:46:38 +00:00
|
|
|
|
2022-08-03 13:27:27 +00:00
|
|
|
var statusStr string
|
|
|
|
if err == nil {
|
|
|
|
statusStr = "ok"
|
|
|
|
} else {
|
|
|
|
statusStr = "fail"
|
|
|
|
}
|
|
|
|
metrics.TemperatureSensorReadingStatus.
|
|
|
|
WithLabelValues(sensor.Name, statusStr).
|
|
|
|
Inc()
|
2021-08-30 20:46:38 +00:00
|
|
|
}
|
2022-03-15 20:07:53 +00:00
|
|
|
|
2022-07-25 07:35:02 +00:00
|
|
|
// Throw away reading if any sensor failed
|
|
|
|
if hasReadFailure {
|
2022-08-02 03:15:03 +00:00
|
|
|
return false
|
2022-07-25 07:35:02 +00:00
|
|
|
}
|
|
|
|
|
2022-07-23 23:26:22 +00:00
|
|
|
select {
|
|
|
|
case Reading <- r:
|
2022-07-25 07:35:02 +00:00
|
|
|
break
|
|
|
|
|
2022-07-23 23:26:22 +00:00
|
|
|
default:
|
2022-07-25 08:55:25 +00:00
|
|
|
err := fmt.Errorf("channel overflow on ds18b20 temperature channel")
|
|
|
|
hub.CaptureException(err)
|
|
|
|
log.Fatal(err)
|
2022-07-23 23:26:22 +00:00
|
|
|
}
|
2022-08-02 03:15:03 +00:00
|
|
|
|
|
|
|
return true
|
2021-08-30 20:46:38 +00:00
|
|
|
}
|
|
|
|
|
2021-11-16 05:19:24 +00:00
|
|
|
func triggerBulkRead() error {
|
2022-03-05 19:42:17 +00:00
|
|
|
f, err := os.OpenFile("/sys/bus/w1/devices/w1_bus_master1/therm_bulk_read", os.O_WRONLY, 0644)
|
2021-11-16 05:19:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
2022-03-06 21:52:21 +00:00
|
|
|
_, err = f.WriteString("trigger\n")
|
2021-11-16 05:19:24 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-15 16:09:53 +00:00
|
|
|
func pollBusState() (BulkReadBusState, error) {
|
2024-06-15 19:37:00 +00:00
|
|
|
b, err := os.ReadFile("/sys/bus/w1/devices/w1_bus_master1/therm_bulk_read")
|
2022-03-15 16:09:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return BulkReadIdle, err
|
|
|
|
}
|
|
|
|
|
2022-03-18 12:48:33 +00:00
|
|
|
i, err := strconv.Atoi(strings.TrimSpace(string(b)))
|
2022-03-15 16:09:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return BulkReadIdle, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return BulkReadBusState(i), nil
|
|
|
|
}
|
|
|
|
|
2021-08-30 20:46:38 +00:00
|
|
|
// read returns the temperature of the specified sensor in millidegrees celcius.
|
|
|
|
func read(sensor string) (int64, error) {
|
2022-03-05 19:42:17 +00:00
|
|
|
path := filepath.Join("/sys/bus/w1/devices", sensor, "w1_slave")
|
2024-06-15 19:37:00 +00:00
|
|
|
data, err := os.ReadFile(path)
|
2021-08-30 20:46:38 +00:00
|
|
|
if err != nil {
|
2022-08-03 13:27:27 +00:00
|
|
|
return NaN, err
|
2021-08-30 20:46:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
raw := string(data)
|
|
|
|
|
2022-08-03 13:30:52 +00:00
|
|
|
if raw == "" {
|
|
|
|
return NaN, fmt.Errorf("%v: %w: empty file", sensor, ErrReadSensor)
|
|
|
|
}
|
|
|
|
|
2021-08-30 20:46:38 +00:00
|
|
|
if !strings.Contains(raw, " YES") {
|
2022-08-03 13:27:27 +00:00
|
|
|
return NaN, fmt.Errorf("%v: %w: checksum failed [%v]", sensor, ErrReadSensor, raw)
|
2021-08-30 20:46:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
i := strings.LastIndex(raw, "t=")
|
|
|
|
if i == -1 {
|
2022-08-03 13:27:27 +00:00
|
|
|
return NaN, fmt.Errorf("%v: %w: t= not found in [%v]", sensor, ErrReadSensor, raw)
|
2021-08-30 20:46:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c, err := strconv.ParseInt(raw[i+2:len(raw)-1], 10, 64)
|
|
|
|
if err != nil {
|
2022-08-03 13:27:27 +00:00
|
|
|
return NaN, err
|
2021-08-30 20:46:38 +00:00
|
|
|
}
|
|
|
|
|
2022-07-26 10:12:19 +00:00
|
|
|
// Ignore magic value -128
|
|
|
|
if c == -128000 {
|
2022-08-03 13:27:27 +00:00
|
|
|
return NaN, fmt.Errorf("%v: magic value -128 detected", sensor)
|
2022-07-26 10:12:19 +00:00
|
|
|
}
|
|
|
|
|
2021-08-30 20:46:38 +00:00
|
|
|
return c, nil
|
|
|
|
}
|