コード例 #1
0
def get_history():
    # id = authenticator.verify_token(request.json['token'])
    router_id = request.json['router_id']
    sensor_id = request.json['sensor_id']
    start = request.json['start']
    end = request.json['end']
    print(request.json)

    if start == 0 or end == 0:
        start = None
        end = None

    hh = HistoricHandler()

    if sensor_id == "ALL":
        data = {}
        db = DatabaseHandler()
        sensors = db.get_router_sensors(router_id)

        for x in sensors[:]:
            s_id = x[0]
            res = hh.get_reading(router_id, s_id, start=start, end=end)
            print("Results: " + res)
            data.update({s_id: res})

        return jsonify(data=data)

    result = hh.get_reading(router_id, sensor_id, start=start, end=end)
    ret = {}
    ret.update({sensor_id: result})

    return jsonify(data=ret)
コード例 #2
0
def get_actuators():
    res = DatabaseHandler().get_actuators(request.json['router_id'])
    send_message_admin(
        "GET ACTUATOR", "by user " +
        DatabaseHandler().get_user_from_id(request.json['token']) +
        " from router " + str(request.json['router_id']))
    return jsonify(res), 200
コード例 #3
0
def get_sensors():
    result = DatabaseHandler().get_router_sensors(request.json['router_id'])
    send_message_admin(
        "GET SENSOR", "By user " +
        DatabaseHandler().get_user_from_id(request.json['token']) +
        " to router " + str(request.json['router_id']))
    return jsonify(result), 200
コード例 #4
0
ファイル: api.py プロジェクト: SCC331Penguins/CloudServerV2
def request_live_data():
    router_id = request.json['router_id']
    if DatabaseHandler().user_has_router(router_id, request.json['token']) is False:
        return jsonify(result=False)
    topic_name = str(uuid.uuid4())
    message = MessageCreator(MessageCreator.NEW_CHANNEL, topic_name)
    send_message(router_id, message)
    send_message_admin("API", "New Live data link by user " + DatabaseHandler().get_user_from_id(request.json['token']) + " to router " + str(router_id))
    return jsonify(topic_name=topic_name), 200
コード例 #5
0
ファイル: user.py プロジェクト: SCC331Penguins/CloudServerV2
def claim_router():
    router_id = request.json['router_id']
    resp = DatabaseHandler().claim_router(router_id, request.json['token'])
    if resp is True:
        send_message_admin(
            "USER CLAIM", "By user " +
            DatabaseHandler().get_user_from_id(request.json['token']) +
            " to router " + str(request.json['router_id']))
    return jsonify(result=resp), 200
コード例 #6
0
 def check_auth(*args, **kwargs):
     res = DatabaseHandler().user_has_router(
         user_id=request.json['token'], router_id=request.json['router_id'])
     if res is False:
         authed = DatabaseHandler().user_has_auth(
             user_id=request.json['token'],
             router_id=request.json['router_id'])
         if authed is False:
             return jsonify(False), 200
     return f(*args, **kwargs)
コード例 #7
0
ファイル: user.py プロジェクト: SCC331Penguins/CloudServerV2
def remove_authenticated_user():
    router_id = request.json['router_id']
    username = request.json['username']
    result = DatabaseHandler().get_user(username=username)
    if result is None:
        return jsonify(result=False), 201
    id = result.id
    result = DatabaseHandler().remove_auth_user(id, router_id)
    if result is False:
        return jsonify(False), 201
    return jsonify(id=id, username=username), 200
コード例 #8
0
def set_config():
    sensors = request.json["sensors"]
    payload = []
    for sensor in sensors[:]:
        DatabaseHandler().set_sensor_config(sensor['id'], sensor['config'])
        payload.append({"id": sensor['id'], "config": sensor['config']})
    message = MessageCreator(MessageCreator.UPDATE_SENSORS, payload)
    send_message(request.json['router_id'], message)
    send_message_admin(
        "POST SENSOR", "By user " +
        DatabaseHandler().get_user_from_id(request.json['token']) +
        " to router " + str(request.json['router_id']))
    return jsonify(True), 200
コード例 #9
0
def Server(id=None):
    db = DatabaseHandler()
    router_warnings = db.getRouterWarnings("The Server")
    newRouter = {}
    newRouter['name'] = "The Server"
    newRouter['lastOnline'] = round(time.time())
    newRouter['warnings'] = []
    for warning in router_warnings[:20]:
        new_warning = {}
        new_warning['level'] = warning.w_level
        new_warning['msg'] = warning.w_msg
        new_warning['time'] = warning.w_time
        newRouter['warnings'].append(new_warning)
    return jsonify(newRouter), 200
