Timed script/function in Lua

£可爱£侵袭症+ 提交于 2019-12-11 18:35:25

问题


good morning friends... I am working on with Lua scripting language for a mobile app and have a requirement as follows -

The application's main aim is to schedule appointments for an individual with a Doctor. So once a user's appointment is scheduled, for e.g. 8th May @ 4:30 PM, the user should receive a "reminder alert" before an hour i.e. @ 3:30 PM.

am absolutely having a blank mind on how to get this done. I can get the user's date-time value and use the logic that a function should invoke just before 60 mins of that date-time. And that function contains my "Alert message". But how to do this?

Can anyone guide me with a clue?

Please let me know if any other inputs are required...

Thanks in advance.


回答1:


I would take an approach like this:

1.

Store each appointment's details as a .txt file containing JSON or Lua tabular data something like this:

{
    date = "14:30 01/07/2013";
    dateBooked = "09:30 23/06/2013";
    venue = "31 Dentist Street";
    appointmentType = "Routine Teeth Cleaning";
}

2.

You can have a timer class like so

Timer = {}
Timer_mt = { __index = Timer; __add = function(a,b) a:tickBy(b) end ; }

function Timer:new(delayTime,callBack)
    local timer = {callBack=callBack}

    timer.initTime = os.date() --MM/DD/YY HH:MM:SS

    --delayTime = HH:MM:SS
    _,_,hour,minute,second = string.find(delayTime,"(%d%d):(%d%d):(%d%d)")
    timer.delay = {hour=hour,minute=minute,second=second}

    --time the timer started
    _,_,hour,minute,second = string.find(timer.initTime,"(%d%d):(%d%d):(%d%d)")
    timer.startTime = {hour=hour,minute=minute,second=second}

    --time the timer started
    timer.initTime = os.date() --MM/DD/YY HH:MM:SS
    print(timer.initTime)
    _,_,hour,minute,second = string.find(timer.initTime,"(%d%d):(%d%d):(%d%d)")
    timer.currentTime = {hour=hour,minute=minute,second=second}
    return setmetatable(timer,Timer_mt)
end

function Timer:tick() --returns true if time expired
    currTime = os.date() --MM/DD/YY HH:MM:SS
    _,_,chour,cminute,csecond = string.find(currTime,"(%d%d):(%d%d):(%d%d)")
    if chour - self.startTime.hour >= tonumber(self.delay.hour) and cminute - self.startTime.minute >= tonumber(self.delay.minute) and csecond - self.startTime.second > tonumber(self.delay.second) then
        self:callBack()
        self.startTime.hour,self.startTime.minute, self.startTime.second = chour,cminute,csecond
        --return true
    end
    --return false
end

t = Timer:new("00:00:02",function () print("DONE") end)
print(t.currentTime.hour,t.currentTime.minute,t.currentTime.second)
while t:tick() or true do
    io.read()
end

(I just made this up so I advice you test it but it seems to work for me).

3. On start-up, or when a new appointment is added create a new timer, and then tick() each one at some point during the main execution, you could even have a timer which is the only one you tick() and it's callback ticks() the others... Anyway set the callback for each timer to display an alarm



来源:https://stackoverflow.com/questions/16433776/timed-script-function-in-lua

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!