class Sftui::Commands::Stream

Public Class Methods

new(uart, options) click to toggle source
# File lib/sftui/commands/stream.rb, line 13
def initialize(uart, options)
  @uart = uart
  @options = options
  @serialport = SerialPort.new(uart)
  @frame_buffer = FrameBuffer.new
  @byte_buffer = []
end

Public Instance Methods

execute() click to toggle source
# File lib/sftui/commands/stream.rb, line 21
def execute
  pastel = Pastel.new
  puts pastel.red.bold('📡 Streaming')

  trap 'INT' do
    @serialport.flush
    puts pastel.red.bold('Stopped!')
    exit
  end

  @serialport.flush

  main
end
handle_frame(frame) click to toggle source
# File lib/sftui/commands/stream.rb, line 54
def handle_frame(frame) # rubocop:disable Metrics/MethodLength
  previous_frame = @frame_buffer.latest_frame

  if frame.machine_id == previous_frame&.machine_id
    @frame_buffer.pop(previous_frame)
    @frame_buffer.push(previous_frame + frame)
  else
    @frame_buffer.push(frame)
  end

  latest_frame = @frame_buffer.latest_frame

  return unless latest_frame.last_frame_in_transaction?

  publish_scanner_data(latest_frame)
  @frame_buffer.pop(latest_frame)
end
main() click to toggle source
# File lib/sftui/commands/stream.rb, line 36
def main
  loop do
    c = @serialport.read(1).ord

    @byte_buffer << c

    frame = Frame.new(@byte_buffer)

    next unless frame.completed?

    puts frame.display

    handle_frame(frame)

    @byte_buffer = []
  end
end
publish_scanner_data(frame) click to toggle source
# File lib/sftui/commands/stream.rb, line 72
def publish_scanner_data(frame)
  PublishScannerDataWorker.perform_async(
    {
      machine_id: frame.machine_id,
      data: frame.sanitized_data
    }
  )
end