Skip to content

Instantly share code, notes, and snippets.

@aaronpk
Created November 27, 2011 01:43
Show Gist options
  • Select an option

  • Save aaronpk/1396767 to your computer and use it in GitHub Desktop.

Select an option

Save aaronpk/1396767 to your computer and use it in GitHub Desktop.
require 'socket'
s = UDPSocket.new
s.bind(nil, 43333)
while(true) do
text, sender = s.recvfrom(39)
puts
71.times{print "="}
puts
puts "Received message from " + sender[3].to_s + ":" + sender[1].to_s
raw = text.unpack('H*')[0].to_str
if raw.length != 78
puts "Message received had the wrong length (" + raw.length.to_s + ", expecting 78)!"
puts raw
s.send([0].pack('N'), 0, sender[3], sender[1])
next
end
puts "c ts lat lng spd hdg alt acc batt uuid"
print raw[0,2], " ", raw[2,8], " ", raw[10,8], " ", raw[18,8], " ", raw[26,4], " ", raw[30,4], " ", raw[34,4], " ", raw[38,4], " ", raw[42,4], " ", raw[46,32]
puts
date = text[1,4].unpack('N')[0]
latitude = text[5,4].unpack('N')[0]
longitude = text[9,4].unpack('N')[0]
speed = text[13,2].unpack('n')[0]
heading = text[15,2].unpack('n')[0]
altitude = text[17,2].unpack('n')[0]
accuracy = text[19,2].unpack('n')[0]
battery = text[21,2].unpack('n')[0]
uuid = text[23,16].unpack('H*')[0]
print "Date: ", date, " ("
print Time.at(date)
print ")"
puts
print "Location: "
print (((latitude / 4294967296.0) * 180.0) - 90.0)
print ", "
print (((longitude / 4294967296.0) * 360.0) - 180.0)
puts
print "Speed: ", speed, "km/h "
print "Heading: ", heading, "deg "
print "Altitude: ", altitude, "m "
print "Accuracy: ", accuracy, "m "
print "Battery: ", battery, "% "
puts
print "UUID: ", uuid
puts
s.send([date].pack('N'), 0, sender[3], sender[1])
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment