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]
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.



10
11
12
13
14
15
16
17
# File 'lib/signalwire/relay/calling/component/detect.rb', line 10

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



64
65
66
67
# File 'lib/signalwire/relay/calling/component/detect.rb', line 64

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

#event_typeObject



23
24
25
# File 'lib/signalwire/relay/calling/component/detect.rb', line 23

def event_type
  Relay::CallNotification::DETECT
end

#inner_paramsObject



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

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

#methodObject



19
20
21
# File 'lib/signalwire/relay/calling/component/detect.rb', line 19

def method
  Relay::ComponentMethod::DETECT
end

#notification_handler(event) ⇒ Object



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
# File 'lib/signalwire/relay/calling/component/detect.rb', line 37

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

  return complete(event) if FINISHED_EVENTS.include?(@state) || @type == Relay::CallDetectType::DIGIT

  if has_blocker?
    @received_events << @state 
    return
  end

  if @waiting_for_ready
    return (@state == Relay::CallDetectState::READY ? complete(event) : nil)
  end

  if (@wait_for_beep && @state == Relay::CallDetectState::MACHINE)
    @waiting_for_ready = true
    return
  end
  
  check_for_waiting_events
  broadcast_event(event)
end