def Sys_Reply_ParamBlock_Result(self, bstrOrigParamBlockSign, iResultCode): bstrOrigParamBlockSign = CTYLB_MainSys_MiscFunc.SafeGetUTF8(bstrOrigParamBlockSign) self.Viul_FinishParamBlock( bstrOrigParamBlockSign, iResultCode) if( bstrOrigParamBlockSign in self.s_bstrPromptWaitFinishParamSignArray): bRemove = False if( iResultCode == CTYBot_CTUnit_CommonData.s_g_iIntValue_Task_Finish): bRemove = True elif( iResultCode == CTYBot_CTUnit_CommonData.s_g_iIntValue_Task_Finish_Fail): bRemove = True if( bRemove): self.s_bstrPromptWaitFinishParamSignArray.remove( bstrOrigParamBlockSign) CTYLB_Log.ShowLog( 0,'param-reply', 'block %s run-finish. %d, still_buff_len:[%d]' % ( bstrOrigParamBlockSign, iResultCode, len( self.s_bstrPromptWaitFinishParamSignArray)) ) msgObject={ "monitor_type":"block-finish-reply", "level":"info", "target":"", "plugin_id":self.s_iRunPluginID, "block_id":bstrOrigParamBlockSign, "block_size":0, "free_size":0, "wait_size":len( self.s_bstrPromptWaitFinishParamSignArray), "success_size":0, "result_code":iResultCode, "msg":""} CTYLB_Log.ShowMonitor(msgType="PLC",msgObject=msgObject) pass pass
def Sys_Schedule_RequestParamBlock(self): retParamBlockArray = [] while( self.s_iCanSendRequestParamCount > 0): # 检查时间符合? assignWebNameBlock = self.Viul_Internal_ParamBlock_ReadEach_CTUnit() if( assignWebNameBlock): self.s_iCanSendRequestParamCount -= 1 # 执行数-1 # 提交,发送 retParamBlockArray.append( assignWebNameBlock) # 加入参数到等待完成队列 self.s_bstrPromptWaitFinishParamSignArray.append(assignWebNameBlock.s_bstrUniqueSign) CTYLB_Log.ShowLog(0, 'send-task-center', 'prompt-run-param-block:%d [%s]' % (len(assignWebNameBlock.s_bstrDomainNameArray), assignWebNameBlock.s_bstrUniqueSign)) msgObject={ "monitor_type":"send-task-block", "level":"info", "target":"", "plugin_id":assignWebNameBlock.s_iRunPluginID, "block_id":assignWebNameBlock.s_bstrUniqueSign, "block_size":len(assignWebNameBlock.s_bstrDomainNameArray), "free_size":0, "wait_size":0, "success_size":0, "result_code":0, "msg":""} CTYLB_Log.ShowMonitor(msgType="PLC",msgObject=msgObject) else: break return retParamBlockArray
def Sys_Reply_SendResultCount_Finish(self, iResultCount, bstrRequestCTUnitSign): CTYLB_Log.ShowLog( 0, 'recv-task-center', 'recv result count:%d' % (iResultCount)) msgObject={ "monitor_type":"send-task-block-reply", "level":"info", "target":"", "plugin_id":self.s_iRunPluginID, "block_id":"", "block_size":0, "free_size":0, "wait_size":0, "success_size":iResultCount, "result_code":0, "msg":""} CTYLB_Log.ShowMonitor(msgType="PLC",msgObject=msgObject)
def CtrlCHandle(signum, frame): global g_bSysRunning CTYLB_Log.ShowLog(1, 'CTRL_C', "Ctrl+C Input, exiting") msgObject = { "monitor_type": "status", "level": "error", "target": "", "plugin_id": -1, "block_id": "", "block_size": 0, "free_size": 0, "wait_size": 0, "success_size": 0, "result_code": 0, "msg": "Ctrl+C keyboard detected, PLC exiting..." } CTYLB_Log.ShowMonitor(msgType="PLC", msgObject=msgObject) g_bSysRunning = False
def Sys_Reply_Plugin_RunParam_Count(self, iPluginID, iRemoteTaskCenterFreeCount): if( iPluginID == self.s_iRunPluginID): self.s_iCanSendRequestParamCount = iRemoteTaskCenterFreeCount CTYLB_Log.ShowLog( 0, 'recv-task-center', 'plugin:%d free:%d' % (iPluginID, iRemoteTaskCenterFreeCount)) msgObject={ "monitor_type":"recv-task-block-free", "level":"info", "target":"", "plugin_id":iPluginID, "block_id":"", "block_size":0, "free_size":iRemoteTaskCenterFreeCount, "wait_size":0, "success_size":0, "result_code":0, "msg":""} CTYLB_Log.ShowMonitor(msgType="PLC",msgObject=msgObject)
def Viul_Notify_Recv_RunResult(self, bstrParamSign, iReplyPluginID, bstrOrigDomain, bstrResult): bstrParamSign = CTYLB_MainSys_MiscFunc.SafeGetUTF8(bstrParamSign) bstrOrigDomain = CTYLB_MainSys_MiscFunc.SafeGetUTF8(bstrOrigDomain) bstrResult = CTYLB_MainSys_MiscFunc.SafeGetUTF8(bstrResult) bstrShowResult = bstrResult s2 = bstrShowResult.replace(b'\r', b'') bstrShowResult = s2.replace(b'\n', b'') CTYLB_Log.ShowLog( 0, 'result-arrive', '[%s] - [%s] [%d] [%s]' % (bstrParamSign, bstrOrigDomain, iReplyPluginID, bstrShowResult)) msgObject={ "monitor_type":"result", "level":"info", "target":bstrOrigDomain, "plugin_id":self.s_iRunPluginID, "block_id":"", "block_size":0, "free_size":0, "wait_size":0, "success_size":0, "result_code":0, "msg":bstrResult } CTYLB_Log.ShowMonitor(msgType="PLC",msgObject=msgObject) pass
# 主程序入口 if __name__ == '__main__': msgObject = { "monitor_type": "status", "level": "info", "target": "", "plugin_id": -1, "block_id": "", "block_size": 0, "free_size": 0, "wait_size": 0, "success_size": 0, "result_code": 0, "msg": "PLC starting..." } CTYLB_Log.ShowMonitor(msgType="PLC", msgObject=msgObject) iNextFileReadPos = 0 iArgCount = len(sys.argv) if (iArgCount < 2): print('input run: [skip_block_count], default is 0') elif (iArgCount >= 2): iNextFileReadPos = int(sys.argv[1]) msgObject = { "monitor_type": "status", "level": "info", "target": "", "plugin_id": -1, "block_id": "", "block_size": 0,
def Vilu_TimerCheck(self): bRetValue = False # 对每个扫描任务进行检查调度 for eachPromptTask in self.s_RunnDTaskArray: if( eachPromptTask.Vilu_ExecTimerCheck()): bRetValue = True if( self.s_x_TaskRegCenter_Sock.ExecNextCheck()): bRetValue = True if( self.s_x_TaskRegCenter_Sock.CanExecSendData()): bRetValue = True # 对每个任务的准备函数进行调用 needPromptParamArray = [] for eachDTask in self.s_RunnDTaskArray: curParamArray = eachDTask.Sys_Schedule_RequestParamBlock() needPromptParamArray.extend(curParamArray) # 请求插件ID的空余数量 newCommDataCTUnit = CTYBot_CTUnit_CommonData() newCommDataCTUnit.SetIntData(CTYBot_CTUnit_CommonData.s_g_iIntValue_Query_PluginID_Run_ParamBlock_Count) newCommDataCTUnit.SetParam(str(eachDTask.s_iRunPluginID).encode(), b'') needPromptParamArray.append(newCommDataCTUnit) # 提交发送 if( not needPromptParamArray): newCommDataCTUnit = CTYBot_CTUnit_CommonData() needPromptParamArray.append( newCommDataCTUnit) self.s_x_TaskRegCenter_Sock.ExecSendData( needPromptParamArray) pass recvCTArray = self.s_x_TaskRegCenter_Sock.PopRetriveRecvCTArray() if( recvCTArray): bRetValue = True bstrPeerName, iPeerPort = self.s_hlSockMang.GetSockPeerAddr(self.s_x_TaskRegCenter_Sock.s_iExecConnectSockIndex) for eachUnit in recvCTArray: # CTYLB_Log.ShowLog(0, 'data-receive', 'from [%s:%d] recv [%d]' % (strPeerName, iPeerPort, eachUnit.s_iMyDataType)) msgObject = { "monitor_type": "status", "level": "info", "target": "", "plugin_id": -1, "block_id": "", "block_size": 0, "free_size": 0, "wait_size": 0, "success_size": 0, "result_code": 0, "msg": "PLC received <%d> bytes from <%s:%d>" % (eachUnit.s_iMyDataType, bstrPeerName, iPeerPort) } CTYLB_Log.ShowMonitor(msgType="PLC", msgObject=msgObject) if (eachUnit.s_iMyDataType == CTYLB_Bot_BaseDef.s_g_iDataType_CommonTask): self.HandleRecv_CommonData(bstrPeerName, eachUnit) elif (eachUnit.s_iMyDataType == CTYLB_Bot_BaseDef.s_g_iDataType_TaskRegCenter_sk_v1): if (eachUnit.s_iBotCmd_Main == CTYLB_Bot_BaseDef.s_g_iMainCmd_TaskRegCenter_ReportResult_V2): self.HandleRecv_Result_V2(bstrPeerName, eachUnit) self.s_x_TaskRegCenter_Sock.SetNextTempSendWaitTick(0) # 有结果到达,可能还有结果数据,这时候不等待,立即再发送获取 elif (eachUnit.s_iBotCmd_Main == CTYLB_Bot_BaseDef.s_g_iMainCmd_TaskRegCenter_ReportResult): self.HandleRecv_Result(bstrPeerName, eachUnit) self.s_x_TaskRegCenter_Sock.SetNextTempSendWaitTick(0) # 有结果到达,可能还有结果数据,这时候不等待,立即再发送获取 else: CTYLB_Log.ShowLog(1, 'data-receive', 'Unknown packet:%d-%d-%d' % (eachUnit.s_iMyDataType, eachUnit.s_iBotCmd_Main, eachUnit.s_iBotCmd_Sub)) msgObject = { "monitor_type": "status", "level": "warning", "target": "", "plugin_id": -1, "block_id": "", "block_size": 0, "free_size": 0, "wait_size": 0, "success_size": 0, "result_code": 0, "msg": "PLC unknown package received <Type:%d,MainCMD:%d,SubCMD:%d>" % (eachUnit.s_iMyDataType, eachUnit.s_iBotCmd_Main, eachUnit.s_iBotCmd_Sub) } CTYLB_Log.ShowMonitor(msgType="PLC", msgObject=msgObject) pass return bRetValue