93 lines
1.8 KiB
Go
93 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
"github.com/stianeikeland/go-rpio/v4"
|
|
|
|
"git.tcp.direct/kayos/door5/config"
|
|
"git.tcp.direct/kayos/door5/iot"
|
|
"git.tcp.direct/kayos/door5/logger"
|
|
)
|
|
|
|
var conf *config.Config
|
|
|
|
func OpenDoor() {
|
|
pin := rpio.Pin(conf.Pins[iot.Door])
|
|
pin.Output()
|
|
pin.High()
|
|
}
|
|
|
|
func CloseDoor() {
|
|
pin := rpio.Pin(conf.Pins[iot.Door])
|
|
pin.Output()
|
|
pin.Low()
|
|
}
|
|
|
|
func watchButton() {
|
|
logger.Get().Logger.Debug().Uint8("pin", uint8(conf.Pins[iot.Button])).Msg("exit button daemon started")
|
|
defer logger.Get().Logger.Debug().Msg("exit button daemon stopped")
|
|
pin := rpio.Pin(conf.Pins[iot.Button])
|
|
pin.Input()
|
|
pin.PullUp()
|
|
count := 0
|
|
for {
|
|
if pin.Read() != rpio.Low {
|
|
time.Sleep(5 * time.Millisecond)
|
|
if count > 0 {
|
|
count--
|
|
}
|
|
continue
|
|
}
|
|
count++
|
|
if count >= 100 {
|
|
logger.Get().Logger.Info().Int("count", count).Msg("door opened")
|
|
OpenDoor()
|
|
time.Sleep(5 * time.Second)
|
|
CloseDoor()
|
|
count = 0
|
|
}
|
|
time.Sleep(5 * time.Millisecond)
|
|
}
|
|
}
|
|
|
|
func loadConfig() {
|
|
var err error
|
|
path, _ := os.UserConfigDir()
|
|
path = filepath.Join(path, "door5", "config.toml")
|
|
if conf, err = config.ReadConfig(path); err != nil {
|
|
println(err.Error())
|
|
println("Writing default config")
|
|
_ = os.MkdirAll(filepath.Dir(path), 0755)
|
|
c := &config.Config{
|
|
Pins: config.Pins{
|
|
iot.Door: 22,
|
|
iot.Button: 5,
|
|
},
|
|
}
|
|
if err = c.WriteConfig(path); err != nil {
|
|
println(err.Error())
|
|
os.Exit(1)
|
|
}
|
|
conf = c
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
logger.Get().Logger.Info().Msg("starting door5")
|
|
logger.Get().Logger.Trace().Msg("loading config")
|
|
loadConfig()
|
|
logger.Get().Logger.Debug().Msg(spew.Sdump(conf))
|
|
logger.Get().Logger.Info().Msg("initializing gpio")
|
|
if err := rpio.Open(); err != nil {
|
|
logger.Get().Logger.Fatal().Msg(err.Error())
|
|
}
|
|
|
|
go watchButton()
|
|
|
|
select {}
|
|
}
|