
import cv2
import base64
import io,os,sys
sys.path.append('/home/thsw2/WJ/src/yolov5/utils')
from get_offline_url import update_websource_offAndLive
import requests
import time,json
import string,random
 
    
def SendPost(txtSource):

    api = 'http://212.129.223.66:8888/detector'         
    filename='THWangJin'
    for info in txtSource:
        
        input_ ={
              'imgData':'',
              'imgName':filename
        }
        if 'live' in info['name']:
            input_['outSource'] = "rtmp://127.0.0.1:%s/live/test"%(info['port'])
        else:
            input_['outSource'] = None    
        input_['inSource'] =  info['url']
        input_['streamName'] = info['name']

        time0=time.time()
        response=requests.post(api,json=input_).json()
        time1=time.time()
        print('###source_query:',response,'time:%.3f s'%(time1-time0))  
        if 'live' in info['name']:
            print('#####process online video stream######')
            break
            
        
        
def getLogFp(pollLogFile):
    #print(pollLogFile)
    #print(os.path.pardir(pollLogFile))
    if not os.path.exists(pollLogFile):
        os.makedirs(os.path.dirname(pollLogFile),exist_ok=True)
        
        fp_log = open(pollLogFile,'w')
    else:    
        fp_log = open(pollLogFile,'a+')
    return  fp_log       

if __name__=='__main__':

    ##建立日志文件
    pollLogFile='logs/poll/poll.txt'
    fp_log = getLogFp(pollLogFile)
    
    
        
        

    platform_query_url='http://47.96.182.154:9051/api/suanfa/getPlatformInfo'
    sourceFile='/home/thsw2/WJ/src/yolov5/config/source.txt'
    offlineFile='/home/thsw2/WJ/src/yolov5/mintors/offlines/doneCodes.txt'
    
    txtSource=update_websource_offAndLive(platform_query_url,sourceFile,offlineFile)
    
    txtSource=[
      #{'url': 'rtmp://demoplay.yunhengzhizao.cn/live/THSA_HD5M', 'port': '1935', 'name': 'live-THSAHD5M'}]
    
      {'url': 'http://images.5gai.taauav.com/video/DJI_20220111115516_0001_ZBJ.MP4', 'port': '1935', 'name': 'off-202202190001'},
      {'url': 'http://images.5gai.taauav.com/video/8bc32984dd893930dabb2856eb92b4d1.mp4', 'port': '1935', 'name': 'off-202202190002'},
      {'url': 'http://images.5gai.taauav.com/video/DJI_20220110145546_0003_W.MP4', 'port': '1935', 'name': 'off-202202190002'}
     ]
    
    print(txtSource)
    SendPost(txtSource)
    
