Пример #1
0
def register_sensor():
    """Registers given sensor in the system if the provided JSON POST message is correctly formed.
    An id is assigned to it and is returned"""
    try:
        sensor = json.loads(request.data)
        with open(sensor_schema, 'r') as f:
            validate(sensor, json.load(f))
    except ValueError as e:
        return 'JSON data is malformed: {0}'.format(e.message)
    except ValidationError as e:
        return 'JSON does not comply with schema: {0}'.format(e.message)
    else:
        name = sensor['name']
        sensor_type = sensor['type']
        #location_id = sensor['location']
        location_name=sensor['location']
        #l = models.Location.query.get(location_id)
        l=models.Location.query.filter_by(name=location_name).all()
        if len(l) == 0:
            l=models.Location(name=location_name)
            db.session.add(l)
            db.session.commit()
            if 'sensorRef' in sensor:
                s = models.Sensor(name=name, type=sensor_type, located_in=l,sensorRef=sensor['sensorRef'])
            else:
                s = models.Sensor(name=name, type=sensor_type, located_in=l)
            db.session.add(s)
            db.session.commit()
            if not isfile(schema_prefix + format_filename(sensor_type) + schema_suffix):
                with open(schema_prefix + format_filename(sensor_type) + schema_suffix, 'w+') as f:
                    json.dump(sensor['event_definition'], f)

            return "{0}".format(s.id)

        else:
            if 'sensorRef' in sensor:
                s = models.Sensor(name=name, type=sensor_type, located_in=l[0],sensorRef=sensor['sensorRef'])
            else:
                s = models.Sensor(name=name, type=sensor_type, located_in=l[0])
            db.session.add(s)
            db.session.commit()

            if not isfile(schema_prefix + format_filename(sensor_type) + schema_suffix):
                with open(schema_prefix + format_filename(sensor_type) + schema_suffix, 'w+') as f:
                    json.dump(sensor['event_definition'], f)

            return "{0}".format(s.id)
        #Previous way, giving the ID.
        """if l is None:
Пример #2
0
def on_event():
    """Saves the given event from a sensor if the provided JSON POST message is correctly formed"""
    try:
        event = json.loads(request.data)
        with open(event_schema, 'r') as f:
            validate(event, json.load(f))
    except ValueError as e:
        return 'JSON data is malformed: {0}'.format(e.message)
    except ValidationError as e:
        return 'JSON does not comply with schema: {0}'.format(e.message)
    else:
        timestamp = int(event['timestamp'] / 1000)
        value = event['value']
        value=json.dumps(value)
        sensor_id = event['sensor_id']
        s = models.Sensor.query.get(sensor_id)
        if s is None:
            return 'Invalid sensor id: {0}'.format(sensor_id)
        else:
            s.last_alive = datetime.now()
            try:
                with open(schema_prefix + format_filename(s.type) + schema_suffix, 'r') as f:
                    validate(event['value'], json.load(f))
            except ValidationError as e:
                return 'JSON does not comply with schema: {0}'.format(e.message)
            else:
                e = models.Event(timestamp=datetime.fromtimestamp(timestamp), fired_by=s, value=str(value))
                db.session.add(e)
                db.session.commit()

                #_append_sensor_event(e)

                return "{0}".format(e.id)
Пример #3
0
def _remove_sensor(sensor):
    """Removes the given sensor from the system"""
    sensor_type = sensor.type
    db.session.delete(sensor)
    db.session.commit()

    remaining = db.session.query(models.Sensor).filter(models.Sensor.type == sensor_type).count()

    if remaining == 0:
        remove(schema_prefix + format_filename(sensor_type) + schema_suffix)