Conf file move, multi config support, on time support

This commit is contained in:
Ian Gulliver
2022-09-24 17:20:10 -07:00
parent 40c4abd0cd
commit 035df9e7fd
5 changed files with 133 additions and 13 deletions

View File

@@ -1,29 +1,124 @@
package main
import "context"
import "fmt"
import "flag"
import "math"
import "os"
import "time"
import "github.com/firestuff/hh/hhio"
import "gopkg.in/yaml.v3"
var conf = flag.String("conf", "", "path to config file")
type Config struct {
Relay
Ultrasonics []Ultrasonic
MedianBuffer int
OnCM float64
OffCM float64
OnSeconds float64
}
type Relay struct {
Control int
}
type Ultrasonic struct {
Trigger int
Echo int
}
func main() {
err := hhio.Open()
flag.Parse()
cf, err := readConf()
if err != nil {
panic(err)
}
err = hhio.Open()
if err != nil {
panic(err)
}
defer hhio.Close()
us := hhio.NewUltrasonic(context.Background(), 6, 5)
mf := hhio.NewMedianFilter(us.C, 47)
uss := []chan float64{}
r := hhio.NewRelay(21)
for _, uscf := range cf.Ultrasonics {
us := hhio.NewUltrasonic(context.Background(), uscf.Trigger, uscf.Echo)
mf := hhio.NewMedianFilter(us.C, cf.MedianBuffer)
uss = append(uss, mf)
}
for dist := range mf {
fmt.Printf("%f\n", dist)
r := hhio.NewRelay(cf.Relay.Control)
if dist < 50 {
r.On()
} else if dist > 70 {
r.Off()
}
last := make([]float64, len(uss))
for i := range uss {
last[i] = math.MaxFloat64
}
onUntil := time.Time{}
for {
// Fetch new values
for i, us := range uss {
select {
case dist := <-us:
last[i] = dist
default:
}
}
// Count votes
var on, off int
for _, v := range last {
if v < cf.OnCM {
on++
} else if v > cf.OffCM {
off++
}
}
if on > 0 {
// At least one on, turn on
if onUntil.IsZero() {
r.On()
onUntil = time.Now().Add(time.Duration(cf.OnSeconds * float64(time.Second)))
}
} else if off == len(uss) {
// All off, turn off
if !r.IsOn() {
onUntil = time.Time{}
}
}
if !onUntil.IsZero() && onUntil.Before(time.Now()) {
r.Off()
}
}
}
func readConf() (*Config, error) {
fh, err := os.Open(*conf)
if err != nil {
return nil, err
}
defer fh.Close()
dec := yaml.NewDecoder(fh)
dec.KnownFields(true)
c := &Config{}
err = dec.Decode(c)
if err != nil {
return nil, err
}
return c, nil
}