Exemple #1
0
def store_sensor():
    sensor = Sensor()

    if request.form.get('identificator') == "":
        flash('You left the identificator blank, try again!', category={ 'theme': 'error' } )
        app.logger.error("Adding sensor: Blank identificator")
        return redirect('/sensors/add/')

    if request.form.get('type'):
        sensor.type = request.form.get('type')

    sensor.identificator = request.form.get("identificator").lower().replace(" ", "_")
    sensor.pin_id = request.form.get("pin")
    sensor.module_id = request.form.get("module")

    if SensorInteractor.get_by_identificator(sensor.identificator):
        flash("Sensor with same identifier already exists!", category={ 'theme': 'error' } )
        app.logger.error("Adding sensor: Identificator already exists")
        return redirect("/sensors/add/")

    try:
        sensor.save()

        for method in sensor.module.methods:
            sm = SensorMethods()
            sm.method = method
            sensor.sensor_methods.append(sm)

        sensor.save()

        if request.form.get('is_active'):

            activate_sensor(sensor.id)

            if sensor.pin.arduino_pin == "D":
                r = request_helper.change_pin_mode(sensor.pin.arduino_pin[1:], sensor.pin.io)

                if r!= False:
                    flash("Pin %s mode successfully changed to %s!" % (sensor.pin.arduino_pin, sensor.pin.io), category={'theme' : 'success'} )
                else:
                    flash("Pin mode could not be changed!", category={'theme': 'error'})
                    app.logger.error("Adding sensor: Couldn't change pin mode - %s (%s)" % (sensor.pin.arduino_pin, sensor.pin.io))

        flash("Sensor added!", category={ 'theme': 'success' } )
        return redirect("/sensors/#%s" % sensor.identificator)

    except IntegrityError:
        flash("Sensor with same identifier already exists!", category={ 'theme': 'error' } )
        app.logger.error("Adding sensor: Identificator already exists")
        return redirect("/sensors/add/")
Exemple #2
0
def retargeting_sensor_toggle(identificator, method_path, value=None):
    if request.remote_addr in [gateway.address.split(":")[1][2:] for gateway in GatewayInteractor.get_all_device_registered()]:
        sensor = SensorInteractor.get_by_identificator(identificator)
        if sensor and sensor.active:
            method = MethodInteractor.get_by_path(method_path, sensor.module_id)
            if method:
                if method.type != "read":
                    sensor_method = SensorMethodsInteractor.get(sensor.id, method.id)
                    if sensor_method:
                        if sensor.pin.io == "output":
                            path = "/%s/%s/%s/%s" % (sensor.module.hardware.path, sensor.module.path, method_path, sensor.pin.pin)
                            if value:
                                path += "/%s" % value
                            r = request_helper.call_arduino_path( path )
                            if r != False:
                                if r.status_code == 200:
                                    if r.text:
                                        sensor_method.value = r.text
                                        sensor_method.save()
                                        for gateway in GatewayInteractor.get_all_device_registered():
                                            request_helper.send_sensor_value(gateway.address, gateway.post_authorization, sensor.identificator, sensor_method.method.path, sensor_method.value)
                                        app.logger.info("Retargeting call: %s - %s (%s)" % (identificator, method_path, sensor_method.value))
                                return make_response((r.text, r.status_code))
                            app.logger.error("Retargeting call: Can't reach sensor - %s" % (sensor.identificator, ))
                            return make_response(("400: The sensor can't be reached!", 400))
                        app.logger.error("Retargeting call: Called method for an INPUT sensor - %s" % (sensor.identificator))
                        return make_response(("400: You are calling a method for an INPUT sensor!", 400))
                    app.logger.error("Retargeting call: Invalid method for sensor - %s (%s)" % (sensor.identificator, method_path))
                    return make_response(("400: Invalid method for sensor!", 400))
                app.logger.error("Retargeting call: Called read method - %s (%s)" % (sensor.identificator, method_path))
                return make_response(("400: You are calling a read method!", 400))
            app.logger.error("Retargeting call: Invalid method for sensor - %s (%s)" % (sensor.identificator, method_path))
            return make_response(("400: Invalid method for sensor!", 400))
        app.logger.error("Retargeting call: Sensor doesn't exist - %s" % (identificator, ))
        return make_response(("400: Non existant sensor!", 400))
    app.logger.error("Retargeting call: Request didn't come from registered GW - %s" % (request.remote_addr, ))
    return make_response(("400: You are not allowed to access this device!", 400))
Exemple #3
0
def update_sensor(sensor_id):
    identificator = request.form.get("identificator").lower().replace(" ", "_")
    old_sensor = SensorInteractor.get(sensor_id)
    old_io = old_sensor.pin.io
    sensor = SensorInteractor.get_by_identificator(identificator)

    if old_sensor:
        if sensor and sensor != old_sensor:
            flash("Sensor with same identifier already exists!", category={ 'theme': 'error' } )
            app.logger.error("Editing sensor: Identificator already exists")
            return redirect("/sensors/%d/edit/" % sensor_id)

        identificator_changed = False

        if old_sensor.identificator != identificator:
            identificator_changed= True
            old_sensor.identificator = identificator
            gateways = GatewayInteractor.get_all_device_registered()
            if gateways:
                for gateway in gateways:
                    for sensor_method in old_sensor.sensor_methods:
                        request_helper.delete_sensor(gateway.address, gateway.post_authorization, old_sensor.identificator, sensor_method.method.path)

        if request.form.get('type'):
            old_sensor.type = request.form.get('type')

        old_sensor.pin_id = request.form.get("pin")

        if old_sensor.module_id != request.form.get("module"):
            old_sensor.module_id = request.form.get("module")

            if not identificator_changed:
                gateways = GatewayInteractor.get_all_device_registered()
                if gateways:
                    for gateway in gateways:
                        for sensor_method in old_sensor.sensor_methods:
                            request_helper.delete_sensor(gateway.address, gateway.post_authorization, old_sensor.identificator, sensor_method.method.path)

            old_sensor.save()

            SensorMethodsInteractor.delete_all_for_sensor(old_sensor.id)

            for method in old_sensor.module.methods:
                sm = SensorMethods()
                sm.method = method
                old_sensor.sensor_methods.append(sm)

        old_sensor.save()

        if request.form.get('is_active'):

            activate_sensor(old_sensor.id)

            if old_sensor.pin.arduino_pin[0] == "D" and old_sensor.pin.io != old_io:
                r = request_helper.change_pin_mode(old_sensor.pin.arduino_pin[1:], old_sensor.pin.io)

                if r!= False:
                    flash("Pin %s mode successfully changed to %s!" % (old_sensor.pin.arduino_pin, old_sensor.pin.io), category={'theme' : 'success'} )
                else:
                    flash("Pin mode could not be changed!", category={'theme': 'error'})
                    app.logger.error("Editing sensor: Couldn't change pin mode - %s (%s)" % (old_sensor.pin.arduino_pin, old_sensor.pin.io))

        elif old_sensor.active:
            deactivate_sensor(old_sensor.id)

        flash("Sensor edited!", category={ 'theme': 'success' } )
        return redirect("/sensors/#%s" % old_sensor.identificator)

    flash("Sensor doesn't exist!", category={ 'theme': 'error' } )
    app.logger.error("Editing sensor: Sensor doesn't exist")
    return redirect("/sensors/")
Exemple #4
0
def check_identificator():
    identificator = request.form.get("identificator")
    sensor = SensorInteractor.get_by_identificator(identificator)
    if sensor:
        return jsonify({ "exists" : True })
    return jsonify({ "exists" : False })