from flask import Blueprint, render_template, jsonify, request, Response, send_from_directory bp = Blueprint('routes', __name__) from .utils import Utils import subprocess, os, time, random, json # from app.models import mapdata_sub, map_save import app.models as mod @bp.route('/', methods=["GET", "POST"]) def chassis(): u = Utils() code, cmd, msg, data = u.parse_json(request.get_json()) resp = "" match cmd: case u.MAPPING: # subprocess.run(["bash", "/home/lenovo/dev/srv/sh/1mapping.sh"], shell=False) subprocess.run(["bash", "/workspaces/srv/sh/1mapping.sh"], shell=False) case u.SAVE_MAP: # subprocess.run(["bash", "/home/lenovo/dev/srv/sh/2save_map.sh"], shell=False) subprocess.run(["bash", "/workspaces/srv/sh/2save_map.sh"], shell=False) # map_save() case u.NAV: # subprocess.run(["bash", "/home/lenovo/dev/srv/sh/3start_nav.sh"], shell=False) op = "" if type(data) is dict: op = data.get("op") op = "" if op == None else op subprocess.run(["bash", "/workspaces/srv/sh/3start_nav.sh", f"{op}"], shell=False) # if op == "": # subprocess.run(["bash", "/workspaces/srv/sh/13recorder.sh", "4"], shell=False) # else: # subprocess.run(["bash", "/workspaces/srv/sh/13recorder.sh", "5"], shell=False) # subprocess.run(["bash", "/workspaces/srv/sh/13recorder.sh", f"{op}"], shell=False) case u.MAPPING_DATA: try: resp = mod.mapdata_sub() except: resp = {} case u.MAP_EDIT: resp = mod.map_edit(data=data) # subprocess.run(["bash", "/home/lenovo/dev/srv/sh/4hello.sh"], shell=False) # subprocess.run(["bash", "/workspaces/srv/sh/4hello.sh"], shell=False) case u.MOVE: # resp = mod.move(data=data) x_speed, y_speed, angular, stop = data.get("x_speed"), data.get("y_speed"), data.get("angular"), data.get("stop") if stop: subprocess.run(["bash", "/workspaces/srv/sh/4move.sh", "0.0", "0.0", "0.0", "0.0"], shell=False, stdout=subprocess.DEVNULL) else: subprocess.run(["bash", "/workspaces/srv/sh/4move.sh", f"{x_speed}", f"{y_speed}", f"{angular}"], shell=False, stdout=subprocess.DEVNULL) # x_speed, y_speed, angular = data.get("x_speed"), data.get("y_speed"), data.get("angular") # subprocess.run(["bash", "/workspaces/srv/sh/4move.sh", f"{x_speed}", f"{y_speed}", f"{angular}"], shell=False) case u.STOP_AUTO_CHARGE: subprocess.run(["bash", "/workspaces/srv/sh/5stop_charge.sh"], shell=False) case u.STATION_EDIT: resp = mod.station_edit(data=data) case u.SCHEDULE: resp = mod.schedule(data=data) case u.START_WS: subprocess.run(["bash", "/workspaces/srv/sh/6start_ws.sh"], shell=False) # resp = mod.mot_ctrl(code=code, data=data) case u.OTA: heart_beat = data.get("heart_beat") if heart_beat != None: if heart_beat: resp = mod.upgrade_info(data=data) else: repo = data.get("repo") branch = data.get("branch") item = data.get("item") subprocess.run(["bash", "/workspaces/srv/sh/7ota.sh", f"{repo}", f"{branch}", f"{item}"], shell=False) case u.LOG: resp = mod.logger(data=data) case u.MONITOR: resp = mod.monitor(data=data) case u.MANAGE_MAP: resp = mod.map_manager(data=data) case u.MANAGE_SET: resp = mod.set_manager(data=data) case u.VISION: resp = mod.vision(data=data) case u.DBM: pass case u.RECORDER: resp = mod.recorder(data=data) case u.START_MAPPING: subprocess.run(["curl", "http://localhost:5000/start_mapping"], shell=False) case u.STOP_MAPPING: subprocess.run(["curl", "http://localhost:5000/stop_mapping"], shell=False) case u.DISCARD_MAPPING: subprocess.run(["curl", "http://localhost:5000/discard_mapping"], shell=False) case u.UPLOAD_MAPPING: number = data.get("number") path = "/workspaces/srv/app/static/imgs/" map_path = f"{path}" if number != None: map_path = f"{path}{number}" os.mkdir(map_path) ret = subprocess.run(["curl", "http://localhost:5000/map_name"], shell=False, capture_output=True, text=True) resp = json.loads(ret.stdout) map_name = resp.get("map_name") subprocess.run(["curl", "-X", "POST", "-F" f"pgm=@{map_path}/map.pgm", "-F", f"yaml=@{map_path}/map.yaml", f"http://localhost:5000/upload_map"], shell=False) subprocess.run(["curl", "-F", f"pcd=@{map_path}/map.pcd", "-F", f"map_name={map_name}", "-F", "reload=true", f"http://localhost:5000/upload_pcd"], shell=False) # subprocess.run(["curl", "-F", f"pcd=@{map_path}/map.pcd", "-F", f"map_name={map_name}", "-F", "reload=true", f"http://localhost:5000/upload_pcd"], shell=False) case u.LIST_MAPPING: ret = subprocess.run(["curl", "http://localhost:5000/list_map"], shell=False, capture_output=True, text=True) resp = json.loads(ret.stdout) case u.MAP_NAME: ret = subprocess.run(["curl", "http://localhost:5000/map_name"], shell=False, capture_output=True, text=True) resp = json.loads(ret.stdout) case u.SWITCH_MAP: number = data.get("number") ret = subprocess.run(["curl", "-X", "POST", "-H","Content-Type: application/json", "-d", json.dumps({"map_name": str(number)}), "http://localhost:5000/upload_map"], shell=False, capture_output=True, text=True) resp = json.loads(ret.stdout) case u.DOWNLOAD_ALL_MAP: path = "/workspaces/srv/app/static/imgs/" ret = subprocess.run(["curl", "http://localhost:5000/list_map"], shell=False, capture_output=True, text=True) maps = json.loads(ret.stdout).get("maps") for item in maps: map_path = f"{path}{item}" if not os.path.exists(map_path): os.mkdir(map_path) subprocess.run(["curl", "-o", f"{map_path}/map.pgm", f"http://localhost:5555/api/maps/{item}/pgm/map.pgm"], shell=False) subprocess.run(["curl", "-o", f"{map_path}/map.yaml", f"http://localhost:5555/api/maps/{item}/pgm/map.yaml"], shell=False) subprocess.run(["curl", "-o", f"{map_path}/map.pcd", f"http://localhost:5000/download_pcd?map_name={item}"], shell=False) subprocess.run(["curl", "-o", f"{map_path}/save.json", f"http://localhost:5000/download_geojson?map_name={item}"], shell=False) subprocess.run(["curl", "-o", f"{map_path}/speedlimit.json", f"http://localhost:5000/download_speedlimit?map_name={item}"], shell=False) case u.MAP_EDIT2: resp = mod.map_edit2(data) case u.DOWNLOAD_MAP: ret = subprocess.run(["curl", "http://localhost:5000/map_name"], shell=False, capture_output=True, text=True) resp = json.loads(ret.stdout) mode = resp.get("mode") map_path = "/workspaces/srv/app/static/imgs/" if mode == "NAVIGATION": map_name = resp.get("map_name") subprocess.run(["curl", "-o", f"{map_path}/map.pgm", f"http://localhost:5555/api/maps/{map_name}/pgm/map.pgm"], shell=False) subprocess.run(["curl", "-o", f"{map_path}/map.yaml", f"http://localhost:5555/api/maps/{map_name}/pgm/map.yaml"], shell=False) subprocess.run(["curl", "-o", f"{map_path}/map.pcd", f"http://localhost:5000/download_pcd?map_name={item}"], shell=False) subprocess.run(["curl", "-o", f"{map_path}/save.json", f"http://localhost:5000/download_geojson?map_name={item}"], shell=False) subprocess.run(["curl", "-o", f"{map_path}/speedlimit.json", f"http://localhost:5000/download_speedlimit?map_name={item}"], shell=False) resp = "done" else: resp = "fail" case u.GET_NAV_STATE: ret = subprocess.run(["curl", "-X", "POST", "http://localhost:4000/navigation/get_state"], shell=False, capture_output=True, text=True) resp = json.loads(ret.stdout).get("current_state") print(resp) case u.SET_NAV_STATE: state = {"state": data.get("state")} subprocess.run(["curl", "-X", "POST", "-H", "Content-Type: application/json", "-d", json.dumps(state), f"http://localhost:4000/navigation/set_state"], shell=False) case u.RESTART_LOCATION: ret = subprocess.run(["pm2", "restart", "all"]) case u.GET_MAP_LIST: ret = subprocess.run(["curl", "http://localhost:5555/api/maps"], shell=False, capture_output=True, text=True) resp = json.loads(ret.stdout) case u.MAKE_MAP_DIR: map_name = data.get("map_name") ret = subprocess.run(["curl", "-X", "POST", "-H", "Content-Type: application/json", "-d", json.dumps({"mapName": str(map_name)}), "http://localhost:5555/api/maps"], shell=False) case u.GET_MAP_DATA: map_name = data.get("map_name") ret = subprocess.run(["curl", f"http://localhost:5555/api/maps/{map_name}"], shell=False, capture_output=True, text=True) resp = json.loads(ret.stdout) case u.SAVE_MAP_DATA: map_name = data.get("map_name") number = data.get("number") file_path = f"/workspaces/srv/app/static/imgs/{number}/save.json" with open(file_path, "w+", encoding="utf-8") as f: json.dump(data.get("save"), f, ensure_ascii=False) f.close() ret = subprocess.run(["curl", "-X", "POST", "-H", "Content-Type: application/json", "--data-binary", f"@{file_path}", f"http://localhost:5555/api/maps/{map_name}"], shell=False) case u.DELETE_MAP_DIR: map_name = data.get("map_name") ret = subprocess.run(["curl", "-X", "DELETE", f"http://localhost:5555/api/maps/{map_name}"], shell=False) case u.DOWNLOAD_PGM: map_name = data.get("map_name") file_name = data.get("file_name") ret = subprocess.run(["curl", f"http://localhost:5555/api/maps/{map_name}/pgm/{file_name}"], shell=False) # case u.UOLOAD_PGM: # ret = subprocess.run(["curl", f"http://localhost:5555/api/maps/{map_name}/pgm/{file_name}"], shell=False) case u.GET_SPD_DATA: map_name= data.get("map_name") ret = subprocess.run(["curl", f"http://localhost:5555/api/maps/{map_name}/speedlimit/speedlimit.json"], shell=False, capture_output=True, text=True) resp = json.loads(ret.stdout) case u.SAVE_SPD_DATA: map_name = data.get("map_name") file_path = "/workspaces/srv/app/static/js/speedlimit.json" with open(file_path, "w+", encoding="utf-8") as f: json.dump(data.get("speedlimit"), f, ensure_ascii=False) f.close() ret = subprocess.run(["curl", "-X", "PUT", "-H", "Content-Type: application/json", "--data-binary", f"@{file_path}", f"http://localhost:5555/api/maps/{map_name}"], shell=False) case u.REGISTER: subprocess.run(["python3", "/workspaces/ws/client/register.py"]) return u.generate_json(cmd, "", resp) @bp.route('/upload_setting', methods=["POST"]) def upload_setting(): number = request.form.get("settingDir") if number == None or len(number) == 0: number = f"setting{time.time()*1000:.0f}{random.randint(1000, 9999)}" dir = f"/workspaces/srv/app/static/js/{number}" subprocess.run(["mkdir", "-p", dir], shell=False) return upload(upload_files=request.files.getlist("files"), dirs=[ dir ]) @bp.route('/upload_map', methods=["POST"]) def upload_map(): # dir = f"/workspaces/srv/app/static/imgs/map{time.time():.0f}" number = request.form.get("mapDir") if number == None or len(number) == 0: number = f"map{time.time()*1000:.0f}{random.randint(1000, 9999)}" dir = f"/workspaces/srv/app/static/imgs/{number}" subprocess.run(["mkdir", "-p", dir], shell=False) return upload(upload_files=request.files.getlist("files"), dirs=[ dir, ], prefix=(), suffix=("map.pgm", "map.yaml", "map.pcd", "map.png")) # r"/workspaces/nav_ws/src/classic_nav_bringup/map/" # def upload_map(): # return upload(upload_files=request.files.getlist("files"), dirs=[ # r"/workspaces/srv/app/static/imgs/", # ], prefix=("bk"), suffix=("map.pgm", "map.yaml", "map.pcd")) def upload(upload_files=None, dirs=[], prefix=(), suffix=()): u = Utils() save_result = [] prefix_number = f"{prefix}{time.time():.0f}" for file in upload_files: filename = file.filename # 过滤无文件名的空文件 if not filename: continue if len(suffix) > 0 and not filename.endswith(suffix): continue if len(prefix) > 0: if not filename.startswith(prefix): filename = f"{prefix_number}_{filename}" else: prefix_number = filename[:filename.find("_")] # 保存文件 for dir in dirs: file.save(os.path.join(dir, filename)) save_result.append(filename) number = [] for dir in dirs: dir_ = dir[dir.rfind("/")+1:] if len(dir_) > 0: number.append(dir_) if len(number) == 0: number.append(prefix_number) return u.generate_json(u.MANAGE_MAP, "", {"saved": save_result, "number": number}) @bp.route('/templates/', methods=["GET"]) def web(): return render_template("index.html") @bp.route("/assets/") def serve_assets(filename): assets_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "assets") return send_from_directory(assets_dir, filename)