Example #1
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()
Example #2
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("/")
Example #3
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/")