Class: TimexDatalinkClient::Protocol4::Time

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations, Helpers::CharEncoders, Helpers::CrcPacketsWrapper
Defined in:
lib/timex_datalink_client/protocol_4/time.rb

Constant Summary collapse

CPACKET_TIME =
[0x32]
DATE_FORMAT_MAP =
{
  "%_m-%d-%y" => 0,
  "%_d-%m-%y" => 1,
  "%y-%m-%d" => 2,
  "%_m.%d.%y" => 4,
  "%_d.%m.%y" => 5,
  "%y.%m.%d" => 6
}.freeze

Constants included from Helpers::CharEncoders

Helpers::CharEncoders::CHARS, Helpers::CharEncoders::CHARS_PROTOCOL_6, Helpers::CharEncoders::EEPROM_CHARS, Helpers::CharEncoders::EEPROM_TERMINATOR, Helpers::CharEncoders::INVALID_CHAR, Helpers::CharEncoders::PHONE_CHARS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::CharEncoders

#chars_for, #eeprom_chars_for, #phone_chars_for, #protocol_6_chars_for

Constructor Details

#initialize(zone:, is_24h:, date_format:, time:, name: nil) ⇒ Time

Create a Time instance.

Parameters:

  • zone (Integer)

    Time zone number (1 or 2).

  • is_24h (Boolean)

    Toggle 24 hour time.

  • date_format ("%_m-%d-%y", "%_d-%m-%y", "%y-%m-%d", "%_m.%d.%y", "%_d.%m.%y", "%y.%m.%d")

    Date format (represented by Time#strftime format).

  • time (::Time)

    Time to set (including time zone).

  • name (String, nil) (defaults to: nil)

    Name of time zone (defaults to zone from time; 3 chars max).



47
48
49
50
51
52
53
# File 'lib/timex_datalink_client/protocol_4/time.rb', line 47

def initialize(zone:, is_24h:, date_format:, time:, name: nil)
  @zone = zone
  @is_24h = is_24h
  @date_format = date_format
  @time = time
  @name = name
end

Instance Attribute Details

#date_formatObject

Returns the value of attribute date_format.



36
37
38
# File 'lib/timex_datalink_client/protocol_4/time.rb', line 36

def date_format
  @date_format
end

#is_24hObject

Returns the value of attribute is_24h.



36
37
38
# File 'lib/timex_datalink_client/protocol_4/time.rb', line 36

def is_24h
  @is_24h
end

#nameObject

Returns the value of attribute name.



36
37
38
# File 'lib/timex_datalink_client/protocol_4/time.rb', line 36

def name
  @name
end

#timeObject

Returns the value of attribute time.



36
37
38
# File 'lib/timex_datalink_client/protocol_4/time.rb', line 36

def time
  @time
end

#zoneObject

Returns the value of attribute zone.



36
37
38
# File 'lib/timex_datalink_client/protocol_4/time.rb', line 36

def zone
  @zone
end

Instance Method Details

#packetsArray<Array<Integer>>

Compile packets for a time.

Returns:

  • (Array<Array<Integer>>)

    Two-dimensional array of integers that represent bytes.

Raises:

  • (ActiveModel::ValidationError)

    One or more model values are invalid.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/timex_datalink_client/protocol_4/time.rb', line 59

def packets
  validate!

  [
    [
      CPACKET_TIME,
      zone,
      time.sec,
      time.hour,
      time.min,
      time.month,
      time.day,
      year_mod_1900,
      name_characters,
      wday_from_monday,
      is_24h_value,
      date_format_value
    ].flatten
  ]
end