Class: LIFX::Message Private

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/lifx/message.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Defined Under Namespace

Classes: InvalidFields, InvalidFrame, MessageError, NoPath, NoPayload, NotAddressableFrame, PackError, UnmappedPayload, UnpackError, UnsupportedProtocolVersion

Constant Summary collapse

PROTOCOL_VERSION =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

1024

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Message

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Message.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/lifx/message.rb', line 95

def initialize(*args)
  if args.count == 3 
    @path, @message, @payload = args
  elsif (hash = args.first).is_a?(Hash)
    path = hash.delete(:path)
    payload = hash.delete(:payload)

    check_valid_fields!(hash)

    @message = Protocol::Message.new(hash)
    self.payload = payload
    self.path = path
    @message.tagged = path.tagged? if path
  else
    @message = Protocol::Message.new
  end
  @message.msg_size = @message.num_bytes
  @message.protocol = PROTOCOL_VERSION
rescue => ex
  raise MessageError.new("Unable to initialize message with args: #{args.inspect} - #{ex}")
end

Class Attribute Details

.log_invalid_messagesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



25
26
27
# File 'lib/lifx/message.rb', line 25

def log_invalid_messages
  @log_invalid_messages
end

Instance Attribute Details

#pathObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



94
95
96
# File 'lib/lifx/message.rb', line 94

def path
  @path
end

#payloadObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



94
95
96
# File 'lib/lifx/message.rb', line 94

def payload
  @payload
end

Class Method Details

.message_type_for_id(type_id) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



66
67
68
# File 'lib/lifx/message.rb', line 66

def message_type_for_id(type_id)
  Protocol::TYPE_ID_TO_CLASS[type_id]
end

.type_id_for_message_class(klass) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



70
71
72
# File 'lib/lifx/message.rb', line 70

def type_id_for_message_class(klass)
  Protocol::CLASS_TO_TYPE_ID[klass]
end

.unpack(data) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/lifx/message.rb', line 27

def unpack(data)
  raise InvalidFrame if data.length < 2

  header = Protocol::Header.read(data)
  raise UnsupportedProtocolVersion.new("Expected #{PROTOCOL_VERSION} but got #{header.protocol} instead") if header.protocol != PROTOCOL_VERSION
  raise NotAddressableFrame if header.addressable == 0

  message = Protocol::Message.read(data)
  path = ProtocolPath.new(raw_site: message.raw_site, raw_target: message.raw_target, tagged: message.tagged)
  payload_class = message_type_for_id(message.type.snapshot)
  if payload_class.nil?
    if self.log_invalid_messages
      logger.error("Message.unpack: Unrecognised payload ID: #{message.type}")
      logger.error("Message.unpack: Message: #{message}")
    end
    return nil # FIXME
    raise UnmappedPayload.new("Unrecognised payload ID: #{message.type}")
  end
  begin
    payload = payload_class.read(message.payload)
  rescue => ex
    if message.raw_site == "\x00" * 6
      logger.info("Message.unpack: Ignoring malformed message from virgin bulb")
    else
      if self.log_invalid_messages
        logger.error("Message.unpack: Exception while unpacking payload of type #{payload_class}: #{ex}")
        logger.error("Message.unpack: Data: #{data.inspect}")
      end
    end
  end
  new(path, message, payload)
rescue => ex
  if self.log_invalid_messages
    logger.debug("Message.unpack: Exception while unpacking #{data.inspect}")
    logger.debug("Message.unpack: #{ex} - #{ex.backtrace.join("\n")}")
  end
  raise ex
end

.valid_fieldsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



74
75
76
# File 'lib/lifx/message.rb', line 74

def valid_fields
  @valid_fields ||= Protocol::Message.new.field_names.map(&:to_sym)
end

Instance Method Details

#packObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises:



127
128
129
130
131
132
133
134
135
# File 'lib/lifx/message.rb', line 127

def pack
  raise NoPayload if !payload
  raise NoPath if !path
  @message.raw_site = path.raw_site
  @message.raw_target = path.raw_target
  @message.tagged = path.tagged?
  @message.msg_size = @message.num_bytes
  @message.pack
end

#to_sObject Also known as: inspect

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/lifx/message.rb', line 137

def to_s
  hash = {site: path.site_id}
  if path.tagged?
    hash[:tags] = path.tag_ids
    hash[:tags] = 'all' if hash[:tags].empty?
  else
    hash[:device] = path.device_id
  end
  hash[:type] = payload.class.to_s.sub('LIFX::Protocol::', '')
  hash[:addressable] = addressable? ? 'true' : 'false'
  hash[:tagged] = path.tagged? ? 'true' : 'false'
  hash[:at_time] = @message.at_time if @message.at_time && @message.at_time > 0
  hash[:protocol] = protocol
  hash[:payload] = payload.snapshot if payload
  attrs = hash.map { |k, v| "#{k}=#{v}" }.join(' ')
  %Q{#<LIFX::Message #{attrs}>}
end