mirror of
https://github.com/shouptech/tempgopher.git
synced 2026-02-03 16:49:42 +00:00
Skeleton for thermostat
This commit is contained in:
parent
fdce8f51e9
commit
bea3f66d81
2 changed files with 22 additions and 11 deletions
31
main.go
31
main.go
|
|
@ -1,23 +1,34 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"sync"
|
||||
|
||||
"github.com/yryz/ds18b20"
|
||||
arg "github.com/alexflint/go-arg"
|
||||
)
|
||||
|
||||
func main() {
|
||||
sensors, err := ds18b20.Sensors()
|
||||
var args struct {
|
||||
Action string `arg:"required,positional" help:"run"`
|
||||
ConfigFile string `arg:"-c,required" help:"path to config file"`
|
||||
}
|
||||
|
||||
p := arg.MustParse(&args)
|
||||
if args.Action != "run" {
|
||||
p.Fail("ACTION must be run")
|
||||
}
|
||||
|
||||
config, err := LoadConfig(args.ConfigFile)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Printf("Sensors: %v\n", sensors)
|
||||
var wg sync.WaitGroup
|
||||
|
||||
for _, sensor := range sensors {
|
||||
t, err := ds18b20.Temperature(sensor)
|
||||
if err == nil {
|
||||
fmt.Printf("Sensor: %s, Temperature %.2f°C\n", sensor, t)
|
||||
}
|
||||
for _, sensor := range config.Sensors {
|
||||
wg.Add(1)
|
||||
go RunThermostat(sensor)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
}
|
||||
|
|
|
|||
2
run.go
2
run.go
|
|
@ -25,7 +25,7 @@ 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) {
|
||||
func RunThermostat(sensor Sensor) {
|
||||
for {
|
||||
t, err := ReadTemperature(sensor.ID)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue