summaryrefslogtreecommitdiff
path: root/misc/freeswitch/scripts/phones/phone.lua
blob: 5cd210b59098c6236ba849fb170e37cfeaf5eb24 (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
-- Gemeinschaft 5 module: phone class
-- (c) AMOOMA GmbH 2012
-- 

module(...,package.seeall)

Phone = {}

-- create phone object
function Phone.new(self, arg)
  arg = arg or {}
  object = arg.object or {}
  setmetatable(object, self);
  self.__index = self;
  self.log = arg.log;
  self.database = arg.database;
  return object;
end



-- Find a hot-deskable phone by sip-account
function Phone.find_all_hot_deskable_by_account(self, account_id)
  require 'common.str'

  local sql_query = 'SELECT \
  `b`.`id`, `b`.`mac_address`, `b`.`ip_address`, `b`.`http_user`, `b`.`http_password`, `b`.`phoneable_type`, `b`.`phoneable_id`, \
  `d`.`ieee_name` \
  FROM `phone_sip_accounts` `a` \
  JOIN `phones` `b` ON `a`.`phone_id` = `b`.`id` \
  JOIN `phone_models` `c` ON `b`.`phone_model_id` = `c`.`id` \
  JOIN `manufacturers` `d` ON `c`.`manufacturer_id` = `d`.`id` \
  WHERE `b`.`hot_deskable` IS TRUE \
  AND `c`.`state` = "active" \
  AND `d`.`state` = "active" \
  AND `a`.`sip_account_id` = ' .. tonumber(account_id);

  local account_phones = {};

  self.database:query(sql_query, function(account_entry)
    local phone = Phone:new(self, {object = parent_class});
    phone.record = account_entry;
    phone.record.ieee_name = common.str.downcase(account_entry.ieee_name);

    if phone.record.ieee_name == 'snom technology ag' then
      require 'phones.snom'
      phone.model = phones.snom.Snom:new();
    elseif account_entry.ieee_name == 'siemens enterprise communicationsgmbh & co. kg' then
      require 'phones.siemens'
      phone.model = phones.siemens.Siemens:new();
    end
    table.insert(account_phones, phone);
  end)

  return account_phones;
end


function Phone.find_hot_deskable_by_account(self, account_id)
  return self:find_all_hot_deskable_by_account(account_id)[1];
end


function Phone.tenant_id_get(self)
  local sql_query = 'SELECT `c`.`sip_accountable_id` \
    FROM `phones` `a` LEFT JOIN `phone_sip_accounts` `b` ON `a`.`id` = `b`.`phone_id` \
    JOIN `sip_accounts` `c` ON `b`.`sip_account_id` = `c`.`id` AND `sip_accountable_type` = "Tenant" \
    WHERE `a`.`id` = ' .. tonumber(self.record.id) .. ' LIMIT 1';

  local tenant_id = nil;
  self.database:query(sql_query, function(tenant_entry)
    tenant_id = tenant_entry.sip_accountable_id;
  end)

  return tenant_id;
end

function Phone.phoneable_set(self, phoneable_id, phoneable_type)
  sql_query = 'UPDATE `phones` SET `phoneable_type` = "' .. phoneable_type ..'", `phoneable_id` = ' .. phoneable_id .. ' \
    WHERE `id` = ' .. tonumber(self.record.id);
  self.database:query(sql_query);
end

function Phone.logout(self, account_id)
  local tenant_id = self:tenant_id_get();

  if not tenant_id then
    self.log:info('PHONE_LOGOUT - tenant not found');
    return false;
  end

  self:phoneable_set(tenant_id, 'Tenant');

  sql_query = 'DELETE FROM `phone_sip_accounts` WHERE `sip_account_id` = ' .. tonumber(account_id);
  return self.database:query(sql_query);
end

function Phone.login(self, account_id, owner_id, owner_type)
  self:phoneable_set(owner_id, owner_type);
  sql_query = 'INSERT INTO `phone_sip_accounts` (`phone_id`, `sip_account_id`, `position`, `created_at`, `updated_at`) \
      VALUES ('.. tonumber(self.record.id) .. ', '.. tonumber(account_id) .. ', 1,  NOW(),  NOW())';

  return self.database:query(sql_query);
end

function Phone.resync(self, arg)
  if not self.model then
    self.log:notice('PHONE_RESYNC - unsupported phone model');
    return false;
  end

  arg.ip_address = arg.ip_address or self.record.ip_address;
  return self.model:resync(arg);
end