docker-sensors-polling/read_one-wire_sensor.py

35 lines
880 B
Python
Raw Normal View History

2024-02-04 23:43:58 +00:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import argparse
import json
import subprocess
import sys
parser = argparse.ArgumentParser(description='Read one-wire sensor.')
parser.add_argument('metric', type=str,
help='Metric name.')
parser.add_argument('path', type=str,
help='owfs sensor path.')
args = parser.parse_args()
try:
returned_output = subprocess.check_output(["/usr/bin/owread", "-s", "localhost:4304", args.path])
try:
value = round(float(returned_output.decode("utf-8").strip().strip("'")), 1)
except ValueError:
2024-02-05 21:57:52 +00:00
print ("Got garbage: "+returned_output)
2024-02-04 23:43:58 +00:00
sys.exit(1)
except Exception as e:
print(e)
sys.exit(1)
data = {}
2024-02-05 21:57:52 +00:00
if "luminosity" in args.metric:
# conversion en lm
data[args.metric] = int(float(value) * 122900.45063498567)
else:
data[args.metric] = value
2024-02-04 23:43:58 +00:00
print(json.dumps(data))