Class: Signalwire::Relay::Calling::Detect

Inherits:
ControlComponent show all
Defined in:
lib/signalwire/relay/calling/component/detect.rb

Constant Summary collapse

FINISHED_EVENTS =
[
  Relay::CallDetectState::FINISHED, 
  Relay::CallDetectState::ERROR,
  Relay::CallDetectState::READY,
  Relay::CallDetectState::NOT_READY,
  Relay::CallDetectState::MACHINE,
  Relay::CallDetectState::HUMAN,
  Relay::CallDetectState::UNKNOWN
]
READY_EVENTS =
[
  Relay::CallDetectState::FINISHED, 
  Relay::CallDetectState::ERROR,
  Relay::CallDetectState::READY,
  Relay::CallDetectState::HUMAN
]
MACHINE_EVENTS =
[Relay::CallDetectState::READY, Relay::CallDetectState::NOT_READY]

Instance Attribute Summary collapse

Attributes inherited from Component

#blocker, #call, #completed, #event, #execute_result, #state, #successful

Instance Method Summary collapse

Methods inherited from ControlComponent

#control_id, #execute_subcommand, #setup_handlers, #stop

Methods inherited from Component

#after_execute, #check_for_waiting_events, #create_blocker, #execute, #execute_params, #handle_execute_result, #has_blocker?, #payload, #setup_handlers, #setup_waiting_events, #terminate, #unblock, #wait_for, #wait_on_blocker

Methods included from Logger

#level=, #logger, logger

Constructor Details

#initialize(call:, detect:, wait_for_beep: false, timeout: 30) ⇒ Detect

Returns a new instance of Detect.



26
27
28
29
30
31
32
33
# File 'lib/signalwire/relay/calling/component/detect.rb', line 26

def initialize(call:, detect:, wait_for_beep: false, timeout: 30)
  super(call: call)
  @detect = detect
  @timeout = timeout
  @wait_for_beep = wait_for_beep
  @received_events = Concurrent::Array.new
  @waiting_for_ready = false
end

Instance Attribute Details

#resultObject (readonly)

Returns the value of attribute result.



5
6
7
# File 'lib/signalwire/relay/calling/component/detect.rb', line 5

def result
  @result
end

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/signalwire/relay/calling/component/detect.rb', line 5

def type
  @type
end

Instance Method Details

#broadcast_event(event) ⇒ Object



76
77
78
79
# File 'lib/signalwire/relay/calling/component/detect.rb', line 76

def broadcast_event(event)
  @call.broadcast "detect_#{@state}".to_sym, event
  @call.broadcast :detect_state_change, event
end

#event_typeObject



39
40
41
# File 'lib/signalwire/relay/calling/component/detect.rb', line 39

def event_type
  Relay::CallNotification::DETECT
end

#inner_paramsObject



43
44
45
46
47
48
49
50
51
# File 'lib/signalwire/relay/calling/component/detect.rb', line 43

def inner_params
  {
    node_id: @call.node_id,
    call_id: @call.id,
    control_id: control_id,
    detect: @detect,
    timeout: @timeout
  }
end

#methodObject



35
36
37
# File 'lib/signalwire/relay/calling/component/detect.rb', line 35

def method
  Relay::ComponentMethod::DETECT
end

#notification_handler(event) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/signalwire/relay/calling/component/detect.rb', line 53

def notification_handler(event)
  detect_result = event.call_params[:detect]
  @type = detect_result[:type]
  params = detect_result[:params]
  @state = params[:event]

  # if we are detecting digits we are done
  return complete(event) if @type == Relay::CallDetectType::DIGIT

  if @type == 'machine'
    if @wait_for_beep
      return complete(event) if READY_EVENTS.include?(@state)
    else
      return complete(event) if FINISHED_EVENTS.include?(@state)
    end
  else
    check_for_waiting_events
  end
  
  @received_events << @state
  broadcast_event(event)
end