Class: Qcmd::Handler

Inherits:
Object
  • Object
show all
Includes:
Plaintext
Defined in:
lib/qcmd/handler.rb

Instance Method Summary collapse

Methods included from Plaintext

#ascii_qlab, #centered_text, #columns, #joined_wrapped_text, #log, #pluralize, #print, #right_text, #split_text, #table, #word_wrap, #wrapped_text

Instance Method Details

#handle(reply) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
70
71
72
73
74
75
76
77
78
# File 'lib/qcmd/handler.rb', line 5

def handle reply
  Qcmd.debug "(handling #{ reply })"

  case reply.address
  when %r[/workspaces]
    Qcmd.context.machine.workspaces = reply.data.map {|ws| Qcmd::QLab::Workspace.new(ws)}

    print centered_text(" Workspaces ", '-')
    print
    Qcmd.context.machine.workspaces.each_with_index do |ws, n|
      print "#{ n + 1 }. #{ ws.name }"
    end

    print
    print wrapped_text('Type `use "WORKSPACE_NAME" PASSCODE` to load a workspace. ' +
                       'Only enter a passcode if your workspace uses one')
    print

  when %r[/workspace/[^/]+/connect]
    # connecting to a workspace
    if reply.data == 'badpass'
      print 'failed to connect to workspace'
      Qcmd.context.disconnect_workspace
    elsif reply.data == 'ok'
      print 'connected to workspace'
      Qcmd.context.workspace_connected = true
    end

  when %r[/cueLists]
    Qcmd.debug "(received cueLists)"

    # load global cue list
    Qcmd.context.workspace.cues = cues = reply.data.map {|cue_list|
      cue_list['cues'].map {|cue| Qcmd::QLab::Cue.new(cue)}
    }.compact.flatten

    print "loaded #{pluralize cues.size, 'cue'}"

  when %r[/(selectedCues|runningCues|runningOrPausedCues)]
    cues = reply.data.map {|cue|
      cues = [Qcmd::QLab::Cue.new(cue)]

      if cue['cues']
        cues << cue['cues'].map {|cue| Qcmd::QLab::Cue.new(cue)}
      end

      cues
    }.compact.flatten

    title = case reply.address
            when /selectedCues/;        "Selected Cues"
            when /runningCues/;         "Running Cues"
            when /runningOrPausedCues/; "Running or Paused Cues"
            end

    print
    print centered_text(" #{title} ", '-')
    table(['Number', 'Id', 'Name', 'Type'], cues.map {|cue|
      [cue.number, cue.id, cue.name, cue.type]
    })
    print

  when %r[/(cue|cue_id)/[^/]+/[a-zA-Z]+]
    # properties, just print reply data
    result = reply.data
    if result.is_a?(String) || result.is_a?(Numeric)
      print result
    else
      print result.inspect
    end
  else
    Qcmd.debug "(unrecognized message from QLab, cannot handle #{ reply.address })"
  end
end