1
0
Fork 0
mirror of https://github.com/shouptech/tempgopher.git synced 2026-02-03 16:49:42 +00:00

Add State struct

This commit is contained in:
Emma 2018-09-28 14:40:12 -06:00
parent 15a24a392e
commit 7214eb6685

15
run.go
View file

@ -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)
}
}