Another shot at this.
[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-unwrapped_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(
50                         """
51 <!DOCTYPE html>
52 <html>
53   <head>
54     <title>Current Recipe</title>
55     <style>
56       #recipe {
57           height: 100%;
58       }
59       html,
60       body {
61           height: 100%;
62           margin: 0;
63           padding: 0;
64       }
65     </style>
66     <script>
67       const sleep = (milliseconds) => {
68           return new Promise(resolve => setTimeout(resolve, milliseconds))
69       }
70
71       function countdown() {
72           setTimeout(
73               function() {
74                   var now = new Date();
75                   var deltaMs = now.getTime() - loadedDate.getTime();
76                   var totalMs = 120000;
77                   var remainingMs = (totalMs - deltaMs);
78
79                   if (remainingMs <= 0) {
80                       // Reload unconditionally every two minutes.
81                       window.location.reload(true);
82                   }
83
84                   // Brief sleep before doing it all over again.
85                   sleep(1000).then(() => {
86                       countdown();
87                   });
88               }, 1000)
89       }
90     </script>
91   </head>
92   <body onload='javascript:loadedDate = new Date(); countdown();'>
93     <span>
94       <div id="recipe">
95         <IFRAME WIDTH=100% HEIGHT=100% SRC="{url}"></IFRAME>
96       </div>
97     </span>
98   </body>
99 </html>
100                         """
101                     )
102                     triggered = True
103
104         if not triggered:
105             file_utils.remove(RECIPE_PAGE)
106             logger.debug("Signaling the trigger")
107         globals.put("recipe_page_triggered", triggered)
108         return True