diff options
author | ben <ben@nagy.contact> | 2025-01-07 11:28:40 -0800 |
---|---|---|
committer | ben <ben@nagy.contact> | 2025-01-07 11:28:40 -0800 |
commit | 994e076d326ca58ba9110bac5ad764679b172943 (patch) | |
tree | 3141c5e3c1b310017ffdf0badda53dab5da218bc /lua | |
parent | 9c8a9a894ac1436b1b5f7ef9ed8c2e163ba8eb88 (diff) |
added main sleep timer
Diffstat (limited to 'lua')
-rw-r--r-- | lua/sleep.lua | 69 |
1 files changed, 56 insertions, 13 deletions
diff --git a/lua/sleep.lua b/lua/sleep.lua index 3a48469..09aa62c 100644 --- a/lua/sleep.lua +++ b/lua/sleep.lua @@ -1,13 +1,13 @@ local mp = require 'mp' ---local socket = require("socket") -- not portable local config_file = "/storage/emulated/0/Android/media/is.xyz.mpv/scripts/sleep.json" local time = { - minutes = 25, + minutes = 0.1, active = false, format = "%02d:%02d:%02d", -- (HH:MM:SS) remaining = nil, + display_time = true, prev_state = { timestamp = "", @@ -85,6 +85,8 @@ local function read_config() mp.msg.info("[sleep.lua]: closing", config_file .. ".") io.close(openf) + -- TODO: put in table, then index with @param and return it + -- TODO: add display_time boolean local default_time = read_jsonkey_value(file_str, "\"config\"", "\"default_time\"%s*:%s*(%d+)") local prevstate_tstamp = read_jsonkey_value(file_str, "\"previous_state\"", "\"timestamp\"%s*:%s*\"(.-)\"") local prevstate_lastup = read_jsonkey_value(file_str, "\"previous_state\"", "\"last_updated\"%s*:%s*\"(.-)\"") @@ -96,28 +98,64 @@ local function read_config() mp.msg.info("prevstate_active: " .. prevstate_active) end +--------------------------- +-- Sleep / Timer -- +--------------------------- + local function reinstate_tstamp(time) -- according to a gesture, this should be called, and seek to @param in file -- we also need to verify that we're in the correct file. end local function export_time() - -- we should retrieve the current time stamp and export it to sleep.json - -- at this point, mpv should be paused or terminated. -end + mp.msg.info("[sleep.lua]: exporting date and timestamp") + time.prev_state.timestamp = mp.get_property_number("time-pos") + time.prev_state.was_active = false + time.prev_state.last_update = os.date("!%Y-%m-%dT%H:%M:%SZ") -- ISO 8601 UTC -local function stop_timer() -end - -local function remove_timer() + -- TODO: EXPORT TO sleep.json end local function set_timer() -end + time.active = true + time.remaining = time.minutes * 60 + + local update_timer + update_timer = mp.add_periodic_timer(1, function() + if not time.active then + update_timer:kill() + return + end + time.remaining = time.remaining - 1 + + if time.display_time then + local hrs = math.floor(time.remaining / 3600) + local min = math.floor((time.remaining % 3600) / 60) + local sec = time.remaining % 60 + local rem = string.format(time.format, hrs, min, sec) -local function display_time() + mp.osd_message("Sleep Timer: " .. rem, 3) + mp.msg.info("[sleep.lua]: time remaining: " .. rem) + end + + if time.remaining <= 0 then + mp.msg.info("[sleep.lua]: time expired. Killing timer") + update_timer:kill() + time.active = false + time.remaining = nil + + export_time() + mp.osd_message("Sleep timer expired - pausing playback", 3) + mp.msg.info("[sleep.lua]: pausing playback.") + mp.set_property("pause", "yes") + end + end) end +--------------------------- +-- Action / Gestures -- +--------------------------- + local function cancel_pending_action() if gesture_state.actions.timer then gesture_state.actions.timer:kill() @@ -169,11 +207,16 @@ local function handle_gesture() if gesture_state.triggers.count == ActionType.SET_TIMER then local confirmed = confirm_action(ActionType.SET_TIMER) - if confirmed then + if confirmed and not time.active then gesture_state.actions.callback = function() - --set_timer() + mp.msg.info("[sleep.lua]: sleep action confirmed. Setting timer for " .. time.minutes .. " minutes") + set_timer() mp.osd_message("Timer has been set!") end + else + -- TODO: + mp.osd_message("Timer already exists. [duration remaining]\nGesture again to reset the timer") + mp.msg.info("[sleep.lua]: timer already exists.") end elseif gesture_state.triggers.count == ActionType.REMOVE_TIMER then local confirmed = confirm_action(ActionType.REMOVE_TIMER) |