godoor/godoor.go

133 lines
2.3 KiB
Go
Raw Normal View History

2022-04-02 18:14:21 +00:00
package main
2022-04-03 17:34:46 +00:00
import (
2023-07-28 13:09:54 +00:00
"context"
2022-04-03 17:34:46 +00:00
"encoding/json"
"fmt"
2023-07-28 13:09:54 +00:00
"github.com/joho/godotenv"
2022-04-03 17:34:46 +00:00
"io"
"net/http"
2023-07-28 13:09:54 +00:00
"os"
"os/signal"
"syscall"
2022-04-03 17:34:46 +00:00
)
2022-04-02 18:14:21 +00:00
import "time"
const wiegand_a = 17
const wiegand_b = 18
2022-04-03 13:50:21 +00:00
const wiegand_bit_timeout = time.Millisecond * 8
2022-04-02 18:14:21 +00:00
const solenoid = 21
2022-04-03 17:34:46 +00:00
type card struct {
UidHash string `json:"uid_hash"`
}
2023-07-28 13:09:54 +00:00
type cardList struct {
AllowedUids []struct {
Token card `json:"token"`
} `json:"allowed_uids"`
2022-04-03 17:34:46 +00:00
}
2023-07-28 13:09:54 +00:00
type simpleUids struct {
tokens []string
2022-04-03 17:34:46 +00:00
}
type ValidUids map[string]bool // bool has no meaning
2023-07-28 13:09:54 +00:00
type Config struct {
door string
uid_salt string
api struct {
allowed string
longpoll string
swipe string
key string
}
}
var config Config
2022-04-02 18:14:21 +00:00
func main() {
2023-07-28 13:09:54 +00:00
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
2022-04-02 18:14:21 +00:00
2023-07-28 13:09:54 +00:00
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)
2022-04-03 17:34:46 +00:00
if err != nil {
panic(err)
}
2023-07-28 13:09:54 +00:00
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)
2022-04-03 17:34:46 +00:00
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
}
2022-04-02 18:14:21 +00:00
}