Fix teleinfo for Python3.

This commit is contained in:
yohan 2024-06-09 22:36:43 +02:00
parent a64ca97947
commit 35d74c0833
3 changed files with 68 additions and 27 deletions

View File

@ -2,13 +2,10 @@ FROM debian:bookworm
MAINTAINER yohan <783b8c87@scimetis.net> MAINTAINER yohan <783b8c87@scimetis.net>
ENV DEBIAN_FRONTEND noninteractive ENV DEBIAN_FRONTEND noninteractive
ENV TZ Europe/Paris 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/* 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/*
ENV PIP_BREAK_SYSTEM_PACKAGES 1
RUN pip install teleinfo
WORKDIR /root WORKDIR /root
RUN curl -s -o YoctoLib.cmdline.24497.tar.gz "https://cloud.scimetis.net/s/y7WengZWJpYnGpr/download" RUN curl -s -o YoctoLib.cmdline.24497.tar.gz "https://cloud.scimetis.net/s/y7WengZWJpYnGpr/download"
RUN tar xzvf YoctoLib.cmdline.24497.tar.gz RUN tar xzvf YoctoLib.cmdline.24497.tar.gz
COPY find_ttyUSB.sh /root/
COPY read_one-wire_sensor.py /root/ COPY read_one-wire_sensor.py /root/
COPY read_teleinfo.py /root/ COPY read_teleinfo.py /root/
COPY read_yocto_sensor.py /root/ COPY read_yocto_sensor.py /root/

View File

@ -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

View File

@ -8,25 +8,80 @@ import json
from teleinfo import Parser from teleinfo import Parser
from teleinfo.hw_vendors import UTInfo2 from teleinfo.hw_vendors import UTInfo2
import subprocess import subprocess
import sys
import serial
import serial.tools.list_ports
parser = argparse.ArgumentParser(description='Téléinfo retriever.') parser = argparse.ArgumentParser(description='Téléinfo retriever.')
parser.add_argument("-f", "--format", help="Output format.", parser.add_argument("-f", "--format", help="Output format.",
type=str, choices=['human-readable', 'raw_json', 'custom_json'], default='human-readable') type=str, choices=['human-readable', 'raw_json', 'custom_json'], default='human-readable')
args = parser.parse_args() args = parser.parse_args()
find_ttyUSB_output = subprocess.check_output(["./find_ttyUSB.sh"]) baudrate = 1200
device = None port = 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
if device is None: def list_serial_ports():
print("USB teleinfo device not found.") ports = serial.tools.list_ports.comports()
sys.exit(1) for port, desc, hwid in sorted(ports):
print("{}: {} [{}]".format(port, desc, hwid))
ti = Parser(UTInfo2(port=device)) def find_serial_ports():
res = ti.get_frame() ports = serial.tools.list_ports.comports()
for port, desc, hwid in sorted(ports):
if "Interface USB 1 TIC" in desc:
return port
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': if args.format == 'human-readable':
print("Puissance apparente compteur : "+str(int(res['PAPP']))+"VA") 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_main_power'] = int(res['PAPP'])
data['Modane_elec_energy_index'] = int(res['BASE']) data['Modane_elec_energy_index'] = int(res['BASE'])
print(json.dumps(data)) print(json.dumps(data))
#for frame in ti:
# print(frame)