-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
190 lines (148 loc) · 5.4 KB
/
app.py
File metadata and controls
190 lines (148 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import os
import subprocess
from datetime import datetime
import zoneinfo
import time
from threading import Event
import asyncio
import logging
import sdnotify # Add this import
from flask import Flask, render_template, jsonify, send_from_directory
from flask_socketio import SocketIO, emit
from dotenv import load_dotenv
from myroonapi import MyRoonApi
from art_generator import generate_mondrian
# Load environment variables from .env file
load_dotenv()
my_tz = zoneinfo.ZoneInfo(os.getenv("TZ", "America/New_York"))
myRoonApi = MyRoonApi()
app = Flask(__name__)
socketio = SocketIO(app, cors_allowed_origins="*")
thread = None
thread_stop_event = Event()
name = os.getenv("NAME", "roFrame")
port = int(os.getenv("PORT", 5006))
display_on_hour = int(os.getenv("DISPLAY_ON_HOUR", 9))
display_off_hour = int(os.getenv("DISPLAY_OFF_HOUR", 23))
display_control = os.getenv("DISPLAY_CONTROL", "off")
slideshow_enabled = os.getenv("SLIDESHOW", "on") == "on"
slideshow_folder = os.getenv(
"SLIDESHOW_FOLDER", os.path.join(app.root_path, "./pictures")
)
slideshow_transition_seconds = int(os.getenv("SLIDESHOW_TRANSITION_SECONDS", 15))
slideshow_clock_ratio = int(os.getenv("SLIDESHOW_CLOCK_RATIO", 0)) / 100
clock_size = int(os.getenv("CLOCK_SIZE", 0))
clock_offset = int(os.getenv("CLOCK_OFFSET", 0))
index_file = os.getenv("INDEX_FILE", "index.html")
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def getRoonApi():
global myRoonApi
# if not myRoonApi.is_connected():
# logger.info("Connecting to Roon API")
# myRoonApi.connect(notify_clients=notify_clients)
return myRoonApi
def display(turn_on):
"""Function to control the display"""
state = "on" if turn_on else "off"
if display_control == "on":
subprocess.check_output(["xset", "dpms", "force", state])
logger.info(f"display: {state}")
def is_screen_on(current_hour, on_hour, off_hour):
if on_hour == off_hour:
return False # Device should be off if on and off hours are the same
if on_hour < off_hour:
return on_hour <= current_hour < off_hour # Normal range
else:
return not (off_hour <= current_hour < on_hour) # Overnight case
def background_thread():
"""
A background thread that emits a message every 10 minutes.
"""
while not thread_stop_event.is_set():
myRoonApi = getRoonApi()
album = myRoonApi.get_zone_data()
state = album.get("state")
current_hour = datetime.now().astimezone(my_tz).hour
# Turn off screen during certain hours
if state in ["playing", "loading"] or is_screen_on(
current_hour, display_on_hour, display_off_hour
):
display(True)
else:
display(False)
time.sleep(600)
@app.route("/")
def index():
images = []
art_images = []
if slideshow_enabled:
images = os.listdir(slideshow_folder)
image_extensions = {".png", ".jpg", ".jpeg", ".gif", ".bmp", ".tiff"}
images = [
img
for img in images
if os.path.splitext(img)[1].lower() in image_extensions
]
if not images:
art_images = [generate_mondrian() for _ in range(10)]
return render_template(
index_file,
name=name,
images=images,
art_images=art_images,
transition_seconds=slideshow_transition_seconds,
slideshow_clock_ratio=slideshow_clock_ratio,
clock_size=clock_size,
clock_offset=clock_offset,
)
@app.route("/slideshow/<filename>")
def slideshow_pic(filename):
if filename not in os.listdir(slideshow_folder):
return jsonify({"error": "File not found"}), 404
return send_from_directory(slideshow_folder, filename)
@app.route("/static/<path:filename>")
def static_files(filename):
"""Serve static files from the static directory."""
return send_from_directory(os.path.join(app.root_path, "static"), filename)
@socketio.on("connect")
def handle_connect():
logger.info("Socket client connected")
emit("response", {"data": "Connected"})
# Start the background thread if it’s not running yet
global thread
if thread is None or not thread.is_alive():
thread = socketio.start_background_task(background_thread)
@socketio.on("disconnect")
def handle_disconnect():
logger.info("Client disconnected")
@socketio.on("trigger_album_update")
def trigger_album_update():
logger.info("trigger_album_update")
myRoonApi = getRoonApi()
album = myRoonApi.get_zone_data()
asyncio.run(notify_clients(album)) # Use asyncio.run to await the coroutine
if album["state"] in ["playing", "loading"]:
display(True)
async def notify_clients(message):
logger.info("notify_clients")
logger.info(message)
socketio.emit("album_update", message)
if message.get("state") in ["playing", "loading"]:
display(True)
if __name__ == "__main__":
# start the Roon
if not myRoonApi.check_auth():
logger.error("Please authorise first using discovery.py")
exit()
if not myRoonApi.connect(notify_clients=notify_clients):
logger.error("Unable to connect to Roon")
exit()
# Notify systemd that the service is ready
n = sdnotify.SystemdNotifier()
n.notify("READY=1")
# Start the Flask web server
socketio.run(
app, debug=False, port=port, host="0.0.0.0", allow_unsafe_werkzeug=True
)