025d3c05983e244e4a055620610870d0eec7e037
[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 = os.path.join(constants.pages_dir, "recipe_1_82400.html")
19
20
21 class RecipeTrigger(trigger.trigger):
22     def get_triggered_page_list(self) -> Optional[List[Tuple[str, int]]]:
23         if globals.get("recipe_page_triggered"):
24             logger.debug("Recipe page is triggered!")
25             return [(RECIPE_PAGE, trigger.trigger.PRIORITY_HIGH)]
26         return None
27
28
29 class RecipeRenderer(renderer.abstaining_renderer):
30     def __init__(
31         self,
32         url_location: str,
33         name_to_timeout_dict: Dict[str, int],
34     ) -> None:
35         super().__init__(name_to_timeout_dict)
36         self.url_location = url_location
37
38     def periodic_render(self, key: str) -> bool:
39         triggered = False
40         if file_utils.does_path_exist(self.url_location):
41             with open(self.url_location, "r") as rf:
42                 url = rf.read()
43             url.strip()
44             logger.debug(f"Read {url} from {self.url_location}, writing the page")
45
46             if url and len(url) > 0:
47                 with file_writer.file_writer(RECIPE_PAGE) as f:
48                     f.write('<IFRAME SRC="{url}"></IFRAME>')
49                     triggered = True
50
51         if not triggered:
52             file_utils.remove(RECIPE_PAGE)
53             logger.debug("Signaling the trigger")
54         globals.put("recipe_page_triggered", triggered)
55         return True