-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnfc-scanner.go
134 lines (112 loc) · 3 KB
/
nfc-scanner.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"bytes"
"os"
"fmt"
"log/syslog"
"net/http"
"net/url"
"io/ioutil"
"encoding/json"
"encoding/hex"
"github.com/fuzxxl/nfc/2.0/nfc"
"time"
)
var l *syslog.Writer
func init() {
l, _ = syslog.New(syslog.LOG_ERR, "nfc-scanner")
defer l.Close()
}
func main() {
access_token := get_access_token()
// Open first available device
device, err := nfc.Open("")
if (err != nil) {
panic(err)
}
// Discover NFC device's capabilities
modulations, err := device.SupportedModulations(nfc.TargetMode)
if (err != nil) {
panic(err)
}
rates, err := device.SupportedBaudRates(modulations[0])
if (err != nil) {
panic(err)
}
modulation := nfc.Modulation{modulations[0], rates[0]}
fmt.Println("Listening for NFC tags...")
err = device.InitiatorInit()
if (err != nil) {
panic(err)
}
var previous string
for {
time.Sleep(100 * time.Millisecond)
t, err := device.InitiatorSelectPassiveTarget(modulation, nil)
if (err == nfc.Error(nfc.ETIMEOUT)) {
// On read timeout error, just continue the loop
continue
} else if (err != nil) {
// Panic on any other error
panic(err)
}
if (t.String() != previous) {
target, _ := t.(*nfc.ISO14443aTarget)
hexUID := hex.EncodeToString(target.UID[:target.UIDLen])
fmt.Println("Read " + hexUID)
register_scan(hexUID, access_token)
} else {
// Delay continuation a bit to prevent hanging
time.Sleep(100 * time.Millisecond)
}
previous = t.String()
}
nfc.Device.Close(device)
}
func get_access_token() string {
Url := os.Getenv("API_URL") + "/oauth2/token"
Id := os.Getenv("CLIENT_ID")
Secret := os.Getenv("CLIENT_SECRET")
resp, err := http.PostForm(Url, url.Values{"grant_type": {"client_credentials"}, "client_id": {Id}, "client_secret": {Secret}})
if (err != nil) {
panic(err)
}
if (resp.StatusCode != http.StatusOK) {
panic("Could not get API access token: " + resp.Status)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
var token_response map[string]interface{}
err = json.Unmarshal(body, &token_response)
if (err != nil) {
panic(err)
}
return token_response["access_token"].(string)
}
func register_scan(rfid string, access_token string) {
object_id := os.Getenv("OBJECT_ID")
type ApiRequestBody struct {
Rfids []string `json:"rfids"`
ObjectId string `json:"object_id"`
}
request_body := ApiRequestBody{
Rfids: []string{rfid},
ObjectId: object_id,
}
json, err := json.Marshal(request_body)
if (err != nil) {
panic(err)
}
req, _ := http.NewRequest("POST", os.Getenv("API_URL") + "/tagger/actions", bytes.NewReader(json))
req.Header.Add("Authorization", "Bearer " + access_token)
req.Header.Add("Content-Type", "application/json2")
client := &http.Client{}
resp, _ := client.Do(req)
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
if (resp.StatusCode < 200 || resp.StatusCode >= 400) {
l.Err(resp.Status)
// Ignore any of the RFIDs that are not registered with Tagger
l.Err("Unregistered RFID: " + rfid + " with response: " + string(body))
}
}