-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhaddock.py
202 lines (170 loc) · 8.12 KB
/
haddock.py
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
'''
haddock.py
TODO: more log data viz -- distance since <date>? e.g.
'''
import os
import sys
import math
from rich.console import Console
from rich.markdown import Markdown
from sailaway import saillog
from nmea import NMEAUpdater
from utils import webviz, units, geo
MAX_LOG_ENTRIES = 8
# WIND
forceDescription = ["calm", "light airs", "light breeze", "gentle breeze", "moderate breeze", "fresh breeze", "strong breeze", "near gale", "full gale", "severe gale", "storm", "violent storm", "hurricane"]
windForceTable = [64,56,48,41,34,27,22,17,11,7,4,1]
# HEADINGS
headingNames = ["north", "north by northeast", "northeast", "east by northeast", "east", "east by southeast", "southeast", "south by southeast", "south", "south by southwest", "southwest", "west by southwest", "west", "west by northwest", "northwest", "north by northwest", "north"]
sailAttitudes = ["`in irons`","beating", "on a close reach", "on a reach", "on a broad reach", "running"]
# Converts a wind speed in knots to its corresponding Force level
def windSpeedToForceLevel(w):
for i in range(len(windForceTable)):
if w >= windForceTable[i]:
return 12-i
def windForceToDesc(f):
if f >= 0 and f < len(forceDescription):
return forceDescription[f]
return "Unknown"
def windForceToStr(f):
forceStr = "F" + str(f)
if f > 9:
forceStr = "`" + forceStr + "`"
elif f > 7:
forceStr = "**" + forceStr + "**"
return forceStr
# true if b is within (a-r, a+r]
def withinAngleRange(b, a, r):
mina = a-r
maxa = a+r
if b > mina and b <= maxa:
return True
return False
# returns the description of the heading
def headingDesc(h):
for i in range(len(headingNames)):
if withinAngleRange(h, i*22.5, 11.25):
return headingNames[i]
def sailAttitudeDesc(windAng):
for i in range(len(sailAttitudes)):
if withinAngleRange(windAng, i*30, 30):
return sailAttitudes[i]
port = 10110
def printArgs():
sys.exit("\nusage: haddock [port number]\n\nPort number is 10110 by default.\n")
if len(sys.argv) > 1:
port = sys.argv[1]
try:
port = int(port)
except ValueError:
printArgs()
console = Console()
# Initialize our NMEA server & background updater
updater = NMEAUpdater(port)
updater.start()
console.print(Markdown("### **HADDOCK** " + NMEAUpdater.version()))
print("")
# Initialize our logbook
logbook = updater.getLogbook()
while True:
boats = updater.getBoats()
for i in range(len(boats)):
boat = boats[i]
boatSpeed = int(round(units.mps_to_kts(boat['sog']),0))
boatLat, boatLon = geo.latlon_to_str(boat['latitude'], boat['longitude'])
boatHdg = geo.wrap_angle(boat['cog'])
headingTxt = headingDesc(boatHdg)
heelAngle = abs(int(round(boat['heeldegrees'],0)))
voyageStr = boat['voyage']
voyDiv = voyageStr.find(" -> ")
origin = voyageStr[0:voyDiv]
dest = voyageStr[voyDiv+4:]
locName = geo.nearestSea(boat['latitude'], boat['longitude'])
windSpeed = int(round(units.mps_to_kts(boat['tws']),0))
windDirection = geo.wrap_angle(boat['twd'])
windForce = windSpeedToForceLevel(windSpeed)
windForceStr = windForceToStr(windForce)
windForceDesc = windForceToDesc(windForce)
windHeadingDesc = headingDesc(windDirection)
sailAtt = sailAttitudeDesc(abs(boat['twa']))
console.print(Markdown("# (" + str(i) + ") *" + boat['boatname'] + "* - " + boat['boattype']))
console.print(Markdown("**Position:**\t" + locName + " (" + boatLat + ", " + boatLon + ")"))
console.print(Markdown("**Destination:**\t" + dest))
console.print(Markdown("**Conditions:**\t" + windForceStr + " - " + windForceDesc + " from " + windHeadingDesc + " at " + str(round(windSpeed,1)) + " knots "))
console.print(Markdown("**Heading:**\t" + str(int(round(boatHdg,0))) + "° (" + headingTxt + ") at " + str(boatSpeed) + " knots, " + sailAtt ))
if heelAngle >= 30:
print("")
console.print(Markdown("### WARNING: Heel angle " + str(heelAngle) + "°"))
print("")
if len(boats) > 1:
boatNum = input("Enter boat # (or press return to quit): ")
try:
boatNum = int(boatNum)
except ValueError:
updater.stop()
sys.exit()
else:
boatNum = 0
if boatNum >= 0 and boatNum < len(boats):
boat = boats[boatNum]
while True:
if len(boats) > 1:
console.print(Markdown("# *" + boat['boatname'] + "* - " + boat['boattype']))
console.print(Markdown("**(1)** `Read the logbook`"))
console.print(Markdown("**(2)** `Plot position on OpenSeaMap`"))
console.print(Markdown("**(3)** `Plot position on EarthWindMap`"))
console.print(Markdown("**(4)** `Provide NMEA source to external charting app`"))
print("")
choice = input("Enter # of option (or press return to go back): ")
try:
choice = int(choice)
except ValueError:
break
if choice == 1:
console.print(Markdown("# *" + boat['boatname'] + "* - Captain's Log"))
entries = logbook.getLog(boat['ubtnr'])
if len(entries) > MAX_LOG_ENTRIES:
showEntries = entries[len(entries)-MAX_LOG_ENTRIES:]
else:
showEntries = entries
for entry in showEntries:
boatLat, boatLon = geo.latlon_to_str(float(entry['lat']), float(entry['lon']))
console.print(Markdown("**" + saillog.logTimeToString(entry) + "** - *" + boatLat + ", " + boatLon + "*"))
console.print(Markdown("### Heading " + headingDesc(geo.wrap_angle(float(entry['cog']))) + " / " + str(int(round(float(entry['sog']),0))) + " knots / " + forceDescription[windSpeedToForceLevel(float(entry['windspd']))]))
print("")
if len(entries) > 2:
firstTime = entries[0]['zulu']
lastTime = entries[len(entries)-1]['zulu']
totalTimeHrs = (lastTime - firstTime).total_seconds() / (60*60)
if lastTime.year == firstTime.year:
firstTimeStr = firstTime.strftime("%b %d")
else:
firstTimeStr = firstTime.strftime("%b %d, %Y")
dist = 0
for i in range(len(entries)-1):
index = i+1
curEntry, prevEntry = entries[index], entries[i]
curLat, curLon = float(curEntry['lat']), float(curEntry['lon'])
prevLat, prevLon = float(prevEntry['lat']), float(prevEntry['lon'])
dist += geo.dist_coord(curLat, curLon, prevLat, prevLon)
rate = dist / totalTimeHrs
console.print(Markdown("**Distance since " + firstTimeStr + ":** " + str(round(dist,1)) + " nm"))
console.print(Markdown("**Average speed:** " + str(round(rate,1)) + " knots"))
console.print(Markdown("**Distance per day:** " + str(round(rate*24,1)) + " nm"))
print("")
input("(Press any key to continue)")
elif choice == 2 or choice == 3:
boatLat = str(round(boat['latitude'],4))
boatLon = str(round(boat['longitude'],4))
if choice == 2:
webviz.loadURL(webviz.openseamap(boatLat, boatLon))
else:
webviz.loadURL(webviz.earthwindmap(boatLat, boatLon))
elif choice == 4:
if boatNum == updater.getBoat():
print("\nYou're already serving NMEA sentences for this boat!\n")
else:
updater.setBoat(boatNum)
print("\nNow serving NMEA sentences for this boat on TCP port " + str(updater.getPort()) + ". This will continue in the background until you quit the application.\n")
input("(Press any key to continue)")
updater.stop()