Add data_logging/src/data_logging/cli.py
This commit is contained in:
parent
356b7b17f1
commit
78eb94e173
1 changed files with 78 additions and 0 deletions
78
data_logging/src/data_logging/cli.py
Normal file
78
data_logging/src/data_logging/cli.py
Normal file
|
|
@ -0,0 +1,78 @@
|
||||||
|
import argparse
|
||||||
|
import time
|
||||||
|
import sys
|
||||||
|
from datetime import datetime
|
||||||
|
from pathlib import Path
|
||||||
|
import os
|
||||||
|
from data_logging.core import log_data
|
||||||
|
|
||||||
|
|
||||||
|
def parse_args() -> argparse.Namespace:
|
||||||
|
"""Parses command line arguments for interval and output path."""
|
||||||
|
parser = argparse.ArgumentParser(description="Donau2Space Environmental Data Logger CLI")
|
||||||
|
parser.add_argument(
|
||||||
|
"--interval",
|
||||||
|
type=int,
|
||||||
|
required=True,
|
||||||
|
help="Measurement interval in seconds",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--output",
|
||||||
|
type=str,
|
||||||
|
required=True,
|
||||||
|
help="Path to output JSON file",
|
||||||
|
)
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
if args.interval <= 0:
|
||||||
|
parser.error("--interval must be a positive integer")
|
||||||
|
|
||||||
|
output_path = Path(args.output)
|
||||||
|
if not output_path.parent.exists():
|
||||||
|
try:
|
||||||
|
output_path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
except Exception as e:
|
||||||
|
parser.error(f"Failed to create output directory: {e}")
|
||||||
|
|
||||||
|
return args
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
"""Runs the periodic data logging loop."""
|
||||||
|
args = parse_args()
|
||||||
|
output_file = args.output
|
||||||
|
interval = args.interval
|
||||||
|
|
||||||
|
print(f"Starting Donau2Space logger: interval={interval}s, output={output_file}")
|
||||||
|
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
# Simulated sensor reading (replace with actual sensor input in production)
|
||||||
|
temperature_input = input("Enter temperature (°C): ").strip()
|
||||||
|
wind_speed_input = input("Enter wind speed (km/h): ").strip()
|
||||||
|
|
||||||
|
try:
|
||||||
|
temperature = float(temperature_input)
|
||||||
|
wind_speed = float(wind_speed_input)
|
||||||
|
except ValueError:
|
||||||
|
print("Invalid input. Please enter numeric values.")
|
||||||
|
continue
|
||||||
|
|
||||||
|
timestamp = datetime.utcnow()
|
||||||
|
log_data(timestamp, temperature, wind_speed)
|
||||||
|
|
||||||
|
print(f"Logged data at {timestamp.isoformat()} -> T={temperature}°C, W={wind_speed}km/h")
|
||||||
|
time.sleep(interval)
|
||||||
|
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print("\nLogging stopped by user.")
|
||||||
|
break
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error occurred during logging: {e}", file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Loading…
Reference in a new issue