-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSailingApp-Server.py
327 lines (277 loc) · 10.7 KB
/
SailingApp-Server.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
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
#!/usr/bin/env python
import logging
import os
import sys
import socket
import struct
from urlparse import urlparse
import tornado.httpserver
import tornado.ioloop
import tornado.iostream
import tornado.web
import tornado.httpclient
import tornado.httputil
from libtcd.api import Tcd
from libtcd.api import NodeFactor, NodeFactors
from libtcd.api import ReferenceStation
from math import radians, cos, sin, asin, sqrt, log10, floor
from datetime import datetime
from datetime import date
#TCD_FILENAME='/usr/share/xtide/harmonics-dwf-20100529-free.tcd'
TCD_FILENAME_FREE='./harmonics-dwf-20190620/harmonics-dwf-20190620-free.tcd'
TCD_FILENAME_NONFREE='/usr/share/xtide/harmonics-dwf-20100529-nonfree.tcd'
__all__ = ['MainHandler', 'run_server']
__tcd_free__ = Tcd.open(TCD_FILENAME_FREE)
__tcd_nonfree__ = Tcd.open(TCD_FILENAME_NONFREE)
# dict of stations, key is station id
__tcd_stations__ = {}
def roundsig(x, n=8):
if not x: return 0
power = -int(floor(log10(abs(x)))) + (n - 1)
factor = (10 ** power)
return round(x * factor) / factor
def debug(str):
print("%s: %s" % (datetime.now().strftime("%b %d %I:%M"), str))
#
# CalTopoHandler fetches a custom map from CalTopo and pulls the
# markers and folders out and structures them into the format that
# the SailingApp on the watch wants.
#
# URL format: /CalTopo/{calTopoMapId}
#
# output JSON:
# { groups: [ { folderName: name, marks: [ { name:name, y:lat, x:lon } ] } ] }
# or on error:
# { error: errorText }
#
class CalTopoHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self, caltopoId=None):
if caltopoId == None:
raise tornado.web.HTTPError(500)
http = tornado.httpclient.AsyncHTTPClient()
debug("Called by /CalTopo/" + caltopoId)
caltopoUrl = "https://caltopo.com/m/" + caltopoId + "?format=json"
debug("fetching: " + caltopoUrl)
http.fetch(caltopoUrl, callback=self.on_response)
def on_response(self, response):
if response.error: raise tornado.web.HTTPError(500)
mapData = tornado.escape.json_decode(response.body)
mapDataFeatures = mapData["features"]
folders = {}
# find all folders first
for feature in mapDataFeatures:
properties = feature['properties']
classType = properties['class']
featureId = feature['id']
if classType == 'Folder':
name = properties['title']
folders[featureId] = { 'name':name, 'marks': [] }
# find all marks second
for feature in mapDataFeatures:
properties = feature['properties']
classType = properties['class']
if classType == 'Marker':
name = properties['title']
if 'description' in properties:
name = name + ':' + properties['description']
# create a folder if one doesn't exist
if 'folderId' not in properties:
properties['folderId'] = -1
if -1 not in folders:
folders[-1] = { "name":"marks", "marks": [] }
# load rest of features
geometry = feature['geometry']
lat = geometry['coordinates'][1]
lon = geometry['coordinates'][0]
# create mark
m = {'n':name, 'lat':roundsig(lat), 'lon':roundsig(lon)}
# update folder
folderId = properties['folderId']
folders[folderId]["marks"].append(m)
folderList = []
for folderId in folders:
folders[folderId]["marks"] = sorted(folders[folderId]["marks"], key=lambda m: m["n"])
folderList.append(folders[folderId])
returnData = {"groups":folderList }
json = tornado.escape.json_encode(returnData)
self.write(json)
self.finish()
def on_response_old_format(self, response):
if response.error: raise tornado.web.HTTPError(500)
mapData = tornado.escape.json_decode(response.body)
mapDataFolder = mapData["Folder"]
mapDataMarker = mapData["Marker"]
folders = {}
for folder in mapDataFolder:
folders[folder["id"]] = { "name":folder["label"], "marks": [] }
for mark in mapDataMarker:
name = mark["label"]
# incorporate the comment into the name if it was specified
if "comments" in mark:
name = mark["label"] + ":" + mark["comments"]
# make a default folder if one wasn't specified
if "folderId" not in mark:
mark["folderId"] = -1
if -1 not in folders:
folders[-1] = { "name":"marks", "marks": [] }
m = {
"n":name,
"lat":roundsig(mark["position"]["lat"]),
"lon":roundsig(mark["position"]["lng"])
}
folderId = mark["folderId"]
folders[folderId]["marks"].append(m)
folderList = []
for folderId in folders:
folders[folderId]["marks"] = sorted(folders[folderId]["marks"], key=lambda m: m["n"])
folderList.append(folders[folderId])
returnData = {"groups":folderList }
json = tornado.escape.json_encode(returnData)
self.write(json)
self.finish()
#
# Return all of the tide stations available near a point on the earth
# sorted by distance
#
# URL format: /TideStations/{lat},{lon}
#
# output JSON:
# { stations: [ { id: id, name:name, distance:distanceInKm } ] }
# or on error:
# { error: errorText }
#
class TideStationsHandler(tornado.web.RequestHandler):
def get(self, latlon=None):
if latlon == None:
raise tornado.web.HTTPError(500)
latlon = latlon.split(',')
lat = float(latlon[0])
lon = float(latlon[1])
stations = [];
debug("tide station search near: %f,%f" % (lat,lon))
for id,t in __tcd_stations__.items():
km = self.measureDistance(lat, lon, t.latitude, t.longitude)
if km < 2000:
station = { "id":t.record_number, "name":t.name, "distance":km }
stations.append(station)
stations = sorted(stations, key=lambda station: station["distance"])
if len(stations) > 10: stations = stations[0:9]
returnData = {"stations":stations}
json = tornado.escape.json_encode(returnData)
self.write(json)
self.finish()
def measureDistance(self, lat1, lon1, lat2, lon2):
"""
Calculate the great circle distance between two points
on the earth (specified in decimal degrees)
"""
# convert decimal degrees to radians
lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
# haversine formula
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
c = 2 * asin(sqrt(a))
km = 6367 * c
return km
#
# Return all of the data necessary to compute tides for a tide station
#
# URL format: /TideStation/{id}
# id is a station ID from /TideStations/{lat},{lon}
#
# output JSON:
# { station:{ id:, name:, zone_offset:, datum:[],
# amp:[], epoch:[], speed:[], years:[],
# equilibrium:{ []..[] }, node_factor:{ []..[] }
# dict index into equilibrium and node_factor is the year for those
# parameters
# or on error:
# { error: errorText }
#
class TideStationHandler(tornado.web.RequestHandler):
def get(self, id):
if id == None:
debug("tide station lookup, error no station id")
returnData = {"error":"no station id specified"}
self.write(tornado.escape.json_encode(returnData))
self.finish()
return
id = int(id)
debug("tide station lookup for station id: %i" % (id))
if id not in __tcd_stations__:
debug("tide station lookup, error invalid station id")
returnData = {"error":"invalid station id"}
self.write(tornado.escape.json_encode(returnData))
self.finish()
return
station = __tcd_stations__[id]
amp = []
epoch = []
speed = []
e = {}
n = {}
year = date.today().year
years=range(year, year+3)
for co in station.coefficients:
amp.append(roundsig(co.amplitude, 6))
epoch.append(roundsig(co.epoch, 6))
speed.append(roundsig(co.constituent.speed, 6))
for y in years:
e[y] = []
n[y] = []
for co in station.coefficients:
e[y].append(roundsig(co.constituent.node_factors[y].equilibrium, 6))
n[y].append(roundsig(co.constituent.node_factors[y].node_factor, 5))
zone_offset = station.zone_offset.total_seconds() / 60
d = {
"id":id,
"name":station.name,
"zone_offset":zone_offset,
"datum":roundsig(station.datum_offset, 5),
"amp":amp,
"epoch":epoch,
"speed":speed,
"years":years,
"equilibrium":e,
"node_factor":n
}
debug("tide station lookup for station name: %s" % (station.name))
returnData = {"station":d}
json = tornado.escape.json_encode(returnData)
self.write(json)
self.finish()
# round a floating point number down to 4 trailing digits. This
# is to reduce the size of the return structure.
def round(self, f):
return float("%.4f" % f)
# start the web server
def run_server(port, start_ioloop=True):
app = tornado.web.Application([
(r'/CalTopo/([\d\w]{1,8})$', CalTopoHandler),
(r'/TideStations/([-\d\.]+\,[-\d\.]+)$', TideStationsHandler),
(r'/TideStation/(\d+)$', TideStationHandler),
])
app.listen(port)
ioloop = tornado.ioloop.IOLoop.instance()
if start_ioloop:
ioloop.start()
if __name__ == '__main__':
debug("Loading tide database")
# cache the stations that we can actually work with into an in-memory dict
for t in __tcd_nonfree__:
if type(t) is ReferenceStation and 'Current' not in t.name:
__tcd_stations__[t.record_number] = t
debug("There are %i reference tide stations (outside of US)" % len(__tcd_stations__))
for t in __tcd_free__:
if type(t) is ReferenceStation and 'Current' not in t.name:
__tcd_stations__[t.record_number] = t
debug("There are %i reference tide stations (total)" % len(__tcd_stations__))
# parse cmdline parameters
port = 18277
if len(sys.argv) > 1:
port = int(sys.argv[1])
# start the web server
debug("Starting SailingApp on port %d" % port)
run_server(port)