-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
287 lines (234 loc) · 10.6 KB
/
main.py
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import logging
import multiprocessing
import os
import time
from datetime import datetime, timedelta
from threading import Thread
import coloredlogs
import cv2
import numpy as np
import requests
from plexapi.server import PlexServer
from config import *
from secrets import PLEX_TOKEN, YOUR_LONG_LIVED_ACCESS_TOKEN
# Cache to store video file connections and last access time
video_cache = {}
session_cache = {}
cache_lock = multiprocessing.Lock()
class LightController:
def __init__(self, ha_url, ha_entity_id):
# Setup logging
logging.basicConfig(level=logging.INFO)
self.logger = logging.getLogger(__name__)
coloredlogs.install(level='INFO', logger=self.logger)
self.ha_url = ha_url
self.ha_entity_id = ha_entity_id
self.last_avg_color = None
self.last_light_color = None
self.last_light_update_time = 0
@staticmethod
def rgb_to_xy(r, g, b):
"""Convert RGB to CIE 1931 XY color space."""
def gamma_correction(channel):
return ((channel + 0.055) / 1.055) ** 2.4 if channel > 0.04045 else channel / 12.92
r, g, b = [gamma_correction(channel / 255.0) for channel in (r, g, b)]
X = r * 0.4124 + g * 0.3576 + b * 0.1805
Y = r * 0.2126 + g * 0.7152 + b * 0.0722
Z = r * 0.0193 + g * 0.1192 + b * 0.9505
return (X / (X + Y + Z), Y / (X + Y + Z)) if X + Y + Z else (0, 0)
def set_light_color(self, color, avg_loop_time_ms):
"""Set the light color via Home Assistant API."""
current_time_ms = time.time() * 1000
xy_color = self.rgb_to_xy(*color)
brightness_pct = int(max(color) / 255.0 * 100)
if self._should_update_light(xy_color):
payload = {
"entity_id": self.ha_entity_id,
"xy_color": list(xy_color),
"brightness_pct": brightness_pct,
"transition": (avg_loop_time_ms / 1000) * 8,
}
self._send_light_update(payload)
self.last_light_color = xy_color
self.last_light_update_time = current_time_ms
def _should_update_light(self, xy_color):
"""Determine if the light color needs to be updated."""
return self.last_light_color is None or xy_color != self.last_light_color
def _send_light_update(self, payload):
"""Send a request to update the light's color."""
headers = {
"Authorization": f"Bearer {YOUR_LONG_LIVED_ACCESS_TOKEN}",
"Content-Type": "application/json",
}
try:
response = requests.post(f"{self.ha_url}/api/services/light/turn_on", json=payload, headers=headers)
response.raise_for_status()
self.logger.info(
f"Set light {self.ha_entity_id} to color {payload['xy_color']} with brightness {payload['brightness_pct']}%"
)
except requests.exceptions.RequestException as e:
self.logger.error(f"Failed to set light color: {e}")
class PlexMonitor:
def __init__(self, plex_server_url, plex_token, ha_url):
# Setup logging
logging.basicConfig(level=logging.INFO)
self.logger = logging.getLogger(__name__)
coloredlogs.install(level='INFO', logger=self.logger)
self.plex = PlexServer(plex_server_url, plex_token)
self.ha_url = ha_url
self.left_light = LightController(ha_url, HA_LEFT_LIGHT)
self.right_light = LightController(ha_url, HA_RIGHT_LIGHT)
self.loop_times = []
self.loop_start_time = time.time()
# self.last_frame_log_time = 0
# self.last_avg_color = None
# self.last_light_color = None
# self.last_light_update_time = 0
def background_cache_cleanup(self):
"""Background process that removes old cache items every hour."""
while True:
time.sleep(3600)
self.clean_old_cache_items()
def clean_old_cache_items(self):
"""Remove video cache items that haven't been accessed in the last 12 hours."""
current_time = datetime.now()
with cache_lock:
self._cleanup_video_cache(current_time)
self._cleanup_session_cache(current_time)
def _cleanup_video_cache(self, current_time):
items_to_remove = [
video_path for video_path, (_, last_access_time) in video_cache.items()
if current_time - last_access_time > timedelta(hours=CACHE_EXPIRY_HOURS)
]
for video_path in items_to_remove:
self.logger.info(f"Removing cached video: {video_path}")
video_cache[video_path][0].release()
del video_cache[video_path]
def _cleanup_session_cache(self, current_time):
items_to_remove = [
guid for guid in session_cache
if current_time - session_cache[guid][1] > timedelta(hours=CACHE_EXPIRY_HOURS)
]
for guid in items_to_remove:
self.logger.info(f"Removing cached session GUID: {guid}")
del session_cache[guid]
def get_average_color(self, video_path, playback_time_ms, avg_loop_time_ms):
"""Retrieve average color for left and right halves."""
cap = self._get_video_capture(video_path)
if cap is None:
return None, None
frame_count = self._calculate_frame_count(cap, avg_loop_time_ms)
cap.set(cv2.CAP_PROP_POS_MSEC, playback_time_ms + avg_loop_time_ms + TIME_TO_SET_LIGHT_MS)
frames = self._retrieve_frames(cap, frame_count)
if len(frames) == 0:
return None, None
left_frames = [frame[:, :frame.shape[1] // 2, :] for frame in frames]
right_frames = [frame[:, frame.shape[1] // 2:, :] for frame in frames]
left_color_avg = np.mean(np.stack(left_frames, axis=0), axis=(0, 1, 2))[::-1].tolist()
right_color_avg = np.mean(np.stack(right_frames, axis=0), axis=(0, 1, 2))[::-1].tolist()
return left_color_avg, right_color_avg
def _get_video_capture(self, video_path):
with cache_lock:
if video_path not in video_cache:
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
return None
video_cache[video_path] = (cap, datetime.now())
else:
cap, _ = video_cache[video_path]
video_cache[video_path] = (cap, datetime.now())
return cap
def _calculate_frame_count(self, cap, avg_loop_time_ms):
fps = cap.get(cv2.CAP_PROP_FPS)
return int((AVERAGE_OVER_MS + avg_loop_time_ms) / 1000 * fps)
def _retrieve_frames(self, cap, frame_count):
frames = []
for i in range(frame_count):
if not cap.grab():
self.logger.warning(f"Failed to grab frame {i + 1}/{frame_count}")
continue
ret, frame = cap.retrieve()
if ret:
frames.append(frame)
else:
self.logger.warning(f"Failed to retrieve frame {i + 1}/{frame_count}")
return frames
def plex_monitor(self):
"""Monitor Plex for currently playing media and call the video-color function."""
avg_loop_time_ms = 0
while True:
loop_start = time.time()
sessions = self._get_sessions()
if sessions:
for session in sessions:
if TV_PLAYER in session.player.title:
video_path = self.get_video_path(session)
if video_path:
if os.name == 'nt':
video_path = f"W:{video_path[7:]}"
left_color, right_color = self.get_average_color(
video_path, session.viewOffset, avg_loop_time_ms
)
if left_color and right_color:
self.left_light.set_light_color(left_color, avg_loop_time_ms)
self.right_light.set_light_color(right_color, avg_loop_time_ms)
avg_loop_time_ms = self.log_loop_duration(loop_start, avg_loop_time_ms)
def _get_sessions(self):
try:
return self.plex.sessions()
except Exception as e:
self.logger.error(f"Error while monitoring Plex: {e}")
time.sleep(30)
return []
def get_video_path(self, session):
"""Get video path from session GUID."""
with cache_lock:
if session.guid in session_cache:
return session_cache[session.guid][0]
video_path = self._lookup_video_path(session)
if video_path:
session_cache[session.guid] = (video_path, datetime.now())
return video_path
def _lookup_video_path(self, session):
try:
return self.plex.library.section("films").getGuid(session.guid).media[0].parts[0].file
except Exception:
return self._lookup_tv_path(session)
def _lookup_tv_path(self, session):
try:
return self.plex.library.section("tv programmes").getGuid(session.grandparentGuid).get(session.title).media[
0].parts[0].file
except Exception as e:
self.logger.warning(f"Failed to get video path from TV programmes: {e}")
return None
def log_loop_duration(self, loop_start, avg_loop_time_ms):
"""Calculate loop duration and log average time every 10 seconds."""
loop_duration = time.time() - loop_start
self.loop_times.append(loop_duration)
if time.time() - self.loop_start_time >= 10:
avg_loop_time_ms = np.mean(self.loop_times) * 1000
self.logger.info(f"Average loop time: {avg_loop_time_ms:.2f} ms")
self.loop_start_time = time.time()
self.loop_times.clear()
return avg_loop_time_ms
if __name__ == '__main__':
plex_monitor_instance = PlexMonitor(
PLEX_SERVER_URL,
PLEX_TOKEN,
HA_URL, # Home Assistant URL
)
# Start the background cache cleanup process
# cleanup_process = multiprocessing.Process(target=plex_monitor_instance.background_cache_cleanup, daemon=True)
cleanup_process = Thread(target=plex_monitor_instance.background_cache_cleanup, daemon=True)
cleanup_process.start()
# Start the Plex monitoring thread
# plex_thread = multiprocessing.Process(target=plex_monitor_instance.plex_monitor, daemon=True)
print("Plex monitor starting...")
plex_monitor_instance.plex_monitor()
# plex_thread = Thread(target=plex_monitor_instance.plex_monitor, daemon=True)
# plex_thread.start()
#
#
# # Keep the main process alive
# while True:
# time.sleep(60)