2023-07-01 15:47:28 +08:00
|
|
|
# consumers.py
|
|
|
|
import json
|
|
|
|
from .models import StudentStatus
|
|
|
|
from .serializers import StudentStatusSerializer
|
|
|
|
from djangochannelsrestframework.generics import GenericAsyncAPIConsumer
|
|
|
|
from djangochannelsrestframework.decorators import action
|
|
|
|
from djangochannelsrestframework.observer import model_observer, observer
|
|
|
|
from channels.db import database_sync_to_async
|
|
|
|
import asyncio
|
|
|
|
from djangochannelsrestframework.mixins import (
|
|
|
|
ListModelMixin,
|
|
|
|
RetrieveModelMixin,
|
|
|
|
)
|
2023-07-01 16:09:20 +08:00
|
|
|
from djangochannelsrestframework.permissions import IsAuthenticated
|
2023-09-12 21:36:28 +08:00
|
|
|
from channels.layers import get_channel_layer
|
|
|
|
from asgiref.sync import async_to_sync
|
|
|
|
from django.contrib.gis.measure import Distance
|
|
|
|
from django.contrib.gis.geos import fromstr
|
|
|
|
from .models import StudentStatus
|
|
|
|
from accounts.models import CustomUser
|
2023-07-01 15:47:28 +08:00
|
|
|
|
|
|
|
|
|
|
|
class StudentStatusConsumer(
|
|
|
|
ListModelMixin,
|
|
|
|
RetrieveModelMixin,
|
|
|
|
GenericAsyncAPIConsumer,
|
|
|
|
):
|
2023-07-01 16:09:20 +08:00
|
|
|
permission_classes = [IsAuthenticated]
|
|
|
|
queryset = StudentStatus.objects.filter(active=True)
|
2023-07-01 15:47:28 +08:00
|
|
|
serializer_class = StudentStatusSerializer
|
|
|
|
|
2023-09-12 21:36:28 +08:00
|
|
|
async def send_status_update(self, event):
|
|
|
|
data = event['data']
|
|
|
|
await self.send(text_data=json.dumps(data))
|
|
|
|
|
2023-07-01 15:47:28 +08:00
|
|
|
async def websocket_connect(self, message):
|
2023-09-12 21:36:28 +08:00
|
|
|
await self.channel_layer.group_add('student_status_group', self.channel_name)
|
2023-07-01 15:47:28 +08:00
|
|
|
await self.accept()
|
|
|
|
self.send_updates_task = asyncio.create_task(self.send_updates())
|
|
|
|
|
|
|
|
async def websocket_disconnect(self, message):
|
2023-09-12 21:36:28 +08:00
|
|
|
# ...
|
|
|
|
await self.channel_layer.group_discard('student_status_group', self.channel_name)
|
2023-07-01 15:47:28 +08:00
|
|
|
self.send_updates_task.cancel()
|
|
|
|
|
|
|
|
@database_sync_to_async
|
|
|
|
def get_student_statuses(self):
|
|
|
|
queryset = self.get_queryset()
|
2023-09-12 21:36:28 +08:00
|
|
|
user = self.scope['user']
|
2023-07-01 15:47:28 +08:00
|
|
|
return StudentStatusSerializer(queryset, many=True).data
|
|
|
|
|
|
|
|
async def send_updates(self):
|
2023-09-12 21:36:28 +08:00
|
|
|
channel_layer = get_channel_layer()
|
|
|
|
|
2023-07-01 15:47:28 +08:00
|
|
|
while True:
|
|
|
|
try:
|
2023-09-12 21:36:28 +08:00
|
|
|
print('attempting to get')
|
2023-07-01 15:47:28 +08:00
|
|
|
data = await self.get_student_statuses()
|
2023-09-12 21:36:28 +08:00
|
|
|
print(f"Sending update: {data}")
|
|
|
|
await channel_layer.group_send(
|
|
|
|
'student_status_group',
|
|
|
|
{
|
|
|
|
'type': 'send_status_update',
|
|
|
|
'data': data,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
await asyncio.sleep(3)
|
2023-07-01 15:47:28 +08:00
|
|
|
except Exception as e:
|
|
|
|
print(f"Exception in send_updates: {e}")
|
|
|
|
break # Break the loop on error
|