mirror of
https://github.com/fusionpbx/fusionpbx.git
synced 2026-03-09 01:58:44 +00:00
Add. Event handler to support MWI. (#1720)
* Add. Event handler to support MWI. * Fix. store cache only when get data from memcache * Change. Use UUID as PID.
This commit is contained in:
committed by
FusionPBX
parent
ae1d180b5e
commit
8a72e2afd8
54
resources/install/scripts/resources/functions/api.lua
Normal file
54
resources/install/scripts/resources/functions/api.lua
Normal file
@@ -0,0 +1,54 @@
|
||||
-- Decode result of api execute command to Lua way.
|
||||
-- in case of error function returns `nil` and `error message`.
|
||||
-- in other case function return result as is.
|
||||
local function api_result(result)
|
||||
if string.find(result, '^%-ERR') or string.find(result, '^INVALID COMMAND!') then
|
||||
return nil, string.match(result, "(.-)%s*$")
|
||||
end
|
||||
|
||||
return result
|
||||
end
|
||||
|
||||
local function class(base)
|
||||
local t = base and setmetatable({}, base) or {}
|
||||
t.__index = t
|
||||
t.__class = t
|
||||
t.__base = base
|
||||
|
||||
function t.new(...)
|
||||
local o = setmetatable({}, t)
|
||||
if o.__init then
|
||||
if t == ... then -- we call as Class:new()
|
||||
return o:__init(select(2, ...))
|
||||
else -- we call as Class.new()
|
||||
return o:__init(...)
|
||||
end
|
||||
end
|
||||
return o
|
||||
end
|
||||
|
||||
return t
|
||||
end
|
||||
|
||||
local API = class() do
|
||||
|
||||
function API:__init(...)
|
||||
self._api = freeswitch.API(...)
|
||||
return self
|
||||
end
|
||||
|
||||
function API:execute(...)
|
||||
return api_result(self._api:execute(...))
|
||||
end
|
||||
|
||||
function API:executeString(...)
|
||||
return api_result(self._api:executeString(...))
|
||||
end
|
||||
|
||||
function API:getTime()
|
||||
return self._api:getTime()
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
return API.new()
|
||||
17
resources/install/scripts/resources/functions/ievents.lua
Normal file
17
resources/install/scripts/resources/functions/ievents.lua
Normal file
@@ -0,0 +1,17 @@
|
||||
local ievents = function(events, ...)
|
||||
if type(events) == 'string' then
|
||||
events = freeswitch.EventConsumer(events)
|
||||
end
|
||||
|
||||
local block, timeout = ...
|
||||
if timeout == 0 then block, timeout = 0, 0 end
|
||||
timeout = timeout or 0
|
||||
|
||||
return function()
|
||||
local event = events:pop(block, timeout)
|
||||
if not event then return false end
|
||||
return event
|
||||
end
|
||||
end
|
||||
|
||||
return ievents
|
||||
@@ -0,0 +1,64 @@
|
||||
local os_time = {
|
||||
now = function() return os.time() end;
|
||||
elapsed = function(t) return os.difftime(os.time(), t) end;
|
||||
ms_to_time = function(ms) return ms / 1000 end;
|
||||
time_to_ms = function(t) return t * 1000 end;
|
||||
}
|
||||
|
||||
local os_clock = {
|
||||
now = function() return os.clock() end;
|
||||
elapsed = function(t) return os.clock() - t end;
|
||||
ms_to_time = function(ms) return ms / 1000 end;
|
||||
time_to_ms = function(t) return t * 1000 end;
|
||||
}
|
||||
|
||||
local IntervalTimer = {} do
|
||||
IntervalTimer.__index = IntervalTimer
|
||||
|
||||
function IntervalTimer.new(interval, timer)
|
||||
local o = setmetatable({}, IntervalTimer)
|
||||
o._interval = interval
|
||||
o._timer = timer or os_clock
|
||||
|
||||
return o
|
||||
end
|
||||
|
||||
function IntervalTimer:start()
|
||||
assert(not self:started())
|
||||
return self:restart()
|
||||
end
|
||||
|
||||
function IntervalTimer:restart()
|
||||
self._begin = self._timer.now()
|
||||
return self
|
||||
end
|
||||
|
||||
function IntervalTimer:started()
|
||||
return not not self._begin
|
||||
end
|
||||
|
||||
function IntervalTimer:elapsed()
|
||||
assert(self:started())
|
||||
local e = self._timer.elapsed(self._begin)
|
||||
return self._timer.time_to_ms(e)
|
||||
end
|
||||
|
||||
function IntervalTimer:rest()
|
||||
local d = self._interval - self:elapsed()
|
||||
if d < 0 then d = 0 end
|
||||
return d
|
||||
end
|
||||
|
||||
function IntervalTimer:stop()
|
||||
if self:started() then
|
||||
local d = self:elapsed()
|
||||
self._begin = nil
|
||||
return d
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
return {
|
||||
new = IntervalTimer.new;
|
||||
}
|
||||
Reference in New Issue
Block a user