Skip to content

Instantly share code, notes, and snippets.

@c2h2
Created February 15, 2012 09:56
Show Gist options
  • Save c2h2/1834849 to your computer and use it in GitHub Desktop.
Save c2h2/1834849 to your computer and use it in GitHub Desktop.
ruby gpsd reader
equire 'date'
require 'rubygems'
require 'cool.io'
require 'yajl'
ADDR = '127.0.0.1'
PORT = 2947
class Gps
def self.new_data hash
if (hash.is_a? Hash) && (!hash["class"].nil?)
gps = Gps.new
gps.parse_hash hash
else
puts hash
puts "Something wrong? Received hash is invalid"
end
end
def parse_hash hash
data_type = hash["class"]
if data_type == "SKY"
process_sky hash
elsif data_type == "TPV"
process_tpv hash
end
# ignore other data_type FTM
end
def display_loc
end
def process_sky hash
#puts hash
end
def process_tpv hash
@time = Time.at(hash["time"].to_i)
@lon = hash["lon"]
@lat = hash["lat"]
@alt = hash["alt"]
@spd = hash["speed"]
@trk = hash["track"]
puts "#{@time}| #{@lon}, #{@lat} | #{@alt}M, #{@spd}M/s, #{@trk}"
end
def print_raw
end
def gather_from_gpsd
end
end
### IO ###
def gather_from_gpsd host, prot, _lambda
parser = Yajl::Parser.new
parser.on_parse_complete = _lambda
cool.io.connect ADDR, PORT do
on_connect do
puts "Connected to gpsd on #{ADDR}:#{PORT}"
write "?WATCH={\"enable\":true,\"json\":true}"
end
on_read do |data|
parser << data
end
end
cool.io.run
end
### MAIN ###
gather_from_gpsd ADDR, PORT, lambda{|data| Gps.new_data data}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment