status_display.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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_is_recording_active(cachefile_content: list) -> bool:
  19. return ongoing_cd_recording_detected(cachefile_content)
  20. def get_cd_marker_count(active_recording: bool, cachefile_content: list) -> int:
  21. if not active_recording:
  22. return 0
  23. return int(cachefile_content[1].strip())
  24. def get_cd_count(active_recording: bool, cachefile_content: list) -> int:
  25. if not active_recording:
  26. return 0
  27. return int(cachefile_content[5].strip())
  28. def get_full_rec_time(active_recording: bool, cachefile_content: list) -> str:
  29. if not active_recording:
  30. return "00:00"
  31. return calc_cuesheet_timestamp(
  32. int(cachefile_content[3].strip()), get_unix_milis()
  33. )[:5]
  34. def get_track_rec_time(active_recording: bool, cachefile_content: list) -> str:
  35. if not active_recording:
  36. return "00:00"
  37. return calc_cuesheet_timestamp(
  38. int(cachefile_content[4].strip()), get_unix_milis()
  39. )[:5]
  40. @cd_recording_status_webserver.route("/")
  41. def index():
  42. cachefile_content = get_cachefile_content(
  43. const.CD_RECORD_CACHEFILE, suppress_error=True
  44. )
  45. recording_active = get_is_recording_active(cachefile_content)
  46. cd_marker_count = get_cd_marker_count(recording_active, cachefile_content)
  47. cd_count = get_cd_count(recording_active, cachefile_content)
  48. cd_rec_time = get_full_rec_time(recording_active, cachefile_content)
  49. track_rec_time = get_track_rec_time(recording_active, cachefile_content)
  50. background_color = "green" if recording_active else "grey"
  51. html = f"""
  52. <!DOCTYPE html>
  53. <html lang="en">
  54. <head>
  55. <meta charset="UTF-8">
  56. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  57. <title>CD Recording Status</title>
  58. <style>
  59. body {{
  60. background-color: {background_color};
  61. color: white;
  62. text-align: center;
  63. font-family: Arial, sans-serif;
  64. display: flex;
  65. justify-content: center;
  66. align-items: center;
  67. height: 100vh;
  68. margin: 0;
  69. overflow: hidden;
  70. }}
  71. .content {{
  72. font-size: 4vw; /* Responsive font size */
  73. text-align: center;
  74. max-width: 90vw; /* Prevents overflow on very large text */
  75. }}
  76. p {{
  77. margin: 0.5em 0;
  78. }}
  79. .timestamp {{
  80. font-family: monospace;
  81. }}
  82. </style>
  83. <script>
  84. setTimeout(function() {{
  85. window.location.reload(1);
  86. }}, 300);
  87. </script>
  88. </head>
  89. <body>
  90. <div class="content">
  91. <h1>CD Recording Status</h1>
  92. <p>Recording is currently {'active' if recording_active else 'not active'}.</p>
  93. <p>{f"CD Count: {cd_count}" if recording_active else '&ZeroWidthSpace;'}</p>
  94. <p>{f"CD Marker Count: {cd_marker_count}" if recording_active else '&ZeroWidthSpace;'}</p>
  95. <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>
  96. </div>
  97. </body>
  98. </html>
  99. """
  100. return render_template_string(html)