mirror of
https://github.com/shouptech/tempgopher.git
synced 2026-02-03 16:49:42 +00:00
Add some skeletons
This commit is contained in:
parent
cae08151d0
commit
fdce8f51e9
2 changed files with 52 additions and 0 deletions
38
run.go
Normal file
38
run.go
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"github.com/yryz/ds18b20"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ReadTemperature will return the current temperature (in degrees celsius) of a specific sensor.
|
||||||
|
func ReadTemperature(id string) (float64, error) {
|
||||||
|
sensors, err := ds18b20.Sensors()
|
||||||
|
if err != nil {
|
||||||
|
return 0.0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, sensor := range sensors {
|
||||||
|
if sensor != id {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
return ds18b20.Temperature(sensor)
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0.0, errors.New("Sensor not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
// RunThermostat monitors the temperature of the supplied sensor and does its best to keep it at the desired state.
|
||||||
|
func RunThermostat(sensor *Sensor) {
|
||||||
|
for {
|
||||||
|
t, err := ReadTemperature(sensor.ID)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Panicln(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("%.2f", t)
|
||||||
|
}
|
||||||
|
}
|
||||||
14
run_test.go
Normal file
14
run_test.go
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_ReadTemperature(t *testing.T) {
|
||||||
|
data, err := ReadTemperature("28-000008083108")
|
||||||
|
|
||||||
|
assert.Equal(t, 0.0, data)
|
||||||
|
assert.NotEqual(t, nil, err)
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue