Skip to content

Commit 296fc64

Browse files
threaded status checking and envanced clarity of online/offline/testing cameras
1 parent 3590b76 commit 296fc64

1 file changed

Lines changed: 32 additions & 18 deletions

File tree

src/gui/maingui.py

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ def __init__(self, root):
5959
self.status_checker_active = True
6060
self.is_shutting_down = False
6161

62-
# Initialize thread pool for camera checks
63-
self.thread_pool = ThreadPoolExecutor(max_workers=4)
62+
# Initialize thread pool for camera checks with more workers for faster checking
63+
self.thread_pool = ThreadPoolExecutor(max_workers=8)
6464

6565
# Initialize status checker thread
6666
self.status_checker_thread = None
@@ -232,7 +232,7 @@ def status_checker():
232232
break
233233
# Submit camera check to thread pool
234234
if not self.thread_pool._shutdown and not self.is_shutting_down:
235-
self.thread_pool.submit(self.check_camera_status, ip, ip)
235+
self.thread_pool.submit(self.check_camera_status_threaded, ip, ip)
236236

237237
# Wait before next check
238238
for _ in range(300): # Check every 5 minutes, but check status every second
@@ -249,10 +249,14 @@ def status_checker():
249249
self.status_checker_thread = threading.Thread(target=status_checker, daemon=True)
250250
self.status_checker_thread.start()
251251

252-
def check_camera_status(self, item_id, ip):
253-
"""Check if camera is accessible and update status"""
252+
def check_camera_status_threaded(self, item_id, ip, delay=0):
253+
"""Check if camera is accessible and update status with threading"""
254254
if self.is_shutting_down or not hasattr(self, 'thread_pool') or self.thread_pool._shutdown:
255255
return
256+
257+
# Add small delay to prevent overwhelming the system
258+
if delay > 0:
259+
time.sleep(delay)
256260

257261
try:
258262
from backend.cameradown import capture_single_frame, default_stream_params
@@ -271,13 +275,13 @@ def check_camera_status(self, item_id, ip):
271275
camera_url = f"{camera_url}{params}"
272276
break
273277

274-
# Try to capture a frame
278+
# Try to capture a frame with timeout
275279
frame = capture_single_frame(camera_url)
276280

277281
if frame is not None:
278282
height, width = frame.shape[:2]
279283
resolution = f"{width}x{height}"
280-
status = "Online"
284+
status = "ONLINE"
281285

282286
# Get location information
283287
try:
@@ -308,6 +312,10 @@ def check_camera_status(self, item_id, ip):
308312
self.update_camera_status(ip=ip, status="Error")
309313
if not self.is_shutting_down:
310314
self.tree.after(0, lambda: self.update_tree_item(item_id, ip, "Error"))
315+
316+
def check_camera_status(self, item_id, ip):
317+
"""Legacy method for backward compatibility"""
318+
self.check_camera_status_threaded(item_id, ip)
311319

312320
def setup_gui(self):
313321
# Create menu bar
@@ -823,7 +831,7 @@ def on_tab_change(self, event):
823831
fill='white', font=('Arial', 10), anchor='center')
824832

825833
def load_ip_addresses(self):
826-
"""Load IP addressses from file and populate the treeview"""
834+
"""Load IP addresses from file and populate the treeview with threaded status checking"""
827835
try:
828836
# Clear existing items
829837
for item in self.tree.get_children():
@@ -835,19 +843,22 @@ def load_ip_addresses(self):
835843
# Populate treeview with IP addresses
836844
for i, ip in enumerate(ip_list, 1):
837845
item_id = self.tree.insert('', 'end', text=str(i),
838-
values=(ip, "Unknown"))
846+
values=(ip, "Checking..."))
847+
848+
# Set initial checking status
849+
self.tree.item(item_id, tags=('checking',))
839850

840851
# Initialize camera data
841852
self.camera_data[item_id] = {
842853
'ip': ip,
843-
'status': 'Unknown',
854+
'status': 'Checking...',
844855
'location': None,
845856
'resolution': None,
846857
'last_check': None
847858
}
848859

849-
# Submit camera check to thread pool
850-
self.thread_pool.submit(self.check_camera_status, item_id, ip)
860+
# Submit camera check to thread pool with delay to prevent overwhelming
861+
self.thread_pool.submit(self.check_camera_status_threaded, item_id, ip, i * 0.1)
851862

852863
except Exception as e:
853864
print(f"Error loading IP addresses: {e}")
@@ -868,8 +879,10 @@ def update_tree_item(self, item_id, ip, status):
868879
self.tree.item(item_id, tags=('online',))
869880
# Move online items to top
870881
self.tree.move(item_id, '', 0)
871-
elif status == "offline":
882+
elif status == "Offline":
872883
self.tree.item(item_id, tags=('offline',))
884+
elif status == "Checking...":
885+
self.tree.item(item_id, tags=('checking',))
873886
elif status == "Error":
874887
self.tree.item(item_id, tags=('error',))
875888
else:
@@ -1460,11 +1473,12 @@ def create_list_view(self):
14601473
self.tree.column('Name', width=200, minwidth=150)
14611474
self.tree.column('Status', width=120, minwidth=80)
14621475

1463-
# Configure status colors
1464-
self.tree.tag_configure('online', background='#1a472a', foreground='#ffffff')
1465-
self.tree.tag_configure('offline', background='#4a1a1a', foreground='#ffffff')
1466-
self.tree.tag_configure('error', background='#4a3a1a', foreground='#ffffff')
1467-
self.tree.tag_configure('unknown', background='#2b2b2b', foreground='#ffffff')
1476+
# Configure status colors with better visibility
1477+
self.tree.tag_configure('online', background='#2b2b2b', foreground='#00ff00') # Green text
1478+
self.tree.tag_configure('offline', background='#2b2b2b', foreground='#ff0000') # Red text
1479+
self.tree.tag_configure('checking', background='#2b2b2b', foreground='#ffff00') # Yellow text
1480+
self.tree.tag_configure('error', background='#2b2b2b', foreground='#ff6600') # Orange text
1481+
self.tree.tag_configure('unknown', background='#2b2b2b', foreground='#ffffff') # White text
14681482

14691483
# Initialize camera data storage
14701484
self.camera_data = {}

0 commit comments

Comments
 (0)