Checkpoint 5
This commit is contained in:
83
godoor.go
83
godoor.go
@@ -6,7 +6,6 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/joho/godotenv"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
@@ -15,8 +14,12 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"godoor/hash"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
import "time"
|
||||
|
||||
const wiegand_a = 17
|
||||
const wiegand_b = 18
|
||||
@@ -54,12 +57,20 @@ type KeepDoorOpen struct {
|
||||
timer *time.Timer
|
||||
}
|
||||
|
||||
type OpenedTimestamp struct {
|
||||
Opened time.Time
|
||||
Closed *time.Time
|
||||
}
|
||||
|
||||
var config Config
|
||||
var globalLock sync.Mutex
|
||||
var validUids ValidUids
|
||||
var wiegand Wiegand
|
||||
var keepDoorOpen KeepDoorOpen
|
||||
|
||||
var lastSyncedTimestamp *time.Time
|
||||
var openDoorTimestamps []OpenedTimestamp
|
||||
|
||||
func main() {
|
||||
|
||||
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
@@ -85,6 +96,7 @@ func main() {
|
||||
|
||||
// cleanup
|
||||
}
|
||||
|
||||
func setup() {
|
||||
log.Println("Started Setup")
|
||||
|
||||
@@ -99,6 +111,8 @@ func setup() {
|
||||
}
|
||||
log.Println("HW Setup done")
|
||||
|
||||
http.DefaultClient.Timeout = 120 * time.Second
|
||||
|
||||
go func() {
|
||||
for {
|
||||
waitEvents()
|
||||
@@ -107,8 +121,6 @@ func setup() {
|
||||
|
||||
log.Println("Initialized longpoll event loop")
|
||||
|
||||
http.DefaultClient.Timeout = 120 * time.Second
|
||||
|
||||
for {
|
||||
log.Println("Start initial token population")
|
||||
err := reloadTokens()
|
||||
@@ -120,6 +132,8 @@ func setup() {
|
||||
time.Sleep(10 * time.Second)
|
||||
}
|
||||
|
||||
go runHttpServer()
|
||||
|
||||
log.Println("Initial token population success")
|
||||
|
||||
go cardRunner(wiegand)
|
||||
@@ -127,13 +141,36 @@ func setup() {
|
||||
log.Println("Setup completed")
|
||||
}
|
||||
|
||||
func runHttpServer() {
|
||||
http.HandleFunc("/lastsync", func(w http.ResponseWriter, r *http.Request) {
|
||||
e := json.NewEncoder(w)
|
||||
e.Encode(map[string]any{
|
||||
"last_synced": lastSyncedTimestamp,
|
||||
})
|
||||
})
|
||||
http.HandleFunc("/opened", func(w http.ResponseWriter, r *http.Request) {
|
||||
e := json.NewEncoder(w)
|
||||
e.Encode(map[string]any{
|
||||
"open_timestamps": openDoorTimestamps,
|
||||
})
|
||||
})
|
||||
http.HandleFunc("/isopen", func(w http.ResponseWriter, r *http.Request) {
|
||||
e := json.NewEncoder(w)
|
||||
open, _ := wiegand.IsDoorOpen()
|
||||
e.Encode(map[string]any{
|
||||
"open": open,
|
||||
})
|
||||
})
|
||||
http.ListenAndServe(":3334", nil)
|
||||
}
|
||||
|
||||
func OpenAndCloseDoor(w Wiegand) error {
|
||||
err := w.OpenDoor()
|
||||
err := OpenDoor(w)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if keepDoorOpen.until.After(time.Now().Add(5 * time.Second)) {
|
||||
if keepDoorOpen.until.After(time.Now()) {
|
||||
fmt.Println("Door is already open")
|
||||
return nil
|
||||
}
|
||||
@@ -142,7 +179,7 @@ func OpenAndCloseDoor(w Wiegand) error {
|
||||
|
||||
time.Sleep(5 * time.Second)
|
||||
|
||||
err = w.CloseDoor()
|
||||
err = CloseDoor(w)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -151,6 +188,27 @@ func OpenAndCloseDoor(w Wiegand) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func OpenDoor(w Wiegand) error {
|
||||
open, _ := w.IsDoorOpen()
|
||||
if open {
|
||||
return nil
|
||||
}
|
||||
w.OpenDoor()
|
||||
openDoorTimestamps = append(openDoorTimestamps, OpenedTimestamp{Opened: time.Now(), Closed: nil})
|
||||
return nil
|
||||
}
|
||||
|
||||
func CloseDoor(w Wiegand) error {
|
||||
open, _ := w.IsDoorOpen()
|
||||
if !open {
|
||||
return nil
|
||||
}
|
||||
w.CloseDoor()
|
||||
t := time.Now()
|
||||
openDoorTimestamps[len(openDoorTimestamps)-1].Closed = &t
|
||||
return nil
|
||||
}
|
||||
|
||||
func cardRunner(w Wiegand) {
|
||||
for {
|
||||
card, err := w.GetCardUid()
|
||||
@@ -159,7 +217,7 @@ func cardRunner(w Wiegand) {
|
||||
}
|
||||
|
||||
printCardId(card)
|
||||
hashedHex := hashCardUid(card)
|
||||
hashedHex := hash.HashCardUid(card)
|
||||
log.Println(hashedHex)
|
||||
|
||||
globalLock.Lock()
|
||||
@@ -287,6 +345,9 @@ func reloadTokens() error {
|
||||
updateKeepOpenDoor(*cl.KeepOpenUntil)
|
||||
}
|
||||
|
||||
t := time.Now()
|
||||
lastSyncedTimestamp = &t
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -300,20 +361,20 @@ func updateKeepOpenDoor(newKeepOpenTime time.Time) {
|
||||
|
||||
if newKeepOpenTime.After(time.Now()) {
|
||||
log.Printf("Keeping door open until %v", newKeepOpenTime)
|
||||
wiegand.OpenDoor()
|
||||
OpenDoor(wiegand)
|
||||
timer := time.AfterFunc(time.Until(newKeepOpenTime), handleKeepDoorOpenCloseCleanup)
|
||||
keepDoorOpen = KeepDoorOpen{
|
||||
timer: timer,
|
||||
until: newKeepOpenTime,
|
||||
}
|
||||
} else {
|
||||
wiegand.CloseDoor()
|
||||
CloseDoor(wiegand)
|
||||
}
|
||||
}
|
||||
|
||||
func handleKeepDoorOpenCloseCleanup() {
|
||||
fmt.Println("Keep door open time is reached!")
|
||||
wiegand.CloseDoor()
|
||||
CloseDoor(wiegand)
|
||||
keepDoorOpen = KeepDoorOpen{}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user