databaseInterface.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. from flask import Flask, request
  2. from flask_cors import CORS
  3. from pathlib import Path
  4. import json
  5. import os
  6. app = Flask(__name__)
  7. CORS(app)
  8. DIRECTORY = os.path.dirname(os.path.realpath(__file__))
  9. @app.route("/get-path", methods=["GET"])
  10. def get_path():
  11. with open(Path(DIRECTORY, "path.json"), "r") as pathFile:
  12. path = pathFile.read()
  13. return path
  14. @app.route("/set-path", methods=["POST"])
  15. def set_path():
  16. path = request.json.get("path")
  17. filePath = Path(DIRECTORY, "path.json")
  18. with open(filePath, "r") as pathFile:
  19. pathDict = json.load(pathFile)
  20. pathDict[path.get("name")] = path.get("coords")
  21. with open(filePath, "w") as pathFile:
  22. json.dump(pathDict, pathFile, indent=2)
  23. return "ok", 200
  24. @app.route("/set-time", methods=["POST"])
  25. def set_time():
  26. data = request.json
  27. filePath = Path(DIRECTORY, "time.json")
  28. with open(filePath, "r") as timeFile:
  29. timeList = json.load(timeFile)
  30. timeList.append(data)
  31. with open(filePath, "w") as timeFile:
  32. json.dump(timeList, timeFile, indent=2)
  33. return "ok", 200
  34. if __name__=="__main__":
  35. app.run(host='0.0.0.0')