home_router.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. import json
  5. from fastapi import APIRouter, Body
  6. from pathlib import Path
  7. from module.config.utils import write_file
  8. from module.logger import logger
  9. from module.ocr.rpc import shutdown_ocr_server
  10. from module.server.main_manager import MainManager
  11. from module.server.updater import Updater
  12. from module.server.i18n import I18n
  13. home_app = APIRouter(
  14. prefix="/home",
  15. tags=["home"],
  16. )
  17. @home_app.get('/test')
  18. async def home_test():
  19. return {'message': 'test'}
  20. # gcc -Wall -pedantic -shared -fPIC -o group_work.so group_work.c -lwiringPi
  21. @home_app.get('/home_menu')
  22. async def home_menu():
  23. return {'Home': [], 'Updater': [], 'Tool': []}
  24. @home_app.post('/notify_test')
  25. async def notify_test(setting: str, title: str, content: str):
  26. from module.notify.notify import Notifier
  27. try:
  28. notifier = Notifier(setting, True)
  29. if notifier.push(title=title, content=content):
  30. del notifier
  31. return True
  32. else:
  33. del notifier
  34. return False
  35. except Exception as e:
  36. logger.exception(e)
  37. return str(e)
  38. @home_app.get('/kill_server')
  39. async def kill_server():
  40. shutdown_ocr_server()
  41. MainManager.signal_kill_server = True
  42. return 'success'
  43. @home_app.get('/update_info')
  44. async def update_info():
  45. try:
  46. updater = Updater()
  47. result = {'is_update': updater.check_update(),
  48. 'branch': updater.current_branch(),
  49. 'current_commit': updater.current_commit(),
  50. 'latest_commit': updater.latest_commit(),
  51. 'commit': updater.get_commit(n=15),
  52. }
  53. return result
  54. except Exception as e:
  55. logger.error(e)
  56. return None
  57. @home_app.get('/execute_update')
  58. async def execute_update():
  59. # 下拉仓库 -> 关闭所有脚本进程 -> 最后重启oasx
  60. try:
  61. updater = Updater()
  62. updater.execute_pull()
  63. except Exception as e:
  64. logger.error(e)
  65. return '手动更新将会立即结束运行中的脚本服务, 最后你还需重启oasx'
  66. @home_app.put('/chinese_translate')
  67. async def chinese_translate(data: dict = Body(...)):
  68. try:
  69. I18n.save_zh_cn(data)
  70. except Exception as e:
  71. logger.error(e)
  72. return True
  73. @home_app.get('/additional_translate')
  74. async def additional_translate() -> dict:
  75. try:
  76. data = I18n.load_additions()
  77. return data
  78. except Exception as e:
  79. logger.error(e)
  80. return {}
  81. @home_app.get('/export_diagnostic')
  82. async def export_diagnostic(config_name: str = ''):
  83. from module.server.diagnostic import build_diagnostic_zip
  84. try:
  85. zip_path = build_diagnostic_zip(config_name)
  86. return {'success': True, 'path': str(zip_path)}
  87. except Exception as e:
  88. logger.exception(e)
  89. return {'success': False, 'path': '', 'error': str(e)}