Ejemplo n.º 1
0
 def form_valid(self, form):
     hub = AuthServiceApi.get_hub(self.kwargs.get('hub'))
     device_id = self.kwargs.get('device')
     pk = self.kwargs.get('pk')
     token = self.request.COOKIES.get('user_token')
     InternalApi.update_unit(token, hub, device_id, pk, form.cleaned_data)
     return super(UpdateDeviceUnitView, self).form_valid(form)
Ejemplo n.º 2
0
 def post(self, request, *args, **kwargs):
     hub = AuthServiceApi.get_hub(self.kwargs.get('hub'))
     device_id = self.kwargs.get('device')
     token = self.request.COOKIES.get('user_token')
     self.object = InternalApi.get_unit(token, hub, device_id,
                                        self.kwargs.get('pk'))
     return super(UpdateDeviceUnitView, self).post(request, *args, **kwargs)
Ejemplo n.º 3
0
 def get(self, request, *args, **kwargs):
     token = self.request.COOKIES.get('user_token')
     self.hub = AuthServiceApi.get_hub(self.kwargs.get('dest_hub'))
     self.device = InternalApi.get_device(token, self.hub,
                                          self.kwargs.get('dest_device'))
     self.units = InternalApi.get_units(token, self.hub, self.device['pk'])
     return super(ConnectUnitSelectUnitView,
                  self).get(request, *args, **kwargs)
Ejemplo n.º 4
0
 def form_valid(self, form):
     hub = AuthServiceApi.get_hub(self.kwargs['hub'])
     device_id = int(self.kwargs.get('pk'))
     form.cleaned_data['group'] = int(form.cleaned_data['group'])
     form.cleaned_data['device'] = device_id
     AuthServiceApi.add_device_group_permission(hub['pk'], device_id,
                                                form.cleaned_data)
     return HttpResponseRedirect(self.get_success_url())
Ejemplo n.º 5
0
    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())
Ejemplo n.º 6
0
def mqtt_callback(client, userdata, message):
    global models
    global serializers
    print(message.payload)
    try:
        obj = json.loads(message.payload if not isinstance(
            message.payload, bytes) else message.payload.decode("UTF-8"))
    except Exception as e:
        print(e)
    if 'device' not in obj or 'unit' not in obj or 'data' not in obj:
        return

    device = models.Device.objects.get(pk=obj['device'])
    unit = models.DeviceUnit.objects.get(pk=obj['unit'], device=device)

    if device.type_of_device == DeviceType.GENERIC_HUMIDITY_SENSOR:
        return
    elif device.type_of_device == DeviceType.GENERIC_TEMPERATURE_SENSOR:
        objs = drivers.GTS.driver.driver_function(models, unit, obj['data'])
    elif device.type_of_device == DeviceType.GENERIC_LAMP:
        objs = drivers.GL.driver.driver_function(models, unit, obj['data'])
    else:
        return

    prepared_objs = []

    for o in objs:
        o.save()
        if isinstance(o, models.TemperatureUnitValue):
            serializer_obj = json.dumps(
                serializers.TemperatureUnitValueSerializer(o).data)
            prepared_obj = {
                'data': serializer_obj,
                'type': 'TemperatureUnitValue'
            }
            prepared_objs.append(json.dumps(prepared_obj))
        if isinstance(o, models.SwitchUnitValue):
            serializer_obj = json.dumps(
                serializers.SwitchUnitValueSerializer(o).data)
            prepared_obj = {'data': serializer_obj, 'type': 'SwitchUnitValue'}
            prepared_objs.append(json.dumps(prepared_obj))

    connected_units = models.ConnectedUnit.objects.filter(from_unit=unit)
    print(connected_units)
    for connected_unit in connected_units:
        payload = {
            'device': connected_unit.dest_device,
            'unit': connected_unit.dest_unit,
            'data': prepared_objs
        }
        try:
            hub = AuthServiceApi.get_hub(connected_unit.dest_hub)
        except Exception as e:
            print(e)
        try:
            InternalApi.send_data_to_unit(hub, payload)
        except Exception as e:
            print(e)
Ejemplo n.º 7
0
 def get(self, request, *args, **kwargs):
     token = self.request.COOKIES.get('user_token')
     self.hub = AuthServiceApi.get_hub(self.kwargs.get('dest_hub'))
     self.device = InternalApi.get_device(token, self.hub,
                                          self.kwargs.get('dest_device'))
     self.unit = InternalApi.get_unit(token, self.hub, self.device['pk'],
                                      self.kwargs.get('dest_unit'))
     context = self.get_context_data()
     return self.render_to_response(context)
Ejemplo n.º 8
0
 def get(self, request, *args, **kwargs):
     hub = AuthServiceApi.get_hub(self.kwargs.get('hub'))
     self.device = InternalApi.get_device(
         self.request.COOKIES.get('user_token'), hub, self.kwargs.get('pk'))
     self.units = InternalApi.get_units(
         self.request.COOKIES.get('user_token'), hub, self.kwargs.get('pk'))
     self.user = AuthServiceApi.get_me(
         self.request.COOKIES.get('user_token'))
     return super(DeviceUnitsView, self).get(request, *args, **kwargs)
