Class: SDN::Message
- Inherits:
-
Object
- Object
- SDN::Message
- Includes:
- Helpers
- Defined in:
- lib/sdn/message.rb,
lib/sdn/message/get.rb,
lib/sdn/message/set.rb,
lib/sdn/message/post.rb,
lib/sdn/message/control.rb,
lib/sdn/message/helpers.rb,
lib/sdn/message/ilt2/get.rb,
lib/sdn/message/ilt2/set.rb,
lib/sdn/message/ilt2/post.rb,
lib/sdn/message/ilt2/master_control.rb
Direct Known Subclasses
GetGroupAddr, GetMotorIP, GetNodeAddr, ILT2::GetMotorIP, ILT2::PostIRConfig, ILT2::PostLockStatus, ILT2::PostMotorIP, ILT2::PostMotorPosition, ILT2::SetLockStatus, ILT2::SetMotorIP, ILT2::SetMotorPosition, Lock, Move, MoveOf, MoveTo, Nack, PostGroupAddr, PostMotorDirection, PostMotorIP, PostMotorLimits, PostMotorPosition, PostMotorRollingSpeed, PostMotorStatus, PostNodeAddr, PostNodeAppVersion, PostNodeLabel, PostNodeSerialNumber, SetFactoryDefault, SetGroupAddr, SetMotorDirection, SetMotorIP, SetMotorLimits, SetMotorRollingSpeed, SetNetworkLock, SetNodeLabel, SimpleRequest, Stop, UnknownMessage
Defined Under Namespace
Modules: Helpers, ILT2 Classes: Ack, GetGroupAddr, GetMotorDirection, GetMotorIP, GetMotorLimits, GetMotorPosition, GetMotorRollingSpeed, GetMotorStatus, GetNetworkLock, GetNodeAddr, GetNodeAppVersion, GetNodeLabel, GetNodeSerialNumber, GetNodeStackVersion, Lock, Move, MoveOf, MoveTo, Nack, PostGroupAddr, PostMotorDirection, PostMotorIP, PostMotorLimits, PostMotorPosition, PostMotorRollingSpeed, PostMotorStatus, PostNetworkLock, PostNodeAddr, PostNodeAppVersion, PostNodeLabel, PostNodeSerialNumber, PostNodeStackVersion, SetFactoryDefault, SetGroupAddr, SetMotorDirection, SetMotorIP, SetMotorLimits, SetMotorRollingSpeed, SetNetworkLock, SetNodeLabel, SimpleRequest, Stop, UnknownMessage, Wink
Instance Attribute Summary collapse
-
#ack_requested ⇒ Object
Returns the value of attribute ack_requested.
-
#dest ⇒ Object
Returns the value of attribute dest.
-
#node_type ⇒ Object
Returns the value of attribute node_type.
-
#src ⇒ Object
Returns the value of attribute src.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #class_inspect ⇒ Object
-
#initialize(node_type: nil, ack_requested: false, src: nil, dest: nil) ⇒ Message
constructor
A new instance of Message.
- #inspect ⇒ Object (also: #to_s)
- #parse(params) ⇒ Object
- #serialize ⇒ Object
Methods included from Helpers
#checksum, #from_number, #from_string, #group_address?, #node_type_from_number, #node_type_to_number, #node_type_to_string, #parse_address, #print_address, #to_number, #to_string, #transform_param
Constructor Details
#initialize(node_type: nil, ack_requested: false, src: nil, dest: nil) ⇒ Message
Returns a new instance of Message.
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/sdn/message.rb', line 88 def initialize(node_type: nil, ack_requested: false, src: nil, dest: nil) @node_type = node_type || 0 @ack_requested = ack_requested if src.nil? && !dest.nil? && group_address?(dest) src = dest dest = nil end @src = src || [0, 0, 1] @dest = dest || [0, 0, 0] end |
Instance Attribute Details
#ack_requested ⇒ Object
Returns the value of attribute ack_requested.
86 87 88 |
# File 'lib/sdn/message.rb', line 86 def ack_requested @ack_requested end |
#dest ⇒ Object
Returns the value of attribute dest.
86 87 88 |
# File 'lib/sdn/message.rb', line 86 def dest @dest end |
#node_type ⇒ Object
Returns the value of attribute node_type.
86 87 88 |
# File 'lib/sdn/message.rb', line 86 def node_type @node_type end |
#src ⇒ Object
Returns the value of attribute src.
86 87 88 |
# File 'lib/sdn/message.rb', line 86 def src @src end |
Class Method Details
.expected_response?(message) ⇒ Boolean
18 19 20 21 22 23 24 |
# File 'lib/sdn/message.rb', line 18 def expected_response?() if /::Get([A-Za-z]+)/.match?(name) .class.name == name.sub("::Get", "::Post") # rubocop:disable Style/ClassEqualityComparison else .is_a?(Ack) || .is_a?(Nack) end end |
.inherited(klass) ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/sdn/message.rb', line 10 def inherited(klass) super return Message.inherited(klass) unless self == Message @message_map = nil (@subclasses ||= []) << klass end |
.parse(data) ⇒ Object
26 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 65 66 67 68 69 |
# File 'lib/sdn/message.rb', line 26 def parse(data) offset = -1 msg = length = ack_requested = = nil # we loop here scanning for a valid message loop do offset += 1 # give these weird messages a chance result = ILT2::MasterControl.parse(data) return result if result return [nil, 0] if data.length - offset < 11 msg = to_number(data[offset]) length = to_number(data[offset + 1]) ack_requested = length & 0x80 == 0x80 length &= 0x7f # impossible message next if length < 11 || length > 43 # don't have enough data for what this message wants; # it could be garbage on the line so keep scanning next if length > data.length - offset = [msg] || UnknownMessage calculated_sum = checksum(data.slice(offset, length - 2)) read_sum = data.slice(offset + length - 2, 2) next unless read_sum == calculated_sum break end node_type = node_type_from_number(to_number(data[offset + 2])) src = transform_param(data.slice(offset + 3, 3)) dest = transform_param(data.slice(offset + 6, 3)) begin result = .new(node_type: node_type, ack_requested: ack_requested, src: src, dest: dest) result.parse(data.slice(offset + 9, length - 11)) result.msg = msg if == UnknownMessage rescue ArgumentError => e SDN.logger.warn "Discarding illegal message of type #{.name}: #{e}" result = nil end [result, offset + length] end |
Instance Method Details
#==(other) ⇒ Object
116 117 118 |
# File 'lib/sdn/message.rb', line 116 def ==(other) serialize == other.serialize end |
#class_inspect ⇒ Object
131 132 133 134 135 136 |
# File 'lib/sdn/message.rb', line 131 def class_inspect ivars = instance_variables - %i[@node_type @ack_requested @src @dest @params] return if ivars.empty? ivars.map { |iv| ", #{iv}=#{instance_variable_get(iv).inspect}" }.join end |
#inspect ⇒ Object Also known as: to_s
120 121 122 123 124 125 126 127 128 |
# File 'lib/sdn/message.rb', line 120 def inspect format("#<%s @node_type=%s, @ack_requested=%s, @src=%s, @dest=%s%s>", self.class.name, node_type_to_string(node_type), ack_requested, print_address(src), print_address(dest), class_inspect) end |
#parse(params) ⇒ Object
99 100 101 102 103 104 105 |
# File 'lib/sdn/message.rb', line 99 def parse(params) return unless self.class.const_defined?(:PARAMS_LENGTH) && params.length != self.class.const_get(:PARAMS_LENGTH) raise MalformedMessage, "unrecognized params for #{self.class.name}: #{params.map do |b| format("%02x", b) end}" end |
#serialize ⇒ Object
107 108 109 110 111 112 113 114 |
# File 'lib/sdn/message.rb', line 107 def serialize result = transform_param(node_type_to_number(node_type)) + transform_param(src) + transform_param(dest) + params length = result.length + 4 length |= 0x80 if ack_requested result = transform_param(self.class.const_get(:MSG)) + transform_param(length) + result result.concat(checksum(result)) result.pack("C*") end |