示例#1
0
    def Post(jsonDataString, retryCount=1, timeout=3):
        '''
        提交数据到API接口
        :param jsonDataString: 需要提交的数据,Json字符串编码
        :param retryCount: 失败重试次数
        :param timeout: 提交超时时间,单位秒
        :return: 成功返回True,失败返回False
        '''

        CTYLB_Log.ShowLog(0, "准备发送-post", jsonDataString)
        jsonDataString = jsonDataString.encode('utf-8')
        while retryCount > 0:

            try:
                res = requests.post(HttpPoster.ApiUrl,
                                    headers=HttpPoster.Headers,
                                    data=jsonDataString,
                                    timeout=timeout)
                return True
            except Exception as e:
                retryCount = retryCount - 1
                if retryCount == 0:
                    CTYLB_Log.ShowLog(2, AIOSWebOSFiber.FiberName,
                                      "POST数据到WEB OS后台展示失败:{0}".format(e), 1)
        return False
示例#2
0
 def v_On_RemoteReplyMsgResult(self, strMsgExecUID, msgExecRet):
     if(strMsgExecUID in self.s_dict_msgResult.keys()):
         oldTime = self.s_dict_msgResult[strMsgExecUID]
         timeDiff = datetime.now() - oldTime
         CTYLB_Log.ShowLog(0, "recv task reply", "time:%d, still count:%d" %
                           (timeDiff.total_seconds(), len(self.s_dict_msgResult)))
         self.s_dict_msgResult.pop(strMsgExecUID)
     else:
         CTYLB_Log.ShowLog(0, "Recv Reply from:%s"%strMsgExecUID, msgExecRet.s_strRetValue)
     pass
示例#3
0
    def v_HandleRecvPacket(self, strFromName, iMsgType, strMsgContent):
        if(iMsgType == Share_UTRC_CfgDef.g_iFbUTRC_Cmd_Report_FiberUnitArray):

            comuFiberNamesUnit = CUTRC_NATs_ComuFiberList()
            if(comuFiberNamesUnit.LoadFromStr(strMsgContent)):
                CTYLB_Log.ShowLog(0, "Recv %s Report Fiber" % (strFromName), "count:%d" % (len(comuFiberNamesUnit.s_dict_TaskName_strParam_UID)))
                self.s_recvClientFibersDicts[strFromName] = comuFiberNamesUnit

        elif(iMsgType == Share_UTRC_CfgDef.g_iFbUTRC_Cmd_Run_Message):
            comuMsgUnit = CUTRC_NATs_Comu_RunMessageUnit()
            if(comuMsgUnit.LoadFromStr(strMsgContent)):
                self.s_recvClientCmdMsgArray.append(comuMsgUnit)
            pass

        elif(iMsgType == Share_UTRC_CfgDef.g_iFbUTRC_Cmd_Reply_Message_Result):
            comuReplyMsgUnit = CUTRC_NATs_Comu_RunMessageUnit()
            if(comuReplyMsgUnit.LoadFromStr(strMsgContent)):
                self.s_recvClientReplyCmdMsgResult_Array.append(comuReplyMsgUnit)
            pass

        elif(iMsgType==Share_UTRC_CfgDef.g_iFbUTRC_Cmd_Request_AllClientInfo):
            self.s_recvClient_RequestAllClientInfo_Array.append(strFromName)
            pass

        # self.AddContentToSend(eachRecvMsg.s_strPeerMsgName, eachRecvMsg.s_iMsgType, strReplyContent)
        pass
示例#4
0
    def OnListen_Recv_Peer_BroadCastFbUnitName(self, peerName, value, param1, param2, is_last_package):
        CTYLB_Log.ShowLog(0, "from %s"%(peerName), "recv fiber name list")
        self.s_collect_FbNameDict_Mang.Handle_Recv_PeerFBot_FbNames_Dict(value)

        retCTUnitArray = []
        if(is_last_package):
            retCTUnitArray = self.ReplyPackage_Send_To_PeerFBot(peerName)
        return retCTUnitArray
