11habitat .tracker =
2+ # configs
3+ color_names : [" red" , " blue" , " green" , " yellow" , " purple" , " orange" , " cyan" ]
4+ colors : [" #f00" , " blue" , " green" , " #ff0" , " #c700e6" , " #ff8a0f" , " #0fffca" ]
5+
6+ # reserved
7+ db : null
8+ Vehicle : null
9+ Balloon : null
10+
11+ # properties
12+ color_idx : 0
213 flights :
314 active : []
415 upcoming : []
516 options : {}
6- vehicles : []
17+ vehicles : {}
18+ map : null
19+
20+ # methods
21+ init : (opts ) ->
22+ if @map then return
23+
24+ if opts?
25+ @options = opts
26+
27+ @map = new google.maps.Map document .getElementById (' map' ),
28+ zoom : 5
29+ center : new google.maps.LatLng 53.467511 ,- 2.2338940
30+ mapTypeId : google .maps .MapTypeId .ROADMAP
31+ keyboardShortcuts : false
32+ streetViewControl : false
33+ rotateControl : false
34+ panControl : false
35+ scaleControl : false
36+ zoomContro : true
37+ zoomControlOptions :
38+ style : google .maps .ZoomControlStyle .LARGE
39+ scrollwheel : true
40+
41+ @map ._overlay = new google.maps.OverlayView ()
42+ @map ._overlay .draw = ->
43+ @map ._overlay .setMap @map
44+
45+ # pull flight list
46+ @ update_flight_list ()
47+
48+ # wait for the map to load
49+ google .maps .event .addListenerOnce @map ,' tilesloaded' , ->
50+ habitat .tracker .mapLoaded ()
51+ null
52+
53+ mapLoaded : ->
54+ tmp = window .location .search .split (' =' )
55+ if tmp[0 ] is " ?ids"
56+ list = tmp[1 ].split (' ,' )
757
8- ### reset tracker state to pre-init state ###
58+ for id in list
59+ @db .get_telemetry_by_id (@consumer , id)
60+ else
61+ @ fetch_test ()
62+
63+ null
64+
65+ # reset tracker state to pre-init state
966 reset : ->
10- delete this . flights
11- delete this . options
12- delete this . vehicles
67+ delete @ flights
68+ delete @ options
69+ delete @ vehicles
1370
14- this . flights =
71+ @ flights =
1572 active : []
1673 upcoming : []
17- this .options = {}
18- this .vehicles = []
19-
20- init : (options ) ->
21- options = if options then options else {}
22- options .filter = if options .filter then open|| null
23- options .poll = options .poll || true
24- this .options = options
25-
26- """ pull flight list """
27- this .update_flight_list ()
74+ @options = {}
75+ @vehicles = []
2876
77+ # gets the latest flight from habitat
2978 update_flight_list : ->
30- this . db .get_flights (data) ->
79+ @ db .get_flights (data) ->
3180 current_ts = habitat .util .timestamp_now ()
3281
3382 for flight in data
@@ -40,16 +89,99 @@ habitat.tracker =
4089
4190 null
4291
43- test : ->
92+ fetch_latest_telemetry : ->
93+ for flight in @flights .active
94+ for payload in flight .payloads
95+ @db .get_telemetry_by_id @ consumer payload ._id
96+ null
97+
98+ # chews through any data from habitat
99+ consumer : (habitat_result ) ->
100+ if habitat_result .length == 0 then return
101+
102+ for row in habitat_result
103+ switch row .doc .type
104+ when " payload_telemetry" then habitat .tracker .process_telemetry row
105+ when " listener_telemetry" then habitat .tracker .process_listener row
106+ else continue
107+
108+ habitat .tracker .refresh ()
109+ null
110+
111+ refresh : ->
112+ for key of @vehicles
113+ @vehicles [key].redraw ()
114+ null
115+
116+ process_telemetry : (row ) ->
117+ doc = row[' doc' ]
118+ key = row[' key' ]
119+ ts = key[- 1 .. ][0 ]
120+
121+ if not @vehicles [doc .data ._parsed .payload_configuration ]?
122+ @vehicles [doc .data ._parsed .payload_configuration ] = new habitat.tracker.Balloon
123+ map : @map
124+ name : doc .data .payload
125+ color : @color_names [@color_idx ++ % @color_names .length ]
126+
127+ veh = @vehicles [doc .data ._parsed .payload_configuration ]
128+
129+ # if initial packet has 0,0 lat/long, drop it
130+ if veh .path .length is 0 and doc .data .latitude is 0 and doc .data .longitude is 0 then return
131+
132+ # if packet is out of order, drop it
133+ if ts <= veh .timestamp then return
134+ veh .timestamp = ts
135+
136+ veh .setAltitude doc .data .altitude
137+ veh .addPosition doc .data .latitude , doc .data .longitude
138+
139+ telemetry = {}
140+
141+ for key,val of doc .data
142+ if key[0 ] is ' _' then continue
143+
144+ switch key
145+ when " altitude" ," payload" ," latitude" ," longitude" ," time"
146+ continue
147+ else
148+ telemetry[key] = val
149+
150+ veh .setTelemetry telemetry
151+ null
152+
153+ process_listener : (row ) ->
154+ null
155+
156+ # tempolary methods
157+ fetch_test : ->
158+ @db .get_telemetry_by_id (@consumer , " b177187f988c44cce53eca6106381564" )
159+
160+ clear : ->
161+ $ (' body' ).html ' '
162+
163+ list : ->
44164 out = $ (' body' )
165+ @clear
45166
46167 out .append (" Upcoming flights<br><br>" )
47- this . append (this . flights .upcoming )
168+ @ append (@ flights .upcoming )
48169 out .append (" <br>Active flights<br><br>" )
49- this . append (this . flights .active )
170+ @ append (@ flights .active )
50171
51172 append : (list ) ->
52173 out = $ (' body' )
53174
54175 for flight in list
55- out .append " #{ flight .start } #{ flight .name } [#{ flight ._id } ]<br />"
176+ out .append " #{ flight .start } #{ flight .name } [<i>#{ flight ._id } </i>]<br />"
177+ for payload, k in flight .payloads
178+ out .append if k == _len1- 1 then " \\ - " else " |-"
179+ out .append " #{ payload .name } [<i>#{ payload ._id } </i>]<br />"
180+
181+ null
182+
183+ map_pixel_offset : (pos , offset ) ->
184+ new_pos = @map ._overlay .getProjection ().fromLatLngToDivPixel pos
185+ new_pos .x += offset[0 ]
186+ new_pos .y += offset[1 ]
187+ @map ._overlay .getProjection ().fromDivPixelToLatLng new_pos
0 commit comments