Class: MaxCube::Messages::UDP::Parser

Inherits:
Object
  • Object
show all
Includes:
Parser, Handler
Defined in:
lib/maxcube/messages/udp/parser.rb,
lib/maxcube/messages/udp/type/h.rb,
lib/maxcube/messages/udp/type/i.rb,
lib/maxcube/messages/udp/type/n.rb

Overview

Extends Parser and Handler of routines connected to UDP Cube messages parsing.

Defined Under Namespace

Modules: MessageH, MessageI, MessageN

Constant Summary

KEYS =

Mandatory hash keys common for all UDP Cube messages.

%i[prefix serial_number id].freeze
MSG_TYPES =

Known message types in the direction Cube -> client.

%w[I N h c].freeze
MSG_PREFIX =

MSG_PREFIX with a suffix.

(UDP::MSG_PREFIX + 'Ap').freeze

Constants included from Handler

Handler::PACK_FORMAT

Constants included from MaxCube::Messages

DAYS_OF_WEEK, DEVICE_MODE, DEVICE_TYPE

Instance Method Summary collapse

Methods included from Parser

#parse_msg_body, #read

Methods included from Handler

#check_data_type, #check_hash, #check_hash_keys, #check_hash_msg_type, #check_hash_values, #check_msg, #check_msg_msg_type, #check_msg_part_lengths, #check_msg_type, #decode, #encode, #maybe_check_valid_hash_keys, #maybe_check_valid_msg_type, #msg_type_hash_keys, #msg_type_hash_opt_keys, #msg_type_which_hash_keys, #msg_types, #valid_data_type, #valid_hash, #valid_hash_keys, #valid_hash_msg_type, #valid_hash_values, #valid_msg, #valid_msg_msg_type, #valid_msg_part_lengths, #valid_msg_type

Methods included from MaxCube::Messages

#ary_elem, #ary_elem_id, #conv_args, #day_of_week, #day_of_week_id, #device_mode, #device_mode_id, #device_type, #device_type_id, #to_bool, #to_bools, #to_datetime, #to_datetimes, #to_float, #to_floats, #to_int, #to_ints

Methods included from Handler

#check_udp_hash, #check_udp_msg, #check_udp_msg_prefix, #valid_udp_hash, #valid_udp_msg, #valid_udp_msg_prefix

Instance Method Details

#msg_msg_type(msg) ⇒ String (private)

Tells how to get message type from a message.

Parameters:

  • msg (String)

    input message.

Returns:

  • (String)

    message type.



45
46
47
# File 'lib/maxcube/messages/udp/parser.rb', line 45

def msg_msg_type(msg)
  msg[19]
end

#parse_udp_msg(msg) ⇒ Hash

Parameters:

  • msg (String)

    input message.

Returns:

  • (Hash)

    particular message contents separated into hash.



33
34
35
36
37
38
# File 'lib/maxcube/messages/udp/parser.rb', line 33

def parse_udp_msg(msg)
  check_udp_msg(msg)
  hash = parse_udp_msg_head(msg)
  return hash unless parse_msg_body(@io.string, hash, 'udp')
  check_udp_hash(hash)
end

#parse_udp_msg_head(msg) ⇒ Hash (private)

Parses head of UDP Cube message, that is common to all of these. Internal IO variable contains message body string at the end.

Parameters:

  • msg (String)

    input message.

Returns:

  • (Hash)

    particular message head contents separated into hash.



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/maxcube/messages/udp/parser.rb', line 53

def parse_udp_msg_head(msg)
  @io = StringIO.new(msg, 'rb')
  hash = {
    prefix: read(8),
    serial_number: read(10),
    id: read(1, true),
    type: read(1),
  }
  @io.string = @io.read
  hash
end