This repository was archived by the owner on Jun 13, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
1184 lines (1006 loc) · 46.5 KB
/
Copy pathmain.py
File metadata and controls
1184 lines (1006 loc) · 46.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
#!/usr/bin/env python3
import argparse
import json
import logging
import math
import os
import random
import signal
import string
import sys
import threading
import time
from collections.abc import Callable
from datetime import datetime
from pathlib import Path
from typing import Any
import yaml
from pydantic import BaseModel, Field, ValidationError, field_validator
from src.config import ConfigManager
from src.database import SensorReadingSchema
from src.error_utils import raise_with_context
from src.llm_docs import print_llm_documentation
from src.location import LocationGenerator
from src.safe_logger import get_safe_logger, setup_safe_logging
from src.simulator import SensorSimulator
# Pydantic Models for Configuration (config.yaml)
# === Configuration Models (Simplified and Organized) ===
class ParameterRange(BaseModel):
"""Reusable model for parameter ranges."""
min_val: int | float = Field(alias="min")
max_val: int | float = Field(alias="max")
class DatabaseConfig(BaseModel):
"""Database configuration settings."""
path: str
backup_enabled: bool
backup_interval_seconds: int | float
max_backup_size_mb: int | float
compression_enabled: bool
@field_validator("backup_interval_seconds")
def validate_backup_interval(cls, v):
if v < 60:
msg = "backup_interval_seconds must be at least 60"
raise ValueError(msg)
return v
@field_validator("max_backup_size_mb")
def validate_max_backup_size(cls, v):
if v <= 0:
msg = "max_backup_size_mb must be positive"
raise ValueError(msg)
return v
class SimulationSettings(BaseModel):
"""Simulation and runtime settings."""
interval_seconds: int | float
replicas_count: int | None = 1
# Location randomization
random_location_enabled: bool = False
latitude_range: list[int | float] | None = None
longitude_range: list[int | float] | None = None
location_update_interval: int | float | None = None
# Dynamic reloading
dynamic_reload_enabled: bool = False
reload_check_interval: int | float = 5
@field_validator("interval_seconds")
def validate_interval(cls, v):
if v <= 0:
msg = "interval_seconds must be positive"
raise ValueError(msg)
return v
@field_validator("replicas_count")
def validate_replicas(cls, v):
if v is not None and v < 1:
msg = "replicas_count must be at least 1"
raise ValueError(msg)
return v
@field_validator("latitude_range")
def validate_latitude(cls, v):
if v is not None:
if len(v) != 2:
msg = "latitude_range must have exactly 2 values"
raise ValueError(msg)
if not (-90 <= v[0] <= v[1] <= 90):
msg = "latitude_range must be within [-90, 90]"
raise ValueError(msg)
return v
@field_validator("longitude_range")
def validate_longitude(cls, v):
if v is not None:
if len(v) != 2:
msg = "longitude_range must have exactly 2 values"
raise ValueError(msg)
if not (-180 <= v[0] <= v[1] <= 180):
msg = "longitude_range must be within [-180, 180]"
raise ValueError(msg)
return v
class SensorParameters(BaseModel):
"""Sensor parameter configuration including normal ranges and anomalies."""
# Normal parameter ranges
temperature: ParameterRange
vibration: ParameterRange
humidity: ParameterRange
pressure: ParameterRange
voltage: ParameterRange
# Anomaly settings
anomalies_enabled: bool = False
anomaly_probability: int | float = 0.1
anomaly_types: dict[str, Any] | None = None
class AppConfig(BaseModel):
"""Main application configuration."""
# Core configurations
database: DatabaseConfig
simulation: SimulationSettings
parameters: SensorParameters
# Logging
log_level: str = "INFO"
log_file: str = "logs/sensor.log"
# Monitoring (required)
monitoring_enabled: bool = True
monitoring_host: str = "0.0.0.0"
monitoring_port: int = 8080
# Sensor config
sensor: dict[str, Any] | None = None
model_config = {"extra": "forbid"}
@field_validator("log_level")
def validate_log_level(cls, v):
valid_levels = ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]
if v.upper() not in valid_levels:
msg = f"log level must be one of {valid_levels}"
raise ValueError(msg)
return v.upper()
def process_config(raw_config: dict) -> dict:
"""Process configuration into expected format."""
# Simply return the config with defaults for monitoring
config = raw_config.copy()
# Ensure monitoring is enabled (required for dynamic reloading)
config.setdefault("monitoring", {})
config["monitoring"]["enabled"] = True
config["monitoring"].setdefault("host", "0.0.0.0")
config["monitoring"].setdefault("port", 8080)
# Ensure dynamic reloading is enabled
config.setdefault("dynamic_reloading", {})
config["dynamic_reloading"]["enabled"] = True
config["dynamic_reloading"].setdefault("check_interval_seconds", 5)
return config
# Pydantic Models for Identity (node_identity.json)
class LocationData(BaseModel):
city: str | None = None
state: str | None = None
coordinates: dict[str, int | float] | None = None
timezone: str | None = None
address: str | None = None
@field_validator("coordinates")
def validate_coordinates(cls, v):
if v is not None:
if "latitude" not in v or "longitude" not in v:
msg = "coordinates must contain 'latitude' and 'longitude'"
raise ValueError(msg)
lat = v["latitude"]
lon = v["longitude"]
if not isinstance(lat, int | float) or not isinstance(lon, int | float):
msg = "latitude and longitude must be numeric"
raise ValueError(msg)
if not (-90 <= lat <= 90):
msg = "latitude must be between -90 and 90"
raise ValueError(msg)
if not (-180 <= lon <= 180):
msg = "longitude must be between -180 and 180"
raise ValueError(msg)
return v
class DeviceInfoData(BaseModel):
manufacturer: str | None = None
model: str | None = None
firmware_version: str | None = None
serial_number: str | None = None
manufacture_date: str | None = None
@field_validator("manufacture_date")
def validate_manufacture_date(cls, v):
if v is not None:
try:
datetime.fromisoformat(v)
except ValueError:
msg = "manufacture_date must be in ISO format"
raise ValueError(msg)
return v
class DeploymentData(BaseModel):
deployment_type: str | None = None
installation_date: str | None = None
height_meters: int | float | None = None
orientation_degrees: int | float | None = None
@field_validator("installation_date")
def validate_installation_date(cls, v):
if v is not None:
try:
datetime.fromisoformat(v)
except ValueError:
msg = "installation_date must be in ISO format"
raise ValueError(msg)
return v
@field_validator("orientation_degrees")
def validate_orientation(cls, v):
if v is not None and not (0 <= v <= 360):
msg = "orientation_degrees must be between 0 and 360"
raise ValueError(msg)
return v
class MetadataData(BaseModel):
instance_id: str | None = None
identity_generation_timestamp: str | None = None
generation_seed: int | str | None = None
sensor_type: str | None = None
@field_validator("identity_generation_timestamp")
def validate_timestamp(cls, v):
if v is not None:
try:
datetime.fromisoformat(v)
except ValueError:
msg = "identity_generation_timestamp must be in ISO format"
raise ValueError(msg)
return v
class IdentityData(BaseModel):
sensor_id: str | None = None
location: str | LocationData | None = None
device_info: DeviceInfoData | None = None
deployment: DeploymentData | None = None
metadata: MetadataData | None = None
def load_config(config_path: str) -> dict:
"""Load configuration from a YAML file with validation.
Args:
config_path: Path to the YAML configuration file
Returns:
Validated configuration dictionary
Raises:
SystemExit: If configuration is invalid
"""
logger = get_safe_logger(__name__)
try:
with Path(config_path).open() as f:
raw_config_data = yaml.safe_load(f)
if not isinstance(raw_config_data, dict):
logger.error(f"Config file {config_path} content must be a dictionary.")
raise_with_context(
"Config file content must be a dictionary.",
TypeError("Config file content must be a dictionary."),
)
# Process config to ensure required fields
config = process_config(raw_config_data)
# Ensure monitoring and dynamic reloading are enabled
if not config.get("monitoring", {}).get("enabled"):
logger.warning("Monitoring is required - enabling it")
config.setdefault("monitoring", {})["enabled"] = True
if not config.get("dynamic_reloading", {}).get("enabled"):
logger.warning("Dynamic reloading is required - enabling it")
config.setdefault("dynamic_reloading", {})["enabled"] = True
except FileNotFoundError as e:
logger.exception(f"Configuration file not found: {config_path}")
raise_with_context(f"Configuration file not found: {config_path}", e)
except yaml.YAMLError as e:
logger.exception(f"Error parsing YAML from configuration file {config_path}: {e}")
raise_with_context(f"Error parsing YAML from configuration file {config_path}", e)
except ValidationError as e:
logger.exception(f"Invalid configuration in {config_path}:\n{e}")
raise_with_context(f"Invalid configuration: {e}", e)
except Exception as e:
logger.exception(f"Error loading configuration file {config_path}: {e!s}")
raise_with_context(f"Error loading configuration file {config_path}: {e!s}", e)
return config
def load_identity(identity_path: str) -> dict:
"""Load sensor identity from JSON file and validate its structure using Pydantic."""
from src.error_utils import raise_with_context
logger = get_safe_logger(__name__)
try:
with Path(identity_path).open() as f:
raw_identity_data = json.load(f)
if not isinstance(raw_identity_data, dict):
logger.error(f"Identity file {identity_path} content must be a dictionary.")
raise_with_context(
"Identity file content must be a dictionary.",
TypeError("Identity file content must be a dictionary."),
)
# Validate and parse using Pydantic model
identity_data_model = IdentityData(**raw_identity_data)
return identity_data_model.model_dump() # Convert Pydantic model to dict
except FileNotFoundError as e:
logger.exception(f"Identity file not found: {identity_path}")
raise_with_context(f"Identity file not found: {identity_path}", e)
except json.JSONDecodeError as e:
logger.exception(f"Error decoding JSON from identity file {identity_path}: {e}")
raise_with_context(f"Error decoding JSON from identity file {identity_path}", e)
except ValidationError as e:
logger.exception(f"Invalid identity data in {identity_path}:\n{e}")
raise_with_context(f"Invalid identity data: {e}", e)
except Exception as e:
logger.exception(f"Error loading identity file {identity_path}: {e}")
raise_with_context(f"Error loading identity file {identity_path}: {e}", e)
# This line should never be reached since all exceptions re-raise, but mypy needs it
raise RuntimeError("Unexpected error in load_identity")
def generate_sensor_id(identity: dict) -> str:
"""Generate a new sensor ID in the format CITY_XXXXXX."""
# Handle both new nested structure and legacy format
location_str = None
# Check for nested location structure first
location_data = identity.get("location")
if isinstance(location_data, dict):
location_str = location_data.get("city") or location_data.get("address")
elif isinstance(location_data, str):
location_str = location_data
if not location_str: # Check if location_str is None or empty
# This function expects location to be present in the identity dict
msg = "Location is required in identity data to generate a sensor ID"
raise ValueError(msg)
uppercity_no_special_chars = "".join(c.upper() for c in location_str if c.isalpha())
# Get the first 4 letters of the location (was 3, but ID format implies 4, e.g. CITY)
location_prefix = uppercity_no_special_chars[:4]
if not location_prefix:
# This would happen if location_str had no alpha characters
msg = "Valid location with alphabetic characters is required to generate a sensor ID"
raise ValueError(msg)
# Ensure it's padded or truncated to 4 chars if needed, or make it variable
# For now, assume it must result in a prefix.
# If location is "Ny", prefix is "NY". If "A", prefix "A".
vowels = "aeiou"
consonants = "".join(c.upper() for c in string.ascii_letters if c not in vowels)
random_suffix = "".join(random.choice(consonants + string.digits) for _ in range(6))
return f"{location_prefix.upper()}_{random_suffix}"
def process_identity_and_location(identity_data: dict, app_config: dict) -> dict:
"""
Processes the identity data, generating or validating location information.
Args:
identity_data: The raw identity data from the identity file.
app_config: The application configuration.
Returns:
The processed identity data with location information.
Raises:
RuntimeError: If location generation fails when required.
"""
working_identity = identity_data.copy()
logger = get_safe_logger(__name__) # Use a local logger
# Get random_location settings from app_config
random_location_config = app_config.get("random_location", {})
random_location_enabled = random_location_config.get("enabled", False)
# Ensure gps_variation_meters is defined at a scope accessible by the fuzzing logic
gps_variation_meters = random_location_config.get("gps_variation")
# Check for presence and validity of location, latitude, longitude in identity_data
# Handle both new nested structure and legacy format
location_value = None
latitude_value = None
longitude_value = None
location_data = working_identity.get("location")
if isinstance(location_data, dict):
# New nested structure
location_value = location_data.get("city") or location_data.get("address")
coords = location_data.get("coordinates", {})
if isinstance(coords, dict):
latitude_value = coords.get("latitude")
longitude_value = coords.get("longitude")
elif isinstance(location_data, str):
# Legacy structure or already converted by model validator
location_value = location_data
latitude_value = working_identity.get("latitude")
longitude_value = working_identity.get("longitude")
# Location must be a non-empty string
has_location = isinstance(location_value, str) and bool(location_value.strip())
has_latitude = isinstance(latitude_value, int | float)
has_longitude = isinstance(longitude_value, int | float)
all_geo_fields_valid_and_present = has_location and has_latitude and has_longitude
if all_geo_fields_valid_and_present:
logger.info(
f"Using location '{location_value}' (Lat: {latitude_value}, Lon: {longitude_value}) "
"from identity file as base for geo-coordinates."
)
# Ensure flat values are set for backward compatibility
working_identity["latitude"] = latitude_value
working_identity["longitude"] = longitude_value
elif random_location_enabled:
logger.info(
"Not all geo-fields (location, latitude, longitude) are valid or present in identity. "
"'random_location.enabled' is true. Attempting to generate random location data."
)
location_generator = LocationGenerator(random_location_config)
available_cities = location_generator.cities
if not available_cities:
logger.error("City data is not available or empty. Cannot generate random location.")
msg = "City data not available for random location generation."
raise RuntimeError(msg)
random_city_name = random.choice(list(available_cities.keys()))
random_city_data = available_cities[random_city_name]
# Set base coordinates from the randomly chosen city
# Update both nested and flat structure for compatibility
if isinstance(working_identity.get("location"), dict):
working_identity["location"]["city"] = random_city_name
working_identity["location"]["coordinates"] = {
"latitude": random_city_data["latitude"],
"longitude": random_city_data["longitude"],
}
else:
# Create new location structure if needed
working_identity["location"] = {
"city": random_city_name,
"coordinates": {
"latitude": random_city_data["latitude"],
"longitude": random_city_data["longitude"],
},
}
# Also set flat values for backward compatibility
working_identity["latitude"] = random_city_data["latitude"]
working_identity["longitude"] = random_city_data["longitude"]
logger.info(
f"Randomly selected base location: {random_city_name} "
f"(Lat: {random_city_data['latitude']:.6f}, Lon: {random_city_data['longitude']:.6f})"
)
has_location = True # Ensure this is set for ID generation logic
location_value = random_city_name
latitude_value = random_city_data["latitude"]
longitude_value = random_city_data["longitude"]
# The old fuzzing logic and log_suffix that were here are removed.
# Fuzzing will be handled in a separate block later.
else:
# Not all geo fields are valid or present, and random_location is not enabled. This is an error.
missing_fields = []
if not has_location:
missing_fields.append("'location' (must be a non-empty string)")
if not has_latitude:
missing_fields.append("'latitude' (must be a number)")
if not has_longitude:
missing_fields.append("'longitude' (must be a number)")
error_msg = (
f"Required geo-fields ({', '.join(missing_fields)}) are missing or invalid in the identity file, "
"and 'random_location.enabled' is false. "
"Please provide valid 'location', 'latitude', and 'longitude' in the identity file, "
"or enable 'random_location.enabled=true' in the configuration."
)
logger.error(error_msg)
raise RuntimeError(error_msg)
# --- Unified Fuzzing Logic ---
# Apply fuzzing if random_location is enabled and gps_variation is configured positively,
# using the coordinates currently in working_identity (either from file or random generation).
if (
random_location_enabled
and gps_variation_meters
and isinstance(gps_variation_meters, int | float)
and gps_variation_meters > 0
):
current_lat = working_identity.get("latitude")
current_lon = working_identity.get("longitude")
if isinstance(current_lat, int | float) and isinstance(current_lon, int | float):
gps_variation_km = gps_variation_meters / 1000.0
logger.info(
f"Applying GPS fuzzing (up to {gps_variation_meters}m / {gps_variation_km:.2f}km) "
f"to location: {working_identity.get('location')} "
f"(Base Lat: {current_lat:.6f}, Base Lon: {current_lon:.6f})."
)
# Approximate conversion for fuzzing
lat_variation_degrees = gps_variation_km / 111.0
lon_variation_degrees = (
gps_variation_km / (111.0 * math.cos(math.radians(current_lat)))
if math.cos(math.radians(current_lat)) != 0
else lat_variation_degrees
)
lat_offset = random.uniform(-lat_variation_degrees, lat_variation_degrees)
lon_offset = random.uniform(-lon_variation_degrees, lon_variation_degrees)
fuzzed_latitude = current_lat + lat_offset
fuzzed_longitude = current_lon + lon_offset
# Round to 5 decimal places (precision of ~1.11 meters)
fuzzed_latitude = round(fuzzed_latitude, 5)
fuzzed_longitude = round(fuzzed_longitude, 5)
# Update both nested and flat structure
if (
isinstance(working_identity.get("location"), dict)
and "coordinates" in working_identity["location"]
):
working_identity["location"]["coordinates"]["latitude"] = fuzzed_latitude
working_identity["location"]["coordinates"]["longitude"] = fuzzed_longitude
# Always update flat values for backward compatibility
working_identity["latitude"] = fuzzed_latitude
working_identity["longitude"] = fuzzed_longitude
location_name = location_value or working_identity.get("location")
logger.info(
f"Fuzzed coordinates for '{location_name}': "
f"New Lat: {fuzzed_latitude:.5f}, New Lon: {fuzzed_longitude:.5f}"
)
else:
logger.warning(
f"GPS fuzzing enabled, but current latitude/longitude in working_identity are invalid. Skipping fuzzing. "
f"Lat: {current_lat}, Lon: {current_lon}"
)
elif (
random_location_enabled and gps_variation_meters
): # gps_variation_meters is not None but not positive float/int
logger.info(
f"Random location enabled, but GPS variation value ({gps_variation_meters}) is not a positive number. "
"Using exact coordinates."
)
elif random_location_enabled: # gps_variation_meters is None
logger.info(
"Random location enabled, but GPS variation ('gps_variation') not configured or is null. "
"Using exact coordinates."
)
# If random_location is not enabled, no fuzzing occurs, and no message about it is needed here.
# Now, ensure an ID is generated if it's missing.
# This relies on 'location' being present and valid in working_identity,
# which should be guaranteed by the logic above if no error was raised.
sensor_id = working_identity.get("sensor_id") or working_identity.get("id")
if not sensor_id:
# Check location again, as generate_sensor_id depends on it.
# It should be set if we reached here (either from file or random generation).
current_location = working_identity.get("location")
if isinstance(current_location, dict):
# For nested structure, create a string representation for generate_sensor_id
location_str = current_location.get("city") or current_location.get("address")
else:
location_str = current_location
if not (isinstance(location_str, str) and location_str.strip()):
critical_error_msg = (
"Critical internal error: 'location' is missing or invalid in identity data "
"just before ID generation, despite prior checks. This should not happen."
)
logger.error(critical_error_msg)
raise RuntimeError(critical_error_msg)
logger.info(
"Identity file does not contain an 'id' or 'sensor_id' key, or it is empty. Generating new sensor ID."
)
try:
generated_id = generate_sensor_id(working_identity)
# Set both fields for compatibility
working_identity["sensor_id"] = generated_id
working_identity["id"] = generated_id
logger.info(f"Generated sensor ID: {generated_id}")
except ValueError as e:
# This could happen if generate_sensor_id raises an error (e.g. location becomes invalid unexpectedly)
logger.exception(f"Failed to generate sensor ID: {e}")
raise # Re-raise to be caught by main
else:
# Ensure both fields are set for compatibility
working_identity["sensor_id"] = sensor_id
working_identity["id"] = sensor_id
return working_identity
def setup_logging(config):
"""Set up logging based on configuration."""
log_config = config.get("logging", {})
log_level = getattr(logging, log_config.get("level", "INFO"))
log_format = log_config.get("format", "%(asctime)s - %(name)s - %(levelname)s - %(message)s")
log_file = log_config.get("file")
console_output = log_config.get("console_output", True)
# Configure root logger
logger = get_safe_logger("")
logger.setLevel(log_level)
# Remove existing handlers
for handler in logger.handlers[:]:
logger.removeHandler(handler)
# Add console handler if enabled
if console_output:
console_handler = logging.StreamHandler()
console_handler.setFormatter(logging.Formatter(log_format))
logger.addHandler(console_handler)
# Add file handler if log file is specified
if log_file:
# Ensure the log directory exists
log_dir = Path(log_file).parent
if log_dir and not log_dir.exists():
log_dir.mkdir(parents=True)
file_handler = logging.FileHandler(log_file)
file_handler.setFormatter(logging.Formatter(log_format))
logger.addHandler(file_handler)
def file_watcher_thread(
file_path: str,
load_function: Callable[[str], dict],
config_manager_instance: ConfigManager,
update_type: str, # "config" or "identity"
simulator_instance: SensorSimulator,
simulator_update_method_name: str, # "handle_config_updated" or "handle_identity_updated"
stop_event: threading.Event,
check_interval: float,
):
"""
Monitors a file for changes and updates the ConfigManager and SensorSimulator.
"""
logger = get_safe_logger(__name__)
last_mtime = None
if Path(file_path).exists():
last_mtime = Path(file_path).stat().st_mtime
else:
logger.warning(
f"File watcher: Initial file not found at {file_path}. Will watch for creation."
)
while not stop_event.is_set():
try:
if not Path(file_path).exists():
if last_mtime is not None: # File was deleted
logger.warning(f"File watcher: Watched file {file_path} has been deleted.")
last_mtime = None # Reset mtime so recreation is detected
# Wait and check again
stop_event.wait(check_interval)
continue
current_mtime = Path(file_path).stat().st_mtime
if last_mtime is None or current_mtime > last_mtime:
if last_mtime is None:
logger.info(f"File watcher: File {file_path} has been created/appeared.")
else:
logger.info(f"File watcher: File {file_path} has changed. Reloading...")
try:
new_data = load_function(file_path)
if update_type == "config":
config_manager_instance.config = new_data
# Potentially re-process identity if config affects it, e.g., random location settings
# For now, assuming direct update is sufficient for config.
# If identity processing logic in main needs to be rerun, that's more complex.
# The main config doesn't usually change identity structure, but identity file does.
elif update_type == "identity":
# When identity file changes, it might need re-processing (e.g. ID generation, location fixing)
# This requires the *current app_config* for process_identity_and_location
# It's safer to re-run the original processing logic from main.py for identity.
# However, process_identity_and_location takes app_config, not config_manager.config.
# For now, we'll assume `load_identity` gives the final, processed identity.
# A more robust solution would re-run `process_identity_and_location`.
# Let's keep it simple: just load and set.
# The `config_manager.identity` will be updated by the simulator's handler.
# We need to pass the *raw loaded data* to `process_identity_and_location`
# and the *current config dictionary* from ConfigManager.
current_app_config = config_manager_instance.config
processed_new_identity = process_identity_and_location(
new_data, current_app_config
)
config_manager_instance.identity = processed_new_identity
else:
logger.error(
f"File watcher: Unknown update type '{update_type}' for {file_path}"
)
last_mtime = current_mtime # Update mtime to avoid reprocessing error
stop_event.wait(check_interval)
continue
logger.info(
f"File watcher: Successfully reloaded {file_path}. Notifying simulator."
)
update_method = getattr(simulator_instance, simulator_update_method_name)
update_method() # Call handle_config_updated() or handle_identity_updated()
last_mtime = current_mtime
except Exception as e:
logger.exception(
f"File watcher: Error reloading {file_path}: {e}. Using previous configuration for this source."
)
# last_mtime should not be updated here, so it tries again next interval
# unless the file truly hasn't changed, in which case an mtime update is needed
if Path(file_path).exists(): # If error was not file not found
last_mtime = Path(file_path).stat().st_mtime
except Exception as e:
logger.exception(f"File watcher: Unexpected error for {file_path}: {e}")
# Avoid tight loop on unexpected errors
# Use shorter intervals to be more responsive to shutdown
wait_intervals = 10
interval = check_interval / wait_intervals
for _ in range(wait_intervals):
if stop_event.wait(interval):
break # Stop event was set
logger.info(f"File watcher: Thread for {file_path} is stopping.")
def main():
"""Main function to run the sensor simulator."""
# Argument parsing
parser = argparse.ArgumentParser(description="Sensor Log Generator")
parser.add_argument(
"--config",
type=str,
default="config/config.yaml",
help="Path to the configuration file (default: config/config.yaml)",
)
parser.add_argument(
"--identity",
type=str,
default="config/identity.json",
help="Path to the identity file (default: config/identity.json)",
)
parser.add_argument(
"--output-schema",
action="store_true",
help="Output the database schema as JSON and exit.",
)
parser.add_argument(
"--generate-identity",
action="store_true",
help="Generate a new format identity template with placeholder values and exit.",
)
parser.add_argument(
"--llm-docs",
action="store_true",
help="Output comprehensive LLM documentation and exit.",
)
parser.add_argument(
"--debug",
action="store_true",
help="Enable extremely verbose debug logging with periodic status updates.",
)
args = parser.parse_args()
if args.output_schema:
# Generate schema from Pydantic model
schema_dict = SensorReadingSchema.model_json_schema()
schema_json = json.dumps(schema_dict, indent=2)
print(schema_json)
sys.exit(0)
if args.llm_docs:
# Output LLM documentation
print_llm_documentation()
sys.exit(0)
if args.generate_identity:
# Generate a new format identity template
import uuid
from datetime import datetime
template = {
"sensor_id": "SENSOR_XX_YYY_ZZZZ",
"location": {
"city": "YourCity",
"state": "XX",
"coordinates": {"latitude": 40.7128, "longitude": -74.0060},
"timezone": "America/New_York",
"address": "YourCity, XX, USA",
},
"device_info": {
"manufacturer": "SensorCorp",
"model": "WeatherStation Pro",
"firmware_version": "2.1.0",
"serial_number": f"SENSOR-{uuid.uuid4().hex[:6].upper()}",
"manufacture_date": datetime.now().strftime("%Y-%m-%d"),
},
"deployment": {
"deployment_type": "stationary_unit",
"installation_date": datetime.now().strftime("%Y-%m-%d"),
"height_meters": 2.5,
"orientation_degrees": 0,
},
"metadata": {
"instance_id": f"i-{uuid.uuid4().hex[:16]}",
"identity_generation_timestamp": datetime.now().isoformat(),
"generation_seed": random.randint(10**20, 10**40),
"sensor_type": "environmental_monitoring",
},
}
print(json.dumps(template, indent=2))
sys.exit(0)
# Determine configuration file paths, prioritizing environment variables
config_file_path = os.environ.get("CONFIG_FILE") or args.config
identity_file_path = os.environ.get("IDENTITY_FILE") or args.identity
# Set up basic logging first (before config is fully loaded)
# Enable debug mode if requested
if args.debug:
setup_safe_logging(debug=True)
# Set debug level for all loggers
root_logger = get_safe_logger("")
root_logger.setLevel(logging.DEBUG)
for name in ["src.simulator", "src.database", "src.anomaly", "SensorDatabase"]:
logger = get_safe_logger(name)
logger.setLevel(logging.DEBUG)
logger = get_safe_logger(__name__)
logger.info("🔍 DEBUG MODE ENABLED - Extremely verbose logging active")
logger.debug("Debug flag detected from command line")
# Store debug flag globally
os.environ["DEBUG_MODE"] = "true"
else:
setup_safe_logging(level=logging.INFO)
if not config_file_path:
# This condition might be less likely to be hit if args.config has a default
print(
"Error: Configuration file path is not set via --config or CONFIG_FILE env var.",
file=sys.stderr,
)
sys.exit(1)
if not identity_file_path:
# This condition might be less likely to be hit if args.identity has a default
print(
"Error: Identity file path is not set via --identity or IDENTITY_FILE env var.",
file=sys.stderr,
)
sys.exit(1)
if not Path(config_file_path).is_file():
print(f"Error: Config file not found at {config_file_path}", file=sys.stderr)
sys.exit(1)
if not Path(identity_file_path).is_file():
print(f"Error: Identity file not found at {identity_file_path}", file=sys.stderr)
sys.exit(1)
initial_config = {}
initial_identity = {}
config_manager = None
simulator = None
watcher_threads = []
stop_watcher_event = None
shutdown_requested = False
# Track signal count for forced exit
signal_count = 0
# Set up signal handler for graceful shutdown
def signal_handler(signum, frame):
nonlocal shutdown_requested, simulator, stop_watcher_event, signal_count
signal_count += 1
# Force exit on third signal
if signal_count >= 3:
logging.critical("Received 3 signals, forcing immediate exit")
sys.exit(1)
# Prevent multiple signal handling
if shutdown_requested:
logging.warning(f"Already shutting down (signal {signal_count}/3)")
return
sig_name = signal.Signals(signum).name if hasattr(signal, "Signals") else signum
logging.info(f"Received {sig_name} signal, initiating graceful shutdown...")
shutdown_requested = True
# Stop components
if simulator:
try:
simulator.stop()
except Exception as e:
logging.exception(f"Error stopping simulator: {e}")
if stop_watcher_event:
try:
stop_watcher_event.set()
except Exception as e:
logging.exception(f"Error stopping watchers: {e}")
# Raise KeyboardInterrupt to break out of blocking calls
if signal_count == 1:
msg = "Signal received"
raise KeyboardInterrupt(msg)
# Register signal handlers
signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGINT, signal_handler)
try:
# Load initial configuration
try:
initial_config = load_config(config_file_path)
except Exception as e:
# load_config logs, but print for early exit before logger is fully set
print(
f"Critical: Failed to load initial configuration from {config_file_path}. Exiting. Error: {e}",
file=sys.stderr,
)
sys.exit(1)
# Set up logging as early as possible after getting config
setup_logging(initial_config) # Uses the loaded config
# Load initial raw identity data
try:
raw_identity = load_identity(identity_file_path)
except Exception as e:
logging.exception(
f"Failed to load initial identity from {identity_file_path}. Exiting. Error: {e}"
)
sys.exit(1)
# Process initial identity, handle location, and generate ID if needed
try:
initial_identity = process_identity_and_location(raw_identity, initial_config)
except (ValueError, RuntimeError) as e: # Catch errors from processing
logging.exception(
f"Failed to process initial identity or generate ID. Exiting. Error: {e}"
)
sys.exit(1)