Module: MaxCube::Messages::TCP::Parser::MessageM

Defined in:
lib/maxcube/messages/tcp/type/m.rb

Overview

Metadata message.

Constant Summary

LENGTHS =
[2, 2].freeze
KEYS =

Mandatory hash keys.

%i[index count unknown1 unknown2
rooms_count rooms devices_count devices].freeze

Instance Method Summary collapse

Instance Method Details

#parse_tcp_m(body) ⇒ Object (private)



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 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) ⇒ Object (private)



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# 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) ⇒ Object (private)



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# 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) ⇒ Object (private)



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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