Add python_sensor_logger/src/python_sensor_logger/cli.py
This commit is contained in:
parent
d385a4c602
commit
5991b59eb8
1 changed files with 45 additions and 0 deletions
45
python_sensor_logger/src/python_sensor_logger/cli.py
Normal file
45
python_sensor_logger/src/python_sensor_logger/cli.py
Normal file
|
|
@ -0,0 +1,45 @@
|
||||||
|
import argparse
|
||||||
|
import time
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
from python_sensor_logger import core
|
||||||
|
|
||||||
|
def _validate_positive_float(value: str) -> float:
|
||||||
|
try:
|
||||||
|
val = float(value)
|
||||||
|
if val <= 0:
|
||||||
|
raise ValueError
|
||||||
|
return val
|
||||||
|
except ValueError:
|
||||||
|
raise argparse.ArgumentTypeError(f"Ungültiger Wert für Intervall: '{value}'. Muss eine positive Zahl sein.")
|
||||||
|
|
||||||
|
def _parse_args() -> argparse.Namespace:
|
||||||
|
parser = argparse.ArgumentParser(description="Thermal Sensor Data Logger")
|
||||||
|
parser.add_argument("--sensor", required=True, help="Sensorbezeichnung oder Schnittstellen-ID")
|
||||||
|
parser.add_argument("--interval", required=False, type=_validate_positive_float, default=5.0, help="Messintervall in Sekunden (Standard: 5s)")
|
||||||
|
parser.add_argument("--output", required=False, default="output/temperature_log.json", help="Pfad zur Ausgabedatei")
|
||||||
|
return parser.parse_args()
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
args = _parse_args()
|
||||||
|
output_path = Path(args.output)
|
||||||
|
output_path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
print(f"Starte Messung mit Sensor '{args.sensor}' alle {args.interval} Sekunden. Ausgabe: {output_path}")
|
||||||
|
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
timestamp = time.time()
|
||||||
|
try:
|
||||||
|
temperature = core.read_temperature()
|
||||||
|
if not isinstance(temperature, (float, int)):
|
||||||
|
raise TypeError("Ungültiger Rückgabewert von read_temperature: erwartet float oder int.")
|
||||||
|
core.log_temperature(timestamp, float(temperature))
|
||||||
|
print(f"{time.strftime('%Y-%m-%d %H:%M:%S')} - Temperatur: {temperature:.2f} °C")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Fehler beim Lesen oder Loggen der Temperatur: {e}", file=sys.stderr)
|
||||||
|
time.sleep(args.interval)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print("\nMessung beendet.")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Loading…
Reference in a new issue