Esempio n. 1
0
def activate_sensor(sensor_id):
    sensor = SensorInteractor.get(sensor_id)
    if sensor:
        sensors = SensorInteractor.get_active_for_pin(sensor.pin.arduino_pin)
        old_io = ""
        if sensors:
            for sensor_on_pin in sensors:
                deactivate_sensor(sensor_on_pin.id, False)
                old_io = sensor_on_pin.pin.io

        sensor.active = True
        sensor.save()

        if sensor.pin.io != old_io:
            r = request_helper.change_pin_mode(sensor.pin.arduino_pin, 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("Activating sensor: Couldn't change pin mode - %s (%s)" % (sensor.pin.arduino_pin, sensor.pin.io))

        for sensor_method in sensor.sensor_methods:
            if sensor_method.method.type in ["read"]:
                r = request_helper.get_sensor_value(sensor, sensor_method.method.path)
                if r != False:
                    sensor_method.value = r
                    sensor_method.save()
                else:
                    app.logger.error("Getting value: Couldn't connect to YunServer - sensor %s (method %s) - path %s" % (sensor.id, sensor_method.method.id, sensor_method.method.path))
                    return jsonify({ "value" : 'error', 'error' : 'The arduino server is not available.' })

        gateways = GatewayInteractor.get_all_device_registered()

        if gateways:
            for gateway in gateways:
                r = request_helper.send_descriptor(gateway.address, gateway.post_authorization)
                if r != False:
                    for sensor_method in sensor.sensor_methods:
                        r = request_helper.init_sensor(gateway.address, gateway.post_authorization, sensor.identificator, sensor_method.method.path)
                        if r != False and sensor_method.method.type in ["read", "write"] and sensor_method.value:
                            r = request_helper.send_sensor_value(gateway.address, gateway.post_authorization, sensor.identificator, sensor_method.method.path, sensor_method.value)
                    flash('Sensor successfully added to gateway %s!' % gateway.address, category={ 'theme': 'success' } )
            flash("Sensor activated!", category={ 'theme': 'success' } )
        else:
            flash("No gateways with registered device", category={ 'theme': 'warning' } )

        return redirect("/sensors/#%s" % sensor.identificator)

    flash("Sensor does not exist!", category={ 'theme': 'error' } )
    app.logger.error("Activating sensor: Sensor does not exist")
    return redirect("/sensors/")
Esempio n. 2
0
def init_pin_modes():
    app.logger.info("----START init pin modes START----")
    sensors = SensorInteractor.get_all_active()
    if sensors:
        for sensor in sensors:
            if sensor.pin.arduino_pin[0] == "D":
                try:
                    requests.get("http://%s%s%s/%s/%s" % (
                            settings.LOCAL,
                            settings.REST_ROOT,
                            settings.MODE,
                            sensor.pin.pin[1:],
                            sensor.pin.io
                            ),
                        timeout = 5
                    )

                    app.logger.info("Pin %s mode successfully changed to %s!" % (sensor.pin.arduino_pin, sensor.pin.io))

                except ConnectionError:
                    app.logger.error("Cannot connect to %s!" % settings.IP_DNS)
                    continue

                except Timeout:
                    app.logger.error("Request timed out. Wrong IP?")
                    continue
    app.logger.info("----END init pin modes END----")
Esempio n. 3
0
def invoke_sensor_method(sensor_id, method_id):
    path = request.form.get("path")
    r = request_helper.call_arduino_path(path)

    app.logger.info("Invoking method %d for sensor %d: %s (status %d)" % (method_id, sensor_id, r.text, r.status_code))

    if r != False:
        if r.status_code == 200:
            values = []
            sensor_method = SensorMethodsInteractor.get(sensor_id, method_id)
            if r.text != "":
                sensor_method.value = r.text
                sensor_method.save()
                if sensor_method.method.type in ["write", "call"]:
                    sensor = SensorInteractor.get(sensor_id)
                    if sensor:
                        for sensor_method in sensor.sensor_methods:
                            if sensor_method.method.type == "read":
                                rq = request_helper.call_arduino_path("/%s/%s/%s/%s" % (sensor.module.hardware.path, sensor.module.path, sensor_method.method.path, sensor.pin.pin))
                                if rq != False and rq.status_code == 200:
                                    sensor_method.value = rq.text
                                    sensor_method.save()
                                values.append({"path":sensor_method.method.path, "value" : sensor_method.value})

                return jsonify({ "value" : r.text, "values": values })
        elif r.status_code == 500:
            app.logger.error("Invoking method: Couldn't connect to YunServer - sensor %s (method %s) - path %s" % (sensor_id, method_id, path))
            return jsonify({ "value" : 'error', 'error' : 'The arduino server is not available.' })

    app.logger.error("Invoking method: Something went wrong - sensor %s (method %s) - path %s" % (sensor_id, method_id, path))
    return jsonify({ "value" : 'error', 'error' : 'Something went wrong while contacting the server.' })
Esempio n. 4
0
def sensor_send_value(sensor_id):
    sensor = SensorInteractor.get(sensor_id)

    if sensor:
        if sensor.active:
            gateways = GatewayInteractor.get_all_device_registered()

            if gateways:
                for gateway in gateways:
                    for sensor_method in sensor.sensor_methods:
                        if sensor_method.method.type == "write" and sensor_method.value:
                            r = request_helper.send_sensor_value(gateway.address, gateway.post_authorization, sensor.identificator, sensor_method.method.path, sensor_method.value)
                        elif sensor_method.method.type == "read":
                            r = request_helper.get_sensor_value(sensor, sensor_method.method.path)
                            if r != False:
                                sensor_method.value = r
                                sensor_method.save()
                                r = request_helper.send_sensor_value(gateway.address, gateway.post_authorization, sensor.identificator, sensor_method.method.path, sensor_method.value)
                            else:
                                app.logger.error("Getting value: Couldn't connect to YunServer - sensor %s (method %s) - path %s" % (sensor.id, sensor_method.method.id, sensor_method.method.path))
                                return jsonify({ "value" : 'error', 'error' : 'The arduino server is not available.' })
                    flash('Sensor method values successfully sent to gateway %s!' % gateway.address, category={ 'theme' : 'success' } )
            else:
                flash("No gateways with registered device!", category={ 'theme': 'warning' } )

            return redirect("/sensors/#%s" % sensor.identificator)
        else:
            flash('Sensor is not active!', category={ 'theme': 'warning' } )
            return redirect("/sensors/#%s" % sensor.identificator)

    flash('Sensor does not exist!', category={ 'theme': 'error' } )
    app.logger.error("Sending sensor values: Sensor does not exist")
    return redirect("/sensors/")
Esempio n. 5
0
def ip_cron():
    import json
    from app.arduino.common import PublicIPInteractor
    from urllib2 import urlopen

    ip = PublicIPInteractor.get()
    currentIP = "%s:%s" % ( json.load(urlopen('http://httpbin.org/ip'))['origin'].rstrip(), settings.PORT )

    app.logger.info("---- START IP cron START----")
    app.logger.info("IP ADDRESS - %s" % currentIP)

    if ( not ip.address or ip.address != currentIP ):
        try:
            for gateway in GatewayInteractor.get_all_device_registered():
                r = request_helper.delete_device(gateway.address, gateway.post_authorization)
                if r != False:
                    app.logger.info("Delete dev: %d" % r.status_code)
                    ip.address = currentIP
                    ip.save()
                    r = request_helper.init_device(gateway.address, gateway.post_authorization)
                    if r != False:
                        app.logger.info("Init dev: %d" % r.status_code)
                        r = request_helper.init_descriptor(gateway.address, gateway.post_authorization)
                        if r != False:
                            app.logger.info("Init descriptor: %d" % r.status_code)
                            r = request_helper.send_descriptor(gateway.address, gateway.post_authorization)
                            if r != False:
                                app.logger.info("Descriptor: %d" % r.status_code)
                                for sensor in SensorInteractor.get_all_active():
                                    for sensor_method in sensor.sensor_methods:
                                        r = request_helper.delete_sensor(gateway.address, gateway.post_authorization, sensor.identificator, sensor_method.method.path)
                                        if r != False:
                                            app.logger.info("Delete sensor method %s: %d" % (sensor_method.method.path, r.status_code))
                                            r = request_helper.init_sensor(gateway.address, gateway.post_authorization, sensor.identificator, sensor_method.method.path)
                                            if r != False:
                                                app.logger.info("Init sensor method %s: %d" % (sensor_method.method.path, r.status_code))
                                                if sensor_method.method.type in ["read", "write"]:
                                                    r = request_helper.send_sensor_value(gateway.address, gateway.post_authorization, sensor.identificator, sensor_method.method.path, sensor_method.value)
                                                    if r != False:
                                                        app.logger.info("Send method value %s: %d" % (sensor_method.method.path, r.status_code))
                                                        log = "%s - %s %s %s - %s (%s)" % ( gateway.address, sensor.module.hardware.name, sensor.module.name, sensor.identificator, sensor_method.method.path, sensor_method.value )
                                                        app.logger.info(log)
                            else:
                                ip.address = ""
                                ip.save()
                        else:
                            ip.address = ""
                            ip.save()
                    else:
                        ip.address = ""
                        ip.save()
        except:
            app.logger.error( "%s" % sys.exc_info()[0] )
            ip.address = ""
            ip.save()
    else:
        app.logger.warning("address wasn't changed")
    app.logger.info("----END IP cron END----")
    return make_response()
Esempio n. 6
0
def check_pin(identificator, pin):
    sensors = SensorInteractor.get_active_for_pin(pin)
    sensor_dict = {}
    if len(sensors) > 0:
        for sensor in sensors:
            if sensor.identificator != identificator:
                sensor_dict[str(sensor.id)] = sensor.identificator
    return jsonify(sensor_dict)
Esempio n. 7
0
def edit_sensor(sensor_id):
    sensor = SensorInteractor.get(sensor_id)
    if sensor:
        return render_template("/sensor/add.html", sensor = sensor, modules = ModuleInteractor.get_all() )

    flash("Sensor doesn't exist!", category={ 'theme': 'error' } )
    app.logger.error("Sensor editing view: Sensor doesn't exist")
    return redirect("/sensors/")
Esempio n. 8
0
def delete_sensor(sensor_id=None):
    if not sensor_id:
        sensor_id = request.form.get('sensor_id')

    sensor = SensorInteractor.get(sensor_id)

    if sensor:
        if sensor.active:
            gateways = GatewayInteractor.get_all_device_registered()

            if gateways:
                deleted_all_remote = True
                for gateway in gateways:
                    for sensor_method in sensor.sensor_methods:
                        r = request_helper.delete_sensor(gateway.address, gateway.post_authorization, sensor.identificator, sensor_method.method.path)
                        if r == False:
                            flash("Sensor method %s was not deleted on gateway %s!" % (sensor_method.method.path, gateway.address), category={ 'theme': 'warning' } )
                            deleted_all_remote = False

                    if deleted_all_remote:
                        if SensorInteractor.delete(sensor_id):
                            flash("Sensor deleted from gateway %s!" % gateway.address, category={ 'theme': 'success' } )
                    else:
                        flash("Sensor could not be deleted!", category={ 'theme': 'error' })
                        app.logger.error("Deleting sensor: Couldn't delete sensor from db because not all sensor methods could be deleted from GW")
                        return redirect("/sensors/#%s" % sensor.identificator)

                    request_helper.send_descriptor(gateway.address, gateway.post_authorization)
            else:
                flash("No gateways with registered device", category={ 'theme': 'warning' } )

        else:
            if SensorInteractor.delete(sensor_id):
                flash("Sensor deleted!", category={ 'theme': 'success' } )
            else:
                flash("Sensor could not be deleted!", category={ 'theme': 'error' } )
                app.logger.error("Deleting sensor: Could not delete sensor from db")
                return redirect("/sensors/#%s" % sensor.identificator)

    else:
        flash('Sensor does not exist!', category={ 'theme': 'error' } )
        app.logger.error("Deleting sensor: Sensor does not exist")
    return redirect("/sensors/")
Esempio n. 9
0
def register_device_on_gateway(gateway_id):
    gateway = GatewayInteractor.get(gateway_id)
    if gateway:
        r = request_helper.init_device(gateway.address, gateway.post_authorization)

        if r != False:
            if r.status_code == 201 or r.status_code == 409:
                r = request_helper.init_descriptor(gateway.address, gateway.post_authorization)
                if r != False:
                    r = request_helper.send_descriptor(gateway.address, gateway.post_authorization)
                    if r != False:
                        sensors = SensorInteractor.get_all_active()
                        if sensors:
                            for sensor in sensors:
                                if sensor.active:
                                    sensor.save()
                                    for sensor_method in sensor.sensor_methods:
                                        r = request_helper.init_sensor(
                                            gateway.address,
                                            gateway.post_authorization,
                                            sensor.identificator,
                                            sensor_method.method.path,
                                        )
                                        if (
                                            r != False
                                            and sensor_method.method.type in ["read", "write"]
                                            and sensor_method.value
                                        ):
                                            request_helper.send_sensor_value(
                                                gateway.address,
                                                gateway.post_authorization,
                                                sensor.identificator,
                                                sensor_method.method.path,
                                                sensor_method.value,
                                            )

                        flash("Device successfully registered!", category={"theme": "success"})
                        gateway.device_registered = True
                        gateway.save()

            elif r.status_code == 400 or r.status_code == 401:
                flash("Wrong authorization for registration!", category={"theme": "error"})
                app.logger.error("Registering device: Wrong authorization")

            else:
                flash("Something went wrong!", category={"theme": "error"})
                app.logger.error("Registering device: Unknown error during device initialization")
    else:
        flash("Gateway does not exist!", category={"theme": "error"})
        app.logger.error("Registering device: Gateway doesn't exist")

    return redirect("/")
Esempio n. 10
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/")
Esempio n. 11
0
def deactivate_sensor(sensor_id, descriptor=True):
    sensor = SensorInteractor.get(sensor_id)
    if sensor:
        sensor.active = False
        sensor.save()
        gateways = GatewayInteractor.get_all_device_registered()

        if gateways:
            for gateway in gateways:
                if descriptor:
                    r = request_helper.send_descriptor(gateway.address, gateway.post_authorization)
                for sensor_method in sensor.sensor_methods:
                    r = request_helper.delete_sensor(gateway.address, gateway.post_authorization, sensor.identificator, sensor_method.method.path)
                if r != False:
                    flash('Sensor successfully removed from gateway %s!' % gateway.address, category={ 'theme': 'success' } )
        else:
            flash("No gateways with registered device", category={ 'theme': 'warning' } )
        flash("Sensor deactivated!", category={ 'theme': 'success' } )
        return redirect("/sensors/#%s" % sensor.identificator)

    flash("Sensor does not exist!", category={ 'theme': 'error' } )
    app.logger.error("Activating sensor: Sensor does not exist")
    return redirect("/sensors/")
Esempio n. 12
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))
Esempio n. 13
0
def cron():
    app.logger.info("----START cron START----")
    try:
        for gateway in GatewayInteractor.get_all_device_registered():
            for sensor in SensorInteractor.get_all_active():
                for sensor_method in sensor.sensor_methods:
                    if sensor_method.method.type in ["read", "write"]:
                        if sensor_method.method.type == "read":
                            r = request_helper.get_sensor_value(sensor, sensor_method.method.path)
                            if r != False:
                                sensor_method.value = r
                                sensor_method.save()
                                if sensor_method.value:
                                    r = request_helper.send_sensor_value(gateway.address, gateway.post_authorization, sensor.identificator, sensor_method.method.path, sensor_method.value)
                                    if r != False:
                                        log = "%s - %s %s %s - %s (%s)" % ( gateway.address, sensor.module.hardware.name, sensor.module.name, sensor.identificator, sensor_method.method.path, sensor_method.value )
                                        app.logger.info(log)
                            else:
                                app.logger.error("CRON getting value: Couldn't connect to YunServer - sensor %s (method %s) - path %s" % (sensor.id, sensor_method.method.id, sensor_method.method.path))
                                return make_response()
    except:
        app.logger.error( "%s" % sys.exc_info()[0] )
    app.logger.info("----END cron END----")
    return make_response()
