Checkpoint 5
This commit is contained in:
63
wiegand.go
63
wiegand.go
@@ -1,15 +1,11 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"golang.org/x/crypto/scrypt"
|
||||
"log"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
import (
|
||||
"github.com/warthog618/gpiod"
|
||||
)
|
||||
|
||||
@@ -29,41 +25,45 @@ type WiegandHW struct {
|
||||
bitTimeoutTimer *time.Timer
|
||||
|
||||
solenoidLine *gpiod.Line
|
||||
lock sync.RWMutex
|
||||
}
|
||||
|
||||
func (w *WiegandHW) OpenDoor() error {
|
||||
w.lock.Lock()
|
||||
defer w.lock.Unlock()
|
||||
|
||||
open, _ := w.isDoorOpen()
|
||||
if open {
|
||||
return nil
|
||||
}
|
||||
|
||||
return w.solenoidLine.SetValue(1)
|
||||
}
|
||||
|
||||
func (w *WiegandHW) CloseDoor() error {
|
||||
w.lock.Lock()
|
||||
defer w.lock.Unlock()
|
||||
|
||||
open, _ := w.isDoorOpen()
|
||||
if !open {
|
||||
return nil
|
||||
}
|
||||
|
||||
return w.solenoidLine.SetValue(0)
|
||||
}
|
||||
|
||||
func (w *WiegandHW) IsDoorOpen() (bool, error) {
|
||||
w.lock.RLock()
|
||||
defer w.lock.RUnlock()
|
||||
return w.isDoorOpen()
|
||||
}
|
||||
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)
|
||||
}
|
||||
fmt.Printf("\n")
|
||||
}
|
||||
|
||||
func hashCardUid(card uint64) string {
|
||||
b := make([]byte, 8)
|
||||
binary.LittleEndian.PutUint64(b, card)
|
||||
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
|
||||
}
|
||||
|
||||
hashedHex := hex.EncodeToString(hashed)
|
||||
return hashedHex
|
||||
}
|
||||
|
||||
func (w *WiegandHW) GetCardUid() (uint64, error) {
|
||||
// Wait for bit timeout
|
||||
@@ -78,7 +78,7 @@ func (w *WiegandHW) GetCardUid() (uint64, error) {
|
||||
}
|
||||
|
||||
var card uint64 = 0
|
||||
for i := 63; i != 0; i-- {
|
||||
for i := 63; i >= 0; i-- {
|
||||
if w.bits[i] == true {
|
||||
card |= 1 << (63 - i)
|
||||
}
|
||||
@@ -89,15 +89,15 @@ func (w *WiegandHW) GetCardUid() (uint64, error) {
|
||||
|
||||
func (w *WiegandHW) wiegandAEvent(evt gpiod.LineEvent) {
|
||||
w.bitTimeoutTimer.Reset(w.bitTimeout)
|
||||
w.bits[w.bitNr] = false
|
||||
fmt.Printf("0")
|
||||
w.bits[w.bitNr] = true
|
||||
fmt.Printf("1")
|
||||
w.bitNr += 1
|
||||
}
|
||||
|
||||
func (w *WiegandHW) wiegandBEvent(evt gpiod.LineEvent) {
|
||||
w.bitTimeoutTimer.Reset(w.bitTimeout)
|
||||
w.bits[w.bitNr] = true
|
||||
fmt.Printf("1")
|
||||
w.bits[w.bitNr] = false
|
||||
fmt.Printf("0")
|
||||
w.bitNr += 1
|
||||
}
|
||||
|
||||
@@ -139,3 +139,10 @@ func (w *WiegandHW) WiegandClose() {
|
||||
w.bLine.Close()
|
||||
w.solenoidLine.Close()
|
||||
}
|
||||
|
||||
func printCardId(card uint64) {
|
||||
for i := 0; i < 7; i++ {
|
||||
fmt.Printf("%02x", (card>>(8*i))&0xff)
|
||||
}
|
||||
fmt.Printf("\n")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user