commit cae08151d087098e9f4a0135088579e09fda8354 Author: Mike Shoup Date: Fri Sep 28 12:10:05 2018 -0600 Initial commit diff --git a/config.go b/config.go new file mode 100644 index 0000000..92cb3f9 --- /dev/null +++ b/config.go @@ -0,0 +1,39 @@ +package main + +import ( + "io/ioutil" + + "gopkg.in/yaml.v2" +) + +// Sensor defines configuration for a temperature sensor. +type Sensor struct { + ID string `yaml:"id"` + Alias string `yaml:"alias"` + HighTemp float64 `yaml:"hightemp"` + LowTemp float64 `yaml:"lowtemp"` + HeatGpio int32 `yaml:"heatgpio"` + HeatPullup bool `yaml:"heatpullup"` + HeatMinutes int32 `yaml:"heatminutes"` + CoolGpio int32 `yaml:"coolgpio"` + CoolPullup bool `yaml:"coolpullup"` + CoolMinutes int32 `yaml:"coolminutes"` +} + +// Config contains the applications configuration +type Config struct { + Sensors []Sensor `yaml:"sensors"` +} + +// LoadConfig will loads a file and parses it into a Config struct +func LoadConfig(path string) (*Config, error) { + data, err := ioutil.ReadFile(path) + if err != nil { + return nil, err + } + + var config Config + yaml.Unmarshal(data, &config) + + return &config, nil +} diff --git a/config_test.go b/config_test.go new file mode 100644 index 0000000..83ac804 --- /dev/null +++ b/config_test.go @@ -0,0 +1,30 @@ +package main + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func Test_LoadConfig(t *testing.T) { + testSensor := Sensor{ + ID: "28-000008083108", + Alias: "fermenter", + HighTemp: 8, + LowTemp: 4, + HeatGpio: 5, + HeatPullup: true, + HeatMinutes: 5, + CoolGpio: 17, + CoolPullup: false, + CoolMinutes: 10} + + testConfig := Config{Sensors: []Sensor{testSensor}} + + loadedConfig, err := LoadConfig("test_config.yml") + assert.Equal(t, nil, err) + assert.Equal(t, &testConfig, loadedConfig) + + _, err = LoadConfig("thisfiledoesnotexist") + assert.NotEqual(t, nil, err) +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..3e69a6f --- /dev/null +++ b/main.go @@ -0,0 +1,23 @@ +package main + +import ( + "fmt" + + "github.com/yryz/ds18b20" +) + +func main() { + sensors, err := ds18b20.Sensors() + if err != nil { + panic(err) + } + + fmt.Printf("Sensors: %v\n", sensors) + + for _, sensor := range sensors { + t, err := ds18b20.Temperature(sensor) + if err == nil { + fmt.Printf("Sensor: %s, Temperature %.2f°C\n", sensor, t) + } + } +} diff --git a/test_config.yml b/test_config.yml new file mode 100644 index 0000000..2335b38 --- /dev/null +++ b/test_config.yml @@ -0,0 +1,11 @@ +sensors: +- id: 28-000008083108 + alias: fermenter + hightemp: 8 + lowtemp: 4 + heatgpio: 5 + heatpullup: true + heatminutes: 5 + coolgpio: 17 + coolpullup: false + coolminutes: 10