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

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

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, #setup_handlers, #stop

Methods inherited from Component

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

Constructor Details

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

Returns a new instance of Detect.



7
8
9
10
11
12
# File 'lib/signalwire/relay/calling/component/detect.rb', line 7

def initialize(call:, detect:, timeout: 30)
  super(call: call)
  @detect = detect
  @timeout = timeout
  @received_events = Concurrent::Array.new
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



57
58
59
60
# File 'lib/signalwire/relay/calling/component/detect.rb', line 57

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

#event_typeObject



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

def event_type
  Relay::CallNotification::DETECT
end

#inner_paramsObject



22
23
24
25
26
27
28
29
30
# File 'lib/signalwire/relay/calling/component/detect.rb', line 22

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

#methodObject



14
15
16
# File 'lib/signalwire/relay/calling/component/detect.rb', line 14

def method
  Relay::ComponentMethod::DETECT
end

#notification_handler(event) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/signalwire/relay/calling/component/detect.rb', line 32

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

  if @state != Relay::CallDetectState::FINISHED && @state != Relay::CallDetectState::ERROR
    @received_events << res_event
  end

  @completed = @events_waiting.include?(@state)

  if @completed
    @successful = @state != Relay::CallDetectState::ERROR
    @result = @received_events.join(' ')
    @event = event
    unblock(event)
  end

  broadcast_event(event)
  # This component has complex logic so we handle it separately
  # check_for_waiting_events
end