package main import ( "context" "encoding/json" "fmt" "github.com/joho/godotenv" "io" "net/http" "os" "os/signal" "syscall" ) import "time" const wiegand_a = 17 const wiegand_b = 18 const wiegand_bit_timeout = time.Millisecond * 8 const solenoid = 21 type card struct { UidHash string `json:"uid_hash"` } type cardList struct { AllowedUids []struct { Token card `json:"token"` } `json:"allowed_uids"` } type simpleUids struct { tokens []string } type ValidUids map[string]bool // bool has no meaning type Config struct { door string uid_salt string api struct { allowed string longpoll string swipe string key string } } var config Config 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.uid_salt = os.Getenv("KDOORPI_UID_SALT") //wiegand := WiegandSetup(wiegand_a, wiegand_b, wiegand_bit_timeout, solenoid) http.DefaultClient.Timeout = 60 reloadTokens() //go wiegand.cardRunner(validUids) waitEvents() fmt.Printf("Sleeping\n") <-ctx.Done() fmt.Printf("Cleanup\n") // cleanup } func waitEvents() { req, err := http.NewRequest(http.MethodGet, config.api.longpoll, nil) if err != nil { panic(err) } req.Header.Add("KEY", config.api.key) resp, err := http.DefaultClient.Do(req) if err != nil { panic(err) } fmt.Printf("%v\n", resp) _, err = io.ReadAll(resp.Body) if err != nil { panic(err) } fmt.Printf("%v\n", resp) } func reloadTokens() { req, err := http.NewRequest(http.MethodGet, config.api.allowed, nil) if err != nil { panic(err) } req.Header.Add("KEY", config.api.key) resp, err := http.DefaultClient.Do(req) if err != nil { panic(err) } fmt.Printf("%v\n", resp) var cl cardList body, err := io.ReadAll(resp.Body) if err != nil { panic(err) } err = json.Unmarshal(body, &cl) if err != nil { panic(err) } validUids := make(ValidUids) for i, val := range cl.AllowedUids { fmt.Printf("%d: %+v\n", i, val.Token.UidHash) validUids[val.Token.UidHash] = false } }