status_display.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # Copyright © 2024 Noah Vogt <noah@noahvogt.com>
  2. # This program is free software: you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation, either version 3 of the License, or
  5. # (at your option) any later version.
  6. # This program is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. # You should have received a copy of the GNU General Public License
  11. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. from flask import Flask, render_template_string
  13. import config as const
  14. from input import get_cachefile_content
  15. from utils import get_unix_milis
  16. from .verify import ongoing_cd_recording_detected, calc_cuesheet_timestamp
  17. cd_recording_status_webserver = Flask(__name__)
  18. def get_cd_marker_count(active_recording: bool, cachefile_content: list) -> int:
  19. if not active_recording:
  20. return 0
  21. return int(cachefile_content[1].strip())
  22. def get_cd_count(active_recording: bool, cachefile_content: list) -> int:
  23. if not active_recording:
  24. return 0
  25. return int(cachefile_content[5].strip())
  26. def get_full_rec_time(active_recording: bool, cachefile_content: list) -> str:
  27. if not active_recording:
  28. return "00:00"
  29. return calc_cuesheet_timestamp(
  30. int(cachefile_content[3].strip()), get_unix_milis()
  31. )[:5]
  32. def get_track_rec_time(active_recording: bool, cachefile_content: list) -> str:
  33. if not active_recording:
  34. return "00:00"
  35. return calc_cuesheet_timestamp(
  36. int(cachefile_content[4].strip()), get_unix_milis()
  37. )[:5]
  38. @cd_recording_status_webserver.route("/")
  39. def index():
  40. recording_active = ongoing_cd_recording_detected()
  41. cachefile_content = get_cachefile_content(
  42. const.CD_RECORD_CACHEFILE, suppress_error=True
  43. )
  44. cd_marker_count = get_cd_marker_count(recording_active, cachefile_content)
  45. cd_count = get_cd_count(recording_active, cachefile_content)
  46. cd_rec_time = get_full_rec_time(recording_active, cachefile_content)
  47. track_rec_time = get_track_rec_time(recording_active, cachefile_content)
  48. background_color = "green" if recording_active else "grey"
  49. html = f"""
  50. <!DOCTYPE html>
  51. <html lang="en">
  52. <head>
  53. <meta charset="UTF-8">
  54. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  55. <title>CD Recording Status</title>
  56. <style>
  57. body {{
  58. background-color: {background_color};
  59. color: white;
  60. text-align: center;
  61. font-family: Arial, sans-serif;
  62. display: flex;
  63. justify-content: center;
  64. align-items: center;
  65. height: 100vh;
  66. margin: 0;
  67. overflow: hidden;
  68. }}
  69. .content {{
  70. font-size: 4vw; /* Responsive font size */
  71. text-align: center;
  72. max-width: 90vw; /* Prevents overflow on very large text */
  73. }}
  74. p {{
  75. margin: 0.5em 0;
  76. }}
  77. .timestamp {{
  78. font-family: monospace;
  79. }}
  80. </style>
  81. <script>
  82. setTimeout(function() {{
  83. window.location.reload(1);
  84. }}, 300);
  85. </script>
  86. </head>
  87. <body>
  88. <div class="content">
  89. <h1>CD Recording Status</h1>
  90. <p>Recording is currently {'active' if recording_active else 'not active'}.</p>
  91. <p>{f"CD Count: {cd_count}" if recording_active else '&ZeroWidthSpace;'}</p>
  92. <p>{f"CD Marker Count: {cd_marker_count}" if recording_active else '&ZeroWidthSpace;'}</p>
  93. <p>{f"Recording Time: <span class=timestamp>{cd_rec_time}</span> (Track: <span class=timestamp>{track_rec_time})</span>" if recording_active else '&ZeroWidthSpace;'}</p>
  94. </div>
  95. </body>
  96. </html>
  97. """
  98. return render_template_string(html)