Class: E3DCMqtt::RSCP::Message

Inherits:
Data
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#data_typeObject (readonly)

Returns the value of attribute data_type

Returns:

  • (Object)

    the current value of data_type



10
11
12
# File 'lib/e3dc_mqtt/rscp/message.rb', line 10

def data_type
  @data_type
end

#tagObject (readonly)

Returns the value of attribute tag

Returns:

  • (Object)

    the current value of tag



10
11
12
# File 'lib/e3dc_mqtt/rscp/message.rb', line 10

def tag
  @tag
end

#valueObject (readonly)

Returns the value of attribute value

Returns:

  • (Object)

    the current value of 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

.none(tag) ⇒ Object



11
12
13
# File 'lib/e3dc_mqtt/rscp/message.rb', line 11

def self.none(tag)
  new(tag: tag_id(tag), data_type: DataType::NONE, value: nil)
end

.tag_id(tag) ⇒ Object



35
36
37
# File 'lib/e3dc_mqtt/rscp/message.rb', line 35

def self.tag_id(tag)
  tag.is_a?(Symbol) ? Tags.id_for(tag) : tag
end

Instance Method Details

#container?Boolean

Returns:

  • (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.

Raises:

  • (KeyError)


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

#inspectObject



63
64
65
66
67
# File 'lib/e3dc_mqtt/rscp/message.rb', line 63

def inspect
  name = tag_name || ("0x%08x" % tag)
  shown = Tags.secret?(tag) ? "********" : value.inspect
  "#<Message #{name} #{DataType.name(data_type)} #{shown}>"
end

#tag_nameObject



43
44
45
# File 'lib/e3dc_mqtt/rscp/message.rb', line 43

def tag_name
  Tags.name_for(tag)
end