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:
parent
15a24a392e
commit
7214eb6685
1 changed files with 13 additions and 2 deletions
15
run.go
15
run.go
|
|
@ -3,10 +3,19 @@ package main
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"log"
|
"log"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/yryz/ds18b20"
|
"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.
|
// ReadTemperature will return the current temperature (in degrees celsius) of a specific sensor.
|
||||||
func ReadTemperature(id string) (float64, error) {
|
func ReadTemperature(id string) (float64, error) {
|
||||||
sensors, err := ds18b20.Sensors()
|
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.
|
// RunThermostat monitors the temperature of the supplied sensor and does its best to keep it at the desired state.
|
||||||
func RunThermostat(sensor Sensor) {
|
func RunThermostat(sensor Sensor) {
|
||||||
|
var s State
|
||||||
|
|
||||||
for {
|
for {
|
||||||
t, err := ReadTemperature(sensor.ID)
|
t, err := ReadTemperature(sensor.ID)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicln(err)
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue