переименовал файл

This commit is contained in:
zakhy 2024-11-01 11:58:21 +07:00
parent 3416b27eee
commit be21dc963d
2 changed files with 3 additions and 101 deletions

View File

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

View File

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