diff --git a/Dockerfile b/Dockerfile index c3a8814..b0e4787 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,13 +2,10 @@ FROM debian:bookworm MAINTAINER yohan <783b8c87@scimetis.net> ENV DEBIAN_FRONTEND noninteractive ENV TZ Europe/Paris -RUN apt-get update && apt-get -y install curl gunicorn sqlite3 python3-pip python3-requests python3-yaml python3-flask python3-serial udev ow-shell; rm -rf /var/lib/apt/lists/* -ENV PIP_BREAK_SYSTEM_PACKAGES 1 -RUN pip install teleinfo +RUN apt-get update && apt-get -y install curl gunicorn sqlite3 python3-requests python3-yaml python3-flask python3-serial ow-shell; rm -rf /var/lib/apt/lists/* WORKDIR /root RUN curl -s -o YoctoLib.cmdline.24497.tar.gz "https://cloud.scimetis.net/s/y7WengZWJpYnGpr/download" RUN tar xzvf YoctoLib.cmdline.24497.tar.gz -COPY find_ttyUSB.sh /root/ COPY read_one-wire_sensor.py /root/ COPY read_teleinfo.py /root/ COPY read_yocto_sensor.py /root/ diff --git a/find_ttyUSB.sh b/find_ttyUSB.sh deleted file mode 100755 index a224fe3..0000000 --- a/find_ttyUSB.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/bash - -for sysdevpath in $(find /sys/bus/usb/devices/usb*/ -name dev); do - syspath="${sysdevpath%/dev}" - devname="$(udevadm info -q name -p $syspath)" - [[ "$devname" == "ttyUSB"* ]] || continue - eval "$(udevadm info -q property --export -p $syspath)" - [[ -z "$ID_SERIAL" ]] && exit - echo "/dev/$devname - $ID_SERIAL" -done diff --git a/read_teleinfo.py b/read_teleinfo.py index a4e6d07..024c15b 100755 --- a/read_teleinfo.py +++ b/read_teleinfo.py @@ -8,25 +8,80 @@ import json from teleinfo import Parser from teleinfo.hw_vendors import UTInfo2 import subprocess +import sys +import serial +import serial.tools.list_ports parser = argparse.ArgumentParser(description='Téléinfo retriever.') parser.add_argument("-f", "--format", help="Output format.", type=str, choices=['human-readable', 'raw_json', 'custom_json'], default='human-readable') args = parser.parse_args() -find_ttyUSB_output = subprocess.check_output(["./find_ttyUSB.sh"]) -device = None -for line in find_ttyUSB_output.decode('utf-8').split("\n"): - if "Cartelectronic_Interface_USB_1_TIC_DA6TLO1X" in line: - device = line.split(" ")[0] - break +baudrate = 1200 +port = None + +def list_serial_ports(): + ports = serial.tools.list_ports.comports() + for port, desc, hwid in sorted(ports): + print("{}: {} [{}]".format(port, desc, hwid)) -if device is None: - print("USB teleinfo device not found.") - sys.exit(1) +def find_serial_ports(): + ports = serial.tools.list_ports.comports() + for port, desc, hwid in sorted(ports): + if "Interface USB 1 TIC" in desc: + return port -ti = Parser(UTInfo2(port=device)) -res = ti.get_frame() +port = find_serial_ports() +if port is None: + print("Unable to find serial port for Teleinfo please use one of the following") + list_serial_ports() + sys.exit(2) + +tinfo = serial.Serial( port=port, + baudrate=baudrate, + parity=serial.PARITY_EVEN, + stopbits=serial.STOPBITS_ONE, + bytesize=serial.SEVENBITS ) + +# This can be used to detect overload warning frames +#while True: +# c = tinfo.read(1) +# #print(c, end=''); +# sys.stdout.write(c.decode('utf-8')) + +for x in range(16): + tinfo.read(1) + +while tinfo.read(1)[0]!=0x03: + pass + +lines = [] +fullframe = False +line = "" +# loop thru serial +while fullframe==False: + c = tinfo.read(1) + if c[0]==0x02: + # Startframe character + pass + elif c[0]==0x03: + # Endframe character + fullframe = True + elif c[0]==0x09: + line += " " + elif c[0]==0x0d: + # Endline character + lines.append(line) + elif c[0]==0x0a: + # Startline character + line = "" + else: + line += c.decode("ascii") + +res = dict() +for line in lines: + l = line.split(' ') + res[l[0]] = l[1] if args.format == 'human-readable': print("Puissance apparente compteur : "+str(int(res['PAPP']))+"VA") @@ -44,5 +99,4 @@ elif args.format == 'custom_json': data['Modane_elec_main_power'] = int(res['PAPP']) data['Modane_elec_energy_index'] = int(res['BASE']) print(json.dumps(data)) -#for frame in ti: -# print(frame) +