コード例 #10
0
def set_script():
    router_id = request.json['router_id']
    script = request.json['script']
    res = DatabaseHandler().add_script(router_id, script)
    if res is True:
        scripts = DatabaseHandler().get_script(router_id=router_id)
        scripts_list = []
        for x in scripts[:]:
            scripts_list.append(x.script)
        message = MessageCreator(MessageCreator.UPDATE_SCRIPT, scripts_list)
        send_message(router_id, message)
    send_message_admin(
        "POST SCRIPT", "New script added by user " +
        DatabaseHandler().get_user_from_id(request.json['token']) +
        " to router " + str(router_id))
    return jsonify(res), 200
コード例 #11
0
 def admin(*args, **kwargs):
     id = verify_token(request.json['token'])
     if id is False:
         return jsonify(False), 200
     if DatabaseHandler().check_admin(id) is False:
         return jsonify(False), 200
     return f(*args, **kwargs)
コード例 #12
0
 def admin(*args, **kwargs):
     # token comes from query string instead of POST body because i want to do authorised GET Requests
     id = verify_token(request.args.get('token'))
     if id is False:
         return jsonify(False), 401
     if DatabaseHandler().check_admin(id) is False:
         return jsonify(False), 401
     return f(*args, **kwargs)
コード例 #13
0
def Router(id=None):
    if (id == None):
        return jsonify(None), 400
    db = DatabaseHandler()
    router = db.getRouter(id)
    router_warnings = db.getRouterWarnings(id)
    newRouter = {}
    newRouter['name'] = router.router_id
    newRouter['lastOnline'] = router.last_heard
    newRouter['actuators'] = db.get_actuators(id)
    newRouter['warnings'] = []
    for warning in router_warnings:
        new_warning = {}
        new_warning['level'] = warning.w_level
        new_warning['msg'] = warning.w_msg
        new_warning['time'] = warning.w_time
        newRouter['warnings'].append(new_warning)
    return jsonify(newRouter), 200
コード例 #14
0
ファイル: user.py プロジェクト: SCC331Penguins/CloudServerV2
def register():
    username = request.json['username']
    password = request.json['password']
    resp = DatabaseHandler().register_user(username, password)
    if resp is not False:
        send_message_admin("USER REGISTER", "New user " + str(username))
        print(resp)
        send_user_update(resp['username'], remove=False, id=resp['id'])
        resp = True
    return jsonify(resp), 200
コード例 #15
0
ファイル: user.py プロジェクト: SCC331Penguins/CloudServerV2
def login():
    username = request.json['username']
    password = request.json['password']
    resp = DatabaseHandler().login_user(username, password)
    if resp[0][0] is True:
        send_message_admin("USER LOGIN",
                           "By user " + str(username) + ", logged in")
    else:
        send_message_admin("USER LOGIN",
                           "By user " + str(username) + ", does not exist")
    return jsonify(logged_in=resp[0][0], token=resp[0][1]), 200
コード例 #16
0
def Routers():
    db = DatabaseHandler()

    routers = db.getAllRouters()
    newRouters = []
    router_warnings = db.getAllRouterWarnings()
    for router in routers:
        newRouter = {}
        newRouter['name'] = router.router_id
        newRouter['lastOnline'] = router.last_heard
        newRouter['warnings'] = []
        for warning in router_warnings:
            if warning.originName == router.router_id:
                new_warning = {}
                new_warning['level'] = warning.w_level
                new_warning['msg'] = warning.w_msg
                new_warning['time'] = warning.w_time
                newRouter['warnings'].append(new_warning)
        newRouters.append(newRouter)
    return jsonify(newRouters), 200
