forked from vipconsult/rpi-door-control
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.go
56 lines (48 loc) · 981 Bytes
/
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
package main
import (
"fmt"
"log"
"net"
"os"
"strings"
"time"
//"strconv"
"github.com/zserge/hid"
)
func loop(device hid.Device, addr string) {
if err := device.Open(); err != nil {
log.Println(os.Stderr, err.Error())
return
}
defer device.Close()
udpAddr, _ := net.ResolveUDPAddr("udp4", addr)
conn, err := net.DialUDP("udp", nil, udpAddr)
if err != nil {
fmt.Println(os.Stderr, err.Error())
os.Exit(1)
}
defer conn.Close()
for {
buf, err := device.Read(-1, 10*time.Second)
if (err == nil) {
line := string(buf[:])
//log.Println("LINE: " + line)
//log.Println("LENGTH: " + strconv.Itoa(len(line)))
if len(line) > 2 {
line = strings.Trim(line, "\x00")
line = "CHECK|" + line[1:] + "\x00"
log.Println(line)
conn.Write([]byte(line))
}
}
}
}
func main() {
if len(os.Args) < 2 {
fmt.Printf("USAGE: %s ip:port\n", os.Args[0])
return
}
hid.UsbWalk(func(device hid.Device) {
loop(device, os.Args[1])
})
}