godoor/godoor.go

451 lines
8.8 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 22:22:26 +00:00
"bufio"
2023-07-29 17:04:19 +00:00
"bytes"
2023-07-28 13:09:54 +00:00
"context"
2022-04-03 17:34:46 +00:00
"encoding/json"
"fmt"
2023-08-06 13:48:17 +00:00
"github.com/joho/godotenv"
2022-04-03 17:34:46 +00:00
"io"
2023-07-29 17:04:19 +00:00
"log"
2022-04-03 17:34:46 +00:00
"net/http"
2023-07-28 13:09:54 +00:00
"os"
"os/signal"
2023-08-06 13:48:17 +00:00
"strconv"
2023-07-28 22:22:26 +00:00
"strings"
"sync"
2023-07-28 13:09:54 +00:00
"syscall"
2023-07-30 10:38:47 +00:00
"time"
"godoor/hash"
2022-04-03 17:34:46 +00:00
)
2022-04-02 18:14:21 +00:00
2023-08-06 13:48:17 +00:00
const wiegand_a_default = 17
const wiegand_b_default = 18
2022-04-03 13:50:21 +00:00
const wiegand_bit_timeout = time.Millisecond * 8
2023-08-06 13:48:17 +00:00
const solenoid_default = 21
2022-04-02 18:14:21 +00:00
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"`
2023-07-29 22:03:52 +00:00
KeepOpenUntil *time.Time `json:"keep_open_until,omitempty"`
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 {
2023-07-29 17:04:19 +00:00
door string
uidSalt string
2023-08-06 13:48:17 +00:00
doorOpenTime int
2023-07-29 17:04:19 +00:00
mock bool
api struct {
2023-07-28 13:09:54 +00:00
allowed string
longpoll string
swipe string
key string
}
2023-08-06 13:48:17 +00:00
pins struct {
wiegandA int
wiegandB int
solenoid int
}
2023-07-28 13:09:54 +00:00
}
2023-07-29 22:03:52 +00:00
type KeepDoorOpen struct {
until time.Time
timer *time.Timer
}
2023-07-30 10:38:47 +00:00
type OpenedTimestamp struct {
Opened time.Time
Closed *time.Time
}
2023-07-28 13:09:54 +00:00
var config Config
2023-07-28 22:22:26 +00:00
var globalLock sync.Mutex
var validUids ValidUids
var wiegand Wiegand
2023-07-29 22:03:52 +00:00
var keepDoorOpen KeepDoorOpen
2023-07-28 13:09:54 +00:00
2023-07-30 10:38:47 +00:00
var lastSyncedTimestamp *time.Time
var openDoorTimestamps []OpenedTimestamp
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()
2023-08-06 13:48:17 +00:00
loadConfig()
2023-07-29 17:04:19 +00:00
go func() {
setup()
}()
<-ctx.Done()
log.Printf("Cleanup\n")
2023-07-28 13:09:54 +00:00
2023-07-29 17:04:19 +00:00
// cleanup
}
2023-07-30 10:38:47 +00:00
2023-08-06 13:48:17 +00:00
func loadConfig() {
var err error
log.Println("Loading .env config")
err = godotenv.Load()
if err != nil {
log.Println("Failed to load .env config, using internal defaults")
}
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.uidSalt = os.Getenv("KDOORPI_UID_SALT")
config.doorOpenTime, err = strconv.Atoi(os.Getenv("KDOORPI_OPEN_TIME"))
if err != nil {
config.doorOpenTime = 3
}
_, config.mock = os.LookupEnv("KDOORPI_MOCK_HW")
config.pins.wiegandA, err = strconv.Atoi(os.Getenv("KDOORPI_PIN_WIEGAND_A"))
if err != nil {
config.pins.wiegandA = wiegand_a_default
}
config.pins.wiegandB, err = strconv.Atoi(os.Getenv("KDOORPI_PIN_WIEGAND_B"))
if err != nil {
config.pins.wiegandB = wiegand_b_default
}
config.pins.solenoid, err = strconv.Atoi(os.Getenv("KDOORPI_PIN_SOLENOID"))
if err != nil {
config.pins.solenoid = solenoid_default
}
}
2023-07-29 17:04:19 +00:00
func setup() {
log.Println("Started Setup")
if config.mock {
log.Println("MOCK mode enabled")
2023-07-29 22:03:52 +00:00
if config.door == "" {
config.door = "mockdoor"
}
2023-07-28 22:22:26 +00:00
wiegand = &WiegandMock{}
} else {
2023-08-06 13:48:17 +00:00
wiegand = WiegandSetup(config.pins.wiegandA, config.pins.wiegandB, wiegand_bit_timeout, config.pins.solenoid)
2023-07-28 22:22:26 +00:00
}
2023-07-29 17:04:19 +00:00
log.Println("HW Setup done")
2023-07-30 10:38:47 +00:00
http.DefaultClient.Timeout = 120 * time.Second
2023-07-29 17:04:19 +00:00
go func() {
for {
2023-08-03 08:03:43 +00:00
err := waitEvents()
if err != nil {
log.Printf("LongPoll for events failed: %v", err)
log.Println("Will try to LongPoll again in 120 seconds")
time.Sleep(120 * time.Second)
go reloadTokens()
}
time.Sleep(1 * time.Second)
2023-07-29 17:04:19 +00:00
}
}()
log.Println("Initialized longpoll event loop")
2023-07-28 13:09:54 +00:00
2023-07-28 22:22:26 +00:00
for {
2023-07-29 17:04:19 +00:00
log.Println("Start initial token population")
2023-07-28 22:22:26 +00:00
err := reloadTokens()
if err == nil {
break
}
2023-07-29 17:04:19 +00:00
log.Printf("Initial token population failed. err: %v", err)
log.Println("Retrying in 10 seconds...")
2023-07-28 22:22:26 +00:00
time.Sleep(10 * time.Second)
}
2023-07-28 13:09:54 +00:00
2023-07-30 10:38:47 +00:00
go runHttpServer()
2023-07-29 17:04:19 +00:00
log.Println("Initial token population success")
2023-07-28 22:22:26 +00:00
go cardRunner(wiegand)
2023-07-28 13:09:54 +00:00
2023-07-29 17:04:19 +00:00
log.Println("Setup completed")
}
2023-07-28 13:09:54 +00:00
2023-07-30 10:38:47 +00:00
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)
}
2023-07-29 17:04:19 +00:00
func OpenAndCloseDoor(w Wiegand) error {
2023-07-30 10:38:47 +00:00
err := OpenDoor(w)
2023-07-29 17:04:19 +00:00
if err != nil {
return err
}
2023-07-29 22:03:52 +00:00
2023-07-30 10:38:47 +00:00
if keepDoorOpen.until.After(time.Now()) {
2023-07-29 22:03:52 +00:00
fmt.Println("Door is already open")
return nil
}
2023-07-29 17:04:19 +00:00
fmt.Println("Door is now open")
2023-07-28 13:09:54 +00:00
2023-08-06 13:48:17 +00:00
time.Sleep(time.Duration(config.doorOpenTime) * time.Second)
2023-07-28 13:09:54 +00:00
2023-07-30 10:38:47 +00:00
err = CloseDoor(w)
2023-07-29 17:04:19 +00:00
if err != nil {
return err
}
fmt.Println("Door is now closed")
return nil
2023-07-28 13:09:54 +00:00
}
2023-07-30 10:38:47 +00:00
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
}
2023-07-28 22:22:26 +00:00
func cardRunner(w Wiegand) {
for {
card, err := w.GetCardUid()
if err != nil {
continue
}
printCardId(card)
2023-07-30 10:38:47 +00:00
hashedHex := hash.HashCardUid(card)
2023-07-29 17:04:19 +00:00
log.Println(hashedHex)
2023-07-29 22:03:52 +00:00
globalLock.Lock()
ok := validUids[hashedHex]
globalLock.Unlock()
2023-07-29 17:04:19 +00:00
go func() {
2023-07-29 22:03:52 +00:00
err := sendSwipeEvent(hashedHex, ok)
2023-07-29 17:04:19 +00:00
if err != nil {
log.Println("Failed to send swipe event: %v", err)
}
}()
2023-07-28 22:22:26 +00:00
if ok {
2023-07-29 17:04:19 +00:00
log.Println("Opening door")
err := OpenAndCloseDoor(w)
if err != nil {
log.Println("There was an error opening and closing the Door")
}
2023-07-28 22:22:26 +00:00
} else {
2023-07-29 17:04:19 +00:00
log.Println("Unknown card")
2023-07-28 22:22:26 +00:00
}
}
}
func ParseNextMessage(r *bufio.Reader) (string, error) {
var message string
for {
s, err := r.ReadString('\n')
if err != nil {
return message, err
}
message = message + s
nextBytes, err := r.ReadByte()
if err != nil {
return message, err
}
if nextBytes == '\n' {
return message, nil
}
r.UnreadByte()
}
}
func waitEvents() error {
2023-07-28 13:09:54 +00:00
req, err := http.NewRequest(http.MethodGet, config.api.longpoll, nil)
2022-04-03 17:34:46 +00:00
if err != nil {
2023-07-28 22:22:26 +00:00
return err
2022-04-03 17:34:46 +00:00
}
2023-07-28 13:09:54 +00:00
req.Header.Add("KEY", config.api.key)
resp, err := http.DefaultClient.Do(req)
if err != nil {
2023-07-28 22:22:26 +00:00
return err
2023-07-28 13:09:54 +00:00
}
2023-07-29 17:04:19 +00:00
log.Printf("%v\n", resp)
2023-07-28 13:09:54 +00:00
2023-07-28 22:22:26 +00:00
reader := bufio.NewReader(resp.Body)
for {
msg, err := ParseNextMessage(reader)
if err != nil {
return err
}
for _, line := range strings.Split(msg, "\n") {
data, found_data := strings.CutPrefix(line, "data:")
if !found_data {
continue
}
2023-07-29 17:04:19 +00:00
log.Printf("got server data: %q\n", data)
2023-07-28 22:22:26 +00:00
if strings.TrimSpace(data) == config.door {
2023-07-29 17:04:19 +00:00
err := OpenAndCloseDoor(wiegand)
if err != nil {
log.Println("There was an error opening and closing the Door")
}
2023-07-28 22:22:26 +00:00
}
}
2023-07-28 13:09:54 +00:00
}
2023-07-29 17:04:19 +00:00
log.Printf("%v\n", resp)
2023-07-28 22:22:26 +00:00
return nil
2023-07-28 13:09:54 +00:00
}
2023-07-28 22:22:26 +00:00
func reloadTokens() error {
2023-07-28 13:09:54 +00:00
req, err := http.NewRequest(http.MethodGet, config.api.allowed, nil)
if err != nil {
2023-07-28 22:22:26 +00:00
return err
2023-07-28 13:09:54 +00:00
}
req.Header.Add("KEY", config.api.key)
resp, err := http.DefaultClient.Do(req)
2022-04-03 17:34:46 +00:00
if err != nil {
2023-07-28 22:22:26 +00:00
return err
2022-04-03 17:34:46 +00:00
}
2023-08-03 08:03:43 +00:00
if resp.StatusCode != 200 {
log.Printf("%v\n", resp)
}
2022-04-03 17:34:46 +00:00
var cl cardList
body, err := io.ReadAll(resp.Body)
if err != nil {
2023-07-28 22:22:26 +00:00
return err
2022-04-03 17:34:46 +00:00
}
err = json.Unmarshal(body, &cl)
if err != nil {
2023-07-28 22:22:26 +00:00
return err
2022-04-03 17:34:46 +00:00
}
2023-07-28 22:22:26 +00:00
globalLock.Lock()
defer globalLock.Unlock()
validUids = make(ValidUids)
var totalCardCount int = 0
2022-04-03 17:34:46 +00:00
for i, val := range cl.AllowedUids {
2023-08-03 08:03:43 +00:00
//log.Printf("%d: %+v\n", i, val.Token.UidHash)
2023-07-28 22:22:26 +00:00
validUids[val.Token.UidHash] = true
totalCardCount = i
2022-04-03 17:34:46 +00:00
}
log.Printf("Got %d cards from server", totalCardCount)
2023-07-28 22:22:26 +00:00
2023-07-29 22:03:52 +00:00
if cl.KeepOpenUntil != nil {
updateKeepOpenDoor(*cl.KeepOpenUntil)
}
2023-07-30 10:38:47 +00:00
t := time.Now()
lastSyncedTimestamp = &t
2023-07-28 22:22:26 +00:00
return nil
2022-04-02 18:14:21 +00:00
}
2023-07-29 17:04:19 +00:00
2023-07-29 22:03:52 +00:00
func updateKeepOpenDoor(newKeepOpenTime time.Time) {
// is there one active?
if keepDoorOpen.timer != nil {
keepDoorOpen.timer.Stop()
keepDoorOpen = KeepDoorOpen{}
}
if newKeepOpenTime.After(time.Now()) {
log.Printf("Keeping door open until %v", newKeepOpenTime)
2023-07-30 10:38:47 +00:00
OpenDoor(wiegand)
2023-07-29 22:03:52 +00:00
timer := time.AfterFunc(time.Until(newKeepOpenTime), handleKeepDoorOpenCloseCleanup)
keepDoorOpen = KeepDoorOpen{
timer: timer,
until: newKeepOpenTime,
}
} else {
2023-07-30 10:38:47 +00:00
CloseDoor(wiegand)
2023-07-29 22:03:52 +00:00
}
}
func handleKeepDoorOpenCloseCleanup() {
fmt.Println("Keep door open time is reached!")
2023-07-30 10:38:47 +00:00
CloseDoor(wiegand)
2023-07-29 22:03:52 +00:00
keepDoorOpen = KeepDoorOpen{}
}
func sendSwipeEvent(cardUidHash string, success bool) error {
2023-07-29 17:04:19 +00:00
swipeEvent := map[string]string{
"uid_hash": cardUidHash,
"door": config.door,
"timestamp": time.Now().Format(time.DateTime),
2023-07-29 22:03:52 +00:00
"success": fmt.Sprint(success),
2023-07-29 17:04:19 +00:00
}
data, err := json.Marshal(swipeEvent)
if err != nil {
return err
}
req, err := http.NewRequest(http.MethodPost, config.api.swipe, bytes.NewReader(data))
if err != nil {
return err
}
req.Header.Add("KEY", config.api.key)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
if !(resp.StatusCode >= 200 && resp.StatusCode < 300) {
return fmt.Errorf("Server responded with %d", resp.StatusCode)
}
return nil
}