6d78e7327dcc794bee3bea8222d9c821d8941a73
[kiosk.git] / recipe_renderer_and_trigger.py
1 #!/usr/bin/env python3
2
3 import logging
4 import os
5 from typing import Dict, Optional, List, Tuple
6
7 from pyutils.files import file_utils
8
9 import kiosk_constants as constants
10 import file_writer
11 import globals
12 import renderer
13 import trigger
14
15
16 logger = logging.getLogger(__file__)
17
18 RECIPE_PAGE = "recipe_1_82400.html"
19 RECIPE_PATH = os.path.join(constants.pages_dir, RECIPE_PAGE)
20
21
22 class RecipeTrigger(trigger.trigger):
23     def get_triggered_page_list(self) -> Optional[List[Tuple[str, int]]]:
24         if globals.get("recipe_page_triggered"):
25             logger.debug("Recipe page is triggered!")
26             return [(RECIPE_PAGE, trigger.trigger.PRIORITY_HIGH)]
27         return None
28
29
30 class RecipeRenderer(renderer.abstaining_renderer):
31     def __init__(
32         self,
33         url_location: str,
34         name_to_timeout_dict: Dict[str, int],
35     ) -> None:
36         super().__init__(name_to_timeout_dict)
37         self.url_location = url_location
38
39     def periodic_render(self, key: str) -> bool:
40         triggered = False
41         if file_utils.does_path_exist(self.url_location):
42             with open(self.url_location, "r") as rf:
43                 url = rf.read()
44             url.strip()
45             logger.debug(f"Read {url} from {self.url_location}, writing the page")
46
47             if url and len(url) > 0:
48                 with file_writer.file_writer(RECIPE_PATH) as f:
49                     f.write(f'<IFRAME SRC="{url}"></IFRAME>')
50                     triggered = True
51
52         if not triggered:
53             file_utils.remove(RECIPE_PAGE)
54             logger.debug("Signaling the trigger")
55         globals.put("recipe_page_triggered", triggered)
56         return True