Allow configuring more options via ENV
This commit is contained in:
parent
a9e008b7f2
commit
a6b928dc0e
67
godoor.go
67
godoor.go
@ -6,25 +6,25 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/joho/godotenv"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"godoor/hash"
|
"godoor/hash"
|
||||||
|
|
||||||
"github.com/joho/godotenv"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const wiegand_a = 17
|
const wiegand_a_default = 17
|
||||||
const wiegand_b = 18
|
const wiegand_b_default = 18
|
||||||
const wiegand_bit_timeout = time.Millisecond * 8
|
const wiegand_bit_timeout = time.Millisecond * 8
|
||||||
const solenoid = 21
|
const solenoid_default = 21
|
||||||
|
|
||||||
type card struct {
|
type card struct {
|
||||||
UidHash string `json:"uid_hash"`
|
UidHash string `json:"uid_hash"`
|
||||||
@ -42,7 +42,7 @@ type ValidUids map[string]bool // bool has no meaning
|
|||||||
type Config struct {
|
type Config struct {
|
||||||
door string
|
door string
|
||||||
uidSalt string
|
uidSalt string
|
||||||
doorOpenTime string
|
doorOpenTime int
|
||||||
mock bool
|
mock bool
|
||||||
api struct {
|
api struct {
|
||||||
allowed string
|
allowed string
|
||||||
@ -50,6 +50,11 @@ type Config struct {
|
|||||||
swipe string
|
swipe string
|
||||||
key string
|
key string
|
||||||
}
|
}
|
||||||
|
pins struct {
|
||||||
|
wiegandA int
|
||||||
|
wiegandB int
|
||||||
|
solenoid int
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type KeepDoorOpen struct {
|
type KeepDoorOpen struct {
|
||||||
@ -76,16 +81,7 @@ func main() {
|
|||||||
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
godotenv.Load()
|
loadConfig()
|
||||||
|
|
||||||
config.door = os.Getenv("KDOORPI_DOOR")
|
|
||||||
config.api.allowed = os.Getenv("KDOORPI_API_ALLOWED")
|
|
||||||
config.api.longpoll = os.Getenv("KDOORPI_API_LONGPOLL")
|
|
||||||
config.api.swipe = os.Getenv("KDOORPI_API_SWIPE")
|
|
||||||
config.api.key = os.Getenv("KDOORPI_API_KEY")
|
|
||||||
config.uidSalt = os.Getenv("KDOORPI_UID_SALT")
|
|
||||||
config.doorOpenTime = os.Getenv("KDOORPI_OPEN_TIME")
|
|
||||||
_, config.mock = os.LookupEnv("KDOORPI_MOCK_HW")
|
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
setup()
|
setup()
|
||||||
@ -97,6 +93,41 @@ func main() {
|
|||||||
// cleanup
|
// cleanup
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func loadConfig() {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
log.Println("Loading .env config")
|
||||||
|
err = godotenv.Load()
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Failed to load .env config, using internal defaults")
|
||||||
|
}
|
||||||
|
|
||||||
|
config.door = os.Getenv("KDOORPI_DOOR")
|
||||||
|
config.api.allowed = os.Getenv("KDOORPI_API_ALLOWED")
|
||||||
|
config.api.longpoll = os.Getenv("KDOORPI_API_LONGPOLL")
|
||||||
|
config.api.swipe = os.Getenv("KDOORPI_API_SWIPE")
|
||||||
|
config.api.key = os.Getenv("KDOORPI_API_KEY")
|
||||||
|
config.uidSalt = os.Getenv("KDOORPI_UID_SALT")
|
||||||
|
config.doorOpenTime, err = strconv.Atoi(os.Getenv("KDOORPI_OPEN_TIME"))
|
||||||
|
if err != nil {
|
||||||
|
config.doorOpenTime = 3
|
||||||
|
}
|
||||||
|
_, config.mock = os.LookupEnv("KDOORPI_MOCK_HW")
|
||||||
|
|
||||||
|
config.pins.wiegandA, err = strconv.Atoi(os.Getenv("KDOORPI_PIN_WIEGAND_A"))
|
||||||
|
if err != nil {
|
||||||
|
config.pins.wiegandA = wiegand_a_default
|
||||||
|
}
|
||||||
|
config.pins.wiegandB, err = strconv.Atoi(os.Getenv("KDOORPI_PIN_WIEGAND_B"))
|
||||||
|
if err != nil {
|
||||||
|
config.pins.wiegandB = wiegand_b_default
|
||||||
|
}
|
||||||
|
config.pins.solenoid, err = strconv.Atoi(os.Getenv("KDOORPI_PIN_SOLENOID"))
|
||||||
|
if err != nil {
|
||||||
|
config.pins.solenoid = solenoid_default
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func setup() {
|
func setup() {
|
||||||
log.Println("Started Setup")
|
log.Println("Started Setup")
|
||||||
|
|
||||||
@ -107,7 +138,7 @@ func setup() {
|
|||||||
}
|
}
|
||||||
wiegand = &WiegandMock{}
|
wiegand = &WiegandMock{}
|
||||||
} else {
|
} else {
|
||||||
wiegand = WiegandSetup(wiegand_a, wiegand_b, wiegand_bit_timeout, solenoid)
|
wiegand = WiegandSetup(config.pins.wiegandA, config.pins.wiegandB, wiegand_bit_timeout, config.pins.solenoid)
|
||||||
}
|
}
|
||||||
log.Println("HW Setup done")
|
log.Println("HW Setup done")
|
||||||
|
|
||||||
@ -184,7 +215,7 @@ func OpenAndCloseDoor(w Wiegand) error {
|
|||||||
|
|
||||||
fmt.Println("Door is now open")
|
fmt.Println("Door is now open")
|
||||||
|
|
||||||
time.Sleep(5 * time.Second)
|
time.Sleep(time.Duration(config.doorOpenTime) * time.Second)
|
||||||
|
|
||||||
err = CloseDoor(w)
|
err = CloseDoor(w)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -103,6 +103,7 @@ func (w *WiegandHW) wiegandBEvent(evt gpiod.LineEvent) {
|
|||||||
|
|
||||||
func WiegandSetup(a int, b int, bitTimeout time.Duration, solenoid int) *WiegandHW {
|
func WiegandSetup(a int, b int, bitTimeout time.Duration, solenoid int) *WiegandHW {
|
||||||
|
|
||||||
|
log.Printf("Wiegand GPIO-s: A:%d B:%d Solenoid:%d", a, b, solenoid)
|
||||||
var wiegand WiegandHW
|
var wiegand WiegandHW
|
||||||
wiegand.bitTimeout = bitTimeout
|
wiegand.bitTimeout = bitTimeout
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user