2018-02-26 23:34:53 -06:00
|
|
|
target_time = {hour=hour, min=minute, sec=second}
|
|
|
|
|
|
|
|
function get_total_day_seconds (time_table)
|
|
|
|
return (((time_table.hour * 60) + (time_table.min)) * 60) + time_table.sec
|
|
|
|
end
|
|
|
|
|
|
|
|
SECONDS_PER_DAY = get_total_day_seconds({hour=24, min=0, sec=0})
|
|
|
|
|
|
|
|
function get_sleep_duration_sec ()
|
|
|
|
current_time = os.time()
|
|
|
|
current_time_table = os.date("*t", current_time)
|
|
|
|
current_day_seconds = get_total_day_seconds(current_time_table)
|
|
|
|
target_day_seconds = get_total_day_seconds(target_time)
|
|
|
|
difference = target_day_seconds - current_day_seconds
|
|
|
|
if difference > 0 then
|
|
|
|
return difference
|
|
|
|
else
|
|
|
|
return SECONDS_PER_DAY + difference
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function get_weekday ()
|
2018-02-27 08:10:23 -06:00
|
|
|
return os.date("%a", os.time())
|
2018-02-26 23:34:53 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
function sleep (sec)
|
|
|
|
return os.execute("sleep " .. tonumber(sec))
|
|
|
|
end
|
|
|
|
|
2018-04-29 17:47:25 -05:00
|
|
|
function run_dailies (dailies)
|
|
|
|
while true do
|
|
|
|
sleep_duration = get_sleep_duration_sec()
|
|
|
|
print("sleep for " .. sleep_duration)
|
|
|
|
|
|
|
|
if not sleep(sleep_duration) then
|
|
|
|
print("sleep exited abnormally - break")
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
|
|
|
time_table = os.date("*t", os.time())
|
|
|
|
time_table.weekday = get_weekday()
|
|
|
|
for i, fn in dailies do
|
|
|
|
message = fn(time_table)
|
|
|
|
if message then
|
|
|
|
print("send message " .. message)
|
|
|
|
sender:send({type = "message", channel = channel, message = message})
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2018-02-26 23:34:53 -06:00
|
|
|
end
|
|
|
|
end
|