Exemplo n.º 1
0
def edit_gateway(gateway_id):
    gateway = GatewayInteractor.get(gateway_id)
    if gateway:
        from app.helpers.base64_helper import b64encode_quote

        authorization = b64encode_quote(request.form.get("authorization"))
        name = request.form.get("name")
        is_active = True if request.form.get("is_active") else False

        if authorization == gateway.authorization and name == gateway.name and is_active == gateway.active:
            flash("You didn't change anything!", category={"theme": "warning"})
            return redirect("/gateway/%d/edit/" % (gateway_id,))

        r = request_helper.check_gateway(gateway.address, authorization)

        if r != False:
            if r.status_code == 200:
                if r.text:
                    m = re.findall(r"<m2m:holderRef>(.*?)</m2m:holderRef>", r.text)
                    gateway.post_authorization = b64encode_quote(m[1])
                    gateway.active = True
                    if not is_active:
                        gateway.active = False
                    gateway.name = name if name else m[1].split("//")[1].split(".")[0].lower()

                    gateway.save()

                    r = request_helper.check_device(gateway.address, authorization)

                    if r != False:
                        if r.status_code == 200:
                            flash("Device is already registered on gateway!", category={"theme": "success"})
                            gateway.device_registered = True
                            gateway.save()

                        elif r.status_code == 404:
                            flash("Device is not registered.", category={"theme": "warning"})
                else:
                    flash("Response was not a valid accessRights XML!", category={"theme": "error"})
                    app.logger.error("Editing gateway: Invalid accessRights XML")

            elif r.status_code == 400 or r.status_code == 404:
                flash("Wrong authorization URI! Gateway wasn't edited!", category={"theme": "error"})
                app.logger.error("Editing gateway: Wrong authorization URI")
                return edit_gateway_view(gateway_id)

    else:
        flash("Gateway doesn't exist!", category={"theme": "error"})
        app.logger.error("Editing gateway: Gateway does not exist")
    return redirect("/")
Exemplo n.º 2
0
def gateway():

    gateway = Gateway()

    address = request.form.get("address")
    address = address if "http://" in address else "http://%s" % address

    if GatewayInteractor.check_address(address):
        flash(
            "Gateway wasn't saved beacuse it already exists for this device! If you need to change the authorization, edit the existing one!",
            category={"theme": "warning", "life": 6000},
        )
        return redirect("/")

    from app.helpers.base64_helper import b64encode_quote

    authorization = b64encode_quote(request.form.get("authorization"))

    r = request_helper.check_gateway(address, authorization)

    if r != False:
        if r.status_code == 200:
            if r.text:
                m = re.findall(r"<m2m:holderRef>(.*?)</m2m:holderRef>", r.text)
                gateway.post_authorization = b64encode_quote(m[1])
                gateway.name = (
                    request.form.get("name") if request.form.get("name") else m[1].split("//")[1].split(".")[0]
                )
                gateway.address = address
                gateway.authorization = authorization
                if request.form.get("is_active"):
                    gateway.active = True
                try:
                    gateway.save()
                except:
                    flash(
                        "Gateway wasn't saved beacuse it already exists for this device! If you need to change the authorization, edit the existing one!",
                        category={"theme": "warning", "life": 6000},
                    )
                    return redirect("/")

                r = request_helper.check_device(address, authorization)

                if r != False:
                    if r.status_code == 200:
                        flash("Device is already registered on gateway!", category={"theme": "success"})
                        gateway.device_registered = True
                        gateway.save()

                        # remove_device_from_gateway(gateway.id)
                        # register_device_on_gateway(gateway.id)

                    elif r.status_code == 404:
                        flash("Device is not registered.", category={"theme": "warning"})
            else:
                flash("Response was not a valid accessRights XML!", category={"theme": "error"})
                app.logger.error("Adding gateway: Invalid accessRights XML")

        elif r.status_code == 400 or r.status_code == 404:
            flash("Wrong authorization URI! Gateway wasn't added!", category={"theme": "error"})
            app.logger.error("Adding gateway: Wrong authorization URI")

    return redirect("/")