树莓派和gps python-mutithreading

时间:2018-11-18 09:48:11

标签: python linux raspberry-pi python-multithreading

我正在开发GPS(全球sat bu-353s4 USB型)和树莓派3b型项目。我要在其中获取GPS纬度和经度数据,根据该数据,我要播放一些视频文件,还希望跟踪最近播放的视频名称。所有这些过程都应该在while循环中,这意味着无限循环。 我为此开发了逻辑,我面临的问题是,每当视频播放完成时,它将再次获取GPS数据并转到特定的文件夹中,选择视频文件并播放视频,在两次视频播放之间,屏幕变黑,我想要连续的视频玩。我正在使用OMXplayer打开视频,我知道可以使用多任务/多线程概念来解决它,但是不知道如何在Python中实现它。 这是我的代码:

# class to define background process thread
class Asyncfun(threading.Thread):
def __init__(self, text):
    threading.Thread.__init__(self)
    self.text = text

def run(self):
    os.system(self.text)
    print "play finished "
def Main(): 
try:
    while True:
        # get the GPS latitude longitude data using another gpsdatafetching file
        stdn = gps2.latlong()
        lat1 =  round(float(stdn[0]),5) 
        long1 = round(float(stdn[1]),5) 
        ID = gps_geotest3.test1(lat1,long1)# get the areaid according to current latitudeand longitude
        print "id is :", ID
        if ID == None:
            ID = 17
        print "lat long is in this areaId : ",ID
        location = '/home/pi/files/' 
        counter = 0 #keep a count of all files found
        mp4files = [] #list to store all csv files found at location
        lastplayedindex = 0
        areaID = str(ID)
        location += areaID
        #print "file path is: ",location
        for file in os.listdir(location):
            if file.endswith(".mp4"):
                #print "mp4 file found:\t", file
                mp4files.append(str(file))
                counter = counter+1

        #get the lastplayedindex_aeraid file name
        indexfilename='lastplayedindex_'        
        indexfilename +=areaID
        indexfilename += '.txt'
        #get the index number from lastplayedindex_aeraid file
        with open(indexfilename,'rb') as input_file:
            for line in input_file:
                lastplayedindex = line
        path = "/home/pi/files/" 
        omxcmd = "omxplayer --display=5 " 

        print "lastplayed index: ",lastplayedindex
        if int(lastplayedindex)< (counter-1) : #compare with total filecount in folder

            newlastindex=int(lastplayedindex)+1
            vfile = mp4files[newlastindex] #videofile name
            omxcmd += (location +'/') #append filelocation
            omxcmd += vfile # append filename
            #omxcmd is command to open videofile using OMXplayer
            #play videofile iin backgroundprocess while sending acknowledgment to webserver about that file
            background = Asyncfun(omxcmd)
            background.start()
            # code to update lastplayed index in local file

        counter = 0
        #to send acknowledgment to server about which video played last
        print "sending acknowledgment to server.."
        payload2 = {'DeviceMasterId': '30', 'Fk_Area_ID': areaID,'FileName':vfile}
        R = requests.post('url to my website', params = payload2)
        if R.status_code != 200:
            print "Error:", R.status_code
        else :
            print "acknowledgment sent successfully."
        background.join()
except (KeyboardInterrupt, SystemExit): #when you press ctrl+c
    gpsp.running = False
    gpsp.join()
    scope.__del__()
if __name__ == '__main__':
Main()

0 个答案:

没有答案