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, #print_wrapped, #right_text, #set_columns, #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
79
80
81
82
83
84
85
# 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)}

    unless Qcmd.quiet?
      Qcmd.context.print_workspace_list
    end

  when %r[/workspace/[^/]+/connect]
    # connecting to a workspace
    if reply.data == 'badpass'
      print 'failed to connect to workspace, bad passcode or no passcode given'
      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

  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
      case reply.address
      when %r[/basics]
        keys = result.keys.sort
        table(['Field Name', 'Value'], keys.map {|key|
          [key, result[key]]
        })
      else
        begin
          print JSON.pretty_generate(result)
        rescue JSON::GeneratorError
          print result.to_s
        end
      end
    end

  when %r[/thump]
    print reply.data

  else
    Qcmd.debug "(unrecognized message from QLab, cannot handle #{ reply.address })"
  end
end