Class: TrainPlugins::Serial::Connection
- Inherits:
-
Train::Plugins::Transport::BaseConnection
- Object
- Train::Plugins::Transport::BaseConnection
- TrainPlugins::Serial::Connection
- Defined in:
- lib/train-serial/connection.rb
Instance Method Summary collapse
- #close ⇒ Object
- #create_session ⇒ Object
- #execute_on_channel(cmd, &data_handler) ⇒ Object
-
#initialize(options) ⇒ Connection
constructor
A new instance of Connection.
- #readable_config ⇒ Object
- #reset_session ⇒ Object
- #run_command_via_connection(cmd, &data_handler) ⇒ Object
- #session(retry_options = {}) ⇒ Object
- #uri ⇒ Object
Constructor Details
#initialize(options) ⇒ Connection
Returns a new instance of Connection.
8 9 10 |
# File 'lib/train-serial/connection.rb', line 8 def initialize() super() end |
Instance Method Details
#close ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/train-serial/connection.rb', line 12 def close return if @session.nil? unless [:teardown].empty? logger.debug format("[Serial] Sending teardown command to %s", [:device]) execute_on_channel([:teardown]) end logger.info format("[Serial] Closed connection %s", [:device]) session.close ensure @session = nil end |
#create_session ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/train-serial/connection.rb', line 95 def create_session logger.info format("[Serial] Opening connection %s (%s)", [:device], readable_config) @session = ::Serial.new( [:device], [:baud], [:data_bits], [:parity], [:stop_bits] ) unless [:setup].empty? logger.debug format("[Serial] Sending setup command to %s", [:device]) execute_on_channel([:setup]) end @session end |
#execute_on_channel(cmd, &data_handler) ⇒ Object
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/train-serial/connection.rb', line 39 def execute_on_channel(cmd, &data_handler) stdout = "" stderr = "" exit_status = 0 if [:debug_serial] logger.debug "[Serial] => #{cmd}\n" end session.write(cmd + "\n") retries = 0 loop do chunk = session.read([:buffer_size]) if chunk.empty? if [:debug_serial] logger.debug format("[Serial] Buffering on %s (attempt %d/%d, %d bytes received)", [:device], retries + 1, [:buffer_retries], stdout.size) end retries += 1 sleep [:buffer_wait] / 1000.0 else retries = 0 end break if retries >= [:buffer_retries] stdout << chunk end # Remove \r in linebreaks stdout.delete!("\r") if [:debug_serial] logger.debug "[Serial] <= '#{stdout}'" end # Extract command output only (no leading/trailing prompts) unless [:raw_output] stdout = stdout.match(/#{Regexp.quote(cmd.strip)}\n(.*?)\n#{@options[:prompt_pattern]}/m)&.captures&.first end stdout = "" if stdout.nil? # Simulate exit code and stderr errors = stdout.match(/^(#{@options[:error_pattern]})/) if errors exit_status = 1 stderr = errors.captures.first stdout.gsub!(/^#{@options[:error_pattern]}/, "") end [exit_status, stdout, stderr] end |
#readable_config ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/train-serial/connection.rb', line 119 def readable_config parity = "N" parity = "E" if [:parity] == :even parity = "O" if [:parity] == :odd format("%d %d%s%d", [:baud], [:data_bits], parity, [:stop_bits]) end |
#reset_session ⇒ Object
114 115 116 117 |
# File 'lib/train-serial/connection.rb', line 114 def reset_session session.close unless session.nil? @session = nil end |
#run_command_via_connection(cmd, &data_handler) ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/train-serial/connection.rb', line 30 def run_command_via_connection(cmd, &data_handler) reset_session if session.closed? logger.debug format("[Serial] Sending command to %s", [:device]) exit_status, stdout, stderr = execute_on_channel(cmd, &data_handler) CommandResult.new(stdout, stderr, exit_status) end |
#session(retry_options = {}) ⇒ Object
91 92 93 |
# File 'lib/train-serial/connection.rb', line 91 def session( = {}) @session ||= create_session end |
#uri ⇒ Object
26 27 28 |
# File 'lib/train-serial/connection.rb', line 26 def uri "serial://#{@options[:port]}/#{@options[:baud]}" end |