More changes related to running on new kiosk.house.
[kiosk.git] / kiosk.py
index 6d2fd4e00b5677c3bc8ea1728560ebb5912c6496..bd7137335ea25e5dbae0d8f39a9226c53c9bb19a 100755 (executable)
--- a/kiosk.py
+++ b/kiosk.py
@@ -220,43 +220,48 @@ def thread_change_current(command_queue: Queue) -> None:
 
         if triggered:
             print("chooser[%s] - WE ARE TRIGGERED." % utils.timestamp())
-            if page != page_history[0] or (swap_page_target - now < 10.0):
+            if page != page_history[0] or (swap_page_target - now) < 10.0:
                 print(
                     "chooser[%s] - EMERGENCY PAGE %s LOAD NEEDED"
                     % (utils.timestamp(), page)
                 )
-                try:
-                    with open(os.path.join(constants.pages_dir, "current.shtml"), "w") as f:
-                        emit_wrapped(f, page, override_refresh_sec = 40, command = command)
-                    page_history.insert(0, page)
-                    page_history = page_history[0:10]
-                    swap_page_target = now + 40
-                except:
-                    print("chooser[%s] - page does not exist?!" % (utils.timestamp()))
-                    continue
+#                try:
+                current = os.path.join(constants.pages_dir, "current.shtml")
+                with open(current, "w") as f:
+                    emit_wrapped(f, page, override_refresh_sec = 40, command = command)
+                    print(f'Wrote {current}')
+
+                page_history.insert(0, page)
+                page_history = page_history[0:10]
+                swap_page_target = now + 40
+#                except:
+#                    print("chooser[%s] - page does not exist?!" % (utils.timestamp()))
+#                    continue
 
                 # Also notify XMLHTTP clients that they need to refresh now.
-                path = os.path.join(constants.pages_dir, "reload_immediately.html")
-                with open(path, "w") as f:
-                    f.write("Reload, suckers!")
+                emergency_file = os.path.join(constants.pages_dir, "reload_immediately.html")
+                with open(emergency_file, "w") as f:
+                    f.write(f'Reload, suckers... you HAVE to see {page}!')
+                    print(f'Causing immediate page reload with {emergency_file}...')
 
                 # Fix this hack... maybe read the webserver logs and see if it
                 # actually was picked up?
-                time.sleep(0.75)
-                os.remove(path)
+                time.sleep(3.0)
+                os.remove(emergency_file)
+                print(f'...and removed {emergency_file}')
 
         elif now >= swap_page_target:
             assert page != page_history[0]
             print("chooser[%s] - nominal choice of %s" % (utils.timestamp(), page))
-            try:
-                with open(os.path.join(constants.pages_dir, "current.shtml"), "w") as f:
-                    emit_wrapped(f, page)
-                page_history.insert(0, page)
-                page_history = page_history[0:10]
-                swap_page_target = now + constants.refresh_period_sec
-            except:
-                print("chooser[%s] - page does not exist?!" % (utils.timestamp()))
-                continue
+#            try:
+            with open(os.path.join(constants.pages_dir, "current.shtml"), "w") as f:
+                emit_wrapped(f, page)
+            page_history.insert(0, page)
+            page_history = page_history[0:10]
+            swap_page_target = now + constants.refresh_period_sec
+#            except:
+#                print("chooser[%s] - page does not exist?!" % (utils.timestamp()))
+#                continue
         time.sleep(1)
 
 
