# Path: labshare/models.py
# class Device(models.Model):
#     name = models.CharField(max_length=255)
#     user = models.OneToOneField(User, on_delete=models.CASCADE)
# 
#     class Meta:
#         permissions = (
#             ('use_device', 'User/Group is allowed to use that device'),
#         )
# 
#     def __str__(self):
#         return self.name
# 
#     def can_be_used_by(self, user):
#         content_type = ContentType.objects.get_for_model(self)
#         permission_name = "{app}.use_{model}".format(app=content_type.app_label, model=content_type.model)
#         return user.has_perm(permission_name, self) or user.has_perm(permission_name)
# 
#     def serialize(self):
#         return {
#             'name': self.name,
#         }
# 
# Path: labshare/utils.py
# def publish_device_state(device_data, channel_name=None):
#     channel_layer = channels.layers.get_channel_layer()
#     name = device_data['name']
#     if 'gpus' not in device_data:
#         device_data['gpus'] = []
# 
#     if channel_name is None:
#         send_function = async_to_sync(channel_layer.group_send)
#     else:
#         send_function = async_to_sync(channel_layer.send)
#     send_function(channel_name if channel_name else name, {'type': 'update_info', 'message': json.dumps(device_data)})


from asgiref.sync import async_to_sync
from channels.generic.websocket import WebsocketConsumer
from labshare.models import Device
from labshare.utils import publish_device_state



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

# Path: labshare/models.py
# class Device(models.Model):
#     name = models.CharField(max_length=255)
#     user = models.OneToOneField(User, on_delete=models.CASCADE)
# 
#     class Meta:
#         permissions = (
#             ('use_device', 'User/Group is allowed to use that device'),
#         )
# 
#     def __str__(self):
#         return self.name
# 
#     def can_be_used_by(self, user):
#         content_type = ContentType.objects.get_for_model(self)
#         permission_name = "{app}.use_{model}".format(app=content_type.app_label, model=content_type.model)
#         return user.has_perm(permission_name, self) or user.has_perm(permission_name)
# 
#     def serialize(self):
#         return {
#             'name': self.name,
#         }
# 
# Path: labshare/utils.py
# def publish_device_state(device_data, channel_name=None):
#     channel_layer = channels.layers.get_channel_layer()
#     name = device_data['name']
#     if 'gpus' not in device_data:
#         device_data['gpus'] = []
# 
#     if channel_name is None:
#         send_function = async_to_sync(channel_layer.group_send)
#     else:
#         send_function = async_to_sync(channel_layer.send)
#     send_function(channel_name if channel_name else name, {'type': 'update_info', 'message': json.dumps(device_data)})


# Path: labshare/consumers.py
from asgiref.sync import async_to_sync
from channels.generic.websocket import WebsocketConsumer
from labshare.models import Device
from labshare.utils import publish_device_state




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