From 994e076d326ca58ba9110bac5ad764679b172943 Mon Sep 17 00:00:00 2001 From: ben Date: Tue, 7 Jan 2025 11:28:40 -0800 Subject: added main sleep timer --- .gitignore | 1 + lua/sleep.lua | 69 ++++++++++++++++++++++++++++++++++++++++++++++++----------- sleep.json | 9 ++++---- 3 files changed, 62 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index 1ff3bf8..45a59a8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ push.sh pull_log.sh +mpv.log 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) diff --git a/sleep.json b/sleep.json index 39eb3bd..47b5c25 100644 --- a/sleep.json +++ b/sleep.json @@ -1,12 +1,13 @@ { "config": { - "default_time": 25, - "default_time_format": "%02d:%02d:%02d" - "_comment_default_time_format": "DO NOT CHANGE THIS! ^" + "default_time": 2, + "default_time_format": "%02d:%02d:%02d", + "_comment_default_time_format": "DO NOT CHANGE THIS! ^", + "display_time": true, }, "previous_state": { "timestamp": "00:00:00", - "last_updated": "2025-01-02T23:07:25Z" + "last_updated": "2025-01-02T23:07:25Z", "was_active": false, } } -- cgit v1.2.3