-
Notifications
You must be signed in to change notification settings - Fork 4
/
scanner.go
166 lines (144 loc) · 3.55 KB
/
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package server
import (
"bytes"
"net"
"net/http"
"os"
"sync"
"time"
log "github.com/sirupsen/logrus"
)
type MsgPacket struct {
msg []byte
ts time.Time
homePatrol bool
}
type Locker struct {
sync.Mutex
state bool
name string
pktSent uint64
pktRecv uint64
}
type APRModeType string
type ASTModeType string
type Modal struct {
PSI bool // PSI Mode on/Off
WSClientConnected bool
ASTMode ASTModeType
APRMode APRModeType
}
type ScannerCtrl struct {
locker Locker
rq chan bool
wq chan bool
drained chan bool
radioMsg chan MsgPacket
hostMsg chan MsgPacket
conn *ScannerConn
s *http.Server
c chan os.Signal
GoProcDelay time.Duration
GoProcMultiplier time.Duration
mode Modal
incomingFile *AudioFeedFile
}
func (s *ScannerCtrl) IsLocked() bool {
var locked bool
s.locker.Lock()
if s.locker.state {
locked = true
log.Tracef("UDP Packets Sent: [%d] UDP Packets Recv: [%d]", s.locker.pktSent, s.locker.pktRecv)
} else {
locked = false
}
s.locker.Unlock()
return locked
}
func (s *ScannerCtrl) ReceiveFromRadioMsgChannel() (MsgPacket, bool) {
select {
case msgToHost := <-s.radioMsg:
elapsed := time.Since(msgToHost.ts)
log.Infof("Received Message: [%s] To Send To Host: %s", elapsed, msgToHost.msg)
return msgToHost, true
default:
time.Sleep(time.Millisecond * 50)
}
return MsgPacket{}, false
}
func (s *ScannerCtrl) SendToRadioMsgChannel(msg []byte) bool {
if !s.IsLocked() {
log.Debugln("RadioMsgChannel: No Listener to Receive Msg, Msg Not Sent")
// return false
}
pkt := MsgPacket{
msg: msg,
ts: time.Now(),
homePatrol: bytes.Contains(msg, []byte("\t")),
}
select {
case s.radioMsg <- pkt:
log.Debugf("Send Msg[ql=%d]: [%s] to Radio Msg Channel", len(s.radioMsg), msg)
return true
default:
log.Warnf("Queue Full, No Message Sent: %d", len(s.radioMsg))
time.Sleep(time.Millisecond * 50)
}
return false
}
func (s *ScannerCtrl) SendToHostMsgChannel(msg []byte) bool {
if !s.IsLocked() {
log.Debugln("HostMsgChannel: No Listener to Receive Msg, Msg Not Sent")
// return false
}
pkt := MsgPacket{
msg: msg,
ts: time.Now(),
homePatrol: bytes.Contains(msg, []byte("\t")),
}
if s.conn.Type == ConnTypeNetwork && !pkt.homePatrol && !bytes.HasSuffix(pkt.msg, []byte("\r")) {
pkt.msg = bytes.ReplaceAll(pkt.msg, []byte("\n"), []byte("\r"))
}
select {
case s.hostMsg <- pkt:
return true
default:
log.Warnf("Queue Full, No Message Sent: %d", len(s.radioMsg))
time.Sleep(time.Millisecond * 50)
}
return false
}
func (c *ScannerCtrl) drain() {
if c.conn.Type == ConnTypeUSB {
if flushErr := c.conn.usbConn.Flush(); flushErr != nil {
log.Fatalln("Error while flushing USB", flushErr)
}
return
}
buffer := make([]byte, 8192)
for {
// c.conn.SetReadDeadline(time.Now().Add(1 * time.Second))
_, err := c.conn.Read(buffer)
if err != nil {
if e, ok := err.(net.Error); !ok || !e.Timeout() {
} else {
log.Infoln("Drained...")
return
}
} else {
log.Infoln("Packet Draining on WS Close...")
}
}
}
func CreateScannerCtrl() *ScannerCtrl {
ctrl := &ScannerCtrl{}
ctrl.rq = make(chan bool, 1)
ctrl.wq = make(chan bool, 1)
ctrl.drained = make(chan bool, 1)
ctrl.radioMsg = make(chan MsgPacket, 100)
ctrl.hostMsg = make(chan MsgPacket, 100)
ctrl.c = make(chan os.Signal)
ctrl.GoProcDelay = DefaultGoProcDelay
ctrl.GoProcMultiplier = DefaultGoProcMultiplier
return ctrl
}