示例#5
0
    def OnListen_Recv_Peer_ExecRunMessage(self, peerName, value, param1, param2, is_last_package):
        # 找到节点,投递,执行命令
        CTYLB_Log.ShowLog(0, "from %s" % (peerName), "recv peer-fbot-srv exec fiber cmd")

        self.ExecRunMessage(Collect_FbNameDict_Mang.s_g_iType_Peer_FBot, value)

        retCTUnitArray = []
        if(is_last_package):
            retCTUnitArray = self.ReplyPackage_Send_To_PeerFBot(peerName)
        return retCTUnitArray
示例#6
0
    def Exec_Reply_Message_Result(self, iFromDomainType, strMsgContent):
        comuMsgUnit_ReplyResult = CUTRC_NATs_Comu_RunMessageUnit()
        if (comuMsgUnit_ReplyResult.LoadFromStr(strMsgContent)):
            iDomainType, strDestClientName = self.s_collect_FbNameDict_Mang.GetRemoteClientName_By_FbUnit_Content(
                comuMsgUnit_ReplyResult.s_strFromFbUnit_FullName, comuMsgUnit_ReplyResult.s_strFromFbUnit_StrParam,
                comuMsgUnit_ReplyResult.s_lFromFbUnit_lParam1, comuMsgUnit_ReplyResult.s_lFromFbUnit_lParam2)

            strFrom, strDest = self.GetFromDestFullName(comuMsgUnit_ReplyResult)
            CTYLB_Log.ShowLog(0, "执行 回复", "[%s] -> [%s]:%d" % (strFrom, strDest, comuMsgUnit_ReplyResult.s_iRun_MessageID))

            if (strDestClientName):
                if (iDomainType == Collect_FbNameDict_Mang.s_g_iType_LocalPeerNats):
                    # 传递给该client单元
                    self.s_AsyncNATS_ProcComu.AddContentToSend(
                        strDestClientName, Share_UTRC_CfgDef.g_iFbUTRC_Cmd_Reply_Message_Result,
                        strMsgContent)

                elif (iDomainType == Collect_FbNameDict_Mang.s_g_iType_Local_FBotClient):
                    # 增加到队列
                    newPackage = FBotV4.make_package(command_id=Share_UTRC_CfgDef.g_iFbUTRC_Cmd_Reply_Message_Result,
                                                     value=strMsgContent)
                    # 传递给FBot单元
                    toBeSendArray = []
                    if (strDestClientName in self.s_toBeSend_FBotClient_PackageDict.keys()):
                        toBeSendArray = self.s_toBeSend_FBotClient_PackageDict[strDestClientName]
                    else:
                        self.s_toBeSend_FBotClient_PackageDict[strDestClientName] = toBeSendArray
                    toBeSendArray.append(newPackage)

                elif (iDomainType == Collect_FbNameDict_Mang.s_g_iType_Peer_FBot):
                    # 增加到队列
                    newPackage = FBotV4.make_package(command_id=Share_UTRC_CfgDef.g_iPeerFbUTRC_Cmd_NeedPeerFBot_Reply_Message_Result,
                                                     value=strMsgContent)
                    # 传递给FBot单元
                    self.s_toBeSend_PeerFBot_PackageArray.append(newPackage)
                    pass
            else:
                CTYLB_Log.ShowLog(1, "Not Found Dest", "%s -> %s" % (strFrom, strDest))
                pass

        pass
