From c20557cc32f8ae11b786f5074120026e07837ec0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Rudowicz?= Date: Tue, 10 Sep 2024 18:39:15 +0200 Subject: [PATCH] Display network request result on screen --- menu.py | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/menu.py b/menu.py index 4277150..674de62 100644 --- a/menu.py +++ b/menu.py @@ -4,7 +4,6 @@ import math import argparse import json from enum import Enum, auto -from time import sleep from threading import Thread from queue import Queue, Empty from typing import Tuple, Callable, TypedDict, List, Dict, Optional @@ -162,6 +161,7 @@ class MenuBoard(IBoard): class App: FPS = 30 + SHOW_REQUEST_MESSAGE_FOR_AT_LEAST_S = 5 def __init__(self, actions: List[Action], theme: Dict, fullscreen: bool, screensaver_delay: int): pygame.init() @@ -183,6 +183,11 @@ class App: def get_screen_rect(self) -> pygame.Rect: return pygame.Rect(0, 0, self.screen.get_width(), self.screen.get_height()) + def show_message(self, msg: str) -> Callable[[], None]: + def x(): + self.board = MessageBoard(self.get_screen_rect(), msg, theme=self.theme) + return x + def show_message_handler(self, action: Action) -> Callable[[], None]: def impl(): previous_board = self.board @@ -193,15 +198,10 @@ class App: try: sleep_time = int(action['action_param']) - sleep(sleep_time) + pygame.time.wait(sleep_time * 1000) except Exception as e: - def f(ex): - def x(): - self.board = MessageBoard(self.get_screen_rect(), f"Exception caught: {ex}", - theme=self.theme) - return x - self.task_q.put(f(e)) - sleep(5) + self.task_q.put(self.show_message(f"Exception caught: {e}")) + pygame.time.wait(5 * 1000) finally: self.task_q.put(end_thr) @@ -217,14 +217,16 @@ class App: def thr_fun(): def end_thr(): self.board = previous_board - + req_start_ts = pygame.time.get_ticks() ret = requests.get(action['action_param']) print(f"GET {action['action_param']}: {ret}") + self.task_q.put(self.show_message(f"Request done: {ret}")) + pygame.time.wait(max(self.SHOW_REQUEST_MESSAGE_FOR_AT_LEAST_S * 1000 - (pygame.time.get_ticks() - req_start_ts), 0)) self.task_q.put(end_thr) + self.board = MessageBoard(self.get_screen_rect(), action['action_message'], theme=self.theme) process_thr = Thread(target=thr_fun) process_thr.start() - self.board = MessageBoard(self.get_screen_rect(), action['action_message'], theme=self.theme) return impl def button_press_handler(self, action: Action) -> Callable[[], None]: