<commit_before>import atexit, sys, os

from django.utils.six import BytesIO
from django.core.management.base import OutputWrapper
from django.core.management.color import color_style, no_style

from django.db.models.signals import post_save, pre_delete
from django.dispatch import receiver
from django.conf import settings

from rest_framework.parsers import JSONParser
from rest_framework.renderers import JSONRenderer

from .mqttclient import BaseClient, MQTTException
from .models import Ambulance, Equipment, HospitalEquipment, Hospital
from .serializers import AmbulanceSerializer, HospitalSerializer, \
    HospitalEquipmentSerializer, EquipmentSerializer, \
    ExtendedProfileSerializer


# UpdateClient class

class UpdateClient(BaseClient):

    def publish(self, topic, message, *vargs, **kwargs):
        self.client.publish(topic, message, *vargs, **kwargs)

    def update_topic(self, topic, serializer, qos=0, retain=False):
        # Publish to topic
        self.publish(topic,
                     JSONRenderer().render(serializer.data),
                     qos=qos,
                     retain=retain)
        
    def remove_topic(self, topic, serializer, qos=0):
        # Publish null to retained topic
        self.publish(topic,
                     null,
                     qos=qos,
                     retain=True)

    def update_profile(self, obj, qos=2, retain=True):
        self.update_topic('user/{}/profile'.format(obj.user.username),
                            ExtendedProfileSerializer(obj),
                            qos=qos,
                            retain=retain)
        
    def update_ambulance(self, obj, qos=2, retain=True):
        self.update_topic('ambulance/{}/data'.format(obj.id),
                            AmbulanceSerializer(obj),
                            qos=qos,
                            retain=retain)

    def remove_ambulance(self, obj):
        self.remove_topic('ambulance/{}/data'.format(obj.id))
        
    def update_hospital(self, obj, qos=2, retain=True):
        self.update_topic('hospital/{}/data'.format(obj.id),
                            HospitalSerializer(obj),
                            qos=qos,
                            retain=retain)

    def remove_hospital(self, obj):
        self.remove_topic('hospital/{}/data'.format(obj.id))
        self.remove_topic('hospital/{}/metadata'.format(obj.id))
        
    def update_hospital_metadata(self, hospital, qos=2, retain=True):
        hospital_equipment = hospital.hospitalequipment_set.values('equipment')
        equipment = Equipment.objects.filter(id__in=hospital_equipment)
        self.update_topic('hospital/{}/metadata'.format(hospital.id),
                            EquipmentSerializer(equipment, many=True),
                            qos=qos,
                            retain=retain)

    def update_hospital_equipment(self, obj, qos=2, retain=True):
        self.update_topic('hospital/{}/equipment/{}/data'.format(obj.hospital.id,
                                                                 obj.equipment.name),
                            HospitalEquipmentSerializer(obj),
                            qos=qos,
                            retain=retain)
        
    def remove_hospital_equipment(self, obj):
        self.remove_topic('hospital/{}/equipment/{}/data'.format(obj.hospital.id,
                                                                   obj.equipment.name))

        
# Loop until client disconnects

# Start client
stdout = OutputWrapper(sys.stdout)
style = color_style()

# Instantiate broker
broker = {
    'HOST': 'localhost',
    'PORT': 1883,
    'KEEPALIVE': 60,
    'CLEAN_SESSION': True
}

broker.update(settings.MQTT)
broker['CLIENT_ID'] = 'signals_' + str(os.getpid())

connected = False
attempts = 0
while not connected and attempts < 10:

    try:

        # try to connect
        client = UpdateClient(broker, stdout, style, 0)
        
    except MQTTException as e:

        if e.value == 5:
            time.sleep()
            print('Could not connect to MQTT brocker. Retrying...')
            attempts += 1
        else:<commit_msg>Test with live server<commit_after>import atexit, sys, os

from django.utils.six import BytesIO
from django.core.management.base import OutputWrapper
from django.core.management.color import color_style, no_style

from django.db.models.signals import post_save, pre_delete
from django.dispatch import receiver
from django.conf import settings

from rest_framework.parsers import JSONParser
from rest_framework.renderers import JSONRenderer

from .mqttclient import BaseClient, MQTTException
from .models import Ambulance, Equipment, HospitalEquipment, Hospital
from .serializers import AmbulanceSerializer, HospitalSerializer, \
    HospitalEquipmentSerializer, EquipmentSerializer, \
    ExtendedProfileSerializer


# UpdateClient class

class UpdateClient(BaseClient):

    def publish(self, topic, message, *vargs, **kwargs):
        self.client.publish(topic, message, *vargs, **kwargs)

    def update_topic(self, topic, serializer, qos=0, retain=False):
        # Publish to topic
        self.publish(topic,
                     JSONRenderer().render(serializer.data),
                     qos=qos,
                     retain=retain)
        
    def remove_topic(self, topic, serializer, qos=0):
        # Publish null to retained topic
        self.publish(topic,
                     null,
                     qos=qos,
                     retain=True)

    def update_profile(self, obj, qos=2, retain=True):
        self.update_topic('user/{}/profile'.format(obj.user.username),
                            ExtendedProfileSerializer(obj),
                            qos=qos,
                            retain=retain)
        
    def update_ambulance(self, obj, qos=2, retain=True):
        self.update_topic('ambulance/{}/data'.format(obj.id),
                            AmbulanceSerializer(obj),
                            qos=qos,
                            retain=retain)

    def remove_ambulance(self, obj):
        self.remove_topic('ambulance/{}/data'.format(obj.id))
        
    def update_hospital(self, obj, qos=2, retain=True):
        self.update_topic('hospital/{}/data'.format(obj.id),
                            HospitalSerializer(obj),
                            qos=qos,
                            retain=retain)

    def remove_hospital(self, obj):
        self.remove_topic('hospital/{}/data'.format(obj.id))
        self.remove_topic('hospital/{}/metadata'.format(obj.id))
        
    def update_hospital_metadata(self, hospital, qos=2, retain=True):
        hospital_equipment = hospital.hospitalequipment_set.values('equipment')
        equipment = Equipment.objects.filter(id__in=hospital_equipment)
        self.update_topic('hospital/{}/metadata'.format(hospital.id),
                            EquipmentSerializer(equipment, many=True),
                            qos=qos,
                            retain=retain)

    def update_hospital_equipment(self, obj, qos=2, retain=True):
        self.update_topic('hospital/{}/equipment/{}/data'.format(obj.hospital.id,
                                                                 obj.equipment.name),
                            HospitalEquipmentSerializer(obj),
                            qos=qos,
                            retain=retain)
        
    def remove_hospital_equipment(self, obj):
        self.remove_topic('hospital/{}/equipment/{}/data'.format(obj.hospital.id,
                                                                   obj.equipment.name))

        
# Loop until client disconnects

# Start client
stdout = OutputWrapper(sys.stdout)
style = color_style()

# Instantiate broker
broker = {
    'HOST': 'localhost',
    'PORT': 1883,
    'KEEPALIVE': 60,
    'CLEAN_SESSION': True
}

broker.update(settings.MQTT)
broker['CLIENT_ID'] = 'signals_' + str(os.getpid())

connected = False
attempts = 0
while not connected and attempts < 10:

    try:

        # try to connect
        client = UpdateClient(broker, stdout, style, 0)
        
    except MQTTException as e:

        if e.value == 5:
            time.sleep(1)
            print('Could not connect to MQTT brocker. Retrying...')
            attempts += 1
        else:
{'fraction_non_alphanumeric': 0.09953006386311604, 'fraction_numerical': 0.004699361368839619, 'mean_word_length': 2.0050687907313542, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '887692', 'n_tokens_mistral': 2322, 'n_tokens_neox': 2209, 'n_words': 454}