From a1fb8711ffad232ead10280bd3af83c23ed988e5 Mon Sep 17 00:00:00 2001 From: Felix Claessen <30658763+Flix6x@users.noreply.github.com> Date: Fri, 27 Jan 2023 11:39:21 +0100 Subject: [PATCH] Keyboard replay control (#562) A bit of refactoring along with the introduction of keyboard control of replay (`p` to play/pause/resume and `s` to stop). * Approximate constant replay speed Signed-off-by: F.N. Claessen * Listen to p-key presses to pause/resume Signed-off-by: F.N. Claessen * Refactor: also start replay when hitting 'p' Signed-off-by: F.N. Claessen * Refactor: clean up util functions Signed-off-by: F.N. Claessen * Use stopReplay on calendar selection, too Signed-off-by: F.N. Claessen * Refactor: simplify for-loop Signed-off-by: F.N. Claessen * Listen to s-key presses to stop Signed-off-by: F.N. Claessen * Show previous results when stopping replay Signed-off-by: F.N. Claessen * Use abort signals to resolve glitches when pressing the next key before resolving promises Signed-off-by: F.N. Claessen * Changelog entry Signed-off-by: F.N. Claessen * Add UI hints Signed-off-by: F.N. Claessen Signed-off-by: F.N. Claessen --- documentation/changelog.rst | 1 + flexmeasures/ui/static/js/replay-utils.js | 19 ++ flexmeasures/ui/templates/base.html | 222 +++++++++++-------- flexmeasures/ui/templates/crud/asset.html | 2 +- flexmeasures/ui/templates/views/sensors.html | 2 +- 5 files changed, 150 insertions(+), 96 deletions(-) diff --git a/documentation/changelog.rst b/documentation/changelog.rst index 0f1f68760..b463663b4 100644 --- a/documentation/changelog.rst +++ b/documentation/changelog.rst @@ -10,6 +10,7 @@ v0.13.0 | February XX, 2023 New features ------------- +* Keyboard control over replay [see `PR #562 `_] * The ``FLEXMEASURES_MAX_PLANNING_HORIZON`` config setting can also be set as an integer number of planning steps rather than just as a fixed duration, which makes it possible to schedule further ahead in coarser time steps [see `PR #583 `_] Bugfixes diff --git a/flexmeasures/ui/static/js/replay-utils.js b/flexmeasures/ui/static/js/replay-utils.js index 34b3f266f..2ab52e3c9 100644 --- a/flexmeasures/ui/static/js/replay-utils.js +++ b/flexmeasures/ui/static/js/replay-utils.js @@ -42,3 +42,22 @@ export function updateBeliefs(oldBeliefs, newBeliefs) { // Define the step duration for the replay (value in ms) export var beliefTimedelta = 3600000 + + +/** + * Timer that can be canceled using the optional AbortSignal. + * Adapted from https://www.bennadel.com/blog/4195-using-abortcontroller-to-debounce-settimeout-calls-in-javascript.htm + * MIT License: https://www.bennadel.com/blog/license.htm + */ +export function setAbortableTimeout(callback, delayInMilliseconds, signal) { + signal?.addEventListener( "abort", handleAbort ); + var internalTimer = setTimeout(internalCallback, delayInMilliseconds); + + function internalCallback() { + signal?.removeEventListener( "abort", handleAbort ); + callback(); + } + function handleAbort() { + clearTimeout( internalTimer ); + } +} diff --git a/flexmeasures/ui/templates/base.html b/flexmeasures/ui/templates/base.html index 2099f300f..6807f2647 100644 --- a/flexmeasures/ui/templates/base.html +++ b/flexmeasures/ui/templates/base.html @@ -231,7 +231,7 @@ {% endblock sensorChartSetup %} diff --git a/flexmeasures/ui/templates/crud/asset.html b/flexmeasures/ui/templates/crud/asset.html index 9203a6ad6..a128a07c0 100644 --- a/flexmeasures/ui/templates/crud/asset.html +++ b/flexmeasures/ui/templates/crud/asset.html @@ -158,7 +158,7 @@

All sensors for {{ asset.name }}

-
+
diff --git a/flexmeasures/ui/templates/views/sensors.html b/flexmeasures/ui/templates/views/sensors.html index 7d9e6f785..a031ca4ef 100644 --- a/flexmeasures/ui/templates/views/sensors.html +++ b/flexmeasures/ui/templates/views/sensors.html @@ -26,7 +26,7 @@
-
+