示例#1
0
    def create_data(self, request):
        def finalizer():
            if self.data is not None:
                for data in self.data:
                    data.delete()
                self.data = None

            if self.sensor is not None:
                self.sensor.delete()
                self.sensor = None

            if self.zone is not None:
                self.zone.delete()
                self.zone = None

        self.zone = Zone(
            name='Test Zone',
            notes='Zone notes.',
            key=uuid4()
        )
        self.zone.save()

        self.sensor = Sensor(
            guid=uuid4(),
            name='Test Sensor',
            notes='Sensor notes.',
            zone=self.zone,
            min_value=20,
            max_value=25,
            min_value_operator='>=',
            max_value_operator='<=',
            state=True,
            state_last_change_date=datetime.utcnow()
        )
        self.sensor.save()

        self.data = []
        for i in range(3):
            datum = SensorData(
                sensor=self.sensor,
                datetime=datetime.utcnow(),
                value=self.sensor.min_value + i,
                state=True,
                state_changed=False
            )
            datum.save()
            self.data.append(datum)

        request.addfinalizer(finalizer)
示例#2
0
    def test_sensor_data_list_get_with_zone_filter(self, api_root):
        excluded_zone = Zone(
            name='Test Zone Excluded',
            notes='Zone notes.',
            key=uuid4()
        )
        excluded_zone.save()

        excluded_sensor = Sensor(
            guid=uuid4(),
            name='Test Sensor Excluded',
            notes='Sensor notes.',
            zone=excluded_zone,
            min_value=20,
            max_value=25,
            min_value_operator='>=',
            max_value_operator='<=',
            state=True,
            state_last_change_date=datetime.utcnow()
        )
        excluded_sensor.save()

        excluded_data = SensorData(
            sensor=excluded_sensor,
            datetime=datetime.utcnow(),
            value=22,
            state=True,
            state_changed=False
        )
        excluded_data.save()

        suffix = 'data/?zone_ids={}&format=json'.format(self.zone.id)
        response = requests.get(api_root + suffix)
        data = response.json()
        assert len(data) > 0

        excluded_data_json = [x for x in data if x['id'] == str(excluded_data.id)]
        assert len(excluded_data_json) == 0

        excluded_data.delete()
        excluded_sensor.delete()
        excluded_zone.delete()

        for i in range(3):
            data_json = [x for x in data if int(x['id']) == self.data[i].id][0]
            self._compare_sensor_data_json(self.data[i], data_json)
示例#3
0
文件: views.py 项目: skyu0221/IoT
def sec_sensor_data(request):
    req_data = request.data

    # save sensor data
    s = SensorData(location=req_data['location'], sensor_data=req_data['sensor_data'],
                   created_by=Now())
    s.save()

    # save location data
    locations = ast.literal_eval(req_data['location'])
    for name in locations:
        if name == 'time':
            continue
        if locations[name] == 'home':
            continue
        loc = LocationData(location=locations[name], name=name, created_by=Now())
        loc.save()

    return Response(status=status.HTTP_201_CREATED)
示例#4
0
    def create_data(self, request):
        def finalizer():
            if self.data is not None:
                for data in self.data:
                    data.delete()
                self.data = None

            if self.sensor is not None:
                self.sensor.delete()
                self.sensor = None

            if self.zone is not None:
                self.zone.delete()
                self.zone = None

        self.zone = Zone(name='Test Zone', notes='Zone notes.', key=uuid4())
        self.zone.save()

        self.sensor = Sensor(guid=uuid4(),
                             name='Test Sensor',
                             notes='Sensor notes.',
                             zone=self.zone,
                             min_value=20,
                             max_value=25,
                             min_value_operator='>=',
                             max_value_operator='<=',
                             state=True,
                             state_last_change_date=datetime.utcnow())
        self.sensor.save()

        self.data = []
        for i in range(3):
            datum = SensorData(sensor=self.sensor,
                               datetime=datetime.utcnow(),
                               value=self.sensor.min_value + i,
                               state=True,
                               state_changed=False)
            datum.save()
            self.data.append(datum)

        request.addfinalizer(finalizer)
示例#5
0
    def test_sensor_data_list_get_with_zone_filter(self, api_root):
        excluded_zone = Zone(name='Test Zone Excluded',
                             notes='Zone notes.',
                             key=uuid4())
        excluded_zone.save()

        excluded_sensor = Sensor(guid=uuid4(),
                                 name='Test Sensor Excluded',
                                 notes='Sensor notes.',
                                 zone=excluded_zone,
                                 min_value=20,
                                 max_value=25,
                                 min_value_operator='>=',
                                 max_value_operator='<=',
                                 state=True,
                                 state_last_change_date=datetime.utcnow())
        excluded_sensor.save()

        excluded_data = SensorData(sensor=excluded_sensor,
                                   datetime=datetime.utcnow(),
                                   value=22,
                                   state=True,
                                   state_changed=False)
        excluded_data.save()

        suffix = 'data/?zone_ids={}&format=json'.format(self.zone.id)
        response = requests.get(api_root + suffix)
        data = response.json()
        assert len(data) > 0

        excluded_data_json = [
            x for x in data if x['id'] == str(excluded_data.id)
        ]
        assert len(excluded_data_json) == 0

        excluded_data.delete()
        excluded_sensor.delete()
        excluded_zone.delete()

        for i in range(3):
            data_json = [x for x in data if int(x['id']) == self.data[i].id][0]
            self._compare_sensor_data_json(self.data[i], data_json)
