def form_valid(self, form): if self.unit['type_of_unit'] == UnitType.TEMPERATURE_UNIT: obj = TemperatureUnitValue(timestamp=timezone.now(), incoming=True, **form.cleaned_data) serializer_obj = json.dumps( serializers.TemperatureUnitValueSerializer(obj).data) prepared_objs = [ json.dumps({ 'data': serializer_obj, 'type': 'TemperatureUnitValue' }) ] elif self.unit['type_of_unit'] == UnitType.SWITCH_UNIT: obj = SwitchUnitValue(timestamp=timezone.now(), incoming=True, **form.cleaned_data) serializer_obj = json.dumps( serializers.SwitchUnitValueSerializer(obj).data) prepared_objs = [ json.dumps({ 'data': serializer_obj, 'type': 'SwitchUnitValue' }) ] elif self.unit['type_of_unit'] == UnitType.HUMIDITY_UNIT: obj = HumidityUnitValue(timestamp=timezone.now(), incoming=True, **form.cleaned_data) serializer_obj = json.dumps( serializers.HumidityUnitValueSerializer(obj).data) prepared_objs = [ json.dumps({ 'data': serializer_obj, 'type': 'HumidityUnitValue' }) ] else: raise Exception("Invalid Type Of Device") payload = { 'device': int(self.kwargs.get('device')), 'unit': self.unit['pk'], 'data': prepared_objs } InternalApi.send_data_to_unit(self.hub, payload) connected_units = InternalApi.get_connected_units_with_unit( self.token, self.hub, self.unit['pk']) for connected_unit in connected_units: payload = { 'device': connected_unit['dest_device'], 'unit': connected_unit['dest_unit'], 'data': prepared_objs } hub = AuthServiceApi.get_hub(connected_unit['dest_hub']) InternalApi.send_data_to_unit(hub, payload) return HttpResponseRedirect(self.get_success_url())
def get(self, request, *args, **kwargs): token = self.request.COOKIES.get('user_token') self.hub = AuthServiceApi.get_hub(self.kwargs.get('hub')) self.device = InternalApi.get_device(token, self.hub, self.kwargs.get('device')) self.unit = InternalApi.get_unit(token, self.hub, self.device['pk'], self.kwargs.get('pk')) self.connected_units = InternalApi.get_connected_units_with_unit( token, self.hub, self.unit['pk']) return super(ConnectedUnitList, self).get(request, *args, **kwargs)