mirror of
https://github.com/shouptech/tempgopher.git
synced 2026-02-03 16:49:42 +00:00
Initial commit
This commit is contained in:
commit
cae08151d0
4 changed files with 103 additions and 0 deletions
39
config.go
Normal file
39
config.go
Normal file
|
|
@ -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
|
||||
}
|
||||
30
config_test.go
Normal file
30
config_test.go
Normal file
|
|
@ -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)
|
||||
}
|
||||
23
main.go
Normal file
23
main.go
Normal file
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
11
test_config.yml
Normal file
11
test_config.yml
Normal file
|
|
@ -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
|
||||
Loading…
Add table
Reference in a new issue