Skip to content

Commit

Permalink
Using new config library
Browse files Browse the repository at this point in the history
  • Loading branch information
CameronRP committed Sep 11, 2019
1 parent 26d256c commit 6353151
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 59 deletions.
67 changes: 27 additions & 40 deletions attinyconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,66 +19,53 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package main

import (
"io/ioutil"
"os"

"github.com/TheCacophonyProject/attiny-controller/location"
"github.com/TheCacophonyProject/go-config"
"github.com/TheCacophonyProject/window"
yaml "gopkg.in/yaml.v2"
)

type AttinyConfig struct {
OnWindow *window.Window
Voltages Voltages
}

type rawConfig struct {
PiWakeUp string `yaml:"pi-wake-time"`
PiSleep string `yaml:"pi-sleep-time"`
Voltages Voltages `yaml:"voltages"`
}

type Voltages struct {
Enable bool `yaml:"enable"` // Enable reading voltage through ATtiny
NoBattery uint16 `yaml:"no-battery"` // If voltage reading is less than this it is not powered by a battery
LowBattery uint16 `yaml:"low-battery"` // Voltage of a low battery
FullBattery uint16 `yaml:"full-battery"` // Voltage of a full battery
Enable bool // Enable reading voltage through ATtiny
NoBattery uint16 // If voltage reading is less than this it is not powered by a battery
LowBattery uint16 // Voltage of a low battery
FullBattery uint16 // Voltage of a full battery
}

func ParseAttinyConfigFile(filename, locationFile string) (*AttinyConfig, error) {
buf, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}

locationBuf, err := ioutil.ReadFile(locationFile)
if err != nil && !os.IsNotExist(err) {
return nil, err
}

loc, err := location.New(locationBuf)
func ParseConfig(configDir string) (*AttinyConfig, error) {
rawConfig, err := config.New(configDir)
if err != nil {
return nil, err
}

return ParseAttinyConfig(buf, loc)
}
var battery config.Battery
rawConfig.Unmarshal("battery", &battery)

func ParseAttinyConfig(buf []byte, loc *location.LocationConfig) (*AttinyConfig, error) {
raw := rawConfig{}
if err := yaml.Unmarshal(buf, &raw); err != nil {
return nil, err
}
windows := config.DefaultWindows
rawConfig.Unmarshal(config.WindowsKey, windows)

conf := &AttinyConfig{
Voltages: raw.Voltages,
}
location := config.DefaultLocation
rawConfig.Unmarshal(config.LocationKey, location)

w, err := window.New(raw.PiWakeUp, raw.PiSleep, loc.Latitude, loc.Longitude)
w, err := window.New(
windows.PowerOn,
windows.PowerOff,
float64(location.Latitude),
float64(location.Longitude))
if err != nil {
return nil, err
}
conf.OnWindow = w

return conf, nil
return &AttinyConfig{
OnWindow: w,
Voltages: Voltages{
Enable: battery.EnableVoltageReadings,
NoBattery: battery.NoBattery,
LowBattery: battery.LowBattery,
FullBattery: battery.FullBattery,
},
}, nil
}
10 changes: 4 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ module github.com/TheCacophonyProject/attiny-controller
go 1.12

require (
github.com/TheCacophonyProject/go-config v0.0.0-20190911220253-593c493b2da3
github.com/TheCacophonyProject/window v0.0.0-20190821235241-ab92c2ee24b6
github.com/alexflint/go-arg v0.0.0-20180205180242-0cc8e30fd64c
github.com/alexflint/go-scalar v0.0.0-20170216020425-e80c3b7ed292 // indirect
github.com/alexflint/go-arg v1.1.0
github.com/godbus/dbus v4.1.0+incompatible
github.com/nathan-osman/go-sunrise v0.0.0-20171121204956-7c449e7c690b // indirect
github.com/stretchr/testify v1.3.0 // indirect
golang.org/x/sys v0.0.0-20180828065106-d99a578cf41b
gopkg.in/yaml.v2 v2.2.1
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a
gopkg.in/yaml.v2 v2.2.2
periph.io/x/periph v3.1.0+incompatible
)
Loading

0 comments on commit 6353151

Please sign in to comment.