Exemple #1
0
def sensor_data_state_change(sender, instance, *args, **kwargs):
    """
    Send Sensor Data notifications
    - If the state of a sensor changed with this save, send out a notification.
    """
    if instance.state_changed:
        if instance.sensor.state:
            # Recovered
            send_notification(instance.sensor, instance, "recovered")
        else:
            # Failed
            send_notification(instance.sensor, instance, "alert")
    def handle(self, *args, **options):
        sensors = Sensor.objects.all()
        for sensor in sensors:
            print sensor
            down = False

            # Get last sensor data entry
            data = SensorData.objects.filter(
                sensor=sensor).order_by("-datetime")[0:1]

            nowtz = timezone.make_aware(datetime.now())
            delta = timedelta(minutes=settings.SENSOR_DOWN_AFTER_MINUTES)

            old_before_datetime = nowtz - delta
            if data and data[0]:
                since = str(data[0].datetime)
            else:
                since = "???"

            # If the data is there, how old is it?
            if len(data) > 0 and data[0].datetime < old_before_datetime:
                # Data is old, device is down.
                down = True
            elif sensor.create_date < old_before_datetime:
                # Device is not a new one, device is down.
                down = True

            if down and not sensor.down:
                print "   - sending alert, down %s - %s" % (sensor, since)
                try:
                    send_notification(sensor, data[0], "down")
                except KeyError:
                    # If data[0] doesn't exist, the sensor doesn't have any
                    # active data anyway, and we probably don't care.
                    pass
                sensor.down = True
                sensor.save()

            elif sensor.down:
                print "   - sensor already down %s" % sensor
            else:
                print "   + still up %s - %s" % (sensor, since)
    def handle(self, *args, **options):
        sensors = Sensor.objects.all()
        for sensor in sensors:
            print sensor
            down = False

            # Get last sensor data entry
            data = SensorData.objects.filter(sensor=sensor).order_by("-datetime")[0:1]

            nowtz = timezone.make_aware(datetime.now())
            delta = timedelta(minutes=settings.SENSOR_DOWN_AFTER_MINUTES)

            old_before_datetime = nowtz - delta
            if data and data[0]:
                since = str(data[0].datetime)
            else:
                since = "???"

            # If the data is there, how old is it?
            if len(data) > 0 and data[0].datetime < old_before_datetime:
                # Data is old, device is down.
                down = True
            elif sensor.create_date < old_before_datetime:
                # Device is not a new one, device is down.
                down = True

            if down and not sensor.down:
                print "   - sending alert, down %s - %s" % (sensor, since)
                try:
                    send_notification(sensor, data[0], "down")
                except KeyError:
                    # If data[0] doesn't exist, the sensor doesn't have any
                    # active data anyway, and we probably don't care.
                    pass
                sensor.down = True
                sensor.save()

            elif sensor.down:
                print "   - sensor already down %s" % sensor
            else:
                print "   + still up %s - %s" % (sensor, since)