-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
1315 lines (1106 loc) · 42.6 KB
/
Copy pathserver.py
File metadata and controls
1315 lines (1106 loc) · 42.6 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
#!/usr/bin/env python3
"""
BASE Station Web Server - Flask API & Dashboard
Production version - Works with existing config.py
"""
from flask import Flask, render_template, jsonify, request, send_from_directory
import json
import logging
import time
from datetime import datetime
from pathlib import Path
import hashlib
import uuid
import sys
# Setup basic logging first
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s [%(levelname)s] %(message)s'
)
# ===== CONFIGURATION =====
# Import from config.py in same directory
from config import *
# Import analytics and prediction components
try:
from analytics import ANALYTICS_AVAILABLE, daily_aggregator, prediction_service, run_daily_aggregation
except ImportError as e:
logging.warning(f"Could not import analytics: {e}")
ANALYTICS_AVAILABLE = False
daily_aggregator = None
prediction_service = None
# Map config variables to expected names
SERVER_HOST = WEB_HOST
SERVER_PORT = WEB_PORT
logging.info("✓ Loaded configuration from config.py")
logging.info(f" Data directory: {RECEIVED_DATA_DIR}")
logging.info(f" Fish data: {FISH_DATA_DIR}")
logging.info(f" Sonar data: {SONAR_DATA_DIR}")
logging.info(f" GPS data: {GPS_DATA_DIR}")
# ===== UTILITY FUNCTIONS =====
def log_audit_event(action, details, operator="Operator", node="N/A", status="info"):
try:
audit_file = DATA_DIR / "audit_log.json"
existing = []
if audit_file.exists():
existing_data = load_json_data(audit_file)
if isinstance(existing_data, list):
existing = existing_data
existing.append({
"timestamp": datetime.utcnow().isoformat(),
"action": action,
"operator": operator,
"details": details,
"status": status,
"node": node
})
save_json_data(existing, audit_file, add_timestamp=False)
except Exception as e:
logging.error(f"Failed to log audit event: {e}")
def get_latest_files(directory: Path, limit=10):
"""Get latest JSON files from directory, sorted by modification time"""
try:
if not directory.exists():
logging.debug(f"Directory not found: {directory}")
return []
json_files = list(directory.glob("*.json"))
# Sort by modification time, newest first
json_files.sort(key=lambda x: x.stat().st_mtime, reverse=True)
return json_files[:limit]
except Exception as e:
logging.error(f"Error getting files from {directory}: {e}")
return []
def save_json_data(data, filepath: Path, add_timestamp=True):
"""Save data to JSON file"""
try:
filepath.parent.mkdir(parents=True, exist_ok=True)
if add_timestamp and isinstance(data, dict):
data["_saved_at"] = datetime.utcnow().isoformat()
with open(filepath, "w", encoding="utf-8") as f:
json.dump(data, f, indent=2, ensure_ascii=False)
return True
except Exception as e:
logging.error(f"Error saving to {filepath}: {e}")
return False
def parse_battery_data(battery_dict):
"""Parse and format battery data for display"""
try:
cells = battery_dict.get("cells") or battery_dict.get("cells_mv", {})
return {
"timestamp": battery_dict.get("timestamp"),
"charge_state": battery_dict.get("charge_state", "unknown"),
"voltage_v": battery_dict.get("voltage_v") or (
battery_dict.get("battery", {}).get("voltage_mv", 0) / 1000
),
"current_ma": battery_dict.get("current_ma") or (
battery_dict.get("battery", {}).get("current_ma", 0)
),
"percent": battery_dict.get("percent") or (
battery_dict.get("battery", {}).get("percent", 0)
),
"remaining_mah": battery_dict.get("remaining_mah") or (
battery_dict.get("battery", {}).get("remaining_mah", 0)
),
"time_to_empty_min": battery_dict.get("time_to_empty_min") or (
battery_dict.get("battery", {}).get("time_to_empty_min")
),
"time_to_full_min": battery_dict.get("time_to_full_min") or (
battery_dict.get("battery", {}).get("time_to_full_min")
),
"low_voltage": battery_dict.get("low_voltage", False),
"vbus_voltage_v": battery_dict.get("vbus_voltage_v") or (
battery_dict.get("vbus", {}).get("voltage_mv", 0) / 1000
),
"cells": cells
}
except Exception as e:
return {"error": str(e), "timestamp": battery_dict.get("timestamp")}
def load_json_data(filepath: Path):
"""
Safely load JSON data.
Supports dict or list JSON roots.
"""
try:
if not filepath.exists():
logging.debug(f"JSON file not found: {filepath}")
return None
with open(filepath, "r", encoding="utf-8") as f:
data = json.load(f)
if not isinstance(data, (dict, list)):
logging.error(f"Invalid JSON root type in {filepath}: {type(data)}")
return None
return data
except json.JSONDecodeError as e:
logging.error(f"Invalid JSON format in {filepath}: {e}")
return None
except Exception as e:
logging.error(f"Error loading {filepath}: {e}")
return None
def load_latest_data(directory: Path, limit=10):
"""Load latest JSON files from directory"""
files = get_latest_files(directory, limit=limit)
results = []
for filepath in files:
data = load_json_data(filepath)
if data is None:
continue
# Normalize structure
if isinstance(data, list):
results.append({
"records": data,
"_filename": filepath.name,
"_filepath": str(filepath)
})
elif isinstance(data, dict):
data["_filename"] = filepath.name
data["_filepath"] = str(filepath)
results.append(data)
return results
def load_data_with_fallback(reference_paths, data_dir, merge_multiple=False):
"""
Smart data loading with fallback strategy:
1. Try reference files (for demo/testing)
2. Try actual data directory (for real collected data)
3. Return None if nothing found
"""
# Try reference files first
for ref_path in reference_paths:
try:
if ref_path.exists():
data = load_json_data(ref_path)
if data:
logging.info(f"📁 Loaded reference data from: {ref_path}")
return data, "reference"
except Exception as e:
logging.debug(f"Error checking {ref_path}: {e}")
continue
# Try actual data directory
try:
if data_dir.exists():
files = get_latest_files(data_dir, limit=50)
if files:
if merge_multiple:
# Load and merge multiple files
all_data = []
for f in files:
data = load_json_data(f)
if data:
if isinstance(data, list):
all_data.extend(data)
elif isinstance(data, dict):
all_data.append(data)
if all_data:
logging.info(f"📁 Loaded {len(all_data)} records from directory: {data_dir}")
return all_data, "directory"
else:
# Return just the latest file
data = load_json_data(files[0])
if data:
logging.info(f"📁 Loaded latest file from: {files[0]}")
return data, "directory"
except Exception as e:
logging.debug(f"Error checking directory {data_dir}: {e}")
logging.warning(f"⚠ No data found in reference paths or directory: {data_dir}")
return None, None
def update_config_file(active_list):
"""Update the config.py file with new active list"""
try:
config_file = Path(__file__).parent / "config.py"
if not config_file.exists():
logging.warning("config.py not found, cannot update")
return False
with open(config_file, 'r') as f:
lines = f.readlines()
new_lines = []
in_active_list = False
for i, line in enumerate(lines):
if line.strip().startswith('ACTIVE_BUOY_LIST'):
new_lines.append('ACTIVE_BUOY_LIST = [\n')
for node in active_list:
new_lines.append(f' "{node}",\n')
new_lines.append(']\n')
in_active_list = True
elif in_active_list and line.strip() == ']':
in_active_list = False
elif not in_active_list:
new_lines.append(line)
with open(config_file, 'w') as f:
f.writelines(new_lines)
return True
except Exception as e:
logging.error(f"Failed to update config.py: {e}")
return False
# ===== FLASK APP SETUP =====
# Suppress Flask logging except errors
log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)
app = Flask(__name__,
template_folder='web/templates',
static_folder='web/static')
app.config['JSON_SORT_KEYS'] = False
# Global state reference
from logic import NetworkState
from hardware import LoRaController
network_state = NetworkState()
network_state.init_caches_from_disk()
lora_controller = LoRaController(SERIAL_PORT)
# Attempt to connect to hardware
# lora_controller.connect()
scheduler = None
# ===== SYSTEM API ENDPOINTS =====
@app.route('/api/status')
def api_status():
"""Get overall system status"""
try:
if not network_state:
return jsonify({"error": "System not initialized"}), 500
return jsonify({
"codename": CODENAME,
"timestamp": datetime.utcnow().isoformat(),
"uptime": time.time(),
"stats": network_state.stats,
"connection_healthy": lora_controller.is_connected() if lora_controller else False
})
except Exception as e:
logging.error(f"Error in api_status: {e}")
return jsonify({"error": str(e)}), 500
@app.route('/api/neighbors')
def api_neighbors():
"""Get neighbor table"""
try:
if not network_state:
return jsonify({"error": "System not initialized"}), 500
summary = network_state.get_neighbor_summary()
neighbors_list = []
current_time = time.time()
for codename, info in summary.get("neighbors", {}).items():
neighbors_list.append({
"codename": codename,
"last_seen": info.get("last_seen"),
"last_direct": info.get("last_direct"),
"hops": info.get("hops", "N/A"),
"rssi": info.get("rssi"),
"status": info.get("status", "UNKNOWN")
})
return jsonify({
"total_neighbors": summary.get("total", 0),
"direct": summary.get("direct", 0),
"relay": summary.get("relay", 0),
"neighbors": neighbors_list
})
except Exception as e:
logging.error(f"Error in api_neighbors: {e}")
return jsonify({"error": str(e)}), 500
@app.route('/api/active_list')
def api_active_list():
"""Get active list with status"""
try:
if not network_state:
return jsonify({"error": "System not initialized"}), 500
active_list_status = network_state.get_active_list_status()
return jsonify({
"nodes": active_list_status
})
except Exception as e:
logging.error(f"Error in api_active_list: {e}")
return jsonify({"error": str(e)}), 500
@app.route('/api/commands', methods=['GET', 'POST'])
def api_commands():
"""Get command status or send new command"""
try:
if not network_state:
return jsonify({"error": "System not initialized"}), 500
if request.method == 'GET':
return jsonify({
"active_commands": network_state.get_all_commands(),
"linux_commands": network_state.get_all_linux_commands(),
"available_commands": AVAILABLE_COMMANDS
})
elif request.method == 'POST':
data = request.json
target = data.get('target')
scripts = data.get('scripts', [])
args_dict = data.get('args', {})
if not target or not scripts:
return jsonify({"error": "Missing target or scripts"}), 400
from logic import send_command_to_buoy
command_id = send_command_to_buoy(
network_state,
lora_controller,
target,
scripts,
args_dict
)
if command_id:
log_audit_event(
action="SEND_SCRIPT",
details=f"Sent script(s) {', '.join(scripts)} with args {args_dict}",
operator=data.get("operator", "Operator"),
node=target,
status="success"
)
return jsonify({
"success": True,
"command_id": command_id,
"target": target,
"scripts": scripts
})
else:
log_audit_event(
action="SEND_SCRIPT_FAIL",
details=f"Failed to send script(s) {', '.join(scripts)}",
operator=data.get("operator", "Operator"),
node=target,
status="error"
)
return jsonify({
"success": False,
"error": "Failed to send command"
}), 500
except Exception as e:
logging.error(f"Error in api_commands: {e}")
import traceback
logging.error(traceback.format_exc())
return jsonify({"error": str(e)}), 500
@app.route('/api/linux_commands', methods=['GET', 'POST'])
def api_linux_commands():
"""Get Linux command status or send new Linux command"""
try:
if not network_state:
return jsonify({"error": "System not initialized"}), 500
if request.method == 'GET':
return jsonify({
"active_linux_commands": network_state.get_all_linux_commands()
})
elif request.method == 'POST':
data = request.json
target = data.get('target')
command = data.get('command', '').strip()
if not target or not command:
return jsonify({"error": "Missing target or command"}), 400
from logic import send_linux_command_to_buoy
command_id = send_linux_command_to_buoy(
network_state,
lora_controller,
target,
command
)
if command_id:
log_audit_event(
action="SEND_LINUX",
details=f"Sent Linux command: {command}",
operator=data.get("operator", "Operator"),
node=target,
status="warning"
)
return jsonify({
"success": True,
"command_id": command_id,
"target": target,
"command": command
})
else:
log_audit_event(
action="SEND_LINUX_FAIL",
details=f"Failed to send Linux command: {command}",
operator=data.get("operator", "Operator"),
node=target,
status="error"
)
return jsonify({
"success": False,
"error": "Failed to send Linux command"
}), 500
except Exception as e:
logging.error(f"Error in api_linux_commands: {e}")
import traceback
logging.error(traceback.format_exc())
return jsonify({"error": str(e)}), 500
# ===== DATA ENDPOINTS (New Format) =====
@app.route('/api/data/fish')
def api_fish_data():
"""Get latest fish detection data"""
try:
limit = int(request.args.get('limit', 20))
if network_state and network_state.cache_initialized:
data_list = network_state.get_cached_data("fish_detection", limit=limit)
else:
data_list = load_latest_data(FISH_DATA_DIR, limit=limit)
return jsonify({
"success": True,
"count": len(data_list),
"data": data_list
})
except Exception as e:
logging.error(f"Error loading fish data: {e}")
return jsonify({
"success": False,
"error": str(e),
"data": []
})
@app.route('/api/data/sonar')
def api_sonar_data():
"""Get latest sonar data"""
try:
limit = int(request.args.get('limit', 20))
if network_state and network_state.cache_initialized:
data_list = network_state.get_cached_data("sonar_detection", limit=limit)
else:
data_list = load_latest_data(SONAR_DATA_DIR, limit=limit)
return jsonify({
"success": True,
"count": len(data_list),
"data": data_list
})
except Exception as e:
logging.error(f"Error loading sonar data: {e}")
return jsonify({
"success": False,
"error": str(e),
"data": []
})
@app.route('/api/data/gps')
def api_gps_data():
"""Get latest GPS data"""
try:
limit = int(request.args.get('limit', 20))
if network_state and network_state.cache_initialized:
data_list = network_state.get_cached_data("gps", limit=limit)
else:
data_list = load_latest_data(GPS_DATA_DIR, limit=limit)
return jsonify({
"success": True,
"count": len(data_list),
"data": data_list
})
except Exception as e:
logging.error(f"Error loading GPS data: {e}")
return jsonify({
"success": False,
"error": str(e),
"data": []
})
@app.route('/api/data/battery')
def api_battery_data():
"""Get latest battery data"""
try:
limit = int(request.args.get('limit', 20))
if network_state and network_state.cache_initialized:
data_list = network_state.get_cached_data("battery", limit=limit)
parsed_data = []
for d in data_list:
if "cells" in d:
parsed_data.append(d)
else:
parsed_data.append(parse_battery_data(d))
else:
data_list = load_latest_data(BATTERY_DATA_DIR, limit=limit)
parsed_data = [parse_battery_data(d) for d in data_list]
return jsonify({
"success": True,
"count": len(parsed_data),
"data": parsed_data
})
except Exception as e:
logging.error(f"Error loading battery data: {e}")
return jsonify({
"success": False,
"error": str(e),
"data": []
})
@app.route('/api/data/commands')
def api_command_results():
"""Get latest command results"""
try:
limit = int(request.args.get('limit', 20))
if network_state and network_state.cache_initialized:
data_list = network_state.get_cached_data("command_results", limit=limit)
else:
data_list = load_latest_data(COMMAND_RESULTS_DIR, limit=limit)
return jsonify({
"success": True,
"count": len(data_list),
"data": data_list
})
except Exception as e:
logging.error(f"Error loading command results: {e}")
return jsonify({
"success": False,
"error": str(e),
"data": []
})
# ===== LEGACY ENDPOINTS (for HTML compatibility) =====
@app.route("/api/fish_data")
def api_fish():
"""Legacy fish endpoint with summary statistics"""
try:
limit = int(request.args.get('limit', 50))
if network_state and network_state.cache_initialized:
data_list = network_state.get_cached_data("fish_detection", limit=limit)
else:
data_list = load_latest_data(FISH_DATA_DIR, limit=limit)
if not data_list:
logging.warning("⚠ No fish data available")
return jsonify({
"success": False,
"error": "No fish data available",
"summary": {
"total_images": 0,
"total_detections": 0,
"unique_species": 0,
"species_frequency": {}
},
"recent_detections": []
})
# Flatten list if needed
data = []
for item in data_list:
if isinstance(item, list):
data.extend(item)
elif isinstance(item, dict):
if "records" in item and isinstance(item["records"], list):
data.extend(item["records"])
else:
data.append(item)
total_images = len(data)
total_detections = sum(int(item.get("Fish Detected", 0) or 0) for item in data if isinstance(item, dict))
species_counter = {}
for item in data:
if not isinstance(item, dict):
continue
species_list = item.get("Species Detected", [])
if species_list and len(species_list) > 0:
top_species = species_list[0].get("species", "Unknown")
species_counter[top_species] = species_counter.get(top_species, 0) + 1
unique_species = len(species_counter)
logging.info(f"✓ Fish API: {total_images} images, {total_detections} detections, {unique_species} species")
return jsonify({
"success": True,
"summary": {
"total_images": total_images,
"total_detections": total_detections,
"unique_species": unique_species,
"species_frequency": species_counter
},
"recent_detections": data[:50]
})
except Exception as e:
logging.error(f"✗ Error in api_fish: {e}")
return jsonify({
"success": False,
"error": str(e),
"summary": {
"total_images": 0,
"total_detections": 0,
"unique_species": 0,
"species_frequency": {}
},
"recent_detections": []
})
@app.route("/api/sonar_data")
def api_sonar():
"""Legacy sonar endpoint"""
try:
limit = int(request.args.get('limit', 50))
if network_state and network_state.cache_initialized:
data_list = network_state.get_cached_data("sonar_detection", limit=limit)
else:
data_list = load_latest_data(SONAR_DATA_DIR, limit=limit)
if not data_list:
logging.warning("⚠ No sonar data available")
return jsonify({
"success": False,
"error": "No sonar data available",
"total_scans": 0,
"detections": 0,
"records": []
})
# Flatten list if needed
data = []
for item in data_list:
if isinstance(item, dict):
if "records" in item and isinstance(item["records"], list):
data.extend(item["records"])
else:
data.append(item)
total_scans = len(data)
detections = sum(1 for d in data if d.get("Fish Detect", 0) > 0)
logging.info(f"✓ Sonar API: {total_scans} scans, {detections} detections")
return jsonify({
"success": True,
"total_scans": total_scans,
"detections": detections,
"records": data
})
except Exception as e:
logging.error(f"✗ Error in api_sonar: {e}")
return jsonify({
"success": False,
"error": str(e),
"total_scans": 0,
"detections": 0,
"records": []
})
@app.route("/api/gps_data")
def api_gps():
"""Legacy GPS endpoint"""
try:
limit = int(request.args.get('limit', 20))
if network_state and network_state.cache_initialized:
data_list = network_state.get_cached_data("gps", limit=limit)
else:
data_list = load_latest_data(GPS_DATA_DIR, limit=limit)
if not data_list:
logging.warning("⚠ No GPS data available")
return jsonify({
"success": False,
"error": "No GPS data available",
"current": {},
"history": []
})
# Get the most recent entry
latest = data_list[0]
logging.info(f"✓ GPS API: Location at {latest.get('latitude', 'N/A')}, {latest.get('longitude', 'N/A')}")
return jsonify({
"success": True,
"current": latest,
"history": data_list
})
except Exception as e:
logging.error(f"✗ Error in api_gps: {e}")
return jsonify({
"success": False,
"error": str(e),
"current": {},
"history": []
})
@app.route('/api/battery_data')
def api_battery_data_legacy():
"""Legacy endpoint - redirects to new format"""
try:
limit = int(request.args.get('limit', 20))
if network_state and network_state.cache_initialized:
data_list = network_state.get_cached_data("battery", limit=limit)
parsed_data = []
for d in data_list:
if "cells" in d:
parsed_data.append(d)
else:
parsed_data.append(parse_battery_data(d))
else:
data_list = load_latest_data(BATTERY_DATA_DIR, limit=limit)
parsed_data = [parse_battery_data(d) for d in data_list]
return jsonify({
"success": True,
"recent_readings": parsed_data
})
except Exception as e:
logging.error(f"Error loading battery data: {e}")
return jsonify({
"success": False,
"error": str(e),
"recent_readings": []
})
@app.route('/api/data/stats')
def api_data_stats():
"""Get statistics about stored data"""
try:
if network_state and network_state.cache_initialized:
stats = {
"fish_count": network_state.file_counts["fish"],
"sonar_count": network_state.file_counts["sonar"],
"gps_count": network_state.file_counts["gps"],
"battery_count": network_state.file_counts["battery"],
"command_count": network_state.file_counts["command"]
}
else:
stats = {
"fish_count": len(list(FISH_DATA_DIR.glob("*.json"))) if FISH_DATA_DIR.exists() else 0,
"sonar_count": len(list(SONAR_DATA_DIR.glob("*.json"))) if SONAR_DATA_DIR.exists() else 0,
"gps_count": len(list(GPS_DATA_DIR.glob("*.json"))) if GPS_DATA_DIR.exists() else 0,
"battery_count": len(list(BATTERY_DATA_DIR.glob("*.json"))) if BATTERY_DATA_DIR.exists() else 0,
"command_count": len(list(COMMAND_RESULTS_DIR.glob("*.json"))) if COMMAND_RESULTS_DIR.exists() else 0
}
return jsonify({
"success": True,
"stats": stats
})
except Exception as e:
logging.error(f"Error getting data stats: {e}")
return jsonify({
"success": False,
"error": str(e),
"stats": {}
})
# ===== SCHEDULER ENDPOINTS =====
@app.route('/api/schedules', methods=['GET', 'POST', 'DELETE'])
def api_schedules():
"""Manage schedules"""
try:
if not scheduler:
return jsonify({"error": "Scheduler not initialized"}), 500
if request.method == 'GET':
return jsonify({
"schedules": scheduler.get_schedules(),
"next_scheduled": scheduler.get_next_scheduled(limit=5)
})
elif request.method == 'POST':
schedule_data = request.json
schedule_id = scheduler.add_schedule(schedule_data)
return jsonify({
"success": True,
"schedule_id": schedule_id
})
elif request.method == 'DELETE':
schedule_id = request.args.get('id')
if scheduler.remove_schedule(schedule_id):
return jsonify({"success": True})
else:
return jsonify({"success": False, "error": "Schedule not found"}), 404
except Exception as e:
logging.error(f"Error in api_schedules: {e}")
import traceback
logging.error(traceback.format_exc())
return jsonify({"error": str(e)}), 500
@app.route('/api/scheduler/toggle/<schedule_id>', methods=['POST'])
def api_toggle_schedule(schedule_id):
"""Toggle schedule enabled/disabled"""
try:
if not scheduler:
return jsonify({"error": "Scheduler not initialized"}), 500
if scheduler.toggle_schedule(schedule_id):
return jsonify({"success": True})
else:
return jsonify({"success": False, "error": "Schedule not found"}), 404
except Exception as e:
logging.error(f"Error in api_toggle_schedule: {e}")
import traceback
logging.error(traceback.format_exc())
return jsonify({"error": str(e)}), 500
@app.route('/api/send_command', methods=['POST'])
def send_command():
"""
Send manual command to BUOY node
CRITICAL FIX: Uses transmission queue to prevent race conditions
"""
try:
data = request.json
target = data.get('target')
scripts = data.get('scripts', [])
args_dict = data.get('args', {})
if not target or not scripts:
return jsonify({"success": False, "error": "Missing target or scripts"}), 400
from logic import send_command_to_buoy
# CRITICAL FIX: Queue the command instead of sending directly
scheduler.queue_manual_command(
send_command_to_buoy,
network_state,
lora_controller,
target,
scripts,
args_dict
)
return jsonify({
"success": True,
"message": "Command queued for transmission",
"target": target,
"scripts": scripts
})
except Exception as e:
logger.error(f"[API] Send command error: {e}")
return jsonify({"success": False, "error": str(e)}), 500
@app.route('/api/scheduler/status', methods=['GET'])
def get_scheduler_status():
"""Get scheduler and transmission queue status"""
try:
status = scheduler.get_transmission_status()
return jsonify({
"success": True,
"status": status
})
except Exception as e:
return jsonify({"success": False, "error": str(e)}), 500
# ===== SETTINGS ENDPOINTS =====
@app.route('/api/settings', methods=['GET', 'POST'])
def api_settings():
"""Get or update settings"""
settings_file = DATA_DIR / "settings.json"
if request.method == 'GET':
try:
import config
import importlib
importlib.reload(config)
settings = load_json_data(settings_file) or {}
settings.update({
"discovery_interval": config.DISCOVERY_INTERVAL,
"neighbor_timeout": config.NEIGHBOR_TIMEOUT,
"active_list": config.ACTIVE_BUOY_LIST
})
return jsonify(settings)
except Exception as e:
logging.error(f"Error loading settings: {e}")
return jsonify({})
elif request.method == 'POST':
settings = request.json
if 'active_list' in settings:
active_list = settings['active_list']
if update_config_file(active_list):
try: