121 lines
4.3 KiB
Python
121 lines
4.3 KiB
Python
import asyncio
|
|
import datetime
|
|
import os
|
|
|
|
import django
|
|
import websockets
|
|
import json
|
|
|
|
from asgiref.sync import sync_to_async
|
|
from django.db import transaction
|
|
|
|
|
|
# hello()
|
|
def all_comand():
|
|
commands = ['[{"identifier": "attachment:0/initialization_request","payload": {}}]',
|
|
'[{"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": "dgna:0/initialization_request","payload": {}}]',
|
|
'[{"identifier": "server_objects:0/initialization_request","payload": {}}]',
|
|
'[{"identifier": "location:0/initialization_request","payload": {}}]',
|
|
'[{"identifier": "text:1/initialization_request","payload": {}}]',
|
|
'[{"identifier": "command:0/initialization_request","payload": {}}]',
|
|
'[{"identifier": "server_objects:0/state_broadcast","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(datetime.datetime.now())
|
|
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
|
|
|
|
|
|
def send_mess(websocket, user_input):
|
|
websocket.send(user_input)
|
|
|
|
|
|
async def save_location(payload):
|
|
# Асинхронный вызов синхронного метода ORM через sync_to_async
|
|
await create_or_update_location(payload)
|
|
|
|
|
|
@sync_to_async
|
|
def get_settings_from_db():
|
|
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', '')
|
|
|
|
return {
|
|
'radiusip_host_ws': radiusip_host_ws,
|
|
'radiusip_port_ws': radiusip_port_ws,
|
|
'radiusip_api_key': radiusip_api_key,
|
|
'radiusip_login': radiusip_login,
|
|
'radiusip_password': 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())
|