Class: LIFX::Message Private
- Inherits:
-
Object
- Object
- LIFX::Message
- 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
- .log_invalid_messages ⇒ Object private
Instance Attribute Summary collapse
- #path ⇒ Object private
- #payload ⇒ Object private
Class Method Summary collapse
- .message_type_for_id(type_id) ⇒ Object private
- .type_id_for_message_class(klass) ⇒ Object private
- .unpack(data) ⇒ Object private
- .valid_fields ⇒ Object private
Instance Method Summary collapse
-
#initialize(*args) ⇒ Message
constructor
private
A new instance of Message.
- #pack ⇒ Object private
- #to_s ⇒ Object (also: #inspect) private
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_messages ⇒ 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.
25 26 27 |
# File 'lib/lifx/message.rb', line 25 def @log_invalid_messages end |
Instance Attribute Details
#path ⇒ 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.
94 95 96 |
# File 'lib/lifx/message.rb', line 94 def path @path end |
#payload ⇒ 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.
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 (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 (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 = Protocol::Message.read(data) path = ProtocolPath.new(raw_site: .raw_site, raw_target: .raw_target, tagged: .tagged) payload_class = (.type.snapshot) if payload_class.nil? if self. logger.error("Message.unpack: Unrecognised payload ID: #{.type}") logger.error("Message.unpack: Message: #{}") end return nil # FIXME raise UnmappedPayload.new("Unrecognised payload ID: #{.type}") end begin payload = payload_class.read(.payload) rescue => ex if .raw_site == "\x00" * 6 logger.info("Message.unpack: Ignoring malformed message from virgin bulb") else if self. 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, , payload) rescue => ex if self. logger.debug("Message.unpack: Exception while unpacking #{data.inspect}") logger.debug("Message.unpack: #{ex} - #{ex.backtrace.join("\n")}") end raise ex end |
Instance Method Details
#pack ⇒ 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.
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_s ⇒ Object 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 |