-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
executable file
·223 lines (179 loc) · 3.84 KB
/
main.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package main
import (
"bufio"
"encoding/hex"
"flag"
"log"
"net"
"os"
"os/signal"
"runtime/debug"
"strings"
"syscall"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"github.com/tarm/serial"
)
func main() {
loop()
os.Exit(0)
}
func loop() {
log.Println("aquarat's AIS Dispatcher Started")
comPortID := flag.String("serialport",
"/dev/ttyUSB0",
"The serial port's path.",
)
comPortBaud := flag.Int("baud",
38400,
"The port baud.",
)
hostID := flag.String("host",
"5.9.207.224",
"Target host (IP or Domain Name).",
)
hostPort := flag.String("port",
"7018",
"The target UDP port.",
)
dbFile := flag.String("dbFile",
"/dev/shm/ais.db",
"The target SQLite DB file.",
)
dbUse := flag.Bool("useDB",
false,
"Should we write to the database ?")
flag.Parse()
dbChan := make(chan *[]byte, 1000)
go initDB(*dbFile, dbChan, dbUse)
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL)
c := &serial.Config{Name: *comPortID,
Baud: *comPortBaud,
}
s, err := serial.OpenPort(c)
if err != nil {
log.Fatal(err)
debug.PrintStack()
}
defer s.Close()
conn, err := net.Dial("udp", *hostID+":"+*hostPort)
if err != nil {
log.Println(err)
debug.PrintStack()
return
}
dieChan := make(chan bool)
sendChan := make(chan *[]byte, 1000)
go dispatch(sendChan, conn)
defer conn.Close()
defer close(sendChan)
defer close(dieChan)
defer close(dbChan)
go receiver(sendChan, dbChan, s, dieChan)
for range sigs {
break
}
dieChan <- true
close(dieChan)
}
type DBPacket struct {
gorm.Model
Payload string
}
func initDB(dbFile string, dbChan chan *[]byte, dbUse *bool) {
if *dbUse {
db, err := gorm.Open("sqlite3", dbFile)
defer db.Close()
if err != nil {
log.Fatalln(err)
debug.PrintStack()
}
db.CreateTable(DBPacket{})
db.AutoMigrate(DBPacket{})
for p := range dbChan {
obj := DBPacket{Payload: string(*p)}
db.Create(&obj)
}
} else {
for range dbChan {
}
}
}
func receiver(sendChan, dbChan chan *[]byte, s *serial.Port, dieChan chan bool) {
log.Println("receiver started")
bReader := bufio.NewReader(s)
for {
select {
case <-dieChan:
return
default:
buf, err := bReader.ReadBytes('\n')
if err != nil {
log.Println(err)
debug.PrintStack()
continue
}
buf = []byte(strings.TrimSpace(string(buf)))
if len(buf) < 10 {
continue
}
//$GPGGA,092750.000,5321.6802,N,00630.3372,W,1,8,1.03,61.7,M,55.2,M,,*76
if !isChecksumGood(buf) {
continue
}
bufPointer := &buf
sendChan <- bufPointer
dbChan <- bufPointer
}
}
}
func recovery() {
if err := recover(); err != nil {
log.Println("ERROR :", err)
debug.PrintStack()
}
}
func isChecksumGood(sentence []byte) (crcGood bool) {
{
defer recovery()
var (
origCRC string
crc byte = 0
)
indexCRC := strings.Index(string(sentence), "*") + 1
if len(sentence) < indexCRC || len(sentence) < 2 {
return
}
for _, j := range sentence[1 : indexCRC-1] {
crc ^= j
}
origCRC = string(sentence[indexCRC:])
if strings.TrimSpace(strings.ToUpper(hex.EncodeToString([]byte{crc}))) == strings.TrimSpace(strings.ToUpper(origCRC)) {
crcGood = true
}
}
return
}
func dispatch(a chan *[]byte, conn net.Conn) {
for p := range a {
_, err := conn.Write(*p)
CE(err)
log.Println("Tx :", string(*p))
break
}
}
func CE(e error) {
if e != nil {
log.Println(e)
debug.PrintStack()
}
}
/*!AIVDM,1,1,,A,344Nv<5P001DG;=dVBlTs76v0P00,0*6C
!AIVDM,1,1,,B,18uF3s?P001DDNadVB>`Mwv:00Rf,0*42
!AIVDM,2,1,9,A,53o=>V82Duqhh@hL000A:r0I8TA@`tJ0p4q<Dp1S4pS887890@FPH0l5?gwp,0*2B
!AIVDM,2,2,9,A,?wwh0000000,2*7A
!AIVDM,1,1,,B,13aOBG@0081DMuAdVov6BrMf0<1U,0*16
!AIVDM,1,1,,A,18ue2`70001DF1idVB=RRQO008D4,0*1A
!AIVDM,1,1,,B,13cW:J3uh11C;aidd=M:I5o20d0K,0*79
!AIVDM,2,1,0,B,53o=>V82Duqhh@hL000A:r0I8TA@`tJ0p4q<Dp*/