Ejemplo n.º 9
0
 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)
Ejemplo n.º 10
0
 def post(self, request, *args, **kwargs):
     token = self.request.COOKIES.get('user_token')
     hub = AuthServiceApi.get_hub(self.kwargs.get('hub'))
     InternalApi.add_connected_unit(
         token, hub, {
             'from_unit': int(self.kwargs.get('pk')),
             'dest_hub': int(self.kwargs.get('dest_hub')),
             'dest_device': int(self.kwargs.get('dest_device')),
             'dest_unit': int(self.kwargs.get('dest_unit'))
         })
     return HttpResponseRedirect(self.get_success_url())
Ejemplo n.º 11
0
 def get(self, request, *args, **kwargs):
     token = self.request.COOKIES.get('user_token')
     hub = AuthServiceApi.get_hub(kwargs['hub'])
     device = InternalApi.get_device(token, hub, kwargs['pk'])
     self.device = device
     self.users = AuthServiceApi.get_users()
     self.groups = AuthServiceApi.get_groups()
     self.user_permissions = AuthServiceApi.get_device_user_permissions(
         hub['pk'], device['pk'])
     self.group_permissions = AuthServiceApi.get_device_group_permissions(
         hub['pk'], device['pk'])
     return super(DevicePermissionsView, self).get(request, *args, **kwargs)
Ejemplo n.º 12
0
 def get(self, request, *args, **kwargs):
     hub = AuthServiceApi.get_hub(self.kwargs.get('hub'))
     self.hub = hub
     token = self.request.COOKIES.get('user_token')
     self.device = InternalApi.get_device(token, self.hub,
                                          self.kwargs.get('device'))
     self.unit = InternalApi.get_unit(token, hub, self.kwargs.get('device'),
                                      self.kwargs.get('pk'))
     self.data = InternalApi.get_data_from_unit(token, hub,
                                                self.kwargs.get('device'),
                                                self.kwargs.get('pk'))
     self.has_write_perms = AuthServiceApi.has_write_permission(
         hub['pk'], self.kwargs.get('device'), self.user['pk'])
     return super(UnitDataView, self).get(request, *args, **kwargs)
Ejemplo n.º 13
0
 def post(self, request, *args, **kwargs):
     self.hub = AuthServiceApi.get_hub(self.kwargs.get('hub'))
     self.token = self.request.COOKIES.get('user_token')
     self.device = InternalApi.get_device(self.token, self.hub,
                                          self.kwargs.get('device'))
     self.unit = InternalApi.get_unit(self.token, self.hub,
                                      self.kwargs.get('device'),
                                      self.kwargs.get('pk'))
     self.data = InternalApi.get_data_from_unit(self.token, self.hub,
                                                self.kwargs.get('device'),
                                                self.kwargs.get('pk'))
     self.has_write_perms = AuthServiceApi.has_write_permission(
         self.hub['pk'], self.kwargs.get('device'), self.user['pk'])
     if not self.has_write_perms['has_write_perm']:
         return HttpResponseRedirect(self.get_success_url())
     return super(UnitDataView, self).post(request, *args, **kwargs)
Ejemplo n.º 14
0
 def get(self, *args, **kwargs):
     hub = AuthServiceApi.get_hub(self.kwargs.get('hub'))
     token = self.request.COOKIES.get('user_token')
     InternalApi.delete_connected_unit(token, hub, self.kwargs.get('pk'))
     return redirect(self.get_success_url())
Ejemplo n.º 15
0
 def form_valid(self, form):
     hub = AuthServiceApi.get_hub(self.kwargs.get('hub'))
     token = self.request.COOKIES.get('user_token')
     InternalApi.save_device(token, hub, form)
     return HttpResponseRedirect(self.get_success_url())
Ejemplo n.º 16
0
 def get(self, request, *args, **kwargs):
     hub = AuthServiceApi.get_hub(int(kwargs.get('hub')))
     self.hub = hub
     self.devices = InternalApi.get_devices(
         self.request.COOKIES.get('user_token'), hub)
     return super(HubDeviceView, self).get(request, *args, **kwargs)
Ejemplo n.º 17
0
 def get(self, request, *args, **kwargs):
     token = self.request.COOKIES.get('user_token')
     hub = AuthServiceApi.get_hub(kwargs['hub'])
     self.object = InternalApi.get_device(token, hub, kwargs['pk'])
     return super(UpdateDeviceView, self).get(request, *args, **kwargs)
Ejemplo n.º 18
0
 def form_valid(self, form):
     token = self.request.COOKIES.get('user_token')
     hub = AuthServiceApi.get_hub(self.kwargs['hub'])
     device_id = int(self.kwargs.get('pk'))
     InternalApi.update_device(token, hub, device_id, form)
     return HttpResponseRedirect(self.get_success_url())