# Path: labshare/consumers.py
# class GPUInfoUpdater(WebsocketConsumer):
#     def connect(self):
#         self.user = self.scope['user']
#         self.device_name = self.scope['url_route']['kwargs']['device_name']
# 
#         device = Device.objects.get(name=self.device_name)
#         if device.can_be_used_by(self.user):
#             async_to_sync(self.channel_layer.group_add)(
#                 self.device_name,
#                 self.channel_name,
#             )
# 
#             publish_device_state(device.serialize(), self.channel_name)
# 
#             self.accept()
#         else:
#             self.close()
# 
#     def disconnect(self, message, **kwargs):
#         async_to_sync(self.channel_layer.group_discard)(
#             self.device_name,
#             self.channel_name,
#         )
# 
#     def update_info(self, event):
#         self.send(text_data=event['message'])


from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from django.urls import path
from labshare.consumers import GPUInfoUpdater



websocket_urlpatterns = [
    path('ws/device/<device_name>/', GPUInfoUpdater),
]


application = ProtocolTypeRouter({
    'websocket': AuthMiddlewareStack(
        URLRouter(
            websocket_urlpatterns

# Path: labshare/consumers.py
# class GPUInfoUpdater(WebsocketConsumer):
#     def connect(self):
#         self.user = self.scope['user']
#         self.device_name = self.scope['url_route']['kwargs']['device_name']
# 
#         device = Device.objects.get(name=self.device_name)
#         if device.can_be_used_by(self.user):
#             async_to_sync(self.channel_layer.group_add)(
#                 self.device_name,
#                 self.channel_name,
#             )
# 
#             publish_device_state(device.serialize(), self.channel_name)
# 
#             self.accept()
#         else:
#             self.close()
# 
#     def disconnect(self, message, **kwargs):
#         async_to_sync(self.channel_layer.group_discard)(
#             self.device_name,
#             self.channel_name,
#         )
# 
#     def update_info(self, event):
#         self.send(text_data=event['message'])


# Path: labshare/routing.py
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from django.urls import path
from labshare.consumers import GPUInfoUpdater




websocket_urlpatterns = [
    path('ws/device/<device_name>/', GPUInfoUpdater),
]


application = ProtocolTypeRouter({
    'websocket': AuthMiddlewareStack(
        URLRouter(
            websocket_urlpatterns

        )