def publish_settings(self, qos=2, retain=False): self.publish_topic('settings', SettingsView.get_settings(), qos=qos, retain=retain)
def test_mqttseed(self): # seed from django.core import management management.call_command('mqttseed', verbosity=1) print('>> Processing messages...') # Start client as admin broker = { 'HOST': 'localhost', 'PORT': 1883, 'KEEPALIVE': 60, 'CLEAN_SESSION': True } broker.update(settings.MQTT) broker['CLIENT_ID'] = 'test_mqttseed_admin' client = MQTTTestClient(broker) self.is_connected(client) qos = 0 # Expect settings client.expect('settings', JSONRenderer().render(SettingsView.get_settings()), qos) # Expect all ambulances for ambulance in Ambulance.objects.all(): client.expect( 'ambulance/{}/data'.format(ambulance.id), JSONRenderer().render(AmbulanceSerializer(ambulance).data), qos) # Expect all hospitals for hospital in Hospital.objects.all(): client.expect( 'hospital/{}/data'.format(hospital.id), JSONRenderer().render(HospitalSerializer(hospital).data), qos) hospital_equipment = hospital.hospitalequipment_set.values( 'equipment') equipment = Equipment.objects.filter(id__in=hospital_equipment) client.expect( 'hospital/{}/metadata'.format(hospital.id), JSONRenderer().render( EquipmentSerializer(equipment, many=True).data), qos) # Expect all hospital equipments for e in HospitalEquipment.objects.all(): client.expect( 'hospital/{}/equipment/{}/data'.format(e.hospital.id, e.equipment.name), JSONRenderer().render(HospitalEquipmentSerializer(e).data), qos) # Expect all profiles for obj in Profile.objects.all(): client.expect( 'user/{}/profile'.format(obj.user.username), JSONRenderer().render(ExtendedProfileSerializer(obj).data), qos) # Subscribed? self.is_subscribed(client) # Done? self.loop(client) client.wait() # Repeat with same client client = MQTTTestClient(broker) self.is_connected(client) qos = 0 # Expect settings client.expect('settings', JSONRenderer().render(SettingsView.get_settings()), qos) # Expect all ambulances for ambulance in Ambulance.objects.all(): client.expect( 'ambulance/{}/data'.format(ambulance.id), JSONRenderer().render(AmbulanceSerializer(ambulance).data), qos) # Expect all hospitals for hospital in Hospital.objects.all(): client.expect( 'hospital/{}/data'.format(hospital.id), JSONRenderer().render(HospitalSerializer(hospital).data), qos) hospital_equipment = hospital.hospitalequipment_set.values( 'equipment') equipment = Equipment.objects.filter(id__in=hospital_equipment) client.expect( 'hospital/{}/metadata'.format(hospital.id), JSONRenderer().render( EquipmentSerializer(equipment, many=True).data), qos) # Expect all hospital equipments for e in HospitalEquipment.objects.all(): client.expect( 'hospital/{}/equipment/{}/data'.format(e.hospital.id, e.equipment.name), JSONRenderer().render(HospitalEquipmentSerializer(e).data), qos) # Expect all profiles for obj in Profile.objects.all(): client.expect( 'user/{}/profile'.format(obj.user.username), JSONRenderer().render(ExtendedProfileSerializer(obj).data), qos) # Subscribed? self.is_subscribed(client) # Done? self.loop(client) client.wait() # Repeat with same client and different qos client = MQTTTestClient(broker) self.is_connected(client) qos = 2 # Expect settings client.expect('settings', JSONRenderer().render(SettingsView.get_settings()), qos) # Expect all ambulances for ambulance in Ambulance.objects.all(): client.expect( 'ambulance/{}/data'.format(ambulance.id), JSONRenderer().render(AmbulanceSerializer(ambulance).data), qos) # Expect all hospitals for hospital in Hospital.objects.all(): client.expect( 'hospital/{}/data'.format(hospital.id), JSONRenderer().render(HospitalSerializer(hospital).data), qos) hospital_equipment = hospital.hospitalequipment_set.values( 'equipment') equipment = Equipment.objects.filter(id__in=hospital_equipment) client.expect( 'hospital/{}/metadata'.format(hospital.id), JSONRenderer().render( EquipmentSerializer(equipment, many=True).data), qos) # Expect all hospital equipments for e in HospitalEquipment.objects.all(): client.expect( 'hospital/{}/equipment/{}/data'.format(e.hospital.id, e.equipment.name), JSONRenderer().render(HospitalEquipmentSerializer(e).data), qos) # Expect all profiles for obj in Profile.objects.all(): client.expect( 'user/{}/profile'.format(obj.user.username), JSONRenderer().render(ExtendedProfileSerializer(obj).data), qos) # Subscribed? self.is_subscribed(client) # Done? self.loop(client) client.wait() # repeat with another user qos = 0 # Start client as common user broker['USERNAME'] = '******' broker['PASSWORD'] = '******' broker['CLIENT_ID'] = 'test_mqttseed_testuser1' client = MQTTTestClient(broker) self.is_connected(client) # Expect settings client.expect('settings', JSONRenderer().render(SettingsView.get_settings()), qos) # Expect user profile profile = Profile.objects.get(user__username='******') client.expect( 'user/testuser1/profile', JSONRenderer().render(ExtendedProfileSerializer(profile).data), qos) # User Ambulances can_read = profile.ambulances.filter( can_read=True).values('ambulance_id') for ambulance in Ambulance.objects.filter(id__in=can_read): client.expect( 'ambulance/{}/data'.format(ambulance.id), JSONRenderer().render(AmbulanceSerializer(ambulance).data), qos) # User Hospitals can_read = profile.hospitals.filter( can_read=True).values('hospital_id') for hospital in Hospital.objects.filter(id__in=can_read): client.expect( 'hospital/{}/data'.format(hospital.id), JSONRenderer().render(HospitalSerializer(hospital).data), qos) # Expect all user hospital equipments for e in HospitalEquipment.objects.filter(hospital__id__in=can_read): client.expect( 'hospital/{}/equipment/{}/data'.format(e.hospital.id, e.equipment.name), JSONRenderer().render(HospitalEquipmentSerializer(e).data), qos) # Subscribed? self.is_subscribed(client) # Done? self.loop(client) client.wait() # repeat with another user qos = 0 # Start client as common user broker['USERNAME'] = '******' broker['PASSWORD'] = '******' broker['CLIENT_ID'] = 'test_mqttseed_testuser2' client = MQTTTestClient(broker) self.is_connected(client) # Expect settings client.expect('settings', JSONRenderer().render(SettingsView.get_settings()), qos) # Expect user profile profile = Profile.objects.get(user__username='******') client.expect( 'user/testuser2/profile', JSONRenderer().render(ExtendedProfileSerializer(profile).data), qos) # User Ambulances can_read = profile.ambulances.filter( can_read=True).values('ambulance_id') for ambulance in Ambulance.objects.filter(id__in=can_read): client.expect( 'ambulance/{}/data'.format(ambulance.id), JSONRenderer().render(AmbulanceSerializer(ambulance).data), qos) # User Hospitals can_read = profile.hospitals.filter( can_read=True).values('hospital_id') for hospital in Hospital.objects.filter(id__in=can_read): client.expect( 'hospital/{}/data'.format(hospital.id), JSONRenderer().render(HospitalSerializer(hospital).data), qos) # Expect all user hospital equipments for e in HospitalEquipment.objects.filter(hospital__id__in=can_read): client.expect( 'hospital/{}/equipment/{}/data'.format(e.hospital.id, e.equipment.name), JSONRenderer().render(HospitalEquipmentSerializer(e).data), qos) # Subscribed? self.is_subscribed(client) # Done? self.loop(client) client.wait()