From 7214eb668558259735971374242e9010a158007e Mon Sep 17 00:00:00 2001 From: Mike Shoup Date: Fri, 28 Sep 2018 14:40:12 -0600 Subject: [PATCH] Add State struct --- run.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/run.go b/run.go index 274452c..6506309 100644 --- a/run.go +++ b/run.go @@ -3,10 +3,19 @@ package main import ( "errors" "log" + "time" "github.com/yryz/ds18b20" ) +// State represents the current state of the thermostat +type State struct { + Temp float64 + Cooling bool + Heating bool + Duration time.Duration +} + // ReadTemperature will return the current temperature (in degrees celsius) of a specific sensor. func ReadTemperature(id string) (float64, error) { sensors, err := ds18b20.Sensors() @@ -26,13 +35,15 @@ func ReadTemperature(id string) (float64, error) { // RunThermostat monitors the temperature of the supplied sensor and does its best to keep it at the desired state. func RunThermostat(sensor Sensor) { + var s State + for { t, err := ReadTemperature(sensor.ID) - if err != nil { log.Panicln(err) } - log.Printf("%.2f", t) + s.Temp = t + log.Printf("Temp: %.2f, Cooling: %t, Heating: %t Duration: %d", s.Temp, s.Cooling, s.Heating, s.Duration) } }