diff --git a/godoor.go b/godoor.go index 4cca9fb..b7e8e31 100644 --- a/godoor.go +++ b/godoor.go @@ -6,25 +6,25 @@ import ( "context" "encoding/json" "fmt" + "github.com/joho/godotenv" "io" "log" "net/http" "os" "os/signal" + "strconv" "strings" "sync" "syscall" "time" "godoor/hash" - - "github.com/joho/godotenv" ) -const wiegand_a = 17 -const wiegand_b = 18 +const wiegand_a_default = 17 +const wiegand_b_default = 18 const wiegand_bit_timeout = time.Millisecond * 8 -const solenoid = 21 +const solenoid_default = 21 type card struct { UidHash string `json:"uid_hash"` @@ -42,7 +42,7 @@ type ValidUids map[string]bool // bool has no meaning type Config struct { door string uidSalt string - doorOpenTime string + doorOpenTime int mock bool api struct { allowed string @@ -50,6 +50,11 @@ type Config struct { swipe string key string } + pins struct { + wiegandA int + wiegandB int + solenoid int + } } type KeepDoorOpen struct { @@ -76,16 +81,7 @@ func main() { ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) defer cancel() - godotenv.Load() - - 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") + loadConfig() go func() { setup() @@ -97,6 +93,41 @@ func main() { // 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() { log.Println("Started Setup") @@ -107,7 +138,7 @@ func setup() { } wiegand = &WiegandMock{} } 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") @@ -184,7 +215,7 @@ func OpenAndCloseDoor(w Wiegand) error { fmt.Println("Door is now open") - time.Sleep(5 * time.Second) + time.Sleep(time.Duration(config.doorOpenTime) * time.Second) err = CloseDoor(w) if err != nil { diff --git a/wiegand.go b/wiegand.go index d1f2d19..97c9f96 100644 --- a/wiegand.go +++ b/wiegand.go @@ -103,6 +103,7 @@ func (w *WiegandHW) wiegandBEvent(evt gpiod.LineEvent) { 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 wiegand.bitTimeout = bitTimeout