示例#7
0
 def OnMessage(self, fromSenderUnit, strMsgJsonParam, strMsgParam,
               lMsgParam1, lMsgParam2):
     CTYLB_Log.ShowLog(
         0, AIOSWebOSFiber.FiberName,
         "recv {0} message, json:{1}, msg:{2}".format(
             fromSenderUnit.s_strFullTaskName, strMsgJsonParam,
             strMsgParam))
     if AIOSWebOSFiber.EnablePost:
         HttpPoster.Post(strMsgParam)
     msgRet = FiberMsgRet(strRetValue=strMsgParam + "->AIOS指令执行回复",
                          lRetValue=lMsgParam1,
                          lRetValue2=lMsgParam2)
     return msgRet
示例#8
0
    async def AsyncSendRecvRun(loopEventIO, natsExecThread, strNatsServerAddr,
                               strSelfRecvName):
        nats = NATS()
        await nats.connect(io_loop=loopEventIO, servers=[strNatsServerAddr])

        sid = await nats.subscribe_async(
            strSelfRecvName, cb=CNatsExec_Thread.AsyncRecvMsg_Handler)

        while (natsExecThread.s_bThreadRunning):
            bMsgBusy = False

            needSendMsg = natsExecThread.Safe_PopNeedSendUnit()
            if (needSendMsg):
                bReExecConnect = False
                # 获得需要发送的单元消息
                msg = {}

                # strBase64 = CTYLB_MainSys_MiscFunc.SafeConvertStrToBase64(needSendMsg.s_strMsgContent)
                msg[CNatsExec_Thread.s_g_key_Msg] = needSendMsg.s_strMsgContent
                msg[CNatsExec_Thread.s_g_key_Type] = needSendMsg.s_iMsgType
                msg[CNatsExec_Thread.s_g_key_FromSender] = strSelfRecvName

                strExContent = json.dumps(msg, ensure_ascii=False)
                bytesExContent = CTYLB_MainSys_MiscFunc.SafeGetUTF8(
                    strExContent)

                while (True):

                    try:
                        await nats.publish(needSendMsg.s_strPeerMsgName,
                                           bytesExContent)
                        break
                    except:
                        if (nats.is_closed):
                            bReExecConnect = True

                    if (bReExecConnect):
                        CTYLB_Log.ShowLog(
                            0, "Reconnect",
                            "Nats Connect lost. reconnecting...")
                        bReExecConnect = False
                        await nats.connect(io_loop=loopEventIO,
                                           servers=[strNatsServerAddr])

                bMsgBusy = True

            if (not bMsgBusy):
                await asyncio.sleep(0.001)

        await asyncio.sleep(1, loop=loopEventIO)
        await nats.close()
示例#9
0
    async def AsyncRecvMsg_Handler(natsMsg):
        strFromPeerName = natsMsg.subject
        strJsonData = natsMsg.data.decode()

        dictRecvData = json.loads(strJsonData)
        try:
            iMsgType = int(dictRecvData[CNatsExec_Thread.s_g_key_Type])
            strOrigBaseData = dictRecvData[CNatsExec_Thread.s_g_key_Msg]
            strPeerSenderName = dictRecvData[
                CNatsExec_Thread.s_g_key_FromSender]
            #strOrigData = CTYLB_MainSys_MiscFunc.SafeRestoreFromBase64(strOrigBaseData)

            CNatsExec_Thread.Safe_AddRecvUnit(strPeerSenderName, iMsgType,
                                              strOrigBaseData)
        except Exception as e:
            CTYLB_Log.ShowLog(1, "接收异步nats消息解释错误", str(e))
