Fetch tokens after every longpoll close event

This commit is contained in:
Arti Zirk 2023-08-11 00:57:24 +03:00
parent 2259d3ed6b
commit d4f2624635

View File

@ -195,27 +195,6 @@ func setup() {
http.DefaultClient.Timeout = 120 * time.Second http.DefaultClient.Timeout = 120 * time.Second
go func() {
for {
err := waitEvents()
if err != nil {
apiFailuresCount.WithLabelValues("longpoll", config.api.longpoll).Inc()
log.Printf("LongPoll for events failed: %v", err)
log.Println("Will try to LongPoll again in 120 seconds")
time.Sleep(120 * time.Second)
go func() {
err := reloadTokens()
if err != nil {
apiFailuresCount.WithLabelValues("allowed", config.api.allowed).Inc()
}
}()
}
time.Sleep(1 * time.Second)
}
}()
log.Println("Initialized longpoll event loop")
for { for {
log.Println("Start initial token population") log.Println("Start initial token population")
err := reloadTokens() err := reloadTokens()
@ -230,6 +209,27 @@ func setup() {
log.Println("Initial token population success") log.Println("Initial token population success")
go func() {
for {
err := waitEvents()
if err != nil {
apiFailuresCount.WithLabelValues("longpoll", config.api.longpoll).Inc()
log.Printf("LongPoll for events failed: %v", err)
log.Println("Will try to LongPoll again soon")
}
time.Sleep(1 * time.Second)
go func() {
err := reloadTokens()
if err != nil {
log.Printf("ReloadTokens failed: %q", err)
apiFailuresCount.WithLabelValues("allowed", config.api.allowed).Inc()
}
}()
}
}()
log.Println("Initialized longpoll event loop")
go cardRunner(wiegand) go cardRunner(wiegand)
log.Println("Setup completed") log.Println("Setup completed")
@ -290,7 +290,7 @@ func cardRunner(w Wiegand) {
continue continue
} }
printCardId(card) //printCardId(card)
hashedHex := hash.HashCardUid(card) hashedHex := hash.HashCardUid(card)
log.Println(hashedHex) log.Println(hashedHex)
@ -464,11 +464,11 @@ func handleKeepDoorOpenCloseCleanup() {
} }
func sendSwipeEvent(cardUidHash string, success bool) error { func sendSwipeEvent(cardUidHash string, success bool) error {
swipeEvent := map[string]string{ swipeEvent := map[string]any{
"uid_hash": cardUidHash, "uid_hash": cardUidHash,
"door": config.door, "door": config.door,
"timestamp": time.Now().Format(time.DateTime), "timestamp": time.Now().Format(time.RFC3339),
"success": fmt.Sprint(success), "success": success,
} }
data, err := json.Marshal(swipeEvent) data, err := json.Marshal(swipeEvent)
@ -481,6 +481,7 @@ func sendSwipeEvent(cardUidHash string, success bool) error {
return err return err
} }
req.Header.Add("KEY", config.api.key) req.Header.Add("KEY", config.api.key)
req.Header.Add("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req) resp, err := http.DefaultClient.Do(req)
if err != nil { if err != nil {
return err return err