summaryrefslogtreecommitdiff
path: root/app/models/call.rb
blob: 6755a96cea289fb62bbeaac523238c3260fa13d3 (plain)
1
2
3
4
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
class Call < ActiveRecord::Base
  self.table_name = 'calls_active'
  self.primary_key = 'uuid'

  belongs_to :sip_account
  belongs_to :b_sip_account, :class_name => SipAccount

  validates :sip_account_id,
            :presence => true

  validates :destination,
            :presence => true
  
  def save(attributes=nil) 
  end

  def call
    if self.sip_account && self.destination
      return self.sip_account.call(self.destination)
    end

    if !self.sip_account
      errors.add(:sip_account_id, 'no sip_account')
    end

    if self.destination.blank?
      errors.add(:destination, 'no destination')
    end

    return false
  end

  def destroy
    return self.delete
  end

  def delete
    require 'freeswitch_event'
    return FreeswitchAPI.execute('uuid_kill', self.uuid, true)
  end

  def transfer_blind(destination, call_leg=:aleg, auth_account=nil)
    if destination.blank?
      return nil
    end

    channel_uuid = call_leg == :bleg ? self.b_uuid : channel_uuid = self.uuid

    if channel_uuid.blank?
      return nil
    end

    require 'freeswitch_event'
    if auth_account 
      FreeswitchAPI.api('uuid_setvar', channel_uuid, 'gs_auth_account_type', auth_account.class.name)
      FreeswitchAPI.api('uuid_setvar', channel_uuid, 'gs_auth_account_uuid', auth_account.uuid)
    end

    return FreeswitchAPI.api_result(FreeswitchAPI.api('uuid_transfer', channel_uuid, destination))
  end

  def hold(call_leg=:aleg, action=:hold)
    channel_uuid = call_leg == :bleg ? self.b_uuid : channel_uuid = self.uuid
    hold_off = action == :retrieve ? 'off' : ''

    if channel_uuid.blank?
      return nil
    end

    require 'freeswitch_event'
    result = FreeswitchAPI.api_result(FreeswitchAPI.api('uuid_hold', hold_off, channel_uuid))
  end

  def park(call_leg=:aleg)
    channel_uuid = call_leg == :bleg ? self.b_uuid : channel_uuid = self.uuid

    if channel_uuid.blank?
      return nil
    end

    require 'freeswitch_event'
    result = FreeswitchAPI.api_result(FreeswitchAPI.api('uuid_park', channel_uuid))
  end
  
  def get_variable_from_uuid(channel_uuid, variable_name)
    if channel_uuid.blank? 
      return nil
    end

    require 'freeswitch_event'
    result = FreeswitchAPI.channel_variable_get(channel_uuid, variable_name);

    if result == '_undef_'
      return nil
    end

    return result
  end

  def get_variable(variable_name)
    return get_variable_from_uuid(self.uuid, variable_name);
  end

  def get_variable_bleg(variable_name)
    return get_variable_from_uuid(self.b_uuid, variable_name);
  end

  def self.bridge(call_uuid1, call_uuid2, hangup_uuids=[])
    if call_uuid1.blank? || call_uuid2.blank?
      return nil
    end

    require 'freeswitch_event'
    result = FreeswitchAPI.api_result(FreeswitchAPI.api('uuid_bridge', call_uuid1, call_uuid2))
    
    if result 
      hangup_uuids.each do |kill_uuid|
        FreeswitchAPI.execute('uuid_kill', kill_uuid, true)
      end
    end

    return result
  end
end