示例#10
0
    def funcCheckCallBack(self):
        # 提交请求进行发送
        if(self.s_TimerSend.CheckTimeDiff()):
            self.s_utrcClient.PromptQueryUTRCList()

        # 查询接收到的内容
        strList=self.s_utrcClient.PopRecveplyQueryAllClient()
        if(strList):
            srvTypesFiberDict = json.loads(strList)

            # 每个类型的内容进行加载
            for eachType in srvTypesFiberDict:
                execDestDict=None
                if(eachType== "NatsClient"):
                    execDestDict=s_dict_NatsClient
                elif(eachType== "FBotlient"):
                    execDestDict = s_dict_FBotClient
                elif(eachType== "PeerFBotSrv_NatsClient"):
                    execDestDict = s_dict_PeerFBotSrv_NatsClient
                elif(eachType== "PeerFBotSrv_FBotClient"):
                    execDestDict = s_dict_PeerFBotSrv_FBotClient

                subClientFiberDict = srvTypesFiberDict[eachType]
                if(execDestDict != None):
                    # 字典的每个 主机单元的 fiber列表进行加载
                    execDestDict.clear()
                    for eachSubClientName in subClientFiberDict.keys():
                        strSubClientFiberList = subClientFiberDict[eachSubClientName]

                        curComuFiberNamesUnit = CUTRC_NATs_ComuFiberList()
                        if (curComuFiberNamesUnit.LoadFromStr(strSubClientFiberList)):
                            execDestDict[eachSubClientName] = curComuFiberNamesUnit

                    CTYLB_Log.ShowLog(0, eachType, "Count:%d"%(len(execDestDict)))

            #构建本地的客户端信息.
            pass
        pass
示例#11
0
    def v_CreateAsyncProcComu(self, config_file):
        # 创建进程间通信单元
        strNatSrvAddr = CTylb_Bot_Exec_Frame.ReadIniSectionValue(
            config_file, Share_UTRC_CfgDef.g_str_section_FiberUTRC,
            Share_UTRC_CfgDef.g_str_key_NATs_Addr, "")
        self.s_strNATSProcServerName = CTylb_Bot_Exec_Frame.ReadIniSectionValue(
            config_file, Share_UTRC_CfgDef.g_str_section_FiberUTRC,
            Share_UTRC_CfgDef.g_str_key_NAT_Proc_ServerName, "")
        self.s_strNATSProcSelfName = CTylb_Bot_Exec_Frame.ReadIniSectionValue(
            config_file, Share_UTRC_CfgDef.g_str_section_FiberUTRC,
            Share_UTRC_CfgDef.g_str_key_NAT_Proc_ServerName, "")
        strSelfNATSProcComuName = CTylb_Bot_Exec_Frame.ReadIniSectionValue(
            config_file, Share_UTRC_CfgDef.g_str_section_FBot,
            Share_UTRC_CfgDef.g_str_key_myid, "")

        if (strNatSrvAddr and self.s_strNATSProcServerName
                and self.s_strNATSProcSelfName and strSelfNATSProcComuName):
            self.s_AsyncNATS_ProcComu = CAsyncNats_UTRCClient_ProcComu(
                strNATSServerAddr=strNatSrvAddr,
                strSelfRecvName=strSelfNATSProcComuName)
        else:
            CTYLB_Log.ShowLog(1, "NATS-Client_NotCreate", "")

        pass
 def v_OnClose(self):
     CTYLB_Log.ShowLog(0, self.s_g_strMyFiberName, "v_OnClose")
     # 删除
     pass
