Repository navigation
Expand file tree
/
Copy pathaxis_batch_programmer_gui.py
More file actions
1214 lines (1020 loc) · 50.5 KB
/
Copy pathaxis_batch_programmer_gui.py
File metadata and controls
1214 lines (1020 loc) · 50.5 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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Axis Camera Batch Programmer - GUI Version
Professional interface for batch configuring Axis cameras
"""
import tkinter as tk
from tkinter import ttk, scrolledtext, filedialog, messagebox
import threading
import queue
import csv
import logging
from datetime import datetime
from pathlib import Path
import sys
import requests
from requests.auth import HTTPDigestAuth
from concurrent.futures import ThreadPoolExecutor, as_completed
# Import the core logic from the main program
from axis_batch_programmer import (
discover_cameras_on_network,
configure_camera,
read_camera_config_csv,
update_csv_status
)
class TextHandler(logging.Handler):
"""Custom logging handler that writes to a text widget."""
def __init__(self, text_widget):
super().__init__()
self.text_widget = text_widget
def emit(self, record):
msg = self.format(record)
def append():
self.text_widget.configure(state='normal')
self.text_widget.insert(tk.END, msg + '\n')
self.text_widget.see(tk.END)
self.text_widget.configure(state='disabled')
# Color code by log level
if record.levelname == 'ERROR':
# Get the last line and tag it red
line_start = self.text_widget.index("end-2c linestart")
line_end = self.text_widget.index("end-1c")
self.text_widget.tag_add("error", line_start, line_end)
self.text_widget.tag_config("error", foreground="red")
elif record.levelname == 'WARNING':
line_start = self.text_widget.index("end-2c linestart")
line_end = self.text_widget.index("end-1c")
self.text_widget.tag_add("warning", line_start, line_end)
self.text_widget.tag_config("warning", foreground="orange")
elif record.levelname == 'INFO' and '[OK]' in msg:
line_start = self.text_widget.index("end-2c linestart")
line_end = self.text_widget.index("end-1c")
self.text_widget.tag_add("success", line_start, line_end)
self.text_widget.tag_config("success", foreground="green")
self.text_widget.after(0, append)
class AxisBatchProgrammerGUI:
"""Main GUI application for Axis Camera Batch Programmer."""
def __init__(self, root):
self.root = root
self.root.title("Axis Camera Batch Programmer")
self.root.geometry("1200x800")
# State
self.csv_filename = "camera_config.csv"
self.firmware_file = None
self.configs = []
self.is_running = False
self.status_queue = queue.Queue()
# Setup UI
self.setup_ui()
self.setup_logging()
# Load initial data
self.load_csv()
# Start status update checker
self.check_status_updates()
def setup_ui(self):
"""Create the user interface."""
# Style configuration
style = ttk.Style()
style.theme_use('clam')
# Configure colors
style.configure('Title.TLabel', font=('Arial', 16, 'bold'), foreground='#2c3e50')
style.configure('Header.TLabel', font=('Arial', 10, 'bold'), foreground='#34495e')
style.configure('Status.TLabel', font=('Arial', 9), foreground='#7f8c8d')
# Main container
main_frame = ttk.Frame(self.root, padding="10")
main_frame.grid(row=0, column=0, sticky=(tk.W, tk.E, tk.N, tk.S))
# Configure grid weights
self.root.columnconfigure(0, weight=1)
self.root.rowconfigure(0, weight=1)
main_frame.columnconfigure(0, weight=1)
main_frame.rowconfigure(2, weight=1)
# ===== Header Section =====
header_frame = ttk.Frame(main_frame)
header_frame.grid(row=0, column=0, sticky=(tk.W, tk.E), pady=(0, 10))
title_label = ttk.Label(
header_frame,
text="Axis Camera Batch Programmer",
style='Title.TLabel'
)
title_label.pack(side=tk.LEFT)
# CSV File selection
csv_frame = ttk.Frame(header_frame)
csv_frame.pack(side=tk.RIGHT)
ttk.Label(csv_frame, text="CSV File:", style='Header.TLabel').pack(side=tk.LEFT, padx=(0, 5))
self.csv_label = ttk.Label(
csv_frame,
text=self.csv_filename,
style='Status.TLabel',
relief=tk.SUNKEN,
padding=5
)
self.csv_label.pack(side=tk.LEFT, padx=(0, 5))
ttk.Button(
csv_frame,
text="Browse...",
command=self.browse_csv
).pack(side=tk.LEFT)
# ===== Camera Status Table =====
table_frame = ttk.LabelFrame(main_frame, text="Camera Status", padding="5")
table_frame.grid(row=1, column=0, sticky=(tk.W, tk.E, tk.N, tk.S), pady=(0, 10))
table_frame.rowconfigure(0, weight=1)
table_frame.columnconfigure(0, weight=1)
# Create Treeview with scrollbars
tree_scroll_y = ttk.Scrollbar(table_frame, orient=tk.VERTICAL)
tree_scroll_x = ttk.Scrollbar(table_frame, orient=tk.HORIZONTAL)
self.tree = ttk.Treeview(
table_frame,
columns=('MAC', 'Name', 'Current_IP', 'New_IP', 'Username', 'Status', 'Message'),
show='headings',
yscrollcommand=tree_scroll_y.set,
xscrollcommand=tree_scroll_x.set,
height=10
)
tree_scroll_y.config(command=self.tree.yview)
tree_scroll_x.config(command=self.tree.xview)
# Define columns
self.tree.heading('MAC', text='MAC Address')
self.tree.heading('Name', text='Camera Name')
self.tree.heading('Current_IP', text='Current IP')
self.tree.heading('New_IP', text='New IP')
self.tree.heading('Username', text='Username')
self.tree.heading('Status', text='Status')
self.tree.heading('Message', text='Message')
# Column widths
self.tree.column('MAC', width=150)
self.tree.column('Name', width=150)
self.tree.column('Current_IP', width=120)
self.tree.column('New_IP', width=120)
self.tree.column('Username', width=100)
self.tree.column('Status', width=100)
self.tree.column('Message', width=300)
# Tags for row colors
self.tree.tag_configure('pending', background='#ecf0f1')
self.tree.tag_configure('scanning', background='#3498db', foreground='white')
self.tree.tag_configure('discovering', background='#3498db', foreground='white')
self.tree.tag_configure('found', background='#2ecc71', foreground='white')
self.tree.tag_configure('not_found', background='#95a5a6', foreground='white')
self.tree.tag_configure('configuring', background='#f39c12', foreground='white')
self.tree.tag_configure('completed', background='#27ae60', foreground='white')
self.tree.tag_configure('failed', background='#e74c3c', foreground='white')
# Test mode tags
self.tree.tag_configure('Testing', background='#3498db', foreground='white')
self.tree.tag_configure('Compatible', background='#27ae60', foreground='white')
self.tree.tag_configure('Mostly Compatible', background='#f39c12', foreground='white')
self.tree.tag_configure('Issues Found', background='#e67e22', foreground='white')
# Grid layout
self.tree.grid(row=0, column=0, sticky=(tk.W, tk.E, tk.N, tk.S))
tree_scroll_y.grid(row=0, column=1, sticky=(tk.N, tk.S))
tree_scroll_x.grid(row=1, column=0, sticky=(tk.W, tk.E))
# ===== Log Output =====
log_frame = ttk.LabelFrame(main_frame, text="Log Output", padding="5")
log_frame.grid(row=2, column=0, sticky=(tk.W, tk.E, tk.N, tk.S), pady=(0, 10))
log_frame.rowconfigure(0, weight=1)
log_frame.columnconfigure(0, weight=1)
self.log_text = scrolledtext.ScrolledText(
log_frame,
wrap=tk.WORD,
width=100,
height=20,
font=('Consolas', 9),
background='#2c3e50',
foreground='#ecf0f1',
state='disabled'
)
self.log_text.grid(row=0, column=0, sticky=(tk.W, tk.E, tk.N, tk.S))
# ===== Control Buttons =====
button_frame = ttk.Frame(main_frame)
button_frame.grid(row=3, column=0, sticky=(tk.W, tk.E))
self.start_button = ttk.Button(
button_frame,
text="Start Programming",
command=self.start_programming,
width=20
)
self.start_button.pack(side=tk.LEFT, padx=(0, 5))
self.scan_button = ttk.Button(
button_frame,
text="Scan Only",
command=self.scan_only,
width=15
)
self.scan_button.pack(side=tk.LEFT, padx=(0, 5))
self.test_button = ttk.Button(
button_frame,
text="Test Mode",
command=self.test_mode,
width=15
)
self.test_button.pack(side=tk.LEFT, padx=(0, 5))
self.stop_button = ttk.Button(
button_frame,
text="Stop",
command=self.stop_programming,
state=tk.DISABLED,
width=15
)
self.stop_button.pack(side=tk.LEFT, padx=(0, 5))
ttk.Button(
button_frame,
text="Refresh CSV",
command=self.load_csv,
width=15
).pack(side=tk.LEFT, padx=(0, 5))
ttk.Button(
button_frame,
text="Clear Log",
command=self.clear_log,
width=15
).pack(side=tk.LEFT)
# Firmware upgrade section
firmware_frame = ttk.Frame(button_frame)
firmware_frame.pack(side=tk.LEFT, padx=(20, 0))
self.firmware_var = tk.BooleanVar(value=False)
self.firmware_check = ttk.Checkbutton(
firmware_frame,
text="Upgrade Firmware",
variable=self.firmware_var,
command=self.toggle_firmware
)
self.firmware_check.pack(side=tk.LEFT, padx=(0, 5))
self.firmware_button = ttk.Button(
firmware_frame,
text="Select .bin File...",
command=self.select_firmware,
state=tk.DISABLED,
width=15
)
self.firmware_button.pack(side=tk.LEFT)
self.firmware_label = ttk.Label(
firmware_frame,
text="No firmware selected",
style='Status.TLabel',
foreground='gray'
)
self.firmware_label.pack(side=tk.LEFT, padx=(5, 0))
# Status bar
self.status_bar = ttk.Label(
button_frame,
text="Ready",
style='Status.TLabel',
relief=tk.SUNKEN,
padding=5
)
self.status_bar.pack(side=tk.RIGHT, fill=tk.X, expand=True, padx=(10, 0))
def setup_logging(self):
"""Configure logging to output to the text widget."""
# Get the logger from axis_batch_programmer
logger = logging.getLogger('axis_programmer')
# Create text handler
text_handler = TextHandler(self.log_text)
text_handler.setLevel(logging.DEBUG)
# Format
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s',
datefmt='%H:%M:%S')
text_handler.setFormatter(formatter)
# Add handler
logger.addHandler(text_handler)
logger.setLevel(logging.DEBUG)
def browse_csv(self):
"""Open file dialog to select CSV file."""
filename = filedialog.askopenfilename(
title="Select Camera Configuration CSV",
filetypes=[("CSV files", "*.csv"), ("All files", "*.*")],
initialdir=Path.cwd()
)
if filename:
self.csv_filename = filename
self.csv_label.config(text=Path(filename).name)
self.load_csv()
def load_csv(self):
"""Load camera configurations from CSV and populate table."""
# Load all cameras (including completed ones) for GUI display
self.configs = read_camera_config_csv(self.csv_filename, skip_completed=False)
if not self.configs:
messagebox.showwarning(
"No Cameras",
f"No camera configurations found in {self.csv_filename}"
)
return
# Clear existing items
for item in self.tree.get_children():
self.tree.delete(item)
# Add cameras to tree
for cfg in self.configs:
# Use existing status from CSV if present
existing_status = cfg.get('status', '')
if existing_status == 'Completed':
status = 'Completed'
message = 'Previously configured'
tag = 'completed'
else:
status = 'Pending'
message = 'Waiting to start...'
tag = 'pending'
self.tree.insert(
'',
tk.END,
iid=cfg['mac'],
values=(
cfg['mac'],
cfg.get('name', 'N/A'),
'Unknown', # Current IP (will be populated during discovery)
cfg['new_ip'],
cfg['username'],
status,
message
),
tags=(tag,)
)
self.update_status_bar(f"Loaded {len(self.configs)} camera(s) from CSV")
def update_camera_status(self, mac, current_ip=None, status=None, message=None):
"""Update a camera's status in the tree."""
if not self.tree.exists(mac):
return
# Get current values
values = list(self.tree.item(mac)['values'])
# Update values
if current_ip:
values[2] = current_ip # Current_IP column
if status:
values[5] = status # Status column
if message:
values[6] = message # Message column
# Determine tag based on status
tag = 'pending'
if status:
status_lower = status.lower()
if 'scan' in status_lower:
tag = 'scanning'
elif 'discover' in status_lower:
tag = 'discovering'
elif 'found' == status_lower:
tag = 'found'
elif 'not found' in status_lower or 'not_found' in status_lower:
tag = 'not_found'
elif 'configur' in status_lower:
tag = 'configuring'
elif 'completed' in status_lower or 'success' in status_lower:
tag = 'completed'
elif 'failed' in status_lower or 'error' in status_lower:
tag = 'failed'
# Update tree item
self.tree.item(mac, values=values, tags=(tag,))
def update_status_bar(self, message):
"""Update the status bar text."""
self.status_bar.config(text=message)
def clear_log(self):
"""Clear the log output."""
self.log_text.configure(state='normal')
self.log_text.delete(1.0, tk.END)
self.log_text.configure(state='disabled')
def toggle_firmware(self):
"""Enable/disable firmware file selection based on checkbox."""
if self.firmware_var.get():
self.firmware_button.config(state=tk.NORMAL)
else:
self.firmware_button.config(state=tk.DISABLED)
self.firmware_file = None
self.firmware_label.config(text="No firmware selected", foreground='gray')
def select_firmware(self):
"""Open file dialog to select firmware .bin file."""
filename = filedialog.askopenfilename(
title="Select Firmware Binary File",
filetypes=[(".bin files", "*.bin"), ("All files", "*.*")],
initialdir=Path.cwd()
)
if filename:
self.firmware_file = filename
self.firmware_label.config(
text=Path(filename).name,
foreground='green'
)
logging.getLogger('axis_programmer').info(f"Firmware file selected: {Path(filename).name}")
def start_programming(self):
"""Start the batch programming process in a background thread."""
if self.is_running:
return
if not self.configs:
messagebox.showwarning("No Cameras", "Please load a CSV file with camera configurations.")
return
# Count pending vs completed
completed_count = sum(1 for cfg in self.configs if cfg.get('status') == 'Completed')
pending_count = len(self.configs) - completed_count
# Confirm start
message = f"Start programming {len(self.configs)} camera(s)?\n\n"
if completed_count > 0:
message += f"• {pending_count} camera(s) pending\n"
message += f"• {completed_count} camera(s) already completed\n\n"
message += "Note: Already-completed cameras will be reprogrammed if you proceed.\n\n"
message += "This will discover cameras on the network and configure them according to the CSV file."
result = messagebox.askyesno("Start Programming", message)
if not result:
return
# Update UI state
self.is_running = True
self.start_button.config(state=tk.DISABLED)
self.scan_button.config(state=tk.DISABLED)
self.test_button.config(state=tk.DISABLED)
self.stop_button.config(state=tk.NORMAL)
self.firmware_check.config(state=tk.DISABLED)
self.firmware_button.config(state=tk.DISABLED)
self.update_status_bar("Starting batch programming...")
# Start worker thread
thread = threading.Thread(target=self.programming_worker, daemon=True)
thread.start()
def stop_programming(self):
"""Stop the batch programming process."""
self.is_running = False
self.start_button.config(state=tk.NORMAL)
self.scan_button.config(state=tk.NORMAL)
self.test_button.config(state=tk.NORMAL)
self.stop_button.config(state=tk.DISABLED)
self.update_status_bar("Stopping...")
def scan_only(self):
"""Scan for cameras without programming them."""
if self.is_running:
return
if not self.configs:
messagebox.showwarning("No Cameras", "Please load a CSV file with camera configurations.")
return
# Confirm scan
result = messagebox.askyesno(
"Scan for Cameras",
f"Scan network for {len(self.configs)} camera(s)?\n\n"
"This will discover cameras and update their Current IP addresses "
"without programming them."
)
if not result:
return
# Update UI state
self.is_running = True
self.start_button.config(state=tk.DISABLED)
self.scan_button.config(state=tk.DISABLED)
self.test_button.config(state=tk.DISABLED)
self.stop_button.config(state=tk.NORMAL)
self.firmware_check.config(state=tk.DISABLED)
self.firmware_button.config(state=tk.DISABLED)
self.update_status_bar("Scanning for cameras...")
# Start scan worker thread
thread = threading.Thread(target=self.scan_worker, daemon=True)
thread.start()
def programming_worker(self):
"""Worker thread that runs the batch programming logic."""
try:
logger = logging.getLogger('axis_programmer')
logger.info("="*70)
logger.info("Starting Axis Camera Batch Programming")
logger.info("="*70)
# Update all cameras to "Discovering" status
for cfg in self.configs:
self.status_queue.put({
'mac': cfg['mac'],
'status': 'Discovering',
'message': 'Searching for camera on network...'
})
self.status_queue.put({'status_bar': 'Discovering cameras...'})
# Step 1: Discover cameras
logger.info(f"\nDiscovering {len(self.configs)} camera(s) on network...")
discovered = discover_cameras_on_network(self.configs)
if not discovered:
logger.error("No cameras discovered!")
self.status_queue.put({'status_bar': 'Discovery failed - no cameras found'})
for cfg in self.configs:
self.status_queue.put({
'mac': cfg['mac'],
'status': 'Failed',
'message': 'Camera not found on network'
})
return
logger.info(f"Discovered {len(discovered)} camera(s)")
# Update discovered cameras with current IP
discovered_macs = {cam['mac']: cam['ip'] for cam in discovered}
for cfg in self.configs:
if cfg['mac'] in discovered_macs:
self.status_queue.put({
'mac': cfg['mac'],
'current_ip': discovered_macs[cfg['mac']],
'status': 'Ready',
'message': 'Camera found, ready to configure'
})
else:
self.status_queue.put({
'mac': cfg['mac'],
'status': 'Failed',
'message': 'Camera not discovered'
})
# Step 2: Firmware upgrade FIRST (if enabled) - All cameras in parallel
firmware_upgraded_count = 0
firmware_failed_count = 0
firmware_results = [] # Track firmware upgrade results
if self.firmware_var.get() and self.firmware_file:
logger.info("\n" + "="*70)
logger.info(f"STEP 2: FIRMWARE UPGRADE (Parallel - All {len(discovered)} cameras at once)")
logger.info("="*70)
self.status_queue.put({
'status_bar': f'Upgrading firmware on {len(discovered)} cameras in parallel...'
})
# Mark all cameras as upgrading
for cam_info in discovered:
self.status_queue.put({
'mac': cam_info['mac'],
'status': 'Upgrading',
'message': 'Starting firmware upgrade...'
})
# Function to upgrade one camera (for parallel execution)
def upgrade_one_camera(cam_info):
mac = cam_info['mac']
camera = cam_info['camera']
try:
logger.info(f"\n{'='*70}")
logger.info(f"Firmware Upgrade: {mac} at {camera.ip}")
logger.info(f"{'='*70}")
# Progress callback for this specific camera
def firmware_progress(stage, message):
self.status_queue.put({
'mac': mac,
'message': f'Firmware: {message}'
})
# Perform firmware upgrade
success = camera.upgrade_firmware(
self.firmware_file,
progress_callback=firmware_progress
)
if success:
logger.info(f"✓ Firmware upgrade successful for {mac}")
self.status_queue.put({
'mac': mac,
'message': 'Firmware upgraded successfully'
})
return {'mac': mac, 'success': True, 'camera': camera}
else:
logger.error(f"✗ Firmware upgrade failed for {mac}")
self.status_queue.put({
'mac': mac,
'status': 'Failed',
'message': 'Firmware upgrade failed - see log'
})
return {'mac': mac, 'success': False, 'camera': camera}
except Exception as e:
logger.error(f"✗ Firmware upgrade error for {mac}: {e}", exc_info=True)
self.status_queue.put({
'mac': mac,
'status': 'Failed',
'message': f'Firmware error: {str(e)[:50]}'
})
return {'mac': mac, 'success': False, 'camera': camera, 'error': str(e)}
# Execute firmware upgrades in parallel
firmware_results = []
with ThreadPoolExecutor(max_workers=min(len(discovered), 10)) as executor:
# Submit all firmware upgrade jobs
future_to_cam = {executor.submit(upgrade_one_camera, cam_info): cam_info for cam_info in discovered}
# Wait for all to complete
for future in as_completed(future_to_cam):
if not self.is_running:
logger.warning("Programming stopped by user")
executor.shutdown(wait=False, cancel_futures=True)
break
result = future.result()
firmware_results.append(result)
if result['success']:
firmware_upgraded_count += 1
else:
firmware_failed_count += 1
# Update progress
total_done = firmware_upgraded_count + firmware_failed_count
self.status_queue.put({
'status_bar': f'Firmware: {total_done}/{len(discovered)} complete ({firmware_upgraded_count} success, {firmware_failed_count} failed)'
})
logger.info(f"\n{'='*70}")
logger.info(f"Firmware Upgrade Complete: {firmware_upgraded_count} success, {firmware_failed_count} failed")
logger.info(f"{'='*70}\n")
# Update discovered list with new camera states after firmware
# (cameras may have changed IPs during firmware upgrade)
for result in firmware_results:
if result['success']:
# Update the camera object in discovered list
for cam_info in discovered:
if cam_info['mac'] == result['mac']:
cam_info['camera'] = result['camera']
cam_info['ip'] = result['camera'].ip
break
else:
logger.info("\n" + "="*70)
logger.info("STEP 2: FIRMWARE UPGRADE - Skipped (not enabled)")
logger.info("="*70 + "\n")
# Step 3: Configure each discovered camera
logger.info("\n" + "="*70)
logger.info(f"STEP 3: CONFIGURATION (Setting credentials, timezone, name, resolution, IP)")
logger.info("="*70)
success_count = 0
failed_count = 0
for cam_info in discovered:
if not self.is_running:
logger.warning("Programming stopped by user")
break
mac = cam_info['mac']
# Find matching config
config = next((cfg for cfg in self.configs if cfg['mac'] == mac), None)
if not config:
logger.warning(f"No configuration found for discovered camera {mac}")
continue
# Skip if firmware upgrade failed for this camera
if self.firmware_var.get() and self.firmware_file:
firmware_result = next((r for r in firmware_results if r['mac'] == mac), None)
if firmware_result and not firmware_result['success']:
logger.warning(f"Skipping configuration for {mac} - firmware upgrade failed")
failed_count += 1
continue
# Update status to configuring
self.status_queue.put({
'mac': mac,
'status': 'Configuring',
'message': 'Applying configuration...'
})
self.status_queue.put({
'status_bar': f'Configuring {mac}... ({success_count + failed_count + 1}/{len(discovered)})'
})
# Configure the camera (pass the AxisCamera object, not the dict)
result = configure_camera(cam_info['camera'], config, self.csv_filename)
if result:
success_count += 1
self.status_queue.put({
'mac': mac,
'status': 'Completed',
'message': 'Successfully configured' + (' and upgraded' if self.firmware_var.get() and self.firmware_file else '')
})
else:
failed_count += 1
self.status_queue.put({
'mac': mac,
'status': 'Failed',
'message': 'Configuration failed - see log'
})
# Step 4: Final verification pass - check and retry any cameras with wrong IP
if success_count > 0:
logger.info("\n" + "="*70)
logger.info("STEP 4: FINAL VERIFICATION - Checking IP addresses")
logger.info("="*70)
self.status_queue.put({
'status_bar': 'Final verification - checking camera IP addresses...'
})
retry_count = 0
for cam_info in discovered:
if not self.is_running:
break
mac = cam_info['mac']
config = next((cfg for cfg in self.configs if cfg['mac'] == mac), None)
if not config or not config.get('new_ip'):
continue
expected_ip = config['new_ip']
camera = cam_info.get('camera')
if not camera:
continue
# Check if camera is reachable at expected IP
logger.info(f"Verifying {mac} at expected IP {expected_ip}...")
logger.debug(f"Using credentials: {config['username']}/***")
try:
test_session = requests.Session()
test_session.auth = HTTPDigestAuth(config['username'], config['password'])
test_session.trust_env = False
test_session.proxies = {'http': None, 'https': None}
test_url = f"http://{expected_ip}/axis-cgi/param.cgi?action=list&group=Network.eth0"
test_response = test_session.get(test_url, timeout=10)
if test_response.status_code == 200:
logger.info(f"✓ Camera {mac} confirmed at {expected_ip}")
continue
else:
logger.warning(f"⚠ Camera {mac} responded at {expected_ip} with status {test_response.status_code}")
# Still accessible, probably fine
continue
except Exception as verify_err:
logger.warning(f"✗ Cannot reach camera {mac} at expected IP {expected_ip}")
logger.info(f"Searching for camera {mac}...")
# Update status
self.status_queue.put({
'mac': mac,
'status': 'Retrying',
'message': 'IP verification failed, searching...'
})
# Search for camera at potential locations
found_ip = None
search_ips = [
'192.168.0.90', # Axis fallback IP
cam_info.get('ip'), # Original discovery IP
]
# Add ARP scan for this specific MAC
try:
import subprocess
result = subprocess.run(['arp', '-a'], capture_output=True, text=True, timeout=5)
normalized_mac = mac.replace(':', '-').lower()
for line in result.stdout.split('\n'):
if normalized_mac in line.lower():
parts = line.split()
if len(parts) >= 1:
potential_ip = parts[0].strip()
if potential_ip not in search_ips:
search_ips.append(potential_ip)
logger.info(f"Found {mac} in ARP table at {potential_ip}")
except:
pass
# Try each potential IP
for search_ip in search_ips:
if not search_ip:
continue
try:
logger.info(f"Trying {mac} at {search_ip}...")
test_url = f"http://{search_ip}/axis-cgi/param.cgi?action=list&group=Network.eth0"
test_response = test_session.get(test_url, timeout=10)
if test_response.status_code == 200:
found_ip = search_ip
logger.info(f"✓ Found camera {mac} at {found_ip}")
break
except:
continue
if found_ip and found_ip != expected_ip:
# Camera found at wrong IP - retry network configuration
logger.info(f"Retrying network configuration for {mac}: {found_ip} → {expected_ip}")
self.status_queue.put({
'mac': mac,
'message': f'Found at {found_ip}, reconfiguring to {expected_ip}...'
})
try:
# Update camera object with found IP and ensure credentials are set
camera.ip = found_ip
camera.username = config['username']
camera.password = config['password']
camera.session.auth = HTTPDigestAuth(config['username'], config['password'])
logger.info(f"Retrying with credentials: {config['username']}/***")
# Retry network configuration
net_result = camera.set_network_config(
expected_ip,
config.get('subnet_mask', '255.255.255.0'),
config.get('gateway')
)
if net_result:
logger.info(f"✓ Successfully reconfigured {mac} to {expected_ip}")
retry_count += 1
self.status_queue.put({
'mac': mac,
'current_ip': expected_ip,
'message': f'Reconfigured successfully: {found_ip} → {expected_ip}'
})
else:
logger.error(f"✗ Failed to reconfigure {mac}")
self.status_queue.put({
'mac': mac,
'current_ip': found_ip,
'message': f'Network reconfig failed - camera at {found_ip}'
})
except Exception as retry_err:
logger.error(f"✗ Error reconfiguring {mac}: {retry_err}")
self.status_queue.put({
'mac': mac,
'current_ip': found_ip,
'message': f'Reconfig error - camera at {found_ip}'
})
elif found_ip:
logger.info(f"Camera {mac} already at correct IP {found_ip}")
else:
logger.error(f"✗ Could not locate camera {mac}")
self.status_queue.put({
'mac': mac,
'message': 'Camera not found - check network connection'
})
if retry_count > 0:
logger.info(f"\nFinal verification: Fixed {retry_count} camera(s) with incorrect IP")
# Final summary
logger.info("\n" + "="*70)
logger.info("Batch Programming Complete")
logger.info(f" Success: {success_count}")
logger.info(f" Failed: {failed_count}")
logger.info(f" Total: {success_count + failed_count}")
logger.info("="*70)
self.status_queue.put({
'status_bar': f'Complete - Success: {success_count}, Failed: {failed_count}'
})
except Exception as e:
logger.error(f"Programming worker error: {e}", exc_info=True)
self.status_queue.put({'status_bar': f'Error: {e}'})
finally:
# Re-enable start button
def reset_ui():
self.is_running = False
self.start_button.config(state=tk.NORMAL)
self.scan_button.config(state=tk.NORMAL)
self.test_button.config(state=tk.NORMAL)
self.stop_button.config(state=tk.DISABLED)
self.firmware_check.config(state=tk.NORMAL)
if self.firmware_var.get():
self.firmware_button.config(state=tk.NORMAL)
self.root.after(0, reset_ui)
def scan_worker(self):
"""Worker thread that scans for cameras without programming."""
try:
logger = logging.getLogger('axis_programmer')
logger.info("="*70)
logger.info("Scanning for Cameras (Discovery Only)")
logger.info("="*70)
# Update all cameras to "Scanning" status
for cfg in self.configs:
self.status_queue.put({
'mac': cfg['mac'],
'status': 'Scanning',
'message': 'Searching for camera on network...'
})
self.status_queue.put({'status_bar': 'Discovering cameras...'})
# Discover cameras
logger.info(f"\nScanning for {len(self.configs)} camera(s) on network...")
discovered = discover_cameras_on_network(self.configs)
if not discovered:
logger.warning("No cameras discovered!")
self.status_queue.put({'status_bar': 'Scan complete - no cameras found'})
for cfg in self.configs:
self.status_queue.put({
'mac': cfg['mac'],
'current_ip': 'Not Found',
'status': 'Not Found',
'message': 'Camera not found on network'
})
else:
logger.info(f"\nScan complete! Discovered {len(discovered)} camera(s)")
# Create lookup of discovered cameras
discovered_macs = {cam['mac']: cam for cam in discovered}
# Update all cameras with scan results