Skip to content

Instantly share code, notes, and snippets.

@fiddyspence
Created November 4, 2016 11:25
Show Gist options
  • Save fiddyspence/8188b4345501c7b4b01c1ad1a7519fc8 to your computer and use it in GitHub Desktop.
Save fiddyspence/8188b4345501c7b4b01c1ad1a7519fc8 to your computer and use it in GitHub Desktop.
def getjson
thejson = ''
uri = URI.parse("http://#{@dump1090host}:#{@dump1090port}/data.json")
begin
Timeout::timeout(1) {
thejson = Net::HTTP.get(uri) rescue {}
}
rescue Timeout::Error => e
log 'Getting the JSON timed out'
log e.message
rescue => e
log 'Some other JSON problem'
log e.message
end
begin
return JSON.parse(thejson)
rescue => e
log e.message
{}
end
end
def findclosestplane
begin
planedata = getjson rescue {}
rescue => e
log e.message
log e.backtrace
log 'Failed to get JSON plane data'
end
return @closestplane unless planedata.size > 0
closestplane = 100000000
planedata.each do |plane|
next if plane['altitude'] > 10000
begin
distance = @ourcoords.distance_to(Geokit::LatLng.new(plane['lat'], plane['lon']))
rescue => e
log "Failed to get distance because #{e.message}"
end
if distance > 0 and distance < closestplane
closestplane = distance
@closestplane = plane
@closestplane[:distance] = distance
end
end
@closestplane
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment