Class: E3DCMqtt::RSCP::Message
- Inherits:
-
Data
- Object
- Data
- E3DCMqtt::RSCP::Message
- Defined in:
- lib/e3dc_mqtt/rscp/message.rb
Overview
A single RSCP message: { tag (uint32), data_type (uint8), value }. Containers carry an Array<Message> as their value.
Instance Attribute Summary collapse
-
#data_type ⇒ Object
readonly
Returns the value of attribute data_type.
-
#tag ⇒ Object
readonly
Returns the value of attribute tag.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
-
.build(tag, value = nil, type: nil) ⇒ Object
Build a message, inferring data type from the tag registry.
-
.container(tag, children) ⇒ Object
Build a CONTAINER message holding the given child messages.
- .none(tag) ⇒ Object
- .tag_id(tag) ⇒ Object
Instance Method Summary collapse
- #container? ⇒ Boolean
-
#fetch(tag) ⇒ Object
Fetch the value of a child by tag in a CONTAINER; raises if missing.
-
#find(tag) ⇒ Object
Find a child by tag in a CONTAINER (shallow).
- #inspect ⇒ Object
- #tag_name ⇒ Object
Instance Attribute Details
#data_type ⇒ Object (readonly)
Returns the value of attribute data_type
10 11 12 |
# File 'lib/e3dc_mqtt/rscp/message.rb', line 10 def data_type @data_type end |
#tag ⇒ Object (readonly)
Returns the value of attribute tag
10 11 12 |
# File 'lib/e3dc_mqtt/rscp/message.rb', line 10 def tag @tag end |
#value ⇒ Object (readonly)
Returns the value of attribute value
10 11 12 |
# File 'lib/e3dc_mqtt/rscp/message.rb', line 10 def value @value end |
Class Method Details
.build(tag, value = nil, type: nil) ⇒ Object
Build a message, inferring data type from the tag registry. Pass a Symbol (tag name) or an Integer (tag id). Falls back to NONE for empty requests where the registry has no declared type.
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/e3dc_mqtt/rscp/message.rb', line 18 def self.build(tag, value = nil, type: nil) id = tag_id(tag) type ||= Tags.data_type_for(id) if type.nil? raise(ArgumentError, "no known data type for tag #{tag.inspect} — pass type: explicitly") unless value.nil? type = DataType::NONE end new(tag: id, data_type: type, value: value) end |
.container(tag, children) ⇒ Object
Build a CONTAINER message holding the given child messages.
31 32 33 |
# File 'lib/e3dc_mqtt/rscp/message.rb', line 31 def self.container(tag, children) new(tag: tag_id(tag), data_type: DataType::CONTAINER, value: children) end |
Instance Method Details
#container? ⇒ Boolean
39 40 41 |
# File 'lib/e3dc_mqtt/rscp/message.rb', line 39 def container? data_type == DataType::CONTAINER end |
#fetch(tag) ⇒ Object
Fetch the value of a child by tag in a CONTAINER; raises if missing.
56 57 58 59 60 61 |
# File 'lib/e3dc_mqtt/rscp/message.rb', line 56 def fetch(tag) m = find(tag) raise KeyError, "tag %s not found in container %s" % [tag.inspect, tag_name || tag.to_s] if m.nil? m.value end |
#find(tag) ⇒ Object
Find a child by tag in a CONTAINER (shallow).
48 49 50 51 52 53 |
# File 'lib/e3dc_mqtt/rscp/message.rb', line 48 def find(tag) return nil unless container? id = tag.is_a?(Symbol) ? Tags.id_for(tag) : tag value.find { |m| m.tag == id } end |