Checkpoint 3

This commit is contained in:
Arti Zirk
2023-07-29 20:04:19 +03:00
parent 2521a811f5
commit 172dbcc210
5 changed files with 154 additions and 55 deletions

View File

@@ -5,6 +5,7 @@ import (
"encoding/hex"
"fmt"
"golang.org/x/crypto/scrypt"
"log"
"time"
)
@@ -14,7 +15,9 @@ import (
type Wiegand interface {
GetCardUid() (uint64, error)
OpenDoor()
OpenDoor() error
CloseDoor() error
IsDoorOpen() (bool, error)
}
type WiegandHW struct {
@@ -28,15 +31,21 @@ type WiegandHW struct {
solenoidLine *gpiod.Line
}
func (w *WiegandHW) OpenDoor() {
fmt.Println("Open")
w.solenoidLine.SetValue(1)
d, _ := time.ParseDuration("500ms")
time.Sleep(d)
w.solenoidLine.SetValue(0)
fmt.Println("Close")
func (w *WiegandHW) OpenDoor() error {
return w.solenoidLine.SetValue(1)
}
func (w *WiegandHW) CloseDoor() error {
return w.solenoidLine.SetValue(0)
}
func (w *WiegandHW) IsDoorOpen() (bool, error) {
i, err := w.solenoidLine.Value()
if err != nil {
return false, err
}
return i == 1, nil
}
func printCardId(card uint64) {
for i := 0; i < 7; i++ {
fmt.Printf("%02x", (card>>(8*i))&0xff)
@@ -47,7 +56,7 @@ func printCardId(card uint64) {
func hashCardUid(card uint64) string {
b := make([]byte, 8)
binary.LittleEndian.PutUint64(b, card)
hashed, err := scrypt.Key(b, []byte(config.uid_salt), 16384, 8, 1, 64)
hashed, err := scrypt.Key(b, []byte(config.uidSalt), 16384, 8, 1, 64)
if err != nil {
panic(err) // can only happen when scrypt params are garbage
}
@@ -58,10 +67,9 @@ func hashCardUid(card uint64) string {
func (w *WiegandHW) GetCardUid() (uint64, error) {
// Wait for bit timeout
fmt.Printf("Waiting for bit timeout\n")
log.Printf("Waiting for bit timeout\n")
<-w.bitTimeoutTimer.C
fmt.Printf("\n")
defer func() { w.bitNr = 0 }()