forked from craftamap/wishbone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
119 lines (102 loc) · 2.34 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main
import (
"bufio"
"flag"
"io/ioutil"
"log"
"strings"
"time"
"github.com/stianeikeland/go-rpio/v4"
"go.bug.st/serial"
)
var (
list = flag.String("list", "list.txt", "RFID list")
port = flag.String("port", "/dev/ttyUSB0", "reader device")
OpenPin rpio.Pin = rpio.Pin(22)
ClosePin rpio.Pin = rpio.Pin(27)
latestTimestamp time.Time
)
func getRFIDToken(port *serial.Port) chan string {
c := make(chan string)
go func() {
for {
rd := bufio.NewReader(*port)
res, err := rd.ReadBytes('\x03')
if err != nil {
// If there was an error while reading from the port,
// panic so daemon will restart
panic(err)
}
s := strings.Replace(string(res), "\x03", "", -1)
s = strings.Replace(s, "\x02", "", -1)
c <- s
}
}()
return c
}
func parseUserList() (map[string]string, error) {
users := map[string]string{}
bytes, err := ioutil.ReadFile(*list)
if err != nil {
return users, err
}
lines := strings.Split(string(bytes), "\n")
for _, line := range lines {
fields := strings.Fields(line)
if len(fields) > 1 {
users[fields[0]] = strings.Join(fields[1:], " ")
}
}
return users, nil
}
// If token only contains 0 and/or F's, its not a valid token
func isValid(token string) bool {
token = strings.ReplaceAll(token, "F", "")
token = strings.ReplaceAll(token, "0", "")
return len(token) > 0
}
func main() {
flag.Parse()
log.Println(" :: Starting sphincter rfid token...")
log.Println(" :::: Opening GPIO")
err := rpio.Open()
if err != nil {
log.Fatal(err)
}
OpenPin.Output()
ClosePin.Output()
log.Println(" :::: Reading list.txt")
users, err := parseUserList()
if err != nil {
log.Fatal(err)
}
log.Printf(" :::: Found %d users \n", len(users))
// log.Printf("%v\n", users)
log.Println(" :::: Connecting to Serial")
mode := &serial.Mode{
BaudRate: 9600,
}
port, err := serial.Open(*port, mode)
if err != nil {
log.Fatal(err)
}
log.Println(" :: Initialized!")
for msg := range getRFIDToken(&port) {
if time.Since(latestTimestamp) < 5*time.Second {
log.Println("Triggered too fast; skipped unlock")
continue
}
username, ok := users[msg]
if ok {
latestTimestamp = time.Now()
log.Printf("Hello %s %s", msg, username)
OpenPin.High()
time.Sleep(1 * time.Second)
OpenPin.Low()
} else {
if isValid(msg) {
log.Printf("Could not find key %s", msg)
}
}
}
}