示例#13
0
    def v_Base_TimerCheck(self):
        bRet = False

        # 当前是否正在运行远端任务?
        if (self.s_runningMsgID):
            pass
        else:
            if (len(self.s_tobe_RunExecMsg) > 0):
                bRet = True

                # 取出第一个等待单元
                keyValuePair = self.s_tobe_RunExecMsg.popitem()
                self.s_runningMsgID = keyValuePair[0]
                self.s_runningMsgContent = keyValuePair[1]

                # 执行命令
                execFiberMsgRet = None

                if (self.s_runningMsgContent.s_strDestFbUnit_StrParam):
                    execFiberMsgRet = self.SendTaskMsg_StrParam(
                        strFbUnitFullName=self.s_runningMsgContent.
                        s_strDestFbUnit_FullName,
                        messageID=self.s_runningMsgContent.s_iRun_MessageID,
                        strDestTaskParam=self.s_runningMsgContent.
                        s_strDestFbUnit_StrParam,
                        strMsgJsonParam=self.s_runningMsgContent.
                        s_strMsgJsonParam_Orig,
                        strMsgParam=self.s_runningMsgContent.
                        s_strMsgParam_Orig,
                        lMsgParam1=self.s_runningMsgContent.s_lMsgParam1,
                        lMsgParam2=self.s_runningMsgContent.s_lMsgParam2)
                else:
                    execFiberMsgRet = self.SendTaskMsg_LongParam(
                        strFbUnitFullName=self.s_runningMsgContent.
                        s_strDestFbUnit_FullName,
                        messageID=self.s_runningMsgContent.s_iRun_MessageID,
                        lTaskParam1=self.s_runningMsgContent.
                        s_lDestFbUnit_lParam1,
                        lTaskParam2=self.s_runningMsgContent.
                        s_lDestFbUnit_lParam2,
                        strMsgJsonParam=self.s_runningMsgContent.
                        s_strMsgJsonParam_Orig,
                        strMsgParam=self.s_runningMsgContent.
                        s_strMsgParam_Orig,
                        lMsgParam1=self.s_runningMsgContent.s_lMsgParam1,
                        lMsgParam2=self.s_runningMsgContent.s_lMsgParam2)
                if (execFiberMsgRet):
                    if (execFiberMsgRet.s_iExecResult ==
                            FiberMsgRet.s_g_iExecResult_Normal):
                        self.s_finish_RunExecMsg[
                            self.s_runningMsgID] = execFiberMsgRet
                        # 完成,清除
                        self.s_runningMsgContent = None
                        self.s_runningMsgID = ""

                    elif (execFiberMsgRet.s_iExecResult ==
                          FiberMsgRet.s_g_iExecResult_Remote_Wait_Reply):
                        # 等待
                        self.s_runningRemoteExecMsgID = execFiberMsgRet.s_strRetValue
                        pass

                    else:
                        CTYLB_Log.ShowLog(1, "unknown type task.",
                                          "API running error")

            pass

        return bRet
示例#14
0
    def TimerCheck(self):
        bRetTaskBusy = False  # 返回状态值
        bExecTask, bExecTimerCheck = False, False  # 执行任务,执行时间检查

        # 是否为N毫秒检查一次?
        if (self.s_iCheckEveryMilSecond):
            if (self.s_lastFreeMilTimeDiff.CheckTimeDiff()):
                bExecTimerCheck = True
        else:
            bExecTimerCheck = True

        if (bExecTimerCheck):
            #上次繁忙吗?这次直接执行
            if (self.s_bLastBusyStatus):
                bExecTask = True
            else:
                # 上次还在进行持续空闲检查?
                if (self.s_bInContinueCheckFreeStatus):
                    if (self.s_lastContinueIdle_MilTimeDiff.CheckTimeDiff()):
                        bExecTask = True
                else:
                    bExecTask = True

            if (bExecTask):
                # 执行,时间检查
                startTimeDiffCheck = CSkBot_Common_Share.CSkBot_MilTimeIdentify(
                )

                try:
                    bRetTaskBusy = self.v_Base_TimerCheck()
                except Exception as e:
                    CTYLB_MainSys_MiscFunc.ShowExceptionInfo(e)
                    bRetTaskBusy = True

                curTimeDiff = startTimeDiffCheck.GetStartTimeDiff()
                iTotalMilSecond = curTimeDiff.microseconds
                if (iTotalMilSecond > 3000):
                    strFullParam="UID:%d, strParam:%s, lParam:%d-%d" % \
                                 (self.s_lUniqueID, self.s_strTaskParam, self.s_lTaskParam1, self.s_lTaskParam2)
                    CTYLB_Log.ShowLog(0, self.s_strFullTaskName,
                                      ("time:%d, " %
                                       (iTotalMilSecond / 1000)) +
                                      strFullParam)

                if (bRetTaskBusy):
                    # 如果繁忙,清空所有状态
                    self.s_bInContinueCheckFreeStatus = False
                    self.s_iKeepIdleCheckFitCount = 0
                else:
                    # 如果上次也为空闲
                    if (self.s_bLastBusyStatus):
                        self.s_iKeepIdleCheckFitCount += 1
                        if (self.s_iKeepIdleCheckFitCount >=
                                self.s_iMaxKeepIdleCheckCount):
                            # 持续空闲,进行空闲持续状态
                            self.s_bInContinueCheckFreeStatus = True
                            self.s_lastContinueIdle_MilTimeDiff.ResetToNow()

                self.s_bLastBusyStatus = bRetTaskBusy

        return bRetTaskBusy
