rasmus #4

Merged
rasmus merged 2 commits from :rasmus into master 2023-08-17 17:51:38 +00:00
2 changed files with 37 additions and 28 deletions
Showing only changes of commit 8c14e3bc41 - Show all commits

View File

@ -6,10 +6,6 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/joho/godotenv"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"io"
"log"
"net/http"
@ -22,13 +18,20 @@ import (
"syscall"
"time"
"github.com/joho/godotenv"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"godoor/hash"
)
const wiegand_a_default = 17
const wiegand_b_default = 18
const wiegand_bit_timeout = time.Millisecond * 8
const solenoid_default = 21
const (
wiegand_a_default = 17
wiegand_b_default = 18
wiegand_bit_timeout = time.Millisecond * 8
solenoid_default = 21
)
type card struct {
UidHash string `json:"uid_hash"`
@ -46,7 +49,7 @@ type ValidUids map[string]bool // bool has no meaning
type Config struct {
door string
uidSalt string
doorOpenTime int
doorOpenTime time.Duration
mock bool
api struct {
allowed string
@ -86,11 +89,13 @@ var Commit = func() string {
var Version string
var config Config
var globalLock sync.Mutex
var validUids ValidUids
var wiegand Wiegand
var keepDoorOpen KeepDoorOpen
var (
config Config
globalLock sync.Mutex
validUids ValidUids
wiegand Wiegand
keepDoorOpen KeepDoorOpen
)
var (
godoorBuildInfo = promauto.NewGaugeVec(prometheus.GaugeOpts{
@ -153,10 +158,16 @@ func loadConfig() {
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.doorOpenTime = 8 * time.Second
if doorOpenTimeStr, ok := os.LookupEnv("KDOORPI_OPEN_TIME"); ok {
if openTime, err := time.ParseDuration(doorOpenTimeStr); err != nil {
log.Printf("parsing KDOORPI_OPEN_TIME: %v, keeping default %v", err, config.doorOpenTime)
} else {
config.doorOpenTime = openTime
}
}
_, config.mock = os.LookupEnv("KDOORPI_MOCK_HW")
config.prometheusMetricsBind = os.Getenv("KDOORPI_PROMETHEUS_METRICS_BIND")
if config.prometheusMetricsBind == "" {
@ -254,7 +265,7 @@ func OpenAndCloseDoor(w Wiegand) error {
fmt.Println("Door is now open")
time.Sleep(time.Duration(config.doorOpenTime) * time.Second)
time.Sleep(config.doorOpenTime)
err = CloseDoor(w)
if err != nil {
@ -290,7 +301,7 @@ func cardRunner(w Wiegand) {
continue
}
//printCardId(card)
// printCardId(card)
hashedHex := hash.HashCardUid(card)
log.Println(hashedHex)
@ -302,7 +313,7 @@ func cardRunner(w Wiegand) {
err := sendSwipeEvent(hashedHex, ok)
if err != nil {
apiFailuresCount.WithLabelValues("swipe", config.api.swipe).Inc()
log.Println("Failed to send swipe event: %v", err)
log.Printf("Failed to send swipe event: %v", err)
}
}()
@ -343,7 +354,6 @@ func ParseNextMessage(r *bufio.Reader) (string, error) {
r.UnreadByte()
}
}
func waitEvents() error {
@ -384,9 +394,6 @@ func waitEvents() error {
}
}
log.Printf("%v\n", resp)
return nil
}
func reloadTokens() error {
@ -420,7 +427,7 @@ func reloadTokens() error {
validUids = make(ValidUids)
var totalCardCount int = 0
for i, val := range cl.AllowedUids {
//log.Printf("%d: %+v\n", i, val.Token.UidHash)
// log.Printf("%d: %+v\n", i, val.Token.UidHash)
validUids[val.Token.UidHash] = true
totalCardCount = i
}
@ -437,7 +444,6 @@ func reloadTokens() error {
}
func updateKeepOpenDoor(newKeepOpenTime time.Time) {
// is there one active?
if keepDoorOpen.timer != nil {
keepDoorOpen.timer.Stop()

View File

@ -217,7 +217,7 @@ func (doobserver *DoorBoyServer) postKeepDoorOpen(w http.ResponseWriter, r *http
rs, _ := io.ReadAll(r.Body)
parsedTime, err := time.Parse(time.RFC3339, strings.TrimSpace(string(rs)))
if err != nil {
fmt.Println("Error with parsing time: %v", err)
fmt.Printf("Error with parsing time: %v", err)
w.WriteHeader(http.StatusBadRequest)
}
keepOpenUntil = &parsedTime
@ -229,6 +229,9 @@ func main() {
var dumpKeys []cardToken
savedKeys, err := os.ReadFile("keys.json")
if err != nil {
log.Fatalf("reading saved keys from keys.json: %v", err)
}
err = json.Unmarshal(savedKeys, &dumpKeys)
if err == nil {
@ -268,7 +271,7 @@ func main() {
keyLock.Lock()
data, err := json.Marshal(keys)
if err == nil {
os.WriteFile("keys.json", data, 0600)
os.WriteFile("keys.json", data, 0o600)
log.Println("Saved keys successfully!")
}
keyLock.Unlock()