Class: Gts::TK102Handler

Inherits:
AbstractGPSTrackerHandler show all
Defined in:
lib/gts/handlers/tk102_handler.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractGPSTrackerHandler

#devices, register!

Class Method Details

.devicesObject



38
39
40
# File 'lib/gts/handlers/tk102_handler.rb', line 38

def self.devices
  %w( TK-102 TK-102B )
end

Instance Method Details

#parse(raw_data) ⇒ Object

Raises:



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/gts/handlers/tk102_handler.rb', line 5

def parse(raw_data)
  raw_data = raw_data.gsub( /(^[\s\t\r\n]+|[\s\t\r\n]+$)/, '' )
  attrs = raw_data.split(",")
  raise CantParseGPSData if attrs.size != 18 || attrs[2] != "GPRMC"
  datetime = parse_datetime attrs[0]
  begin
    phone = PhoneSystem::PhoneNumber.new(attrs[1]).long
  rescue PhoneSystem::PhoneSystemError
    phone = attrs[1]
  end
  gps_time = attrs[3].scan(/(\d{2})(\d{2})(\d{2})\.(\d{3})/).first
  gps_time = "#{gps_time[0]}:#{gps_time[1]}:#{gps_time[2]}.#{gps_time[3]}"
  gps_date = attrs[11].scan(/([0-9]{2})([0-9]{2})([0-9]{2})/).first
  gps_year = Time.now.year.to_s[0..1] + gps_date[2]
  gps_date = "#{gps_year}-#{gps_date[1]}-#{gps_date[0]}"
  {
    :raw => raw_data,
    :datetime => datetime,
    :phone => phone,
    :gps_date => gps_date,
    :gps_time => gps_time,
    :gps_signal => (attrs[15] == 'F' ? 'full' : 'low'),
    :gps_fix => (attrs[4] == 'A' ? 'active' : 'invalid'),
    :lat => convert_nmea_coordinates(attrs[5], attrs[6]),
    :lng => convert_nmea_coordinates(attrs[7], attrs[8]),
    :bearing => attrs[10].to_i,
    :speed_knots => (attrs[9].to_f * 1000 ).round / 1000,
    :speed_kmh => ( attrs[9].to_f * 1.852 * 1000 ).round / 1000,
    :speed_mph => ( attrs[9].to_f * 1.151 * 1000 ).round / 1000,
    :imei => attrs[16].gsub('imei:', '')
  }
end