use Duration for doorOpenTime + lint
Some checks failed
continuous-integration/drone/pr Build is failing

This commit is contained in:
rasmus 2023-08-17 20:14:38 +03:00
parent e0dacb1e4b
commit 8c14e3bc41
2 changed files with 37 additions and 28 deletions

View File

@ -6,10 +6,6 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "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" "io"
"log" "log"
"net/http" "net/http"
@ -22,13 +18,20 @@ import (
"syscall" "syscall"
"time" "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" "godoor/hash"
) )
const wiegand_a_default = 17 const (
const wiegand_b_default = 18 wiegand_a_default = 17
const wiegand_bit_timeout = time.Millisecond * 8 wiegand_b_default = 18
const solenoid_default = 21 wiegand_bit_timeout = time.Millisecond * 8
solenoid_default = 21
)
type card struct { type card struct {
UidHash string `json:"uid_hash"` UidHash string `json:"uid_hash"`
@ -46,7 +49,7 @@ type ValidUids map[string]bool // bool has no meaning
type Config struct { type Config struct {
door string door string
uidSalt string uidSalt string
doorOpenTime int doorOpenTime time.Duration
mock bool mock bool
api struct { api struct {
allowed string allowed string
@ -86,11 +89,13 @@ var Commit = func() string {
var Version string var Version string
var config Config var (
var globalLock sync.Mutex config Config
var validUids ValidUids globalLock sync.Mutex
var wiegand Wiegand validUids ValidUids
var keepDoorOpen KeepDoorOpen wiegand Wiegand
keepDoorOpen KeepDoorOpen
)
var ( var (
godoorBuildInfo = promauto.NewGaugeVec(prometheus.GaugeOpts{ godoorBuildInfo = promauto.NewGaugeVec(prometheus.GaugeOpts{
@ -153,10 +158,16 @@ func loadConfig() {
config.api.swipe = os.Getenv("KDOORPI_API_SWIPE") config.api.swipe = os.Getenv("KDOORPI_API_SWIPE")
config.api.key = os.Getenv("KDOORPI_API_KEY") config.api.key = os.Getenv("KDOORPI_API_KEY")
config.uidSalt = os.Getenv("KDOORPI_UID_SALT") config.uidSalt = os.Getenv("KDOORPI_UID_SALT")
config.doorOpenTime, err = strconv.Atoi(os.Getenv("KDOORPI_OPEN_TIME"))
if err != nil { config.doorOpenTime = 8 * time.Second
config.doorOpenTime = 3 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.mock = os.LookupEnv("KDOORPI_MOCK_HW")
config.prometheusMetricsBind = os.Getenv("KDOORPI_PROMETHEUS_METRICS_BIND") config.prometheusMetricsBind = os.Getenv("KDOORPI_PROMETHEUS_METRICS_BIND")
if config.prometheusMetricsBind == "" { if config.prometheusMetricsBind == "" {
@ -254,7 +265,7 @@ func OpenAndCloseDoor(w Wiegand) error {
fmt.Println("Door is now open") fmt.Println("Door is now open")
time.Sleep(time.Duration(config.doorOpenTime) * time.Second) time.Sleep(config.doorOpenTime)
err = CloseDoor(w) err = CloseDoor(w)
if err != nil { if err != nil {
@ -302,7 +313,7 @@ func cardRunner(w Wiegand) {
err := sendSwipeEvent(hashedHex, ok) err := sendSwipeEvent(hashedHex, ok)
if err != nil { if err != nil {
apiFailuresCount.WithLabelValues("swipe", config.api.swipe).Inc() 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() r.UnreadByte()
} }
} }
func waitEvents() error { func waitEvents() error {
@ -384,9 +394,6 @@ func waitEvents() error {
} }
} }
log.Printf("%v\n", resp)
return nil
} }
func reloadTokens() error { func reloadTokens() error {
@ -437,7 +444,6 @@ func reloadTokens() error {
} }
func updateKeepOpenDoor(newKeepOpenTime time.Time) { func updateKeepOpenDoor(newKeepOpenTime time.Time) {
// is there one active? // is there one active?
if keepDoorOpen.timer != nil { if keepDoorOpen.timer != nil {
keepDoorOpen.timer.Stop() keepDoorOpen.timer.Stop()

View File

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