×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    2

    running a simple tracker function in the background

    I'm developing a app in which i save the location of the user using GPS and vibrate the device when that location is tracked.

    I want to run a function in d background which keeps checking the database containing the location values and when a match is found, vibrate the device.

    While doing this i want the main menu available to the user i.e he/she must be able to add more locations while d app is running.

    plz help!!!!

  2. #2
    Super Contributor
    Join Date
    Oct 2007
    Location
    Deva, Romania
    Posts
    3,471

    Re: running a simple tracker function in the background

    Sounds like you need to use threads (other useful info is available online, a search will help you a lot). This is for doing the background work.

    If you plan to use a relational database and SQL, see these pages and the material they link to:
    http://pys60.garage.maemo.org/doc/s60/module-e32db.html
    http://wiki.forum.nokia.com/index.ph...n_Python_PyS60

    To make the phone vibrate you need an extension such as miso or misty: http://wiki.forum.nokia.com/index.ph...hon_Extensions

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    15

    Re: running a simple tracker function in the background

    What is the problem exactly?
    positioning module of py supports callback.
    You could set callback to your check on location list function, so when gps updates, it would check upon list. it wont block your active scheduler.
    the rest is as bogdan.galiceanu explained clearly.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    2

    Re: running a simple tracker function in the background

    guys thnx a lott for your quick reply

    @bogdan.galiceanu i ve already implemented database and vibration functions.Now as u said correctly i need to use threads..

    @amatrig ...bro my prob is i want the main menu(having the option to insert a value in the database) available while I'm repeatedly searching for a match in the same database for the current GPS co-ordinates....

    can anyone provide me with a some simple sample code for threads....
    my code is here:

    Code:
    import appuifw, e32
    #import miso
    import time
    import audio
    import positioning
    import e32db,sys,traceback
    import time
    import messaging
    #import apptools
    #import applist
    
    
    
    def initialize_gps():
    	'''This function initializes the GPS. The select_module(module_id) can be used to select the GPS module in this function.
    	In this case we are using the default GPS (integrated GPS) hence we do not need to select it.'''
    	appuifw.note(u'Intializing GPS')
    	global gps_data
    	global flag1
            flag1=True
    	#Intitialize the global dictionary with some initial dummy value (0.0 in this case)
    	gps_data = {
    	'satellites': {'horizontal_dop': 0.0, 'used_satellites': 0, 'vertical_dop': 0.0, 'time': 0.0,'satellites': 0, 'time_dop':0.0},
    	'position': {'latitude': 0.0, 'altitude': 0.0, 'vertical_accuracy': 0.0, 'longitude': 0.0, 'horizontal_accuracy': 0.0},
    	'course': {'speed': 0.0, 'heading': 0.0, 'heading_accuracy': 0.0, 'speed_accuracy': 0.0}
    	}
    	try:
                    
    		# Set requesters - it is mandatory to set at least one
    		positioning.set_requestors([{"type":"service","format":"application","data":"gps_app"}])
    		# Request for a fix every 0.5 seconds
                    positioning.position(course=1,satellites=1,callback=cb_gps, interval=500000,partial=0)
                    print "fa1"
                    print gps_data['position']['latitude'],gps_data['position']['longitude']
                    '''
                            # Sleep for 3 seconds for the intitial fix
                            print "in init GPS"
                            print gps_data['satellites']['used_satellites'], gps_data['position']['latitude'], gps_data['position']['longitude'], gps_data['course']['speed']
                            compare(gps_data['position']['latitude'],gps_data['position']['longitude'])
                    '''
                    #e32.ao_sleep(100)
    	except:
    		appuifw.note(u'Problem with GPS','error')
    
    def cb_gps(event):
    	global gps_data
    	gps_data = event
    
    def print_gps():
        print "hello"      
       #Set time for the script to run. 10 minutes in this case.
        time_to_run=10
        minutes=time_to_run*60
        c=0
    
    # Print the GPS data for 10 minutes.
        while (minutes > 0):
            print gps_data['satellites']['used_satellites'], gps_data['position']['latitude'], gps_data['position']['longitude'], gps_data['course']['speed']
            if gps_data['position']['latitude']<>0 :
                    
               break
            minutes=minutes-1
            e32.ao_sleep(1)
    
    def datab (loc):
       #initiate db access
       global db
       global dbv
       db=e32db.Dbms()
       dbv=e32db.Db_view()
     
    # name of the database file
       dbname = u'c:\\TEST1.db'
        
    #open database, if it does not exist, then create it
       try:
         db.open(dbname)
       except:
         db.create(dbname)
         db.open(dbname)
     
      
    #Create the database table
       try:
       
         #word = appuifw.query(u"Enter a word", 'text')
         word1 = appuifw.query(u"Enter a word", 'number')
      # note that the id field is auto-incremented when new rows are added
         db.execute(u"CREATE TABLE testgps1 (id counter, lat FLOAT, long FLOAT, dest VARCHAR,mode INTEGER )")
         print "Table Created"
       except SymbianError:
         print "Error Creating table: "
      #print traceback.format_exception(*sys.exc_info())
         print sys.exc_info()[0]
         print sys.exc_info()[1]
         print sys.exc_info()[2]
     
    # attempt to insert data
       try:
      # note that the id field is NOT required, as it auto-increments 
         db.execute(u"INSERT INTO testgps1 (lat, long,dest,mode) VALUES(%e,%e,'%s',%d)" %( gps_data['position']['latitude'],gps_data['position']['longitude'],loc,word1))
         #db.execute(u"INSERT INTO testgps1 (lat, long,dest) VALUES(%e,%e,'%s')" %( a,b,word))
         #db.execute("INSERT INTO testgps1(lat,lon,dest) VALUES(" + a + "," + b + ",'" + word + "')")
         print "Data Inserted"
       except SymbianError:
         print "Could not insert data: "
         print sys.exc_info()[0]
         print sys.exc_info()[1]
         print sys.exc_info()[2]
     
     
     
     
    #to fetch the whole dataset
       try:
         dbv.prepare(db,u'SELECT * FROM testgps1')
         for i in range(1,dbv.count_line()+1): #1 to number of rows
           dbv.get_line() #grabs the current row
           for i in range(1,dbv.col_count()+1):
             print dbv.col(i) #prints each column data
           dbv.next_line() #move to the next rowset[[Category:Entertainment]]
       except SymbianError:
         print "failed to read database: "
         print sys.exc_info()[0]
         print sys.exc_info()[1]
         print sys.exc_info()[2]
    # close the database
       db.close()
    
    def tracker():
                    db=e32db.Dbms()
                    dbv=e32db.Db_view()
                    # name of the database file
                    dbname = u'c:\\TEST1.db'
        
    	#open database, if it does not exist, then create it
                    try:
                            db.open(dbname)
                    except:
                            db.create(dbname)
                            db.open(dbname)  
                    while track == 1:
            #apptools.list_running_applications([0,0])
                            curr_lat = gps_data['position']['latitude']
                            curr_lon= gps_data['position']['longitude']
          
                            #apptools.switch_to_background(unicode(app_name))
                            flag1=True
                            while(flag1):
                                    dbv.prepare(db,u'SELECT * FROM testgps1')
                                    for i in range(1,dbv.count_line()+1): #1 to number of rows
                                     dbv.get_line() #grabs the current row
                                     flag = True
                                     for j in range(1,dbv.col_count()+1):
                                        #prints each column data
                                       f=dbv.col(2)
                                       g=dbv.col(3)
                                       h=dbv.col(4)
                                       i=dbv.col(5)
                                       lr=(f-0.01)
                                       ur=(f+0.01)
                                       if (gps_data['position']['latitude'] >lr ):
                                               print "in 1st if"
                                               if(gps_data['position']['latitude']<ur):
                                                   if(i==1):
                                                    appuifw.note(u'Vibrating')
                                                    #new(1,0)
                                                    
                                                    flag = False
                                                    flag1=False
                                                    break
                                                   if(i==2):
                                                    appuifw.note(u'Sending')
                                                    #new(0,1)
                                       else:
                                            appuifw.note(u'Else workin')
                                            print gps_data['position']['latitude'], gps_data['position']['longitude']
                            	
                            	
                                     dbv.next_line()
                                     if not flag:
                                          break
                            #apptools.switch_to_foreground(unicode(app_name))
                   
                        
                   
                        
     
                
                    e32.ao_sleep(5)
    
    
    
    
    
       
    def background_handler():
        #applist.applist()    
        global track
        track=0
        if not track or track == 0:
            track = 1
            appuifw.app.menu = [
                                (u"Stop Tracker", background_handler)]
            #apptools.switch_to_background()
            tracker()
        elif track == 1:
            track = 0
            main()
    
    
    def main():
        while True:
            
            addlocationlist = [u"latitude: " + unicode(str(gps_data['position']['latitude'])),
                               u"longitude: " + unicode(str(gps_data['position']['longitude'])),
                               u"Add Location",
                               u"Start Tracker",
                               u"Refresh"]
            location_opt = appuifw.selection_list(choices=addlocationlist)
            if location_opt or location_opt == 0:
                if location_opt == 2:
                    cellname = appuifw.query(u"Enter the location name for " + unicode(str(gps_data['position']['latitude'])) + u":", "text")
                    if cellname:
                        #cellids[str(cellid)] = str(cellname)
                        #save_location()
                        datab(cellname)
                        appuifw.note(u"Location added!", "info")
                elif location_opt == 4:
                        app_lock.signal()
                elif location_opt ==3:
                        background_handler()
                        
            else:
                break
    
    
    initialize_gps()
    print_gps()
    
    main()
    this might give some error as i am still developing it....
    awaiting reply...

Similar Threads

  1. Replies: 1
    Last Post: 2010-10-14, 14:35
  2. Replies: 1
    Last Post: 2005-08-29, 04:18
  3. Running in the background...
    By impact333 in forum Symbian Networking & Messaging (Closed)
    Replies: 1
    Last Post: 2004-12-03, 01:36
  4. Running application in the background
    By artemg in forum Mobile Java General
    Replies: 0
    Last Post: 2004-01-05, 12:54

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×