use asyncio for the multicast client
This commit is contained in:
+2
-1
@@ -10,7 +10,8 @@ dependencies = [
|
||||
"aioconsole",
|
||||
"fastapi==0.115.11",
|
||||
"uvicorn==0.34.0",
|
||||
"pydantic"
|
||||
"pydantic",
|
||||
"aiohttp==3.9.3"
|
||||
]
|
||||
|
||||
[project.optional-dependencies]
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import requests
|
||||
import aiohttp
|
||||
import asyncio
|
||||
|
||||
from typing import Optional, Dict
|
||||
from auracast import auracast_config
|
||||
@@ -8,65 +9,74 @@ from auracast.auracast_config import AuracastConfigGroup
|
||||
BASE_URL = "http://127.0.0.1:5000" # Default base URL
|
||||
|
||||
|
||||
def init(request_data : AuracastConfigGroup, base_url: Optional[str] = None):
|
||||
async def init(request_data : AuracastConfigGroup, base_url: Optional[str] = None):
|
||||
url = base_url if base_url is not None else BASE_URL
|
||||
response = requests.post(f"{url}/init", json=request_data.model_dump())
|
||||
if response.status_code != 200:
|
||||
raise Exception(f"Error: {response.status_code}, {response.text}")
|
||||
return response.json()
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.post(f"{url}/init", json=request_data.model_dump()) as response:
|
||||
if response.status != 200:
|
||||
raise Exception(f"Error: {response.status}, {await response.text()}")
|
||||
return await response.json()
|
||||
|
||||
|
||||
def send_audio(data_dict: Dict[str, str], base_url: Optional[str] = None):
|
||||
async def send_audio(data_dict: Dict[str, str], base_url: Optional[str] = None):
|
||||
url = base_url if base_url is not None else BASE_URL
|
||||
response = requests.post(f"{url}/stream_lc3", json=data_dict)
|
||||
if response.status_code != 200:
|
||||
raise Exception(f"Error: {response.status_code}, {response.text}")
|
||||
return response.json()
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.post(f"{url}/stream_lc3", json=data_dict) as response:
|
||||
if response.status != 200:
|
||||
raise Exception(f"Error: {response.status}, {await response.text()}")
|
||||
return await response.json()
|
||||
|
||||
|
||||
def shutdown(base_url: Optional[str] = None):
|
||||
async def shutdown(base_url: Optional[str] = None):
|
||||
url = base_url if base_url is not None else BASE_URL
|
||||
response = requests.post(f"{url}/shutdown")
|
||||
if response.status_code != 200:
|
||||
raise Exception(f"Error: {response.status_code}, {response.text}")
|
||||
return response.json()
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.post(f"{url}/shutdown") as response:
|
||||
if response.status != 200:
|
||||
raise Exception(f"Error: {response.status}, {await response.text()}")
|
||||
return await response.json()
|
||||
|
||||
|
||||
def stop_audio(base_url: Optional[str] = None):
|
||||
async def stop_audio(base_url: Optional[str] = None):
|
||||
url = base_url if base_url is not None else BASE_URL
|
||||
response = requests.post(f"{url}/stop_audio")
|
||||
if response.status_code != 200:
|
||||
raise Exception(f"Error: {response.status_code}, {response.text}")
|
||||
return response.json()
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.post(f"{url}/stop_audio") as response:
|
||||
if response.status != 200:
|
||||
raise Exception(f"Error: {response.status}, {await response.text()}")
|
||||
return await response.json()
|
||||
|
||||
|
||||
def get_status(base_url: Optional[str] = None):
|
||||
async def get_status(base_url: Optional[str] = None):
|
||||
url = base_url if base_url is not None else BASE_URL
|
||||
response = requests.get(f"{url}/status")
|
||||
if response.status_code != 200:
|
||||
raise Exception(f"Error: {response.status_code}, {response.text}")
|
||||
return response.json()
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.get(f"{url}/status") as response:
|
||||
if response.status != 200:
|
||||
raise Exception(f"Error: {response.status}, {await response.text()}")
|
||||
return await response.json()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
config = AuracastConfigGroup(
|
||||
auracast_config.AuracastBigConfigDeu()
|
||||
)
|
||||
config.transport = 'serial:/dev/serial/by-id/usb-ZEPHYR_Zephyr_HCI_UART_sample_81BD14B8D71B5662-if00,115200,rtscts'
|
||||
import asyncio
|
||||
|
||||
# Initialize language-based configurations
|
||||
for conf in config.bigs:
|
||||
conf.loop = False
|
||||
async def main():
|
||||
config = AuracastConfigGroup(
|
||||
auracast_config.AuracastBigConfigDeu()
|
||||
)
|
||||
config.transport = 'serial:/dev/serial/by-id/usb-ZEPHYR_Zephyr_HCI_UART_sample_81BD14B8D71B5662-if00,115200,rtscts'
|
||||
|
||||
# Initialize language-based configurations
|
||||
for conf in config.bigs:
|
||||
conf.loop = False
|
||||
|
||||
audio_data = { # TODO: investigate further whats the best way to actually transfer the data
|
||||
"deu": read_lc3_file('src/auracast/testdata/announcement_de_10_16_32.lc3').decode('latin-1'),
|
||||
#"eng": read_lc3_file('src/auracast/testdata/announcement_en_10_16_32.lc3').decode('latin-1'),
|
||||
#"fra": read_lc3_file('src/auracast/testdata/announcement_fr_10_16_32.lc3').decode('latin-1'),
|
||||
}
|
||||
audio_data = { # TODO: investigate further whats the best way to actually transfer the data
|
||||
"deu": read_lc3_file('src/auracast/testdata/announcement_de_10_16_32.lc3').decode('latin-1'),
|
||||
#"eng": read_lc3_file('src/auracast/testdata/announcement_en_10_16_32.lc3').decode('latin-1'),
|
||||
#"fra": read_lc3_file('src/auracast/testdata/announcement_fr_10_16_32.lc3').decode('latin-1'),
|
||||
}
|
||||
|
||||
print("Getting status:", get_status())
|
||||
print("Initializing server:", init(config))
|
||||
print("Getting status:", get_status())
|
||||
print("Sending audio:", send_audio(audio_data))
|
||||
print("Getting status:", get_status())
|
||||
print("Getting status:", await get_status())
|
||||
print("Initializing server:", await init(config))
|
||||
print("Getting status:", await get_status())
|
||||
print("Sending audio:", await send_audio(audio_data))
|
||||
print("Getting status:", await get_status())
|
||||
|
||||
asyncio.run(main())
|
||||
|
||||
Reference in New Issue
Block a user