Class: Sftui::Commands::Stream

Inherits:
Sftui::Command show all
Defined in:
lib/sftui/commands/stream.rb

Instance Method Summary collapse

Methods inherited from Sftui::Command

#command, #cursor, #editor, #exec_exist?, #generator, #pager, #platform, #prompt, #screen, #which

Constructor Details

#initialize(uart, options) ⇒ Stream

Returns a new instance of Stream.



13
14
15
16
17
18
19
# 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

Instance Method Details

#executeObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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) ⇒ Object

rubocop:disable Metrics/MethodLength



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# 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

#mainObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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) ⇒ Object



72
73
74
75
76
77
78
79
# 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