@@ -298,7 +303,7 @@ def emit_wrapped(f,
         pageid = f'"{command}" -> {filename}'
 
     f.write(
-        """
+"""
 <HEAD>
   <TITLE>Kitchen Kiosk</TITLE>
   <LINK rel="stylesheet" type="text/css" href="style.css">
@@ -309,7 +314,7 @@ def emit_wrapped(f,
   function zoomScreen() {
     z = 285;
     do {
-      document.getElementById("content").style.zoom = z+"%%";
+      document.getElementById("content").style.zoom = z+"%";
       var body = document.body;
       var html = document.documentElement;
       var height = Math.max(body.scrollHeight,
@@ -344,7 +349,9 @@ def emit_wrapped(f,
       }
     }
   }
-
+""")
+    f.write(
+"""
   // Operate the clock at the top of the page.
   function runClock() {
     var today = new Date();
@@ -361,7 +368,9 @@ def emit_wrapped(f,
     document.getElementById("date").innerHTML = today.toDateString();
     var t = setTimeout(function(){runClock()}, 1000);
   }
-
+""" % bgcolor)
+    f.write(
+"""
   // Helper method for running the clock.
   function maybeAddZero(x) {
     return (x < 10) ? "0" + x : x;
@@ -392,7 +401,10 @@ def emit_wrapped(f,
   addLoadEvent(zoomScreen);
   addLoadEvent(runClock);
   addLoadEvent(lateLoadImages);
+""")
 
+    f.write(
+"""
   // Runs the countdown line at the bottom and is responsible for
   // normal page reloads caused by the expiration of a timer.
   (function countdown() {
@@ -422,14 +434,16 @@ def emit_wrapped(f,
         });
       }, 50)
   })();
-
+""" % get_refresh_period())
+    f.write(
+"""
   // Periodically checks for emergency reload events.
   (function poll() {
     setTimeout(
       function() {
         var xhr = new XMLHttpRequest();
         xhr.open('GET',
-                 'http://%s/kiosk/pages/reload_immediately.html');
+                 '%s/reload_immediately.html');
         xhr.onload =
           function() {
             if (xhr.status === 200) {
@@ -443,10 +457,13 @@ def emit_wrapped(f,
         xhr.send();
       }, 500);
   })();
-</SCRIPT>
+  </SCRIPT>
 </HEAD>
-<BODY BGCOLOR="#%s">
-    <TABLE style="height:100%%; width:100%%" BORDER=0>
+""" % constants.root_url)
+    f.write(f'<BODY BGCOLOR="#{bgcolor}">')
+    f.write(
+"""
+    <TABLE style="height:100%; width:100%" BORDER=0>
     <TR HEIGHT=30>
         <TD ALIGN="left">
             <DIV id="date">&nbsp;</DIV>
@@ -462,13 +479,19 @@ def emit_wrapped(f,
         <TD COLSPAN=3>
             <DIV ID="content" STYLE="zoom: 1; visibility: hidden;">
               <!-- BEGIN main page contents. -->
-<!--#include virtual=\"%s\"-->
-              <!-- END main page contents. -->
+""")
+    f.write(f'<!--#include virtual="{filename}"-->')
+    f.write(
+"""
+            <!-- END main page contents. -->
             </DIV>
             <BR>
-            <DIV STYLE="position: absolute; top:1030px; width:99%%">
+            <DIV STYLE="position: absolute; top:1030px; width:99%">
             <P ALIGN="right">
-              <FONT SIZE=2 COLOR=#bbbbbb>%s @ %s ago.</FONT>
+""")
+    f.write(f'<FONT SIZE=2 COLOR=#bbbbbb>{pageid} @ {age} ago.</FONT>')
+    f.write(
+"""
             </P>
             <HR id="countdown" STYLE="width:0px;
                                       text-align:left;
@@ -482,17 +505,7 @@ def emit_wrapped(f,
         </TD>
     </TR>
     </TABLE>
-</BODY>"""
-        % (
-            bgcolor,
-            get_refresh_period(),
-            constants.hostname,
-            bgcolor,
-            filename,
-            pageid,
-            age,
-        )
-    )
+</BODY>""")
 
 
 def thread_invoke_renderers() -> None:
@@ -538,12 +551,13 @@ f'''
         with file_writer.file_writer(constants.internal_stats_pagename) as f:
             f.write(
 f'''
-<TABLE BORDER=0>
+<CENTER>
+<TABLE BORDER=0 WIDTH=95%>
     <TR>
-    <TH>Renderer Name</TH>
-    <TH>Last Run</TH>
-    <TH>Num Invocations</TH>
-    <TH>Render Latency</TH>
+    <TH><B>Renderer Name</B></TH>
+    <TH><B>Last Run</B></TH>
+    <TH><B>Num Invocations</B></TH>
+    <TH><B>Render Latency</B></TH>
     </TR>
 ''')
             for n, r in enumerate(renderer_catalog.get_renderers()):
@@ -571,10 +585,10 @@ f'''
                 f.write(
 f'''
     <TR>
-    <TD {style}>{name}</TD>
-    <TD {style}>{last}</TD>
-    <TD {style}><CENTER>{count}</CENTER></TD>
-    <TD {style}>p25={p25:5.2f}, p50={p50:5.2f}, p75={p75:5.2f}, p90={p90:5.2f}, p99={p99:5.2f}</TD>
+    <TD {style}>{name}&nbsp;</TD>
+    <TD {style}>&nbsp;{last}&nbsp;</TD>
+    <TD {style}><CENTER>&nbsp;{count}&nbsp;</CENTER></TD>
+    <TD {style}>&nbsp;p25={p25:5.2f}, p50={p50:5.2f}, p75={p75:5.2f}, p90={p90:5.2f}, p99={p99:5.2f}</TD>
     </TR>
 ''')
             f.write('</TABLE>')