server.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. from flask import Flask, request, jsonify
  2. import configparser
  3. import os
  4. from flask_cors import CORS
  5. import logging
  6. app = Flask(__name__)
  7. CORS(app)
  8. CONFIG_FILE_PATH = '/oem/usr/etc/myconf/IMX283_set.conf'
  9. USER_CONFIG_PATH = '/oem/usr/etc/myconf/USER.conf'
  10. config = configparser.ConfigParser()
  11. user_config = configparser.ConfigParser()
  12. def load_config():
  13. global config
  14. try:
  15. config.read(CONFIG_FILE_PATH, encoding='utf-8')
  16. except Exception as e:
  17. app.logger.error(f"Error loading config file: {e}")
  18. def load_user_config():
  19. global user_config
  20. try:
  21. user_config.read(USER_CONFIG_PATH,encoding='utf-8')
  22. except Exception as e:
  23. app.logger.error(f"Error loading config file: {e}")
  24. load_config()
  25. load_user_config()
  26. @app.route('/getUserConfig',methods=['GET'])
  27. def get_user_config():
  28. try:
  29. user_config_dict = {}
  30. for section in user_config.sections():
  31. user_config_dict[section] = {}
  32. for key,value in user_config.items(section):
  33. user_config_dict[section][key] =value
  34. return jsonify(user_config_dict),200
  35. except Exception as e:
  36. app.logger.error(f"Error reading config: {e}")
  37. return jsonify({"error": str(e), 'code': 500}), 500
  38. @app.route('/getConfig', methods=['GET'])
  39. def get_config():
  40. try:
  41. config_dict = {}
  42. for section in config.sections():
  43. config_dict[section] = {}
  44. for key, value in config.items(section):
  45. config_dict[section][key] = value
  46. return jsonify(config_dict), 200
  47. except Exception as e:
  48. app.logger.error(f"Error reading config: {e}")
  49. return jsonify({"error": str(e), 'code': 500}), 500
  50. @app.route('/setUserConfig',methods=['POST'])
  51. def set_user_config():
  52. try:
  53. update_config = request.json
  54. if not update_config:
  55. return jsonify({"error": "No configuration provided", 'code': 400}), 400
  56. for section,items in update_config.items():
  57. if section not in config:
  58. user_config.add_section(section)
  59. for key ,value in items.items():
  60. app.logger.debug(f"Setting {section} -> {key} = {value}")
  61. user_config.set(section, key, value)
  62. with open(USER_CONFIG_PATH, 'w', encoding='utf-8') as configfile:
  63. user_config.write(configfile)
  64. load_user_config()
  65. return jsonify({"message": "Configuration updated successfully", 'code': 200}), 200
  66. except Exception as e:
  67. app.logger.error(f"Error updating config: {e}")
  68. return jsonify({"error": str(e), 'code': 500}), 500
  69. @app.route('/setConfig', methods=['POST'])
  70. def set_config():
  71. try:
  72. updated_config = request.json
  73. if not updated_config:
  74. return jsonify({"error": "No configuration provided", 'code': 400}), 400
  75. for section, items in updated_config.items():
  76. if section not in config:
  77. config.add_section(section)
  78. for key, value in items.items():
  79. app.logger.debug(f"Setting {section} -> {key} = {value}")
  80. config.set(section, key, value)
  81. with open(CONFIG_FILE_PATH, 'w', encoding='utf-8') as configfile:
  82. config.write(configfile)
  83. load_config()
  84. return jsonify({"message": "Configuration updated successfully", 'code': 200}), 200
  85. except Exception as e:
  86. app.logger.error(f"Error updating config: {e}")
  87. return jsonify({"error": str(e), 'code': 500}), 500
  88. @app.route('/reboot', methods=['POST'])
  89. def reboot():
  90. try:
  91. os.system('reboot') # 重启系统
  92. return jsonify({'message': 'System is rebooting...', 'code': 200}), 200 # 返回消息和状态码
  93. except Exception as e:
  94. return jsonify({'message': 'Error occurred while attempting to reboot.', 'error': str(e), 'code': 500}), 500 # 出错时返回错误信息和状态码
  95. if __name__ == '__main__':
  96. app.run(debug=True, host='0.0.0.0', port=5000)