/
run.go
150 lines (119 loc) · 3.35 KB
/
run.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package commands
import (
"encoding/hex"
"errors"
"fmt"
"net"
"os"
"os/signal"
"regexp"
"strings"
codec "github.com/uhppoted/uhppote-core/encoding/UTO311-L0x"
"github.com/uhppoted/uhppote-core/messages"
"github.com/uhppoted/uhppote-simulator/rest"
"github.com/uhppoted/uhppote-simulator/simulator"
"github.com/uhppoted/uhppote-simulator/simulator/UT0311L04"
)
var debug bool = false
func Simulate(ctx *simulator.Context, dbg bool) {
debug = dbg
bind, err := net.ResolveUDPAddr("udp4", ctx.BindAddress)
if err != nil {
fmt.Printf("%v\n", errors.New(fmt.Sprintf("Failed to resolve UDP bind address [%v]", err)))
return
}
connection, err := net.ListenUDP("udp", bind)
if err != nil {
fmt.Printf("%v\n", errors.New(fmt.Sprintf("Failed to bind to UDP socket [%v]", err)))
return
}
defer connection.Close()
fmt.Printf(" ... bound to address '%s'\n", bind)
wait := make(chan int, 1)
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
go run(ctx, connection, wait)
<-interrupt
connection.Close()
<-wait
os.Exit(1)
}
func run(ctx *simulator.Context, connection *net.UDPConn, wait chan int) {
fmt.Println()
go func() {
err := listenAndServe(ctx, connection)
if err != nil {
fmt.Printf("%v\n", err)
}
wait <- 0
}()
go func() {
for {
msg := ctx.DeviceList.GetMessage()
send(connection, msg.Destination, msg.Message)
}
}()
go func() {
rest.Run(ctx)
}()
}
func listenAndServe(ctx *simulator.Context, c *net.UDPConn) error {
for {
request, remote, err := receive(c)
if err != nil {
return err
}
handle(ctx, c, remote, request)
}
}
func handle(ctx *simulator.Context, c *net.UDPConn, src *net.UDPAddr, bytes []byte) {
request, err := messages.UnmarshalRequest(bytes)
if err != nil {
fmt.Printf("ERROR: %v\n", err)
return
}
f := func(s simulator.Simulator) {
s.Handle(src, request)
}
ctx.DeviceList.Apply(f)
}
func receive(c *net.UDPConn) ([]byte, *net.UDPAddr, error) {
request := make([]byte, 2048)
N, remote, err := c.ReadFromUDP(request)
if err != nil {
return []byte{}, nil, errors.New(fmt.Sprintf("Failed to read from UDP socket [%v]", err))
}
if debug {
fmt.Printf(" ... received %v bytes from %v\n%s\n", N, remote, dump(request[0:N], " ... "))
}
return request[:N], remote, nil
}
func send(c *net.UDPConn, dest *net.UDPAddr, message interface{}) {
msg, err := codec.Marshal(message)
if err != nil {
fmt.Printf("ERROR: %v\n", err)
return
}
// Firmware v6.62 and earlier apparently send 0x19 as the event message type.
// Identify (for simulation purposes only) these boards as having a serial number
// that starts with '0'. This assumes the convention that one door controllers have
// a serial number starting with 1, two door controllers start with 2, etc.
if event, ok := message.(*UT0311L04.Event); ok {
deviceID := fmt.Sprintf("%09d", event.SerialNumber)
if strings.HasPrefix(deviceID, "0") {
msg[0] = 0x19
}
}
N, err := c.WriteTo(msg, dest)
if err != nil {
fmt.Printf("ERROR: %v\n", errors.New(fmt.Sprintf("Failed to write to UDP socket [%v]", err)))
} else {
if debug {
fmt.Printf(" ... sent %v bytes to %v\n%s\n", N, dest, dump(msg[0:N], " ... "))
}
}
}
func dump(m []byte, prefix string) string {
regex := regexp.MustCompile("(?m)^(.*)")
return fmt.Sprintf("%s", regex.ReplaceAllString(hex.Dump(m), prefix+"$1"))
}