mirror of
https://github.com/fusionpbx/fusionpbx.git
synced 2026-01-13 12:08:24 +00:00
* Add. Support `onInterval` method to EventConsumer class Usage ```Lua -- execute action each 30 sec events:onInterval(30*1000, function() end) -- execute action once after 5 min events:onIntervalOnce(5*60*1000, function() end) ``` * Fix. Remove timers * Fix. Reset timer before callback It allows stop timer inside callback. Also it produce more accurate interval invocation if callback take quite a long time. E.g. Interval = 10 sec and callback took 5 sec then if we reset timer after this callback then gap between invocation will be 15 sec. * Add. Timers now have TimeEvent class type. Add. `reset` method to IntervalTimer class. ```Lua events:onIntervalOnce(1000, function(self, timer) -- timer has type TimeEvent -- restart timer so it will be invoke again timer:restart() -- or reset new interval -- timer:reset(5000) end) ``` * Fix. Typo in variable name
89 lines
2.0 KiB
Lua
89 lines
2.0 KiB
Lua
-- absolute timer
|
|
local fs_time if freeswitch then
|
|
local api = require "resources.functions.api"
|
|
fs_time = {
|
|
now = function() return api:getTime() end;
|
|
elapsed = function(t) return api:getTime() - t end;
|
|
ms_to_time = function(ms) return ms end;
|
|
time_to_ms = function(t) return t end;
|
|
}
|
|
end
|
|
|
|
-- absolute timer
|
|
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;
|
|
}
|
|
|
|
-- monotonic timer (not work on my test Debian system)
|
|
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 timers = {
|
|
freeswitch = fs_time;
|
|
time = os_time;
|
|
clock = os_clock;
|
|
}
|
|
|
|
local IntervalTimer = {} do
|
|
IntervalTimer.__index = IntervalTimer
|
|
|
|
function IntervalTimer.new(interval, timer)
|
|
local o = setmetatable({}, IntervalTimer)
|
|
o._interval = interval
|
|
o._timer = timer and assert(timers[timer], "unknown timer: " .. timer) or os_time
|
|
|
|
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:reset(interval)
|
|
self._interval = interval
|
|
if self._begin then self:restart() end
|
|
return self
|
|
end
|
|
|
|
function IntervalTimer:stop()
|
|
if self:started() then
|
|
local d = self:elapsed()
|
|
self._begin = nil
|
|
return d
|
|
end
|
|
end
|
|
|
|
end
|
|
|
|
return {
|
|
new = IntervalTimer.new;
|
|
} |