Skip to content
app.py 3.36 KiB
Newer Older
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""API server, with a web client for free"""
Davide Ricci's avatar
Davide Ricci committed
import time
Davide Ricci's avatar
Davide Ricci committed
import logging
Davide Ricci's avatar
Davide Ricci committed
from flask import Flask, request
Davide Ricci's avatar
Davide Ricci committed
from flask_cors import CORS
from api import api_blueprint as api
Davide Ricci's avatar
Davide Ricci committed
from web.server import instance
app.register_blueprint(api, url_prefix='/api')
app.threaded = True
Davide Ricci's avatar
Davide Ricci committed
cors = CORS(app)

import configparser
# Load the config file
config = configparser.ConfigParser()
config.read('config/api.ini')
@app.route('/all/<string:namespace>')
def get_all(namespace):
#     '''Build a global status for a given namespace'''
#     wait = float(request.args.get('wait', 0))
    
    sections = []

    # sorting GET priority
    for section in config.sections():
        if section.startswith("/"+namespace):
            priority = config[section].getint('get-priority', float('inf'))
            sections.append((priority, section))
    # Sort sections based on priority (higher priority first)
    sections.sort()
    for _, section in sections:
        with app.test_client() as client:
            print(f"getting http://fork.orsa.unige.net:5533/api{section}")
            
            # res = requests.get(f"http://fork.orsa.unige.net:5533/api{section}")
            res = client.get(section)
            
            if res.status_code != 405:  # 405 Method Not Allowed
                
                name = "-".join(section.split("/")[2:]) # "dome-ping"
                # endpoints[name] = res.json()
                endpoints[name] = res.get_json()
                if "raw" in res.json():
                    if not res.json()["raw"]:
                        break
                elif (res.json()["response"] == "FAILED 2"):
                    break
Davide Ricci's avatar
Davide Ricci committed

# @app.route('/all/<string:namespace>')
# def get_status(namespace):
#     '''Build a global status for a given namespace'''
#     wait = float(request.args.get('wait', 0))
#     endpoints = {}

#     # Check for ping before going to all others
#     power = f"/api/{namespace}/ping"
#     with app.test_client() as client:
#         res = client.get(power)
#         name = "-".join(power.split("/")[2:]) # "dome-ping"
#         endpoints[name] = res.get_json()
#         if "raw" in res.get_json():
#             if not res.get_json()["raw"]:            
#                 return endpoints
    
#     for rule in app.url_map.iter_rules():
#         if rule.rule.startswith(f"/api/{namespace}/"): # "/api/dome/shutter/movement"
#             if "GET" in rule.methods:
#                 with app.test_client() as client:
#                     res = client.get(rule.rule)
#                     name = "-".join(rule.rule.split("/")[2:]) # "dome-shutter-movement"
#                     endpoints[name] = res.get_json()
#                     time.sleep(wait)

#     return endpoints

if __name__ == "__main__":
vertighel's avatar
vertighel committed

    with_web = True
        arg = sys.argv[1]
        if arg.startswith("--noweb"):
            with_web = False
            port = 5533
        else:
            port = arg
    except IndexError as e:
Davide Ricci's avatar
Davide Ricci committed

    hostname = "0.0.0.0"
Davide Ricci's avatar
Davide Ricci committed

    if with_web:
        socketio = instance.enable(app)
        socketio.run(app, host=hostname, port=port)
        app.run(host=hostname, port=port)