Class: Cosmos::UnixTimeConversion

Inherits:
Conversion show all
Defined in:
lib/cosmos/conversions/unix_time_conversion.rb

Overview

Converts a unix format time: Epoch Jan 1 1970, seconds and microseconds

Instance Attribute Summary

Attributes inherited from Conversion

#converted_array_size, #converted_bit_size, #converted_type

Instance Method Summary collapse

Constructor Details

#initialize(seconds_item_name, microseconds_item_name = nil) ⇒ UnixTimeConversion

Initializes the time item to grab from the packet

Parameters:

  • seconds_item_name (String)

    The telemetry item in the packet which represents the number of seconds since the UNIX time epoch

  • microseconds_item_name (String) (defaults to: nil)

    The telemetry item in the packet which represents microseconds



31
32
33
34
35
# File 'lib/cosmos/conversions/unix_time_conversion.rb', line 31

def initialize(seconds_item_name, microseconds_item_name = nil)
  super()
  @seconds_item_name = seconds_item_name
  @microseconds_item_name = microseconds_item_name
end

Instance Method Details

#as_jsonObject



62
63
64
# File 'lib/cosmos/conversions/unix_time_conversion.rb', line 62

def as_json
  { 'class' => self.class.name.to_s, 'params' => [@seconds_item_name, @microseconds_item_name] }
end

#call(value, packet, buffer) ⇒ Float

Returns Packet time in seconds since UNIX epoch.

Parameters:

  • value (Object)

    The value to convert

  • packet (Packet)

    The packet which contains the value. This can be useful to reach into the packet and use other values in the conversion.

  • buffer (String)

    The packet buffer

Returns:

  • (Float)

    Packet time in seconds since UNIX epoch



39
40
41
42
43
44
45
# File 'lib/cosmos/conversions/unix_time_conversion.rb', line 39

def call(value, packet, buffer)
  if @microseconds_item_name
    return Time.at(packet.read(@seconds_item_name, :RAW, buffer), packet.read(@microseconds_item_name, :RAW, buffer)).sys
  else
    return Time.at(packet.read(@seconds_item_name, :RAW, buffer), 0).sys
  end
end

#to_config(read_or_write) ⇒ String

Returns Config fragment for this conversion.

Parameters:

  • read_or_write (String)

    Either 'READ' or 'WRITE'

Returns:

  • (String)

    Config fragment for this conversion



58
59
60
# File 'lib/cosmos/conversions/unix_time_conversion.rb', line 58

def to_config(read_or_write)
  "    #{read_or_write}_CONVERSION #{self.class.name.class_name_to_filename} #{@seconds_item_name} #{@microseconds_item_name}\n"
end

#to_sString

Returns The name of the class followed by the time conversion.

Returns:

  • (String)

    The name of the class followed by the time conversion



48
49
50
51
52
53
54
# File 'lib/cosmos/conversions/unix_time_conversion.rb', line 48

def to_s
  if @microseconds_item_name
    return "Time.at(packet.read('#{@seconds_item_name}', :RAW, buffer), packet.read('#{@microseconds_item_name}', :RAW, buffer)).sys"
  else
    return "Time.at(packet.read('#{@seconds_item_name}', :RAW, buffer), 0).sys"
  end
end