示例#15
0
 def FBot_Listen_Accept_Callback(self, **kwargs):
     strTotal=""
     for key in kwargs:
         strTotal += " %s"%(str(kwargs[key]))
     CTYLB_Log.ShowLog(0, "listen_accept", strTotal)
     pass
示例#16
0
 def ShowLog(iWarnLevel, strMsg):
     CTYLB_Log.ShowLog(iWarnLevel, 'Connect Thread', strMsg)
 def v_OnInit(self):
     CTYLB_Log.ShowLog(0, self.s_g_strMyFiberName, "OnInit")
     # 初始化
     pass
示例#18
0
 def v_OnInit(self):
     CTYLB_Log.ShowLog(0, AIOSWebOSFiber.FiberName, "OnInit")
示例#19
0
 def v_OnClose(self):
     CTYLB_Log.ShowLog(0, AIOSWebOSFiber.FiberName, "OnClose")
 def OnMessage_Sample(self, fromSenderUnit, strMsgJsonParam, strMsgParam,
                      lMsgParam1, lMsgParam2):
     CTYLB_Log.ShowLog(
         0, self.s_g_strMyFiberName, "recv %s message,second to first." %
         (fromSenderUnit.s_strFullTaskName))
     pass
示例#21
0
    strNLKAnalyerName="lessnet.FiberExec.NLKer"
    lMsgID_NLKer_SenterToWord=100001  # strParam文字内容, 回复:strValue分词json

    def __init__(self):
        PyExCallAPI_Base.__init__(self)
        pass

    def ExecNLK(self, strInput):
        execCmd=self.ExecFiberCmd( strDestFiberName=self.strNLKAnalyerName, messageID=self.lMsgID_NLKer_SenterToWord,
                                   strMsgParam=strInput)
        return execCmd.s_strRetValue


if __name__ == "__main__":
    Global_TinyBrainCore.InitBrainCore()

    stringArray=["这是一个测试单词句子", "中国的GDP正在持续增长", "今天太阳高照明天马路繁忙"]
    pyAPIExec = pyAPI_NLK()

    timerSend = CSkBot_Common_Share.CSkBot_TimeIdentify()

    while(Global_TinyBrainCore.IsGlobalRunning()):
        if(timerSend.CheckTimeDiff()):
            for eachStr in stringArray:
                strValue=pyAPIExec.ExecNLK(eachStr)
                CTYLB_Log.ShowLog(0, "result", strValue)
                pass

        time.sleep(0.1)
        Global_TinyBrainCore.TimerCheck()
示例#22
0
 def OnMessage_Sample(self, fromSenderUnit, strMsgJsonParam, strMsgParam, lMsgParam1, lMsgParam2):
     CTYLB_Log.ShowLog(0, self.s_g_strMyFiberName, "recv %s message, json:[%s], msg:[%s]" %
                       (fromSenderUnit.s_strFullTaskName, strMsgJsonParam, strMsgParam))
     msgRet=FiberMsgRet(strRetValue=strMsgParam+"->苹果回复ing", lRetValue=lMsgParam1, lRetValue2=lMsgParam2)
     return msgRet
     pass