Permalink
Browse files

defining json serialization and removing puts

  • Loading branch information...
1 parent e599b61 commit a57e7d19c35316b47e7e4e6358b75c5c8ba66e5b @tclem committed Dec 6, 2010
Showing with 29 additions and 6 deletions.
  1. +11 −1 lib/xbeer/frame.rb
  2. +18 −5 lib/xbeer/xbee_api.rb
View
@@ -1,3 +1,4 @@
+require 'yajl/json_gem'
require 'xbeer/string'
module Xbeer
@@ -72,7 +73,7 @@ def cmd_data=(data_string)
src_high, src_low, @signal_strength, @opts = data_string.unpack("NNCC")
self.src_addr = (src_high << 32) + src_low
@signal_strength_db = "-#{@signal_strength} dB"
- @type = data_string[10]
+ self.type = data_string[10]
self.inner_packet = data_string[11..-1]
end
end
@@ -98,6 +99,15 @@ def inner_packet=(data_string)
@summary = "Position in deg: (#{@lat}, #{@long}), Course in deg: #{@course}, Speed in knots: #{@speed}"
end
+ def to_json
+ result = {:name => "0x#{@src_addr.to_s(16)}",
+ :lat => @lat.to_s,
+ :lon => @long.to_s,
+ :speed => @speed.to_s,
+ :bearing => @course.to_s}
+ result.to_json
+ end
+
private
def to_signed(n)
length = 32
View
@@ -26,12 +26,12 @@ def initialize(opts={})
def read_api
stray_bytes = []
until (start_delimiter = @s.readchar) == 0x7e
- puts "Stray byte 0x%x" % start_delimiter
+ # puts "Stray byte 0x%x" % start_delimiter
stray_bytes << start_delimiter
end
- puts "Got some stray bytes for ya: #{stray_bytes.map {|b| "0x%x" % b} .join(", ")}" unless stray_bytes.empty?
+ # puts "Got some stray bytes for ya: #{stray_bytes.map {|b| "0x%x" % b} .join(", ")}" unless stray_bytes.empty?
header = @s.read(3).xb_unescape
- puts "Read header: #{header.unpack("C*").join(", ")}"
+ # puts "Read header: #{header.unpack("C*").join(", ")}"
frame_remaining = frame_length = api_identifier = cmd_data = ""
if header.length == 3
frame_length, api_identifier = header.unpack("nC")
@@ -47,7 +47,7 @@ def read_api
unless sent_checksum == Frame.checksum(data)
raise CheckSumError, "Bad checksum - data discarded"
end
- puts "Raw response: 0x#{data.unpack("H*")}"
+ # puts "Raw response: 0x#{data.unpack("H*")}"
data
end
@@ -103,11 +103,24 @@ def initialize(opts={:no_setup => true, :read_timeout => 0})
end
def rx_gps
- pp r = SafeTraxxPacket.create(read_api); r
+ SafeTraxxPacket.create(read_api)
# pp r = GpsReceivePacket.new(read_api)
# r
end
+ def get_two_gps_packets
+ [get_gps_packet, get_gps_packet]
+ end
+
+ def get_gps_packet
+ begin
+ p1 = rx_gps
+ rescue CheckSumError => e
+ retry
+ end until p1.instance_of? SafeTraxxGpsPacket
+ p1
+ end
+
end # end class XbeeListener
end # end module Xbeer

0 comments on commit a57e7d1

Please sign in to comment.