forked from vipconsult/rpi-door-control
-
Notifications
You must be signed in to change notification settings - Fork 0
/
opener.go
66 lines (56 loc) · 1021 Bytes
/
opener.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
package main
import (
"fmt"
"log"
"net"
"os"
"strings"
"time"
"./display"
"./rpiGpio"
)
func main() {
if len(os.Args) < 2 {
fmt.Printf("USAGE: %s [port]\n", os.Args[0])
return
}
udpAddr, err := net.ResolveUDPAddr("udp4", ":"+os.Args[1])
if err != nil {
fmt.Println(os.Stderr, err.Error())
os.Exit(1)
}
conn, err := net.ListenUDP("udp", udpAddr)
if err != nil {
fmt.Println(os.Stderr, err.Error())
os.Exit(1)
}
for {
handleClient(conn)
}
}
func handleClient(conn *net.UDPConn) {
var buf [512]byte
len, _, err := conn.ReadFromUDP(buf[:])
if err != nil {
fmt.Fprint(os.Stderr, err.Error())
}
recieved := string(buf[:len])
log.Println(recieved)
match := "OPEN|"
if strings.HasPrefix(recieved, match) {
go func() {
display.Success()
}()
go func() {
delay, _ := time.ParseDuration("1s")
control := rpiGpio.NewControl("18", delay);
if err := control.Start(); err != nil {
log.Println(err)
}
}()
} else {
go func() {
display.Error()
}()
}
}