Module: MaxCube::Messages::TCP::Serializer::MessageM

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

Overview

Serializes metadata for Cube. Message body has the same format as response (M) -> reverse operations. Cube does not check data format, so things could break if invalid data is sent.

! I couldn't verify the assumption that bodies should be the same.

Constant Summary

KEYS =

Mandatory hash keys.

%i[rooms_count rooms devices_count devices].freeze
OPT_KEYS =

Optional hash keys.

%i[index unknown1 unknown2].freeze

Instance Method Summary collapse

Instance Method Details

#serialize_tcp_m(hash) ⇒ Object (private)



123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/maxcube/messages/tcp/type/m.rb', line 123

def serialize_tcp_m(hash)
  index = hash.key?(:index) ? to_int(0, 'index', hash[:index]) : 0
  head = format('%02x,', index)

  @io = StringIO.new('', 'wb')
  write(hash.key?(:unknown1) ? hash[:unknown1] : "\x00\x00")

  serialize_tcp_m_rooms(hash)
  serialize_tcp_m_devices(hash)
  write(hash.key?(:unknown2) ? hash[:unknown2] : "\x00")

  head.b << encode(@io.string)
end

#serialize_tcp_m_devices(hash) ⇒ Object (private)



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/maxcube/messages/tcp/type/m.rb', line 161

def serialize_tcp_m_devices(hash)
  write(to_int(0, 'devices count', hash[:devices_count]), esize: 1)
  hash[:devices].each do |device|
    name = device[:name]
    if device.key?(:name_length)
      name_length = to_int(0, 'name length', device[:name_length])
      unless name_length == name.length
        raise InvalidMessageBody
          .new(@msg_type, 'device name length and length of name' \
               " mismatch: #{name_length} != #{name.length}")
      end
    else
      name_length = name.length
    end

    rf_address, room_id =
      to_ints(0, 'device RF address, room ID',
              device[:rf_address], device[:room_id])
    write(serialize(device_type_id(device[:type]), esize: 1) <<
          serialize(rf_address, esize: 3) <<
          serialize(device[:serial_number],
                    name_length, name, room_id, esize: 1))
  end
end

#serialize_tcp_m_rooms(hash) ⇒ Object (private)



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/maxcube/messages/tcp/type/m.rb', line 139

def serialize_tcp_m_rooms(hash)
  write(to_int(0, 'rooms count', hash[:rooms_count]), esize: 1)
  hash[:rooms].each do |room|
    name = room[:name]
    if room.key?(:name_length)
      name_length = to_int(0, 'name length', room[:name_length])
      unless name_length == name.length
        raise InvalidMessageBody
          .new(@msg_type, 'room name length and length of name' \
               " mismatch: #{name_length} != #{name.length}")
      end
    else
      name_length = name.length
    end

    id, rf_address = to_ints(0, 'room id, RF address',
                             room[:id], room[:rf_address])
    write(serialize(id, name_length, name, esize: 1) <<
          serialize(rf_address, esize: 3))
  end
end