module MaxCube::Messages::TCP::Parser::MessageM

Metadata message.

Constants

KEYS

Mandatory hash keys.

LENGTHS

Private Instance Methods

parse_tcp_m(body) click to toggle source
# File lib/maxcube/messages/tcp/type/m.rb, line 16
def parse_tcp_m(body)
  index, count, enc_data = parse_tcp_m_split(body)

  @io = StringIO.new(decode(enc_data), 'rb')

  hash = { index: index, count: count, unknown1: read(2), }
  parse_tcp_m_rooms(hash)
  parse_tcp_m_devices(hash)
  hash[:unknown2] = read(1)

  hash
rescue IOError
  raise InvalidMessageBody
    .new(@msg_type,
         'unexpected EOF reached at unknown parts' \
         ' of decoded message data')
end
parse_tcp_m_devices(hash) click to toggle source
# File lib/maxcube/messages/tcp/type/m.rb, line 79
def parse_tcp_m_devices(hash)
  devices_count = read(1, true)
  hash[:devices_count] = devices_count
  hash[:devices] = []
  devices_count.times do
    device = {
      type: device_type(read(1, true)),
      rf_address: read(3, true),
      serial_number: read(10),
    }
    device_name_length = read(1, true)
    device.merge!(
      name_length: device_name_length,
      name: read(device_name_length),
      room_id: read(1, true),
    )

    hash[:devices] << device
  end
rescue IOError
  raise InvalidMessageBody
    .new(@msg_type,
         'unexpected EOF reached at devices data part' \
         ' of decoded message data')
end
parse_tcp_m_rooms(hash) click to toggle source
# File lib/maxcube/messages/tcp/type/m.rb, line 55
def parse_tcp_m_rooms(hash)
  rooms_count = read(1, true)
  hash[:rooms_count] = rooms_count
  hash[:rooms] = []
  rooms_count.times do
    room_id = read(1, true)
    room_name_length = read(1, true)
    room = {
      id: room_id,
      name_length: room_name_length,
      name: read(room_name_length),
      rf_address: read(3, true)
    }

    # hash[:rooms][room_id] = room
    hash[:rooms] << room
  end
rescue IOError
  raise InvalidMessageBody
    .new(@msg_type,
         'unexpected EOF reached at rooms data part' \
         ' of decoded message data')
end
parse_tcp_m_split(body) click to toggle source
# File lib/maxcube/messages/tcp/type/m.rb, line 36
def parse_tcp_m_split(body)
  index, count, enc_data = body.split(',')
  check_msg_part_lengths(LENGTHS, index, count)
  index, count = to_ints(16, 'message index, count',
                         index, count)
  unless index < count
    raise InvalidMessageBody
      .new(@msg_type,
           "index >= count: #{index} >= #{count}")
  end

  unless enc_data
    raise InvalidMessageBody
      .new(@msg_type, 'message data is missing')
  end

  [index, count, enc_data]
end