module MaxCube::Messages::TCP::Serializer::MessageM

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.

Constants

KEYS

Mandatory hash keys.

OPT_KEYS

Optional hash keys.

Private Instance Methods

serialize_tcp_m(hash) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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