Skip to content

Commit

Permalink
Add subcommand read, for acquiring and displaying a reading on STDOUT
Browse files Browse the repository at this point in the history
... in JSON format.
  • Loading branch information
amotl committed Apr 19, 2024
1 parent 208b5ae commit d058e0b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
The data logger uses a YAML file now, for example like `etc/mois.yaml`.
- Added subcommand `make-config`, for creating a configuration blueprint
- Added subcommand `make-dashboard`, for creating a Grafana Dashboard
- Added subcommand `read`, for acquiring and displaying a reading on STDOUT

## v0.0.2 - 2024-04-15
- Publish as `ds18b20-datalogger` package
Expand Down
15 changes: 13 additions & 2 deletions ds18b20_datalogger/cli.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import sys
from pathlib import Path

Expand All @@ -11,23 +12,33 @@


def main():

# Sanity checks.
if not sys.argv[1:]:
raise ValueError("Program needs a subcommand")
subcommand = sys.argv[1]
if subcommand == "run":

# Evaluate subcommand.
if subcommand in ["run", "read"]:
if not sys.argv[2:]:
raise ValueError("Program needs a configuration file")
configfile = Path(sys.argv[2])
if not configfile.exists():
raise ValueError(f"Configuration file does not exist: {configfile}")
settings = Settings.from_file(configfile)
reading = read_ds18b20_sensor_matrix(settings.devicemap)
send_measurement_mqtt(settings.mqtt, reading)
if subcommand == "read":
print(json.dumps(reading.to_dict(), indent=2)) # noqa: T201
elif subcommand == "run":
send_measurement_mqtt(settings.mqtt, reading)

elif subcommand == "make-config":
config_template = files("ds18b20_datalogger") / "datalogger.yaml"
print(config_template.read_text(), file=sys.stdout) # noqa: T201

elif subcommand == "make-dashboard":
dashboard = files("ds18b20_datalogger") / "grafana-dashboard.json"
print(dashboard.read_text(), file=sys.stdout) # noqa: T201

else:
raise ValueError(f"Subcommand unknown: {subcommand}")
21 changes: 20 additions & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import shlex
import subprocess
import typing as t

Expand All @@ -19,10 +20,28 @@ def test_cli_unknown_subcommand():
assert "Subcommand unknown: foo" in output


def test_cli_run_no_config():
def test_cli_run_read_no_config():
exitcode, output = invoke("ds18b20-datalogger run")
assert "Program needs a configuration file" in output

exitcode, output = invoke("ds18b20-datalogger read")
assert "Program needs a configuration file" in output


def test_cli_read_success():
command = shlex.split("ds18b20-datalogger read ds18b20_datalogger/datalogger.yaml")
process = subprocess.run(command, stdout=subprocess.PIPE) # noqa: S603
reading = json.loads(process.stdout)
assert "temp-ir-1-1" in reading
assert "temp-ir-2-3" in reading
assert len(reading) == 6


def test_cli_run_almost_success():
exitcode, output = invoke("ds18b20-datalogger run ds18b20_datalogger/datalogger.yaml")
assert exitcode == 1
assert "nodename nor servname provided, or not known" in output


def test_cli_make_config():
exitcode, output = invoke("ds18b20-datalogger make-config")
Expand Down

0 comments on commit d058e0b

Please sign in to comment.