Esempio n. 14
0
def send_descriptor(address, post_authorization):

    headers = {'Authorization': 'Basic %s' % post_authorization, "content-type":"application/xml"}

    xml = '<?xml version="1.0" encoding="UTF-8"?>\
<obj xmlns="http://obix.org/ns/schema/1.1">\
    <str name="applicationID" val="%(app_id)s"/>\
    <list name="interfaces">\
        <obj>\
            <str name="interfaceID" val="Basic.Srv"/>\
        </obj>\
        <obj>\
            <str name="interfaceID" val="Identify.Srv"/>\
        </obj>' % { "app_id" : settings.DEVICE_ID }

    sensors = SensorInteractor.get_all_active()
    if sensors:

        sub_xml = ''

        for sensor in sensors:
            for sensor_method in sensor.sensor_methods:

                sub_xml = sub_xml + '<obj>\
                    <str name="interfaceID" val="%(sensor_identificator)s_%(method_path)s.Srv" />' % { "sensor_identificator" : sensor.identificator,"method_path" : sensor_method.method.path }
                if sensor_method.method.type in ["read", "write"]:
                    sub_xml = sub_xml + \
                    '<%(method_value_type)s href="%(sensor_href)s" name="m2mMeasuredValue" unit="obix:units/%(method_unit)s" />' %\
                        {
                            "method_value_type": sensor_method.method.value_type,
                            "method_unit" : sensor_method.method.unit,
                            "sensor_href" : '%s/%s%s/%s_%s%s%s' % (settings.APPLICATIONS, settings.DEVICE_ID, settings.CONTAINERS, sensor.identificator, sensor_method.method.path, settings.CONTENT_INSTANCES, settings.LATEST_CONTENT)
                        }
                if sensor_method.method.type in ["write", "call"]:
                    path = sensor_method.method.path
                    if sensor_method.method.type == "write":
                        path += "/[value-to-be-written]"
                    sub_xml = sub_xml + \
                        '<op type="%(method_type)s" name="%(method_name)s" href="/m2m/applications/%(app_id)s/retargeting1/sensors/%(sensor_id)s/%(method_path)s" />'\
                        % { "method_type" : sensor_method.method.type, "method_name" : sensor_method.method.path, "method_path" : path, "app_id" : settings.DEVICE_ID, "sensor_id" : sensor.identificator}
                sub_xml = sub_xml + '</obj>'

                init_sensor(address, post_authorization, sensor.identificator, sensor_method.method.path)

        xml = xml + sub_xml

    else:
        flash("No active sensors to send values for after DESCRIPTOR initiation!", category={ 'theme' : 'warning' } )

    xml = xml + '</list></obj>'

    try:
        r = requests.post("%s%s/%s%s%s%s" % (
                address,
                settings.APPLICATIONS,
                settings.DEVICE_ID,
                settings.CONTAINERS,
                settings.DESCRIPTOR,
                settings.CONTENT_INSTANCES
            ),
            headers = headers,
            timeout = 5,
            data = xml
        )

        return r

    except ConnectionError as e:
        flash("Cannot connect to gateway %s!" % address, category={ 'theme': 'error' } )
        app.logger.error("Sending descriptor: %s" % (str(e), ))
        return False

    except Timeout as e:
        flash("Request timed out. Wrong IP?", category={ 'theme': 'error' } )
        app.logger.error("Sending descriptor: %s" % (str(e), ))
        return False
Esempio n. 15
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/")
Esempio n. 16
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 })
Esempio n. 17
0
def get_sensors():
    sensors = SensorInteractor.get_all()
    return render_template("sensor/index.html", sensors = sensors )