示例#6
0
    def create_data(self, request):
        def finalizer():
            if self.alerts is not None:
                for alert in self.alerts:
                    alert.delete()
                self.alerts = None

            if self.data is not None:
                for data in self.data:
                    data.delete()
                self.data = None

            if self.sensor is not None:
                self.sensor.delete()
                self.sensor = None

            if self.zone is not None:
                self.zone.delete()
                self.zone = None

            if self.alert_group is not None:
                self.alert_group.delete()
                self.alert_group = None

            if self.user is not None:
                self.user.delete()

        username = str(uuid4())[:30]
        self.user = User(username=username,
                         email='{}@example.net'.format(username))
        self.user.save()

        self.alert_group = SensorAlertGroup(name='Test Group')
        self.alert_group.save()
        self.alert_group.users.add(self.user)

        self.zone = Zone(name='Test Zone', notes='Zone notes.', key=uuid4())
        self.zone.save()

        self.sensor = Sensor(guid=uuid4(),
                             name='Test Sensor',
                             notes='Sensor notes.',
                             zone=self.zone,
                             min_value=60,
                             max_value=85,
                             min_value_operator='>=',
                             max_value_operator='<=',
                             state=True,
                             state_last_change_date=datetime.utcnow())
        self.sensor.save()
        self.sensor.alert_groups.add(self.alert_group)

        self.data = []

        # Inbox is empty
        assert 0 == len(mail.outbox)

        # Create in-range data point; no notification.
        datum = SensorData(sensor=self.sensor,
                           datetime=datetime.utcnow(),
                           value=self.sensor.min_value + 1,
                           state=True,
                           state_changed=False)
        datum.save()
        self.data.append(datum)

        # Inbox still empty
        assert 0 == len(mail.outbox)

        # Create out-of-range notification; sends "alert."
        datum = SensorData(sensor=self.sensor,
                           datetime=datetime.utcnow(),
                           value=self.sensor.min_value - 1,
                           state=True,
                           state_changed=False)
        datum.save()
        self.data.append(datum)

        # Inbox received alert
        assert 1 == len(mail.outbox)

        # Create another in-range data point; sends "recovered."
        datum = SensorData(sensor=self.sensor,
                           datetime=datetime.utcnow(),
                           value=self.sensor.min_value + 1,
                           state=True,
                           state_changed=False)
        datum.save()
        self.data.append(datum)

        # Inbox received recovery email
        assert 2 == len(mail.outbox)

        self.alerts = SensorAlert.objects.all().order_by('id')

        request.addfinalizer(finalizer)
    def create_data(self, request):
        def finalizer():
            if self.alerts is not None:
                for alert in self.alerts:
                    alert.delete()
                self.alerts = None

            if self.data is not None:
                for data in self.data:
                    data.delete()
                self.data = None

            if self.sensor is not None:
                self.sensor.delete()
                self.sensor = None

            if self.zone is not None:
                self.zone.delete()
                self.zone = None

            if self.alert_group is not None:
                self.alert_group.delete()
                self.alert_group = None

            if self.user is not None:
                self.user.delete()

        username = str(uuid4())[:30]
        self.user = User(
            username=username,
            email='{}@example.net'.format(username)
        )
        self.user.save()

        self.alert_group = SensorAlertGroup(name='Test Group')
        self.alert_group.save()
        self.alert_group.users.add(self.user)

        self.zone = Zone(
            name='Test Zone',
            notes='Zone notes.',
            key=uuid4()
        )
        self.zone.save()

        self.sensor = Sensor(
            guid=uuid4(),
            name='Test Sensor',
            notes='Sensor notes.',
            zone=self.zone,
            min_value=60,
            max_value=85,
            min_value_operator='>=',
            max_value_operator='<=',
            state=True,
            state_last_change_date=datetime.utcnow()
        )
        self.sensor.save()
        self.sensor.alert_groups.add(self.alert_group)

        self.data = []

        # Inbox is empty
        assert 0 == len(mail.outbox)

        # Create in-range data point; no notification.
        datum = SensorData(
            sensor=self.sensor,
            datetime=datetime.utcnow(),
            value=self.sensor.min_value + 1,
            state=True,
            state_changed=False
        )
        datum.save()
        self.data.append(datum)

        # Inbox still empty
        assert 0 == len(mail.outbox)

        # Create out-of-range notification; sends "alert."
        datum = SensorData(
            sensor=self.sensor,
            datetime=datetime.utcnow(),
            value=self.sensor.min_value - 1,
            state=True,
            state_changed=False
        )
        datum.save()
        self.data.append(datum)

        # Inbox received alert
        assert 1 == len(mail.outbox)

        # Create another in-range data point; sends "recovered."
        datum = SensorData(
            sensor=self.sensor,
            datetime=datetime.utcnow(),
            value=self.sensor.min_value + 1,
            state=True,
            state_changed=False
        )
        datum.save()
        self.data.append(datum)

        # Inbox received recovery email
        assert 2 == len(mail.outbox)

        self.alerts = SensorAlert.objects.all().order_by('id')

        request.addfinalizer(finalizer)