Checkpoint

This commit is contained in:
Arti Zirk
2023-07-28 16:09:54 +03:00
parent 846a610c58
commit d1348d1af9
5 changed files with 202 additions and 18 deletions

View File

@@ -1,10 +1,15 @@
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/joho/godotenv"
"io"
"net/http"
"os"
"os/signal"
"syscall"
)
import "time"
@@ -17,27 +22,91 @@ type card struct {
UidHash string `json:"uid_hash"`
}
type cardToken struct {
Token card `json:"token"`
type cardList struct {
AllowedUids []struct {
Token card `json:"token"`
} `json:"allowed_uids"`
}
type cardList struct {
AllowedUids []cardToken `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() {
wiegand := WiegandSetup(wiegand_a, wiegand_b, wiegand_bit_timeout, solenoid)
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer cancel()
client := http.Client{}
req, err := http.NewRequest(http.MethodGet, "urlrul", nil)
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", "keykey")
resp, err := client.Do(req)
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)
}
@@ -60,13 +129,4 @@ func main() {
fmt.Printf("%d: %+v\n", i, val.Token.UidHash)
validUids[val.Token.UidHash] = false
}
go wiegand.cardRunner(validUids)
fmt.Printf("Sleeping\n")
for {
time.Sleep(time.Second)
fmt.Printf(".")
}
}