From 760585f9a62792381d0f8c15265caee57028a92b Mon Sep 17 00:00:00 2001 From: Pedro Date: Mon, 6 Nov 2023 21:44:36 +0100 Subject: [PATCH] Add endpoints GET /devices/audio and GET /devices/serial --- modem/serial_ports.py | 19 +++++++++++++++++++ modem/server.py | 14 +++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 modem/serial_ports.py diff --git a/modem/serial_ports.py b/modem/serial_ports.py new file mode 100644 index 00000000..e4151a81 --- /dev/null +++ b/modem/serial_ports.py @@ -0,0 +1,19 @@ +import serial.tools.list_ports +import crcengine + +def get_ports(): + crc_algorithm = crcengine.new("crc16-ccitt-false") # load crc8 library + + serial_devices = [] + ports = serial.tools.list_ports.comports() + print(ports) + for port, desc, hwid in ports: + # calculate hex of hwid if we have unique names + crc_hwid = crc_algorithm(bytes(hwid, encoding="utf-8")) + crc_hwid = crc_hwid.to_bytes(2, byteorder="big") + crc_hwid = crc_hwid.hex() + description = f"{desc} [{crc_hwid}]" + serial_devices.append( + {"port": str(port), "description": str(description)} + ) + return serial_devices diff --git a/modem/server.py b/modem/server.py index 1547910f..131acf9c 100644 --- a/modem/server.py +++ b/modem/server.py @@ -1,8 +1,9 @@ from flask import Flask, request, jsonify from flask_sock import Sock import os -import json +import serial_ports from config import CONFIG +import audio app = Flask(__name__) sock = Sock(app) @@ -55,6 +56,17 @@ def config(): elif request.method == 'GET': return api_response(app.config_manager.read()) +@app.route('/devices/audio', methods=['GET']) +def get_audio_devices(): + dev_in, dev_out = audio.get_audio_devices() + devices = { 'in': dev_in, 'out': dev_out } + return api_response(devices) + +@app.route('/devices/serial', methods=['GET']) +def get_serial_devices(): + devices = serial_ports.get_ports() + return api_response(devices) + # Event websocket @sock.route('/events') def echo(sock):