-
Notifications
You must be signed in to change notification settings - Fork 1
/
shipahoy.go
394 lines (343 loc) · 13.7 KB
/
shipahoy.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package shipahoy
import (
"fmt"
"math/rand"
"strconv"
"strings"
"time"
"github.com/erikbryant/aes"
"github.com/erikbryant/beepspeak"
"github.com/erikbryant/shipahoy/alert"
"github.com/erikbryant/shipahoy/database"
"github.com/erikbryant/shipahoy/marinetraffic"
"github.com/erikbryant/shipahoy/noaa"
"github.com/erikbryant/sh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FSILl7h+D6jVph2W8Ent4qeaA+O4HqiGlJNmZ7DSPwU4+fHgnfJCfN+2oL13GqohPA=="
interestingMMSI = map[string]bool{
"338691000": true, // Matthew Turner
"338359786": true, // Randy S Cummings (USACE, associated with Pacific Responder?)
}
uninterestingAIS = map[string]bool{
"Fishing vessel": true,
"Passenger ship": true,
"Passenger Ship": true,
"Pleasure craft": true,
"Sailing vessel": true,
// "Towing vessel": true,
// "Tug": true,
// "Unknown": true,
}
uninterestingMMSI = map[string]bool{
"367123640": true, // Hawk
"367389640": true, // Oski
"366990520": true, // Del Norte
"367566960": true, // F/V Pioneer
"367469070": true, // Sunset Hornblower
"338234637": true, // Hewescraft 220 OP
"366918840": true, // Happy Days
"338107922": true, // Misty Dawn
"367703860": true, // Vera Cruz
"338236492": true, // Round Midnight
"367517270": true, // Tesa
"367533950": true, // Sausalito Bmpress
"366831930": true, // Millennium
"366864140": true, // Naiad
"338303816": true, // Coastal 24
"319421001": true, // RIB 45
"338099564": true, // Wings
"338365361": true, // 15R2 (SAR)
"338194748": true, // Kranich
"367310560": true, // Kitty Kat
"366975760": true, // Royal Prince (Red & White ferry)
"538071541": true, // Miss Anna
"367773740": true, // Duet
"367654260": true, // Falcon
}
)
// init performs the pre-flight setup.
func init() {
rand.Seed(time.Now().Unix())
}
// myGeo returns the lat/lon pair of the location of the computer running this program.
func myGeo() (lat, lon float64) {
// myIP := web.Request("http://ifconfig.co/ip") <-- site has malware
location, err := web.RequestJSON("http://api.ipstack.com/check?access_key="+geoAPIKey, map[string]string{})
if err != nil {
fmt.Println("ERROR: Unable to get geo location. Assuming you are home. Message:", err)
return 37.8007, -122.4097
}
if location["error"] != nil {
fmt.Println("ERROR: Error getting geo location. Assuming you are home. Message:", location["error"])
return 37.8007, -122.4097
}
lat = location["latitude"].(float64)
lon = location["longitude"].(float64)
return lat, lon
}
// visibleFromApt returns a bool indicating whether the ship is visible
// from our apartment window.
func visibleFromApt(lat, lon float64) bool {
// The bounding box for the area visible from our apartment.
visibleLatA := 37.8052
visibleLonA := -122.48
visibleLatB := 37.8613
visibleLonB := -122.4092
// Note that A is the bottom left corner and B is the upper
// right corner, so we need to work out C and D which are the
// upper left and lower right corners.
latC := visibleLatB
latD := visibleLatA
lonC := visibleLonA
lonD := visibleLonB
// Is the ship within the bounding box of our visible area?
if lat < latD || lat > latC {
return false
}
if lon < lonC || lon > lonD {
return false
}
// Is the ship within our visible triangle (the bottom left
// triangle of the bounding box)? It is if the latitude is
// less than the latitude of the box's diagonal.
// x == longitude, y == latitude
m := (latC - latD) / (lonC - lonD)
b := latC - m*lonC
y := m*lon + b
if lat > y {
fmt.Println("3 ", lat, m, b, y, lon)
return false
}
return true
}
// hydrate consolidates ship information from multiple sources
func hydrate(vfDetails map[string]interface{}, mtDetails []map[string]string) database.Ship {
// Load any details we might already have.
details, _ := database.LookupShip(web.ToString(vfDetails["mmsi"]))
// Add fields from VesselFinder.
details.Beam = web.ToInt(vfDetails["aw"])
details.DW = web.ToInt(vfDetails["dw"])
details.DirectLink = web.ToString(vfDetails["directLink"])
details.Draught = web.ToFloat64(vfDetails["draught"]) / 10
details.GT = web.ToInt(vfDetails["gt"])
details.IMO = web.ToString(vfDetails["imo"])
details.LastPosUpdate = web.ToInt(vfDetails["ts"])
details.Lat = web.ToFloat64(vfDetails["lat"])
details.Length = web.ToInt(vfDetails["al"])
details.Lon = web.ToFloat64(vfDetails["lon"])
details.MMSI = web.ToString(vfDetails["mmsi"])
details.Name = web.ToString(vfDetails["name"])
details.NavigationalStatus = web.ToInt(vfDetails[".ns"])
details.Course = web.ToFloat64(vfDetails["cu"])
details.Speed = web.ToFloat64(vfDetails["ss"])
details.Type = web.ToString(vfDetails["type"])
details.Year = web.ToInt(vfDetails["y"])
// New fields
details.Flag = web.ToString(vfDetails["a2"])
details.Destination = web.ToString(vfDetails["dest"])
details.ETA = web.ToInt64(vfDetails["etaTS"])
if vfDetails["lc."] != nil {
details.LoadCondition = web.ToInt(vfDetails["lc."])
}
// details.InvalidDimensions
// details.MarineTrafficID
// details.RateOfTurn
// details.Heading
// Add fields from MarineTraffic.
for _, ship := range mtDetails {
if strings.EqualFold(ship["SHIPNAME"], details.Name) &&
strings.EqualFold(ship["FLAG"], details.Flag) {
details.InvalidDimensions = web.ToInt(ship["INVALID_DIMENSIONS"]) == 1
details.MarineTrafficID = web.ToInt64(ship["SHIP_ID"])
details.RateOfTurn = web.ToInt(ship["ROT"])
details.Heading = web.ToFloat64(ship["HEADING"])
}
}
return details
}
// lookAtShips looks for interesting ships in a given lat/lon region.
func lookAtShips(latA, lonA, latB, lonB float64) {
// Open channel
c := make(chan map[string]interface{}, 10)
// Get the MarineTraffic details for this area.
mtResponse, err := marinetraffic.ShipsInRegion("1309", "3165", "14")
if err != nil {
fmt.Println(err)
}
go vesselfinder.ShipsInRegion(latA, lonA, latB, lonB, c)
for {
// Read 'response' from channel.
vfResponse, ok := <-c
if !ok {
break
}
details := hydrate(vfResponse, mtResponse)
database.SaveShip(details)
// Ignore ships that are not visible from our apartment.
if !visibleFromApt(details.Lat, details.Lon) {
continue
}
// Ignore ships that have stale position data.
posAge := time.Now().Unix() - int64(details.LastPosUpdate)
if posAge > 60*30 { // 30 minutes
continue
}
// Ignore ships that are not moving.
switch details.NavigationalStatus {
case 0: // under way using engine
if details.Speed < 1.0 {
continue
}
case 1: // at anchor
continue
case 5: // moored
continue
case 15: // undefined / default
if details.Speed < 1.0 {
continue
}
}
if !interestingMMSI[details.MMSI] {
// Skip uninteresting ships.
if uninterestingAIS[details.Type] || uninterestingMMSI[details.MMSI] {
continue
}
}
// Tugs are only interesting if they are towing.
if details.Type == "Towing vessel" || details.Type == "Tug" {
switch details.NavigationalStatus {
case 0: // under way using engine
continue
case 1: // at anchor
continue
case 5: // moored
continue
case 15: // undefined / default
continue
}
}
// If we have recently seen this ship, skip it.
now := time.Now().Unix()
elapsed := now - database.LookupLastSighting(details)
if elapsed < 30*60 {
// The ship is still crossing the visible area.
// No need to alert a second time.
continue
}
// We have passed all the tests! Save and alert.
myLat, myLon := myGeo()
database.SaveSighting(details, myLat, myLon)
err := alert.Alert(details)
if err != nil {
fmt.Println(err)
return
}
}
}
// box returns a bounding box of the circle with center of the
// current location and radius of 'nmiles' nautical miles.
// Returns (latA, lonA, latB, lonB) Where A is the bottom left
// corner and B is the upper right corner.
func box(lat, lon float64, nmiles float64) (latA, lonA, latB, lonB float64) {
// Convert nautical miles to decimal degrees.
delta := nmiles / 60.0
bboxLatA := lat - delta
bboxLonA := lon - delta
bboxLatB := lat + delta
bboxLonB := lon + delta
return bboxLatA, bboxLonA, bboxLatB, bboxLonB
}
// scanNearby continually scans for ships within a given radius of this computer.
func scanNearby(sleepDuration time.Duration) {
for {
lat, lon := myGeo()
latA, lonA, latB, lonB := box(lat, lon, 30)
lookAtShips(latA, lonA, latB, lonB)
time.Sleep(sleepDuration)
}
}
// scanAptVisible continually scans for ships visible from our apartment.
func scanAptVisible(sleepDuration time.Duration) {
lat, lon := 37.82, -122.45 // Center of visible bay
latA, lonA, latB, lonB := box(lat, lon, 10)
for {
lookAtShips(latA, lonA, latB, lonB)
time.Sleep(sleepDuration)
}
}
// scanPlanet continually scans the entire planet for heretofore unseen ships.
func scanPlanet(sleepDuration time.Duration) {
for {
// Pick a random lat/lon box of size 'step' on the surface of the planet.
step := 10
latA := float64(rand.Intn(360-step) - 180)
lonA := float64(rand.Intn(360-step) - 180)
latB := latA + float64(step)
lonB := lonA + float64(step)
lookAtShips(latA, lonA, latB, lonB)
time.Sleep(sleepDuration)
}
}
// tides looks up instantaneous tide data for a given NOAA station.
func tides(sleepDuration time.Duration, station string) {
for {
reading, ok := noaa.Tides(station)
if ok {
fmt.Println("Reading:", reading)
}
time.Sleep(sleepDuration)
}
}
// airGap looks up instantaneous air gap (distance from bottom of bridge to water) for a given NOAA station.
func airGap(sleepDuration time.Duration, station string) {
for {
reading, ok := noaa.AirGap(station)
if ok {
fmt.Println("Reading:", reading)
}
time.Sleep(sleepDuration)
}
}
// dbStats prints interesting statistics about the size of the database.
func dbStats(sleepDuration time.Duration) {
for {
msg := ""
for table, count := range database.TableStats() {
msg += table + ": " + strconv.FormatInt(count, 10) + " "
}
if msg != "" {
fmt.Println("##", msg, "##")
}
time.Sleep(sleepDuration)
}
}
// Start is the entry point for the shipahoy module. It starts each of the scanners.
func Start(passPhrase string) error {
err := database.Open()
if err != nil {
return err
}
geoAPIKey, err = aes.Decrypt(geoAPIKeyCrypt, passPhrase)
if err != nil {
return err
}
err = beepspeak.InitSay(gcpAuthCrypt, passPhrase)
if err != nil {
return err
}
// go scanNearby(5 * 60 * time.Second)
go scanAptVisible(1 * 60 * time.Second)
go scanPlanet(2 * 60 * time.Second)
go tides(10*60*time.Second, "9414290")
go airGap(10*60*time.Second, "9414304")
go dbStats(10 * 60 * time.Second)
return nil
}
// Stop performs any needed shutdown.
func Stop() {
database.Close()
}