Beispiel #1
0
    def Initialize(self, runType='AI'):
        """
        Initialize this module
        :param runType: run type enum: UI+AI or AI or UI
        :return: True or false
        """
        # Load config file
        ret = self._LoadConfig(runType)
        if not ret:
            LOG.error('Load Config failed!')
            return False

        # Construct sub modules
        self.__commMgr = CommManager(self.__runType)
        self.__serviceMgr = ServiceManager(self.__runType)
        self.__gameMgr = GameManager()
        self.__resultMgr = ResultManager()
        self.__msgHandler = MsgHandler(self.__commMgr, self.__gameMgr,
                                       self.__serviceMgr, self.__resultMgr,
                                       self.__resultType, self.__runType)
        self.__monitorMgr = MonitorManager(self.__runType)

        # Initialize sub modules
        if not self.__serviceMgr.Initialize():
            LOG.error('ServiceManager Initialize failed!')
            return False

        if not self.__commMgr.Initialize(DEFAULT_TBUS_CFG_PATH):
            LOG.error('CommMgr Initialize failed!')
            return False

        if not self.__msgHandler.Initialize():
            LOG.error('MsgHandler Initialize failed!')
            return False

        if not self.__monitorMgr.Initialize():
            LOG.error('MonitorManager Initialize failed!')
            return False

        if not self.__resultMgr.Initialize(taskID=self.__taskID,
                                           resultOutputPath=self.__resultPath,
                                           context=self.__resultCfg):
            LOG.error('ResultManager Initialize failed!')
            return False

        self.__initTimestamp = time.time()
        return True
Beispiel #2
0
    def Initialize(self):
        """
        Initialize this module
        :return: True or false
        """
        # Load config file
        ret = self._LoadConfig()
        if not ret:
            LOG.error('Load Config failed!')
            return False

        IO_SERVICE_CONTEXT['test_mode'] = self.__debugTestMode

        # Construct sub modules
        self.__clientSocket = SocketServer()
        self.__httpServer = HttpServer()
        self.__controlSocket = HTTPClient()
        self.__commMgr = TBUSMgr()
        self.__msgHandler = MsgHandler(self.__commMgr, self.__clientSocket,
                                       self.__httpServer, self.__controlSocket)

        # Initialize sub modules
        if not self.__commMgr.Initialize(DEFAULT_TBUS_CFG_PATH):
            LOG.error('TBUSMgr Initialize failed.')
            return False

        if IO_SERVICE_CONTEXT['io_service_type'] == 'HTTP':
            if not self.__httpServer.Initialize(self.__clientCfg):
                LOG.error('Http Server Initialize failed!')
                return False
        else:
            if not self.__clientSocket.Initialize(self.__clientCfg):
                LOG.error('Client Socket Initialize failed!')
                return False

        if not self.__controlSocket.Initialize(self.__controlCfg):
            LOG.error('Control Socket Initialize failed!')
            return False

        if not self.__msgHandler.Initialize():
            LOG.error('MsgHandler Initialize failed!')
            return False

        return True
