forked from HSLdevcom/navigator-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Greater Manchester realtime feeds
- Loading branch information
Showing
3 changed files
with
150 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
net = require 'net' | ||
carrier = require 'carrier' | ||
|
||
col_names = [ | ||
"id", "name", "type", "ip", "lat", "lng", "speed", "bearing", | ||
"acceleration", "gps_time_difference", "unix_epoch_gps_time", "low_floor", "route", "direction", | ||
"departure", "departure_time", "departure_stars_in", "distance_from_start", "snapped_lat", "snapped_lng", | ||
"snapped_bearing", "next_stop_index", "on_stop", "difference_from_timetable" | ||
] | ||
|
||
class HSLClient | ||
constructor: (@callback, @args) -> | ||
|
||
connect: -> | ||
@client = net.connect 8080, '83.145.232.209' | ||
@client.on 'connect', (conn) => | ||
console.log "HSLClient connected" | ||
@client.write '&okffin;tuusula1 onroute:1&' | ||
|
||
line_handler = (line) => | ||
@.handle_line line | ||
@carrier = carrier.carry @client | ||
@carrier.on 'line', line_handler | ||
|
||
handle_line: (line) -> | ||
cols = line.split ';' | ||
if cols.length < 10 | ||
return | ||
info = {} | ||
for n, idx in col_names | ||
info[n] = cols[idx] | ||
# Skip line if no coordinates supplied | ||
if info.lat == '0' or info.lng == '0' | ||
return | ||
timestamp = (parseInt info.unix_epoch_gps_time) / 1000 | ||
if timestamp <= 0 | ||
return | ||
if not info.route | ||
return | ||
out_info = | ||
vehicle: | ||
id: info.id | ||
label: info.name | ||
trip: | ||
route: info.route | ||
direction: info.direction | ||
position: | ||
latitude: parseFloat info.lat | ||
longitude: parseFloat info.lng | ||
bearing: parseFloat info.bearing | ||
odometer: parseFloat info.distance_from_start | ||
speed: (parseFloat info.speed) / 3.6 | ||
timestamp: (parseInt info.unix_epoch_gps_time) / 1000 | ||
route = info.route.replace " ", "_" | ||
vehicle_id = out_info.vehicle.id.replace " ", "_" | ||
path = "/location/helsinki/#{route}/#{vehicle_id}" | ||
@callback path, out_info, @args | ||
|
||
module.exports.HSLClient = HSLClient |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
http = require 'http' | ||
|
||
|
||
class TfGMClient | ||
constructor: (@callback, @args) -> | ||
@dev_key = 'e5541db9-e410-45d0-8481-08adf0a193ce' | ||
@app_key = '2c4abc94-240f-490e-bb86-880471640b37' | ||
@routes = | ||
'MET3': | ||
gtfs_name: 'GMN: 3:C:' | ||
'MET2': | ||
gtfs_name: 'GMN: 2:C:' | ||
'MET1': | ||
gtfs_name: 'GMN: 1:C:' | ||
@poll_delay = 5 | ||
|
||
set_poll_timer: (route_name, is_error) -> | ||
route = @routes[route_name] | ||
if route.timeout | ||
clearTimeout route.timeout | ||
timeout_handler = => | ||
route.timeout = null | ||
@.poll_route route_name | ||
if is_error | ||
delay = @poll_delay * 10 | ||
else | ||
delay = @poll_delay | ||
delay *= 1000 | ||
route.timeout = setTimeout timeout_handler, delay | ||
|
||
update_location: (route_name, data) -> | ||
out_info = | ||
vehicle: | ||
id: data['Registration'] | ||
trip: | ||
route: @routes[route_name].gtfs_name | ||
timestamp: data['LastUpdated'] | ||
position: | ||
latitude: data['Latitude'] | ||
longitude: data['Longitude'] | ||
bearing: data['Heading'] | ||
route = out_info.trip.route.replace(/\ /g, "_").replace(/:/g, '-') | ||
path = "/location/manchester/#{route}/#{out_info.vehicle.id}" | ||
@callback path, out_info, @args | ||
|
||
poll_route: (route_name) -> | ||
route = @routes[route_name] | ||
opts = | ||
host: 'opendata.tfgm.com' | ||
path: "/api/routes/#{route_name}/buses" | ||
headers: | ||
'DevKey': @dev_key | ||
'AppKey': @app_key | ||
|
||
route.req = http.get opts, (resp) => | ||
data = '' | ||
route.req = null | ||
resp.on 'data', (chunk) => | ||
data += chunk | ||
resp.on 'end', (chunk) => | ||
objs = JSON.parse data | ||
for vehicle in objs | ||
if not vehicle['HasFix'] | ||
continue | ||
@.update_location route_name, vehicle | ||
@.set_poll_timer route_name, false | ||
route.req.on 'error', (e) => | ||
console.log "polling failed: " + e.message | ||
route.req = null | ||
@.set_poll_timer route_name, true | ||
|
||
connect: -> | ||
for route_name of @routes | ||
@.poll_route route_name | ||
|
||
#cli = new TfGMClient | ||
#cli.poll_route('MET1') | ||
#cli.connect() | ||
|
||
module.exports.TfGMClient = TfGMClient |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters