Class: BWA::Message
- Inherits:
-
Object
- Object
- BWA::Message
- Defined in:
- lib/bwa/message.rb
Direct Known Subclasses
Unrecognized, BWA::Messages::Configuration, BWA::Messages::ConfigurationRequest, BWA::Messages::ControlConfiguration, BWA::Messages::ControlConfiguration2, BWA::Messages::ControlConfigurationRequest, BWA::Messages::FilterCycles, BWA::Messages::Ready, BWA::Messages::SetTargetTemperature, BWA::Messages::SetTemperatureScale, BWA::Messages::SetTime, BWA::Messages::Status, BWA::Messages::ToggleItem
Defined Under Namespace
Classes: Unrecognized
Constant Summary collapse
- IGNORED_MESSAGES =
Ignore (parse and throw away) messages of these types.
[ (+"\xbf\x00").force_encoding(Encoding::ASCII_8BIT), # request for new clients (+"\xbf\xe1").force_encoding(Encoding::ASCII_8BIT), (+"\xbf\x07").force_encoding(Encoding::ASCII_8BIT) # nothing to send ].freeze
Instance Attribute Summary collapse
-
#raw_data ⇒ Object
readonly
Returns the value of attribute raw_data.
-
#src ⇒ Object
Returns the value of attribute src.
Class Method Summary collapse
-
.common_messages ⇒ Object
Don’t log messages of these types, even in DEBUG mode.
- .format_duration(minutes) ⇒ Object
- .format_time(hour, minute, twenty_four_hour_time: true) ⇒ Object
- .inherited(klass) ⇒ Object
- .parse(data) ⇒ Object
Instance Method Summary collapse
-
#initialize ⇒ Message
constructor
A new instance of Message.
- #inspect ⇒ Object
- #parse(_data) ⇒ Object
- #serialize(message = "") ⇒ Object
Constructor Details
#initialize ⇒ Message
Returns a new instance of Message.
148 149 150 151 |
# File 'lib/bwa/message.rb', line 148 def initialize # most messages we're sending come from this address @src = 0x0a end |
Instance Attribute Details
#raw_data ⇒ Object (readonly)
Returns the value of attribute raw_data.
146 147 148 |
# File 'lib/bwa/message.rb', line 146 def raw_data @raw_data end |
#src ⇒ Object
Returns the value of attribute src.
17 18 19 |
# File 'lib/bwa/message.rb', line 17 def src @src end |
Class Method Details
.common_messages ⇒ Object
Don’t log messages of these types, even in DEBUG mode. They are very frequent and would swamp the logs.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/bwa/message.rb', line 39 def @common_messages ||= begin msgs = [] unless BWA.verbosity >= 1 msgs += [ Messages::Status::MESSAGE_TYPE, (+"\xbf\xe1").force_encoding(Encoding::ASCII_8BIT) ] end unless BWA.verbosity >= 2 msgs += [ (+"\xbf\x00").force_encoding(Encoding::ASCII_8BIT), (+"\xbf\xe1").force_encoding(Encoding::ASCII_8BIT), Messages::Ready::MESSAGE_TYPE, (+"\xbf\x07").force_encoding(Encoding::ASCII_8BIT) ] end msgs end end |
.format_duration(minutes) ⇒ Object
141 142 143 |
# File 'lib/bwa/message.rb', line 141 def format_duration(minutes) format("%d:%02d", minutes / 60, minutes % 60) end |
.format_time(hour, minute, twenty_four_hour_time: true) ⇒ Object
131 132 133 134 135 136 137 138 139 |
# File 'lib/bwa/message.rb', line 131 def format_time(hour, minute, twenty_four_hour_time: true) if twenty_four_hour_time format("%02d:%02d", hour, minute) else print_hour = hour % 12 print_hour = 12 if print_hour.zero? format("%d:%02d%s", print_hour, minute, hour >= 12 ? "PM" : "AM") end end |
.inherited(klass) ⇒ Object
23 24 25 26 27 28 |
# File 'lib/bwa/message.rb', line 23 def inherited(klass) super @messages ||= [] @messages << klass end |
.parse(data) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/bwa/message.rb', line 60 def parse(data) offset = -1 = length = nil loop do offset += 1 # Not enough data for a full message; return and hope for more return nil if data.length - offset < 5 # Keep scanning until message start char next unless data[offset] == "~" # Read length (safe since we have at least 5 chars) length = data[offset + 1].ord # No message is this short or this long; keep scanning next if (length < 5) || (length >= "~".ord) # don't have enough data for what this message wants; # return and hope for more (yes this might cause a # delay, but the protocol is very chatty so it won't # be long) return nil if length + 2 > data.length - offset # Not properly terminated; keep scanning next unless data[offset + length + 1] == "~" # Not a valid checksum; keep scanning next unless CRC.checksum(data.slice(offset + 1, length - 1)) == data[offset + length].ord # Got a valid message! break end = data.slice(offset + 3, 2) BWA.logger.debug "discarding invalid data prior to message #{BWA.raw2str(data[0...offset])}" unless offset.zero? unless .include?() BWA.logger.debug " read: #{BWA.raw2str(data.slice(offset, length + 2))}" end src = data[offset + 2].ord klass = @messages.find { |k| k::MESSAGE_TYPE == } # Ignore these message types return [nil, offset + length + 2] if IGNORED_MESSAGES.include?() if klass valid_length = if klass::MESSAGE_LENGTH.respond_to?(:include?) klass::MESSAGE_LENGTH.include?(length - 5) else length - 5 == klass::MESSAGE_LENGTH end unless valid_length raise InvalidMessage.new("Unrecognized data length (#{length}) for message #{klass}", data) end else BWA.logger.info( "Unrecognized message type #{BWA.raw2str()}: #{BWA.raw2str(data.slice(offset, length + 2))}" ) klass = Unrecognized end = klass.new .parse(data.slice(offset + 5, length - 5)) .instance_variable_set(:@raw_data, data.slice(offset, length + 2)) .instance_variable_set(:@src, src) BWA.logger.debug "from spa: #{.inspect}" unless .include?() [, offset + length + 2] end |
Instance Method Details
#inspect ⇒ Object
163 164 165 |
# File 'lib/bwa/message.rb', line 163 def inspect "#<#{self.class.name} #{raw_data.unpack1("H*")}>" end |
#parse(_data) ⇒ Object
153 |
# File 'lib/bwa/message.rb', line 153 def parse(_data); end |
#serialize(message = "") ⇒ Object
155 156 157 158 159 160 161 |
# File 'lib/bwa/message.rb', line 155 def serialize( = "") length = .length + 5 = (+"#{length.chr}#{src.chr}#{self.class::MESSAGE_TYPE}#{}") .force_encoding(Encoding::ASCII_8BIT) checksum = CRC.checksum() (+"\x7e#{}#{checksum.chr}\x7e").force_encoding(Encoding::ASCII_8BIT) end |