Beispiel #3
0
class IOService(object):
    """
    IOService Main class
    """
    def __init__(self, taskCfgPath, platformCfgPath):
        self.__exited = False
        self.__loopCount = 0
        self.__debugShowFrame = False
        self.__debugTestMode = False

        self.__taskCfgPath = taskCfgPath
        self.__platformCfgPath = platformCfgPath

        self.__clientCfg = {}
        self.__controlCfg = {}

        self.__clientSocket = None
        self.__httpServer = None
        self.__controlSocket = None
        self.__commMgr = None
        self.__msgHandler = None

        self.__lastFrameSeq = 0

    def Initialize(self):
        """
        Initialize this module
        :return: True or false
        """
        # Load config file
        ret = self._LoadConfig()
        if not ret:
            LOG.error('Load Config failed!')
            return False

        IO_SERVICE_CONTEXT['test_mode'] = self.__debugTestMode

        # Construct sub modules
        self.__clientSocket = SocketServer()
        self.__httpServer = HttpServer()
        self.__controlSocket = HTTPClient()
        self.__commMgr = TBUSMgr()
        self.__msgHandler = MsgHandler(self.__commMgr,
                                       self.__clientSocket,
                                       self.__httpServer,
                                       self.__controlSocket)

        # Initialize sub modules
        tbus_cfg_path = os.path.join(SYS_CONFIG_DIR, TBUS_CFG_PATH)
        if not self.__commMgr.Initialize(tbus_cfg_path):
            LOG.error('TBUSMgr Initialize failed.')
            return False

        if IO_SERVICE_CONTEXT['io_service_type'] == 'HTTP':
            if not self.__httpServer.Initialize(self.__clientCfg):
                LOG.error('Http Server Initialize failed!')
                return False
        else:
            if not self.__clientSocket.Initialize(self.__clientCfg):
                LOG.error('Client Socket Initialize failed!')
                return False

        if not self.__controlSocket.Initialize(self.__controlCfg) or not self.__msgHandler.Initialize():
            LOG.error('Control Socket Initialize failed! or MsgHandler Initialize failed!')
            return False
        return True

    def Run(self):
        """
        Run this module
        :return:
        """
        while not self.__exited:
            beginTime = time.time()
            self._UpdateMsgHandler()  # Update Message handler
            updateMsgHandlerTime = time.time()
            self._UpdateFrame()  # Update frame
            updateFrameTime = time.time()
            self._UpdateOthers()
            endTime = time.time()
            sleepTime = MAIN_LOOP_SLEEP_TIME - (endTime - beginTime)
            if sleepTime > 0:
                time.sleep(sleepTime)
            elif endTime - beginTime > 0.05:
                overMS = int(-1000 * sleepTime)
                totalMS = int(1000 * MAIN_LOOP_SLEEP_TIME) + overMS
                msgHandlerMS = int(1000 * (updateMsgHandlerTime - beginTime))
                frameMS = int(1000 * (updateFrameTime - updateMsgHandlerTime))
                othersMS = int(1000 * (endTime - updateFrameTime))
                LOG.warning('MainLoop overschedule %sms: %s=%s+%s+%s', overMS, totalMS,
                            msgHandlerMS, frameMS, othersMS)
            self.__loopCount += 1

        self.__msgHandler.SendUnregisterToAIControl()

        LOG.info('Exit mainloop!')

    def Finish(self):
        """
        Finish this module
        :return:
        """
        self.__commMgr.Finish()
        self.__clientSocket.Finish()
        self.__httpServer.Finish()
        self.__controlSocket.Finish()

    def SetExited(self):
        """
        This module will exit when called
        :return:
        """
        self.__exited = True

    def _UpdateMsgHandler(self):
        self.__msgHandler.Update()

    def _UpdateFrame(self):
        if IO_SERVICE_CONTEXT['task_state'] != TASK_STATUS_INIT_SUCCESS:
            return

        frameBuff = IO_SERVICE_CONTEXT['frame']
        if frameBuff is None:
            return

        frameType = IO_SERVICE_CONTEXT['frame_type']
        extend = IO_SERVICE_CONTEXT['extend']
        frame = ImgDecode(frameBuff, frameType)
        frameSeq = IO_SERVICE_CONTEXT['frame_seq']

        if frame is None:
            LOG.error('Decode image error, check the image encode.')
            return

        if self.__lastFrameSeq == frameSeq:
            return

        if self.__debugShowFrame:
            cv2.imshow('IO', frame)
            cv2.waitKey(1)

        self.__msgHandler.SendFrameMsgToMC(frameSeq, frame, extend)
        self.__lastFrameSeq = frameSeq

    def _UpdateOthers(self):
        if self.__loopCount % (OTHERS_LOOP_COUNT * 10) == 0:
            if IO_SERVICE_CONTEXT['task_id'] is None:
                self.__msgHandler.SendRegisterToAIControl()

    def _LoadConfig(self):
        if os.path.exists(self.__platformCfgPath):
            iniCfg = configparser.ConfigParser()
            iniCfg.read(self.__platformCfgPath)
        else:
            LOG.error('Config File not exist in %s', self.__platformCfgPath)
            return False

        try:
            self.__debugShowFrame = iniCfg.getboolean('DEBUG', 'ShowFrame', fallback=False)
            self.__debugTestMode = iniCfg.getboolean('DEBUG', 'TestMode', fallback=False)

            self.__clientCfg = dict()
            self.__clientCfg['send_port'] = iniCfg.getint('CLIENT_COMMUNICATION', 'SendPort')
            self.__clientCfg['send_pattern'] = iniCfg.getint('CLIENT_COMMUNICATION', 'SendPattern')
            self.__clientCfg['recv_port'] = iniCfg.getint('CLIENT_COMMUNICATION', 'RecvPort')
            self.__clientCfg['recv_pattern'] = iniCfg.getint('CLIENT_COMMUNICATION', 'RecvPattern')
            self.__clientCfg['send_last_action'] = iniCfg.getint('CLIENT_COMMUNICATION',
                                                                 'SendLastAction')

            self.__controlCfg = dict()
            evASMIP = os.getenv('ASM_IP')
            if evASMIP is None:
                self.__controlCfg['ip'] = iniCfg.get('CONTROL_COMMUNICATION', 'IP')
            else:
                self.__controlCfg['ip'] = evASMIP

            evASMPort = os.getenv('ASM_PORT')
            if evASMPort is None:
                self.__controlCfg['port'] = iniCfg.getint('CONTROL_COMMUNICATION', 'Port')
            else:
                self.__controlCfg['port'] = int(evASMPort)

            self.__controlCfg['STAFFNAME'] = iniCfg.get('CONTROL_COMMUNICATION', 'StaffName')
            self.__controlCfg['pattern'] = iniCfg.getint('CONTROL_COMMUNICATION', 'Pattern')
        except KeyError as e:
            LOG.error('Load Config File[%s] failed, err: %s', self.__platformCfgPath, e)
            return False

        return True
