From be21dc963d869d1b026fae5cb79749ceb5215c03 Mon Sep 17 00:00:00 2001 From: zakhy Date: Fri, 1 Nov 2024 11:58:21 +0700 Subject: [PATCH] =?UTF-8?q?=D0=BF=D0=B5=D1=80=D0=B5=D0=B8=D0=BC=D0=B5?= =?UTF-8?q?=D0=BD=D0=BE=D0=B2=D0=B0=D0=BB=20=D1=84=D0=B0=D0=B9=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...{transmit_file_to_radio.py => radiusip.py} | 3 + .../commands/start_pooling_ws.py | 101 ------------------ 2 files changed, 3 insertions(+), 101 deletions(-) rename apps/workerslocation/commands/{transmit_file_to_radio.py => radiusip.py} (99%) delete mode 100644 apps/workerslocation/commands/start_pooling_ws.py diff --git a/apps/workerslocation/commands/transmit_file_to_radio.py b/apps/workerslocation/commands/radiusip.py similarity index 99% rename from apps/workerslocation/commands/transmit_file_to_radio.py rename to apps/workerslocation/commands/radiusip.py index 63b9a7a..3b2591b 100644 --- a/apps/workerslocation/commands/transmit_file_to_radio.py +++ b/apps/workerslocation/commands/radiusip.py @@ -87,13 +87,16 @@ async def receive_messages(websocket, auth_dict): logging.log(f"Соединение установлено. Инициализация модулей...") await send_messages(websocket, get_call_initialize_command()) await send_messages(websocket, get_location_initialize_command()) + if identifier == 'call:1/initialization_response': call_mapi_initialized = True ssrc = message_item['payload'].get('voip_ssrc', SSRC) voip_port = message_item['payload'].get('voip_port', API_PORT) + elif identifier == 'location:0/position_broadcast': logging.log(message_item['payload']) await save_location(message_item['payload']) + elif call_mapi_initialized and voice_call: logging.log("Запрос на отправку аудио. Инициализация трансляции трансляцию...") await set_voice_call_sent(voice_call) diff --git a/apps/workerslocation/commands/start_pooling_ws.py b/apps/workerslocation/commands/start_pooling_ws.py deleted file mode 100644 index 850573a..0000000 --- a/apps/workerslocation/commands/start_pooling_ws.py +++ /dev/null @@ -1,101 +0,0 @@ -import asyncio -import os - -import django -import websockets -import json - -from asgiref.sync import sync_to_async - - -def all_comand(): - commands = [ - '[{"identifier": "call:1/initialization_request","payload": {"voip_port": 54595,"supported_codecs": [{"codec_id": 8,"name": "pcma/8000","packet_duration": 20,"sampling_rate": 8000}],"is_supported_webrtc": false}}]', - '[{"identifier": "location:0/initialization_request","payload": {}}]', - ] - return commands - - -async def receive_messages(websocket): - while True: - try: - message = await websocket.recv() - data = json.loads(message) - identifier = data[0]['identifier'] - if data[0]['identifier'] == 'core/connection_response': - commands = all_comand() - for command in commands: - asyncio.create_task(send_messages(websocket, command)) - print(f"<<< {command}") - if identifier.find('monitoring_frame') == -1: - print(">", data[0]) - if 'location:0/position_broadcast' in identifier: - await save_location(data[0]['payload']) - print("*********************************************************************************") - except websockets.exceptions.ConnectionClosed: - break - - -async def send_messages(websocket, user_input): - # while True: - await websocket.send(user_input) - await asyncio.sleep(0) # передача управления циклу event - - -async def save_location(payload): - await create_or_update_location(payload) - - -@sync_to_async -def get_settings_from_db(): - return { - 'radiusip_host_ws': get_settings('radiusip_host_ws', ''), - 'radiusip_port_ws': get_settings('radiusip_port_ws', ''), - 'radiusip_api_key': get_settings('radiusip_api_key', ''), - 'radiusip_login': get_settings('radiusip_login', ''), - 'radiusip_password': get_settings('radiusip_password', '') - } - - -@sync_to_async -def create_or_update_location(data): - WorkersLocation.objects.update_or_create( - abonent_id=data['abonent_id'], - defaults={ - 'latitude': data['latitude'], - 'longitude': data['longitude'], - 'velocity': data['velocity'], - 'radius': data['radius'], - 'update_time': data['update_time'], - } - ) - - -async def main(): - radiusip_auth_dict = await get_settings_from_db() - uri = f"ws://{radiusip_auth_dict['radiusip_host_ws']}:{radiusip_auth_dict['radiusip_port_ws']}/" - message = [ - { - 'identifier': 'core/connection_request', - 'payload': { - 'login': radiusip_auth_dict['radiusip_login'], - 'password': radiusip_auth_dict['radiusip_password'], - 'api_key': radiusip_auth_dict['radiusip_api_key'], - 'token': None, - } - } - ] - async with websockets.connect(uri) as websocket: - receive_task = asyncio.create_task(receive_messages(websocket)) - asyncio.create_task(send_messages(websocket, json.dumps(message))) - - await asyncio.gather(receive_task) - - -if __name__ == '__main__': - os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mfssogr.settings') - django.setup() - from apps.workerslocation.models.workers_locations import WorkersLocation - from apps.general.views.settingsmodule import get_settings - - asyncio.run(main())