diff --git a/Dockerfile b/Dockerfile index 855828e..7abd491 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,11 +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 gunicorn sqlite3 python3-pip python3-requests python3-yaml python3-flask python3-serial udev +RUN apt-get update && apt-get -y install gunicorn sqlite3 python3-pip python3-requests python3-yaml python3-flask python3-serial ENV PIP_BREAK_SYSTEM_PACKAGES 1 RUN pip install flask-restx==1.3.0 WORKDIR /root -COPY find_ttyUSB.sh /root/ COPY relay.py /root/ COPY thermostat.py /root/ ENV FLASK_APP thermostat.py 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/relay.py b/relay.py index 8612994..6464343 100755 --- a/relay.py +++ b/relay.py @@ -2,6 +2,7 @@ # Pilotage GCE USB 8 Relay Board import time import serial +import serial.tools.list_ports import argparse import sys import subprocess @@ -34,17 +35,24 @@ else: #print(relays) -find_ttyUSB_output = subprocess.check_output(["./find_ttyUSB.sh"]) -device = None -for line in find_ttyUSB_output.decode('utf-8').split("\n"): - if "FTDI_FT232R_USB_UART_A50285BI" in line: - device = line.split(" ")[0] - break +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 ("Relay board not found.") - sys.exit(1) +def find_serial_ports(): + ports = serial.tools.list_ports.comports() + for port, desc, hwid in sorted(ports): + if "FT232R USB UART" in desc: + return port +port = find_serial_ports() +if port is None: + print("Unable to find serial port for relay board please use one of the following") + list_serial_ports() + sys.exit(2) def init_serial(pPort): global ser @@ -67,7 +75,7 @@ def init_serial(pPort): print('Could not connect to ' + ser.portstr) sys.exit(1) -init_serial(device) +init_serial(port) if action != 'status': for relay in relays: command = relay+action+'\r\n'