-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.py
964 lines (783 loc) · 35.4 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
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
import zipfile
import cv2
import time
import csv
import threading
from pyzbar.pyzbar import decode
from datetime import datetime, timedelta
from flask import Flask, render_template, redirect, url_for, request, flash, jsonify, Response, send_file
import json
import os
import pygame
import pandas as pd
import plotly
import plotly.express as px
pygame.mixer.init()
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 320)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 240)
# Define the path for persistent data storage
PERSISTENT_FILE = 'battery_status.json'
stop_flag = threading.Event() # Create an Event object to signal threads to stop
app = Flask(__name__)
app.secret_key = '1234567890'
# Define team number default
TEAM_NUMBER = "5987"
# Create a lock for thread safety
battery_status_lock = threading.Lock()
COOLDOWN_DURATION_TIME = 600 # seconds
ADVANCED_LOGGING = True # Default is on
# Battery status tracking dictionary
battery_status = {}
# List to keep track of pending batteries that are scanned but not in the system
pending_batteries = []
SETTINGS_FILE = 'settings.json'
LOGS_FILE = 'battery_log.csv'
STATUS_FILE = 'battery_status.json'
EXPORT_FOLDER = 'exports' # Folder to temporarily store export files
# Initialize the CSV file and write headers if it doesn’t exist
def initialize_csv():
if not os.path.exists('battery_log.csv'):
with open('battery_log.csv', mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerow([
'Timestamp',
'Battery Code',
'Team Number',
'Purchase Year',
'Battery Number',
'Status',
'Current Usage (J)',
'Battery Feel',
'Charged mAh'
])
# Parse battery code
def parse_battery_code(barcode_data):
team_number = barcode_data[:4]
purchase_year = barcode_data[4:8]
battery_number = barcode_data[8:12]
return {
"team_number": team_number,
"purchase_year": purchase_year,
"battery_number": battery_number
}
# Log scan data to CSV
def log_to_csv(barcode_data, battery_info, status):
initialize_csv() # Ensure CSV is initialized
with open('battery_log.csv', mode='a', newline='') as file:
writer = csv.writer(file)
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
writer.writerow([
timestamp,
barcode_data,
battery_info.get('team_number', ''),
battery_info.get('purchase_year', ''),
battery_info.get('battery_number', ''),
status,
battery_info.get('current_usage', ''),
battery_info.get('battery_feel', ''),
battery_info.get('charged_mAh', '')
])
# Update battery status with timestamp
def update_battery_status(barcode_data, new_status):
battery_status[barcode_data]['status'] = new_status
battery_status[barcode_data]['display_time'] = timedelta(0)
battery_status[barcode_data]['last_change'] = datetime.now()
battery_status[barcode_data]['notes'] = battery_status[barcode_data].get('notes', '')
battery_status[barcode_data]['usage_count'] = battery_status[barcode_data].get('usage_count', 0)
if battery_status[barcode_data]['status'] == "In Use":
battery_status[barcode_data]['usage_count'] += 1
# Set the awaiting_advanced_input flag based on the new status
if ADVANCED_LOGGING and new_status in ["In Use", "Charging"]:
battery_status[barcode_data]['awaiting_advanced_input'] = True
else:
# Reset the flag if the status is not "In Use" or "Charging"
battery_status[barcode_data]['awaiting_advanced_input'] = False
def calculate_average_usage():
with battery_status_lock:
total_usage = sum(battery['usage_count'] for battery in battery_status.values())
battery_count = len(battery_status)
if battery_count == 0:
return 0
average_usage = total_usage / battery_count
return average_usage
def identify_usage_outliers():
average_usage = calculate_average_usage()
overused_batteries = []
underused_batteries = []
with battery_status_lock:
for code, data in battery_status.items():
usage_count = data['usage_count']
if usage_count >= average_usage + 2:
overused_batteries.append(code)
elif usage_count <= average_usage - 2:
underused_batteries.append(code)
return overused_batteries, underused_batteries
def can_change_status(barcode_data, new_status):
with battery_status_lock:
if barcode_data in battery_status:
last_status = battery_status[barcode_data]['status']
last_change = battery_status[barcode_data]['last_change']
# Logic to enforce allowed transitions
valid_transitions = {
"Charging": ["Cooldown To Robot"],
"Cooldown To Robot": ["Ready for ROBOT"],
"Ready for ROBOT": ["In Use"],
"In Use": ["Cooldown To Charge"],
"Cooldown To Charge": ["Ready for CHARGING"],
"Ready for CHARGING": ["Charging"]
}
if new_status in valid_transitions.get(last_status, []):
return True
else:
# Allow initialization to "Charging"
if new_status == "Charging":
return True
return False
def scan_barcode():
global cap
print("Starting barcode scanning...")
scanned_barcodes = {}
cooldown_time = 2
retry_count = 0
max_retries = 5 # Number of consecutive retries before reinitializing the camera
while True:
try:
ret, frame = cap.read()
if not ret:
retry_count += 1
print(f"Warning: Can't grab frame (attempt {retry_count}). Retrying...")
time.sleep(0.1) # Small delay before retrying
# Reinitialize the camera if it fails too many times consecutively
if retry_count >= max_retries:
cap.release()
time.sleep(1) # Small delay before reinitializing
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 320)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 240)
retry_count = 0 # Reset retry counter
continue
# Reset retry count if frame read is successful
retry_count = 0
# Process barcodes if frame capture succeeded
barcodes = decode(frame)
for barcode in barcodes:
barcode_data = barcode.data.decode('utf-8')[:-1] # Discard last digit
try:
battery_info = parse_battery_code(barcode_data)
except Exception as e:
print(f"Invalid barcode format: {barcode_data}")
continue
with battery_status_lock:
if barcode_data not in battery_status:
if barcode_data not in pending_batteries:
pending_batteries.append(barcode_data)
continue
if barcode_data not in scanned_barcodes or time.time() - scanned_barcodes[barcode_data] > cooldown_time:
scanned_barcodes[barcode_data] = time.time()
print(f"Scanned Barcode: {barcode_data}")
with battery_status_lock:
current_status = battery_status.get(barcode_data, {}).get('status', 'Charging')
new_status = get_next_status(barcode_data, current_status)
if new_status:
log_to_csv(barcode_data, battery_info, new_status)
update_battery_status(barcode_data, new_status)
pygame.mixer.music.load("beep.wav")
pygame.mixer.music.play()
else:
print(f"Battery {barcode_data} cannot change status yet.")
except:
print("ERROR HAS BEEN IN SCAN BARCODE")
cap.release()
# Background thread to auto-update cooldown statuses
def auto_update_cooldown_statuses():
while True:
with battery_status_lock:
for barcode_data, data in battery_status.items():
status = data['status']
last_change = data['last_change']
current_time = datetime.now()
if status in ["Cooldown To Robot", "Cooldown To Charge"]:
# Calculate remaining cooldown time as a countdown timer
elapsed_time = current_time - last_change
display_time = max(timedelta(seconds=COOLDOWN_DURATION_TIME) - elapsed_time, timedelta(0))
hours = int(display_time.total_seconds() // 3600)
minutes = int((display_time.total_seconds() % 3600) // 60)
seconds = int(display_time.total_seconds() % 60)
battery_status[barcode_data]['display_time'] = f"{hours}:{minutes:02}:{seconds:02}"
# If countdown reaches zero, change status to ready
if display_time == timedelta(0):
new_status = "Ready for ROBOT" if status == "Cooldown To Robot" else "Ready for CHARGING"
update_battery_status(barcode_data, new_status)
else:
# Show elapsed time as a timer going up
elapsed_time = current_time - last_change
hours = int(elapsed_time.total_seconds() // 3600)
minutes = int((elapsed_time.total_seconds() % 3600) // 60)
seconds = int(elapsed_time.total_seconds() % 60)
battery_status[barcode_data]['display_time'] = f"{hours}:{minutes:02}:{seconds:02}"
time.sleep(1) # Check every second for countdown accuracy
def format_battery_code(code):
# Format battery code as TEAM-YEAR-NUMB
return f"{code[:4]}-{code[4:8]}"
def get_next_status(barcode_data, current_status):
# Logic to enforce allowed transitions
valid_transitions = {
"Charging": "Cooldown To Robot",
"Cooldown To Robot": "Ready for ROBOT",
"Ready for ROBOT": "In Use",
"In Use": "Cooldown To Charge",
"Cooldown To Charge": "Ready for CHARGING",
"Ready for CHARGING": "Charging"
}
next_status = valid_transitions.get(current_status)
# Implement any cooldown checks or additional logic here if necessary
# For simplicity, we'll assume the transition is allowed
return next_status
# Flask route to display battery statuses
@app.route('/')
def index():
average_usage = calculate_average_usage()
overused_batteries, underused_batteries = identify_usage_outliers()
with battery_status_lock:
battery_info = [
{
'battery_code': code,
'status': data['status'],
'display_time': str(data.get('display_time', '00:00:00')),
'last_change': data['last_change'].strftime("%Y-%m-%d %H:%M:%S"),
'usage_count': data.get('usage_count', 0),
'notes': data.get('notes', '')
}
for code, data in battery_status.items()
]
# Display warnings
if overused_batteries:
overused_list = ', '.join(format_battery_code(code) for code in overused_batteries)
flash(f'The following batteries are overused (usage more than average + 2): {overused_list}', 'warning')
if underused_batteries:
underused_list = ', '.join(format_battery_code(code) for code in underused_batteries)
flash(f'The following batteries are underused (usage less than average - 2): {underused_list}', 'warning')
return render_template('index.html', batteries=battery_info, format_battery_code=format_battery_code)
@app.route('/statistics')
def statistics():
# Load the battery log data
df = pd.read_csv('battery_log.csv')
if df.empty:
flash("No data available for statistics.", "warning")
return redirect(url_for('index'))
# Convert 'Timestamp' to datetime
df['Timestamp'] = pd.to_datetime(df['Timestamp'])
# Generate graphs
graphs = []
charged_data = df[df['Charged mAh'].notnull()]
fig_charged = px.line(charged_data, x='Timestamp', y='Charged mAh', color='Battery Code',
title='Charged mAh Over Time')
graphJSON_charged = json.dumps(fig_charged, cls=plotly.utils.PlotlyJSONEncoder)
graphs.append(graphJSON_charged)
charge_used = df[df['Current Usage (J)'].notnull()]
fig_charged_used = px.line(charge_used, x='Timestamp', y='Current Usage (J)', color='Battery Code',
title='Current Usage (J) Over Time')
graphJSON_charged_used = json.dumps(fig_charged_used, cls=plotly.utils.PlotlyJSONEncoder)
graphs.append(graphJSON_charged_used)
# Render the template with the graphs
return render_template('statistics.html', graphs=graphs, advanced_logging=ADVANCED_LOGGING)
@app.route('/battery_statistics/<battery_code>')
def battery_statistics(battery_code):
# Load the battery log data
df = pd.read_csv('battery_log.csv')
df['Battery Code'] = df['Battery Code'].astype(str)
# Filter data for the specific battery
battery_df = df[df['Battery Code'] == battery_code]
print("Unique Battery Codes in DataFrame:")
print(df['Battery Code'].unique())
if battery_df.empty:
flash(f"No data available for battery {battery_code}.", "warning")
return redirect(url_for('index'))
# Convert 'Timestamp' to datetime if not already
battery_df['Timestamp'] = pd.to_datetime(battery_df['Timestamp'])
# Generate graphs
graphs = []
# Example Graph 1: Battery Usage Over Time
usage_over_time = battery_df[battery_df['Status'] == 'In Use']
usage_over_time['Timestamp'] = pd.to_datetime(usage_over_time['Timestamp'], errors='coerce')
usage_over_time['Current Usage (J)'] = pd.to_numeric(usage_over_time['Current Usage (J)'], errors='coerce')
usage_over_time = usage_over_time.dropna(subset=['Timestamp', 'Current Usage (J)'])
fig_charged = px.line(
usage_over_time,
x='Timestamp',
y='Current Usage (J)',
title='Current Usage (J) Over Time',
markers=True,
color=None,
line_group=None
)
graphJSON_usage = json.dumps(fig_charged, cls=plotly.utils.PlotlyJSONEncoder)
graphs.append(graphJSON_usage)
# Example Graph 2: Charged mAh Over Time
charged_over_time = battery_df[battery_df['Status'] == 'Charging']
print(f"Number of data points: {len(charged_over_time)}")
charged_over_time['Timestamp'] = pd.to_datetime(charged_over_time['Timestamp'], errors='coerce')
charged_over_time['Charged mAh'] = pd.to_numeric(charged_over_time['Charged mAh'], errors='coerce')
charged_over_time = charged_over_time.dropna(subset=['Timestamp', 'Charged mAh'])
fig_charged = px.line(
charged_over_time,
x='Timestamp',
y='Charged mAh',
title='Charged mAh Over Time',
markers=True,
color=None,
line_group=None
)
graphJSON_charged = json.dumps(fig_charged, cls=plotly.utils.PlotlyJSONEncoder)
graphs.append(graphJSON_charged)
# Example Graph 3: Battery Feel Ratings Over Time
battery_feel_data = battery_df[battery_df['Battery Feel'].notnull()]
fig_feel = px.line(
battery_feel_data,
x='Timestamp',
y='Battery Feel',
title='Battery Feel Over Time',
markers=True,
color=None,
line_group=None
)
graphJSON_feel = json.dumps(fig_feel, cls=plotly.utils.PlotlyJSONEncoder)
graphs.append(graphJSON_feel)
# Render the template with the graphs
return render_template('battery_statistics.html', battery_code=battery_code, graphs=graphs,
format_battery_code=format_battery_code)
# Route to download all data as a zip file
@app.route('/download_data')
def download_data():
zip_filename = os.path.join(EXPORT_FOLDER, 'battery_data.zip')
save_settings()
save_battery_status()
with zipfile.ZipFile(zip_filename, 'w') as zipf:
# Add each file to the zip archive
zipf.write(LOGS_FILE)
zipf.write(STATUS_FILE)
zipf.write(SETTINGS_FILE)
return send_file(zip_filename, as_attachment=True)
# Route to upload data and restore
@app.route('/upload_data', methods=['POST'])
def upload_data():
uploaded_file = request.files.get('data_file')
if uploaded_file and uploaded_file.filename.endswith('.zip'):
upload_path = os.path.join(EXPORT_FOLDER, uploaded_file.filename)
uploaded_file.save(upload_path)
# Extract the uploaded zip file to replace the data files
zipfile.ZipFile(upload_path, 'r').extractall(
path='exports/batteryUnzipped') # Extract files in the current directory or specify a path
# Clean up by removing the uploaded zip
os.remove(upload_path)
load_settings('exports/batteryUnzipped/settings.json')
load_initial_battery_status('exports/batteryUnzipped/battery_status.json')
load_logs('exports/batteryUnzipped/battery_log.csv', 'battery_log.csv')
save_settings()
save_battery_status()
os.remove('exports/batteryUnzipped/settings.json')
os.remove('exports/batteryUnzipped/battery_status.json')
os.remove('exports/batteryUnzipped/battery_log.csv')
flash('Data uploaded and restored successfully!', 'success')
else:
flash('Invalid file format. Please upload a zip file.', 'danger')
return redirect(url_for('index'))
def load_settings(path):
global COOLDOWN_DURATION_TIME
global TEAM_NUMBER
global ADVANCED_LOGGING
try:
with open(path, 'r') as f:
settings = json.load(f)
COOLDOWN_DURATION_TIME = settings.get('cooldown_duration_time', COOLDOWN_DURATION_TIME)
TEAM_NUMBER = settings.get('team_number', TEAM_NUMBER)
ADVANCED_LOGGING = settings.get('advanced_logging', ADVANCED_LOGGING)
except FileNotFoundError:
# Settings file does not exist, keep default settings
pass
except json.JSONDecodeError:
# Settings file is corrupt or invalid, handle as needed
pass
def save_settings():
settings = {
'cooldown_duration_time': COOLDOWN_DURATION_TIME,
'team_number': TEAM_NUMBER,
'advanced_logging': ADVANCED_LOGGING
}
with open(SETTINGS_FILE, 'w') as f:
json.dump(settings, f)
def load_logs(input_file, output_file):
try:
with open(input_file, mode='r', newline='') as infile:
reader = csv.reader(infile)
with open(output_file, mode='w', newline='') as outfile:
writer = csv.writer(outfile)
# Copy each row from the input file to the output file
for row in reader:
writer.writerow(row)
print(f"Log file '{input_file}' successfully copied to '{output_file}'")
except FileNotFoundError:
print(f"Error: The file '{input_file}' does not exist.")
except Exception as e:
print(f"An error occurred: {e}")
@app.route('/api/advanced_logging_input', methods=['POST'])
def advanced_logging_input():
data = request.json
battery_code = data.get('battery_code')
with battery_status_lock:
if battery_code in battery_status:
# Save the data without checking awaiting_advanced_input
if 'current_usage' in data and 'battery_feel' in data:
battery_status[battery_code]['current_usage'] = data['current_usage']
battery_status[battery_code]['battery_feel'] = data['battery_feel']
elif 'charged_mAh' in data:
battery_status[battery_code]['charged_mAh'] = data['charged_mAh']
else:
return jsonify({'success': False, 'message': 'Invalid data provided.'}), 400
# Remove the awaiting_advanced_input flag
battery_status[battery_code]['awaiting_advanced_input'] = False
# Optionally, log this data to CSV
log_to_csv(battery_code, battery_status[battery_code], battery_status[battery_code]['status'])
return jsonify({'success': True})
else:
return jsonify({'success': False, 'message': 'Battery not found.'}), 404
@app.route('/api/status_changes')
def status_changes():
with battery_status_lock:
# Return batteries that have changed status and require advanced logging input
if ADVANCED_LOGGING:
changes = []
for code, data in battery_status.items():
if data.get('awaiting_advanced_input', False):
changes.append({
'battery_code': code,
'status': data['status']
})
return jsonify(changes)
else:
return jsonify([])
# Flask route for manual battery code entry
@app.route('/manual_entry', methods=['POST'])
def manual_entry():
battery_code = request.form.get('battery_code')
if not battery_code:
flash('Please enter a battery code.', 'error')
return redirect(url_for('index'))
battery_code = battery_code.strip().replace('-', '')
# Parse the battery code
try:
battery_info = parse_battery_code(battery_code)
except Exception as e:
flash('Invalid battery code format.', 'error')
return redirect(url_for('index'))
with battery_status_lock:
if battery_code not in battery_status:
# Battery not found, render a template asking to add it
return render_template('add_battery_prompt.html', battery_code=battery_code, battery_info=battery_info)
else:
# Existing logic for updating battery status
current_status = battery_status[battery_code]['status']
# Determine the next status based on current status
new_status = get_next_status(battery_code, current_status)
if new_status:
log_to_csv(battery_code, battery_info, new_status)
update_battery_status(battery_code, new_status)
flash(f"Battery {battery_code} status updated to {new_status}.", 'success')
else:
flash(f"Battery {battery_code} cannot change status yet.", 'error')
return redirect(url_for('index'))
@app.route('/api/get_battery_info/<battery_code>')
def get_battery_info(battery_code):
battery_code = battery_code.strip()
with battery_status_lock:
if battery_code in battery_status:
data = battery_status[battery_code]
battery_info = {
'battery_code': battery_code,
'status': data['status'],
'notes': data.get('notes', '')
}
return jsonify(battery_info)
else:
return jsonify({'error': 'Battery not found'}), 404
@app.route('/edit_battery', methods=['POST'])
def edit_battery():
with battery_status_lock:
original_battery_code = request.form.get('original_battery_code').strip()
new_battery_code = request.form.get('battery_code').strip()
new_status = request.form.get('status')
notes = request.form.get('notes', '').strip()
if original_battery_code != new_battery_code:
# Handle renaming of battery code
if new_battery_code in battery_status:
flash('Battery code already exists.', 'error')
return redirect(url_for('index'))
battery_status[new_battery_code] = battery_status.pop(original_battery_code)
# Update status and notes
battery_status[new_battery_code]['status'] = new_status
battery_status[new_battery_code]['notes'] = notes
battery_status[new_battery_code]['last_change'] = datetime.now()
battery_status[new_battery_code]['display_time'] = timedelta(0)
# Save changes
save_battery_status()
flash(f'Battery {new_battery_code} has been updated.', 'success')
return redirect(url_for('index'))
@app.route('/confirm_add_battery', methods=['POST'])
def confirm_add_battery():
battery_code = request.form.get('battery_code')
if not battery_code:
flash('Battery code is missing.', 'error')
return redirect(url_for('index'))
battery_code = battery_code.strip().replace('-', '')
# Parse the battery code
try:
battery_info = parse_battery_code(battery_code)
except Exception as e:
flash('Invalid battery code format.', 'error')
return redirect(url_for('index'))
with battery_status_lock:
if battery_code in battery_status:
flash('Battery already exists in the system.', 'error')
return redirect(url_for('index'))
# Add the battery to the system with an initial status
battery_status[battery_code] = {
'status': 'Charging',
'last_change': datetime.now(),
'display_time': timedelta(0),
'usage_count': 0, # Initialize usage count
'notes': '', # If you have notes
'current_usage': None, # Add this line
'battery_feel': None, # Add this line
'charged_mAh': None # Add this line
}
# Optionally, log this action
log_to_csv(battery_code, battery_info, 'Added to System')
flash(f'Battery {battery_code} has been added to the system.', 'success')
return redirect(url_for('index'))
@app.route('/api/confirm_add_battery', methods=['POST'])
def api_confirm_add_battery():
battery_code = request.json.get('battery_code')
if not battery_code:
return jsonify({'success': False, 'message': 'Battery code is missing.'})
battery_code = battery_code.strip().replace('-', '')
# Parse the battery code
try:
battery_info = parse_battery_code(battery_code)
except Exception as e:
return jsonify({'success': False, 'message': 'Invalid battery code format.'})
with battery_status_lock:
if battery_code in battery_status:
return jsonify({'success': False, 'message': 'Battery already exists in the system.'})
# Add the battery to the system with an initial status
battery_status[battery_code] = {
'status': 'Charging',
'last_change': datetime.now(),
'display_time': timedelta(0),
'usage_count': 0, # Initialize usage count
'notes': '', # If you have notes
'current_usage': None, # Add this line
'battery_feel': None, # Add this line
'charged_mAh': None # Add this line # If you have notes
}
# Remove from pending batteries
if battery_code in pending_batteries:
pending_batteries.remove(battery_code)
# Optionally, log this action
log_to_csv(battery_code, battery_info, 'Added to System')
return jsonify({'success': True, 'message': f'Battery {battery_code} has been added to the system.'})
# API endpoint to provide battery status as JSON
@app.route('/api/battery_status')
def battery_status_api():
with battery_status_lock:
battery_info = [
{
'battery_code': code,
'status': data['status'],
'display_time': str(data.get('display_time', '00:00:00')),
'last_change': data['last_change'].strftime("%Y-%m-%d %H:%M:%S"),
'notes': data.get('notes', '') # Include 'notes' in the API response
} for code, data in battery_status.items()
]
return jsonify(battery_info)
@app.route('/api/pending_batteries')
def get_pending_batteries():
with battery_status_lock:
return jsonify(pending_batteries)
@app.route('/api/remove_pending_battery', methods=['POST'])
def remove_pending_battery():
battery_code = request.json.get('battery_code')
if battery_code:
with battery_status_lock:
if battery_code in pending_batteries:
pending_batteries.remove(battery_code)
return jsonify({'success': True})
@app.route('/logs')
def logs():
logs = []
# Read the CSV file
try:
with open('battery_log.csv', mode='r') as file:
reader = csv.DictReader(file)
for row in reader:
logs.append(row)
except FileNotFoundError:
flash("Log file not found.", "error")
# Pass logs data to the template
return render_template('logs.html', logs=logs)
@app.route('/video_feed')
def video_feed():
return Response(generate_frames(),
mimetype='multipart/x-mixed-replace; boundary=frame')
def generate_frames():
while True:
success, frame = cap.read()
if not success:
break
else:
# Encode the frame as JPEG
ret, buffer = cv2.imencode('.jpg', frame)
frame = buffer.tobytes()
# Yield the frame as part of the response
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
cap.release()
@app.route('/settings', methods=['GET', 'POST'])
def settings():
global COOLDOWN_DURATION_TIME
global TEAM_NUMBER
global ADVANCED_LOGGING
if request.method == 'POST':
# Retrieve and apply settings
try:
COOLDOWN_DURATION_TIME = int(request.form.get('cooldown_time', COOLDOWN_DURATION_TIME))
TEAM_NUMBER = request.form.get('team_number', TEAM_NUMBER)
ADVANCED_LOGGING = 'advanced_logging' in request.form
flash("Settings have been updated.", "success")
save_settings() # Save settings to JSON file
except:
try:
TEAM_NUMBER = request.form.get('team_number', TEAM_NUMBER)
flash("Settings have been updated (ONLY TEAM NUMBER)", "success")
save_settings() # Save settings to JSON file
except:
try:
COOLDOWN_DURATION_TIME = int(request.form.get('cooldown_time', COOLDOWN_DURATION_TIME))
flash("Settings have been updated (ONLY COOLDOWN )", "success")
save_settings() # Save settings to JSON file
except:
try:
ADVANCED_LOGGING = 'advanced_logging' in request.form
flash("Settings have been updated (ONLY Advanced Logging toggle)", "success")
save_settings() # Save settings to JSON file
except:
flash("Settings have NOT been updated.", "warning")
save_settings() # Save settings to JSON file
return render_template('settings.html',
advanced_logging=ADVANCED_LOGGING,
cooldown_time=COOLDOWN_DURATION_TIME,
team_number=TEAM_NUMBER)
@app.route('/add_battery', methods=['POST'])
def add_battery():
global TEAM_NUMBER
# Get current year
current_year = datetime.now().year
# Initialize battery number as a string with leading zeros
number = 1
battery_number = f"{number:03d}" # Format number as "0001"
battery_code = f"{current_year}{battery_number}"
# Increment battery number if code already exists
while battery_code in battery_status:
number += 1
battery_number = f"{number:03d}" # Format number with leading zeros
battery_code = f"{current_year}{battery_number}"
# Add the new battery to `battery_status`
battery_status[battery_code] = {
'status': 'Charging',
'last_change': datetime.now(),
'display_time': timedelta(0),
'usage_count': 0, # Initialize usage count
'notes': '', # If you have notes
'current_usage': None, # Add this line
'battery_feel': None, # Add this line
'charged_mAh': None # Add this line # If you have notes
}
# Return a JSON response
return jsonify({'message': f"Battery {battery_code} added successfully."})
@app.route('/stop', methods=['POST'])
def stop_system():
stop_flag.set() # Set the stop flag to terminate background threads
flash("System stopped successfully. Shutting down...", "success")
# Give a moment for flash message to register
time.sleep(1)
# Exit the program
save_battery_status()
os.abort() # Forcefully terminate the Flask server and Python process
# Alternatively, use sys.exit() but note that os._exit(0) ensures immediate termination
@app.route('/delete_battery', methods=['POST'])
def delete_battery():
battery_code = request.form.get('battery_code', '').strip()
if not battery_code:
flash('Battery code is required to delete a battery.', 'error')
return redirect(url_for('index'))
with battery_status_lock:
if battery_code in battery_status:
del battery_status[battery_code]
# Optionally, save the updated battery status
save_battery_status()
flash(f'Battery {battery_code} has been deleted.', 'success')
else:
flash(f'Battery {battery_code} not found.', 'error')
return redirect(url_for('index'))
def save_battery_status():
with open(PERSISTENT_FILE, 'w') as f:
data_to_save = {
code: {
'status': data['status'],
'last_change': data['last_change'].strftime("%Y-%m-%d %H:%M:%S"),
'usage_count': data.get('usage_count', 0),
'notes': data.get('notes', ''),
'current_usage': data.get('current_usage'),
'battery_feel': data.get('battery_feel'),
'charged_mAh': data.get('charged_mAh'),
'awaiting_advanced_input': data.get('awaiting_advanced_input', False)
}
for code, data in battery_status.items()
}
json.dump(data_to_save, f)
def load_initial_battery_status(path):
if os.path.exists(path):
with open(path, 'r') as f:
data_loaded = json.load(f)
for code, data in data_loaded.items():
battery_status[code] = {
'status': data['status'],
'last_change': datetime.strptime(data['last_change'], "%Y-%m-%d %H:%M:%S"),
'display_time': timedelta(0),
'usage_count': data.get('usage_count', 0),
'notes': data.get('notes', ''),
'current_usage': data.get('current_usage'),
'battery_feel': data.get('battery_feel'),
'charged_mAh': data.get('charged_mAh'),
'awaiting_advanced_input': data.get('awaiting_advanced_input', False)
}
# Start the Flask app and background tasks
if __name__ == "__main__":
# Load settings from file
load_settings(SETTINGS_FILE)
# Load initial battery status from persistent file
load_initial_battery_status(PERSISTENT_FILE)
# Initialize CSV if necessary
initialize_csv()
# Start the barcode scanning in a background thread
scanning_thread = threading.Thread(target=scan_barcode, daemon=True)
scanning_thread.start()
# Start the auto-update cooldown statuses in a background thread
cooldown_thread = threading.Thread(target=auto_update_cooldown_statuses, daemon=True)
cooldown_thread.start()
try:
app.run(host='0.0.0.0', port=5000, debug=False, use_reloader=False)
finally:
# Save battery status to persistent file on exit
save_battery_status()
save_settings()