Add endpoints GET /devices/audio and GET /devices/serial

This commit is contained in:
Pedro 2023-11-06 21:44:36 +01:00
parent 3b5131d63e
commit 760585f9a6
2 changed files with 32 additions and 1 deletions

19
modem/serial_ports.py Normal file
View file

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

View file

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