#!D:/IDE/anaconda3/envs/Microscope_Scan python
# -*- coding: UTF-8 -*-
"""
@FileName：MicScanSystem.py
@Description：
@Author：XSH
@Time：2025/11/11 10:11
@Department：研发部
@Copyright：©2025 乐辰生物
"""
import os
import threading
from PyQt5 import Qt
import sys
import asyncio
from qasync import QEventLoop, asyncSlot
from AppInterfaces.AppTimeLoop import AppTimeLoop
from AppSettings.Settings import *
from AppCore.Levels import *
from AppInterfaces.AppDaemonController import AppDaemonController, DaemonController

if __name__ == "__main__":
    app = Qt.QApplication(sys.argv)
    """使用异步"""
    loop = QEventLoop(app)
    asyncio.set_event_loop(loop)
    DaemonController.Manager = loop
    from AppContexts.Contexts import *
    from AppUtils.AppUtils import AppUtils
    from AppUtils.AppConvert import AppConvert
    from AppSettings.AppSetting import AppConfig
    """加载各模块参数"""
    AppConfig.reload()
    """判断服务端口是否可用 如果不可用 直接退出"""
    if AppUtils.IsServerAvailable(AppConfig.WebConfig.API):
        from AppUtils.DataBaseDeploy import DataBaseDeploy, ChamberStatusModel, TrayModel
        DataBaseDeploy.init()
        from AppSettings.SystemSetting import SystemConfig
        SystemConfig.UpdateMaxLocation()
        TrayModel.UpdateMaxLocation()
        from AppCore.AppManagers.HardWareManager            import HardWareManager
        from AppHeadEnd.ControlWindows.MainController       import MainController
        from AppBackEnd.AppScanningFlow                     import AppScanningFlow, AppBusinessInterface
        from AppCore.DBModels                               import *
        from AppBackEnd.AppBackEndBusiness                  import *
        from AppHeadEnd.DBModelViews.DBModelView            import DBModelView
        AppEventBusPool.GetInstance()
        business: AppBusinessInterface = AppScanningFlow.GetInstance()
        business.initModules()
        frame: MainController = MainController.GetInstance(business=business)
        frame.show()
        DaemonController.Start(loop)
        sys.exit(app.exec())
    else:
        print("端口被占用，请检查！")
        sys.exit()