コード例 #17
0
def get_home_data():
    routers = DatabaseHandler().get_routers()
    routers_total = len(routers)
    routers_online = 0
    for router in routers[:]:
        status = DatabaseHandler().get_router_status(router['router_id'])
        if status is True:
            routers_online = routers_online + 1
    routers_claimed = 0
    for router in routers[:]:
        id = router['router_id']
        owner = DatabaseHandler().get_router_owner(id)
        if owner is None:
            routers_claimed = routers_claimed + 1
    routers_unclaimed = routers_total - routers_claimed
    sensors = DatabaseHandler().get_sensors()
    sensors_total = len(sensors)
    sensors_claimed = 0
    for sensor in sensors[:]:
        result = DatabaseHandler().is_sensor_claimed(sensor['sensor_id'])
        if result is True:
            sensors_claimed = sensors_claimed + 1
    sensor_online = 0
    for sensor in sensors[:]:
        owner = sensor['router']
        if owner is not None:
            res = DatabaseHandler().get_router_status(owner)
            if res is True:
                sensor_online = sensor_online + 1
    sensor_unclaimed = sensors_total - sensors_claimed
    users_total = len(DatabaseHandler().get_users())
    return jsonify(routers={
        "total": routers_total,
        "online": routers_online,
        "claimed": routers_unclaimed,
        "unclaimed": routers_claimed
    },
                   sensors={
                       "total": sensors_total,
                       "claimed": sensors_claimed,
                       "unclaimed": sensor_unclaimed,
                       "online": sensor_online
                   },
                   users={"total": users_total}), 200
コード例 #18
0
def create_app():
    logger.info("Creating Flask init_app")
    app = Flask(__name__)
    app.config.from_pyfile('config.py')
    CORS(app)
    with app.app_context():
        db.init_app(app)
    flask_admin = Admin(app)
    with app.test_request_context():
        from database.models import Users, Router, Sensor, Warnings
        flask_admin.add_view(ModelView(Users, db.session))
        flask_admin.add_view(ModelView(Router, db.session))
        flask_admin.add_view(ModelView(Sensor, db.session))
        flask_admin.add_view(ModelView(Warnings, db.session))
        register_blueprints(app)
        db.create_all()
        from MQTT.mqtt_client import client
        from handlers.database_handler import DatabaseHandler
        channels = DatabaseHandler().get_router_channels()
        for ch in channels[:]:
            client.subscribe(ch)
            print("MQTT: Subscribing to: " + str(ch))
        client.loop_start()
    return app
コード例 #19
0
def get_users():
    result = DatabaseHandler().get_users_admin()
    return jsonify(result), 200
コード例 #20
0
ファイル: user.py プロジェクト: SCC331Penguins/CloudServerV2
def get_authed():
    router_id = request.json['router_id']
    result = DatabaseHandler().get_authed_users(router_id)
    if result is None:
        return jsonify(result=[]), 201
    return jsonify(result), 200
コード例 #21
0
def get_routers():
    res = DatabaseHandler().get_user_routers(request.json['token'])
    send_message_admin(
        "GET ROUTER", "Get router request from user " +
        DatabaseHandler().get_user_from_id(request.json['token']))
    return jsonify(res), 200
コード例 #22
0
ファイル: api.py プロジェクト: SCC331Penguins/CloudServerV2
def set_rooms():
    router_id = request.json['router_id']
    sensors = request.json['sensors']
    DatabaseHandler().save_rooms(router_id, sensors)
    return jsonify(result=True), 200
コード例 #23
0
ファイル: api.py プロジェクト: SCC331Penguins/CloudServerV2
def control_actuator():
    router_id = request.json['router_id']
    message = MessageCreator(MessageCreator.COMMAND, {'MAC': request.json['MAC'], "command": request.json['command']})
    send_message(router_id, message)
    send_message_admin("API", "Actuator control by user " + DatabaseHandler().get_user_from_id(request.json['token']) + " to router " + str(router_id))
    return jsonify(True), 200
コード例 #24
0
def add_router():
    result = DatabaseHandler().add_router(request.json['router_id'])
    if result is True:
        send_router_update(request.json['router_id'], remove=False)
    return jsonify(result=result), 200
コード例 #25
0
def get_sensors():
    return jsonify(DatabaseHandler().get_sensors()), 200
コード例 #26
0
def get_routers():
    return jsonify(DatabaseHandler().get_routers()), 200
コード例 #27
0
def remove_router():
    DatabaseHandler().remove_router(request.json['router_id'])
    send_router_update(request.json['router_id'], remove=True)
    return jsonify(result=True), 200
コード例 #28
0
def remove_user():
    DatabaseHandler().remove_user(request.json['username'])
    send_user_update(request.json['username'], remove=True)
    return jsonify(result=True), 200
コード例 #29
0
def set_user_admin():
    DatabaseHandler().set_user_admin(request.json['username'],
                                     request.json['admin'])
    send_admin_update(request.json['username'], request.json['admin'])
    return jsonify(result=True), 200