52 lines
2.5 KiB
Python
52 lines
2.5 KiB
Python
import asyncio
|
|
import json
|
|
import os
|
|
import ssl
|
|
import sys
|
|
import wave
|
|
from pathlib import Path
|
|
|
|
import websockets
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT))
|
|
from protocol import pack, unpack
|
|
|
|
|
|
async def main():
|
|
ssl_context = ssl.create_default_context(cafile=str(ROOT / '.runtime/gateway-cert.pem'))
|
|
with wave.open(str(ROOT / 'tmp/public_tts_16k.wav')) as f: pcm = f.readframes(f.getnframes())
|
|
pcm += bytes(32000)
|
|
events = []; frames = 0; turn = 0; seq = 0; stopped = asyncio.Event()
|
|
async with websockets.connect('wss://192.168.31.61:5011/ws/dialogue', ssl=ssl_context, origin='https://192.168.31.61:5011', proxy=None) as ws:
|
|
await ws.send(json.dumps({'type': 'auth', 'token': os.environ['LAN_GATEWAY_TOKEN']})); await ws.recv()
|
|
await ws.send(json.dumps({'type': 'hello', 'codec': 'pcm16', 'sample_rate': 16000, 'frame_duration': 20})); await ws.recv()
|
|
await ws.send(json.dumps({'type': 'listen', 'mode': 'auto', 'state': 'start'}))
|
|
turn = json.loads(await ws.recv())['turn_id']; await ws.recv()
|
|
async def receive():
|
|
nonlocal turn, seq, frames
|
|
while True:
|
|
raw = await ws.recv()
|
|
if isinstance(raw, bytes):
|
|
actual, _, _ = unpack(raw, 2); assert actual == turn; frames += 1
|
|
else:
|
|
event = json.loads(raw); events.append(event)
|
|
if event['type'] == 'abort': turn = event['turn_id']; seq = 0
|
|
if event['type'] == 'error': raise AssertionError(event)
|
|
if event['type'] == 'tts' and event['state'] == 'stop': stopped.set(); return
|
|
task = asyncio.create_task(receive())
|
|
for i in range(0, len(pcm), 640):
|
|
if any(e['type'] == 'capture.end' for e in events): break
|
|
seq += 1; await ws.send(pack(1, turn, seq, pcm[i:i+640])); await asyncio.sleep(.02)
|
|
await asyncio.wait_for(task, 60)
|
|
assert frames > 0
|
|
await ws.send(json.dumps({'type':'playback.drained','turn_id':turn}))
|
|
ack = json.loads(await ws.recv()); assert ack['state'] == 'resumed'
|
|
await ws.send(json.dumps({'type':'abort'})); await ws.recv()
|
|
result = {'status':'passed','mode':'auto','downlink_frames':frames,'drain_ack':'passed','events':events}
|
|
(ROOT/'tmp/auto_integration.json').write_text(json.dumps(result,ensure_ascii=False,indent=2)+'\n')
|
|
print(json.dumps(result,ensure_ascii=False))
|
|
|
|
|
|
if __name__=='__main__': asyncio.run(main())
|