Beispiel #4
0
class ManageCenter(object):
    """
    ManageCenter Main class
    """
    def __init__(self, taskCfgPath, platformCfgPath):
        self.__exited = False
        self.__loopCount = 0
        self.__debugShowFrame = False
        self.__initTimestamp = time.time()

        self.__taskCfgPath = taskCfgPath
        self.__platformCfgPath = platformCfgPath

        self.__runType = RUN_TYPE_UI_AI
        self.__resultType = RESULT_TYPE_AI

        self.__commMgr = None
        self.__gameMgr = None
        self.__resultMgr = None
        self.__msgHandler = None
        self.__serviceMgr = None
        self.__monitorMgr = None
        self.__lastFrameSeq = 0
        self.__lastMonitorResult = None

        self.__taskID = time.strftime("%Y%m%d_%H%M%S")

    def Initialize(self, runType='AI'):
        """
        Initialize this module
        :param runType: run type enum: UI+AI or AI or UI
        :return: True or false
        """
        # Load config file
        ret = self._LoadConfig(runType)
        if not ret:
            LOG.error('Load Config failed!')
            return False

        # Construct sub modules
        self.__commMgr = CommManager(self.__runType)
        self.__serviceMgr = ServiceManager(self.__runType)
        self.__gameMgr = GameManager()
        self.__resultMgr = ResultManager()
        self.__msgHandler = MsgHandler(self.__commMgr, self.__gameMgr,
                                       self.__serviceMgr, self.__resultMgr,
                                       self.__resultType, self.__runType)
        self.__monitorMgr = MonitorManager(self.__runType)

        # Initialize sub modules
        if not self.__serviceMgr.Initialize():
            LOG.error('ServiceManager Initialize failed!')
            return False

        if not self.__commMgr.Initialize(DEFAULT_TBUS_CFG_PATH):
            LOG.error('CommMgr Initialize failed!')
            return False

        if not self.__msgHandler.Initialize():
            LOG.error('MsgHandler Initialize failed!')
            return False

        if not self.__monitorMgr.Initialize():
            LOG.error('MonitorManager Initialize failed!')
            return False

        if not self.__resultMgr.Initialize(taskID=self.__taskID,
                                           resultOutputPath=self.__resultPath,
                                           context=self.__resultCfg):
            LOG.error('ResultManager Initialize failed!')
            return False

        self.__initTimestamp = time.time()
        return True

    def Run(self):
        """
        Loop run funtion
        :return:
        """
        while not self.__exited:
            beginTime = time.time()
            self._UpdateMsgHandler()  # Update Message handler
            updateMsgTime = time.time()
            self._UpdateFrame()  # Update frame
            updateFrameTime = time.time()
            self._UpdateOthers()
            endTime = time.time()
            sleepTime = MAIN_LOOP_SLEEP_TIME - (endTime - beginTime)
            if sleepTime > 0:
                time.sleep(sleepTime)
            elif endTime - beginTime > 0.05:
                overMS = int(-1000 * sleepTime)
                totalMS = int(1000 * MAIN_LOOP_SLEEP_TIME) + overMS
                msgMS = int(1000 * (updateMsgTime - beginTime))
                frameMS = int(1000 * (updateFrameTime - updateMsgTime))
                othersMS = int(1000 * (endTime - updateFrameTime))
                LOG.warning(
                    'MainLoop overschedule {0}ms: {1}={2}+{3}+{4}'.format(
                        overMS, totalMS, msgMS, frameMS, othersMS))
            self.__loopCount += 1

        self.__msgHandler.SendServiceRegisterMsgToIO(SERVICE_UNREGISTER)

        LOG.info('Exit mainloop!')

    def Finish(self):
        """
        Finish this module
        :return:
        """
        self.__resultMgr.Finish()
        self.__commMgr.Finish()
        self.__serviceMgr.Finish()

    def SetExited(self):
        """
        This module will exit when called
        :return:
        """
        self.__exited = True

    def _UpdateMsgHandler(self):
        self.__msgHandler.Update()

    def _UpdateFrame(self):
        if not self.__serviceMgr.IsTaskReady():
            return

        frame = self.__gameMgr.GetGameFrame()
        if frame is None:
            return

        frameSeq = self.__gameMgr.GetFrameSeq()
        if self.__lastFrameSeq == frameSeq:
            return

        data = self.__gameMgr.GetGameData()

        if self.__debugShowFrame:
            cv2.imshow('MC', frame)
            cv2.waitKey(1)

        if self.__gameMgr.GameStarted():
            addrList = self.__serviceMgr.GetAllServiceAddr(
                serviceType=SERVICE_TYPE_REG)
            for addr in addrList:
                self.__msgHandler.SendGameFrameMsgTo(addr=addr,
                                                     gameFrame=frame,
                                                     gameData=data,
                                                     frameSeq=frameSeq)

        addrList = self.__serviceMgr.GetAllServiceAddr(
            serviceType=SERVICE_TYPE_UI)
        for addr in addrList:
            self.__msgHandler.SendUIAPIStateMsgTo(addr=addr,
                                                  gameFrame=frame,
                                                  frameSeq=frameSeq)

        self.__lastFrameSeq = frameSeq

    def _UpdateOthers(self):
        now = time.time()
        if now - self.__initTimestamp < 30:
            return

        if self.__loopCount % OTHERS_LOOP_COUNT == 0:
            self._UpdateMonitor()

    def _UpdateMonitor(self):
        result = self.__monitorMgr.GetResult()

        if result is None:
            return

        if self.__lastMonitorResult == result:
            return

        if result == ALL_NORMAL:
            LOG.info('All services process is running!')
        else:
            if result & AGENT_EXIT:
                LOG.warning('Agent service process exit!')
            if result & UI_EXIT:
                LOG.warning('UI service process exit!')
            if result & REG_EXIT:
                LOG.warning('Reg service process exit!')

        self.__msgHandler.SendAIServiceStateToIO(result)
        self.__lastMonitorResult = result

    def _LoadConfig(self, runType):
        if runType == 'UI+AI':
            self.__runType = RUN_TYPE_UI_AI
            self.__resultType = RESULT_TYPE_UI
        elif runType == 'AI':
            self.__runType = RUN_TYPE_AI
            self.__resultType = RESULT_TYPE_AI
        elif runType == 'UI':
            self.__runType = RUN_TYPE_UI
            self.__resultType = RESULT_TYPE_UI
        else:
            LOG.error('Invalid RunType {0}'.format(runType))
            return False

        LOG.info('RunType is {0}'.format(self.__runType))

        if not self._LoadPlatformConfig():
            return False

        self._LoadTaskConfig()

        self.__resultPath = os.getenv('AI_SDK_RESULT_SAVE_PATH',
                                      os.path.join('..', 'result'))

        return True

    def _LoadPlatformConfig(self):
        if os.path.exists(self.__platformCfgPath):
            iniCfg = configparser.ConfigParser()
            iniCfg.read(self.__platformCfgPath)
        else:
            LOG.error('Config File not exist in {0}'.format(
                self.__platformCfgPath))
            return False

        try:
            self.__debugShowFrame = iniCfg.getboolean('DEBUG',
                                                      'ShowFrame',
                                                      fallback=False)

            self.__resultCfg = dict()
            self.__resultCfg['enable'] = iniCfg.getboolean('RESULT',
                                                           'Enable',
                                                           fallback=False)
            self.__resultCfg['fps'] = iniCfg.getint('RESULT',
                                                    'FPS',
                                                    fallback=10)
            self.__resultCfg['result_url'] = iniCfg.get('RESULT', 'ResultURL')
            self.__resultCfg['state_url'] = iniCfg.get('RESULT', 'StateURL')
            self.__resultCfg['token'] = iniCfg.get('RESULT', 'Token')
            self.__resultCfg['api_user'] = iniCfg.get('RESULT',
                                                      'APIUser',
                                                      fallback='ai_sdk')
            self.__resultCfg['rtx_user'] = iniCfg.get('RESULT',
                                                      'RTXUser',
                                                      fallback='ai_sdk')
            self.__resultCfg['timeout'] = iniCfg.getint('RESULT',
                                                        'Timeout',
                                                        fallback=3600)

            self.__resultType = iniCfg.get('RESULT',
                                           'Type',
                                           fallback=self.__resultType)
        except Exception as e:
            LOG.error('Load Config File[{}] failed, err: {}'.format(
                self.__platformCfgPath, e))
            return False

        return True

    def _LoadTaskConfig(self):
        if os.path.exists(self.__taskCfgPath):
            pass
            # with open(self.__taskCfgPath) as fd:
            #     taskCfg = json.load(fd)
        else:
            # LOG.error('Config File not exist in {0}'.format(self.__taskCfgPath))
            return False

        return True