open_localisateur_ANDROID.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/usr/bin/python
  2. # -*- coding: utf8 -*-
  3. from __future__ import unicode_literals
  4. import time, os, sys
  5. import thread
  6. try :
  7. import androidhelper
  8. except ImportError:
  9. print('Uniquement compatible avec périphérique sous ANDROID\n')
  10. from utils import *
  11. #Emplacement de la base de données sur le smartphone
  12. table_name = '/storage/emulated/0/qpython/scripts/pkfieur/data/geo_sncf.db'
  13. rgps = False
  14. def Gps():
  15. global rgps
  16. while True:
  17. droid.eventClearBuffer()
  18. res = droid.eventWait(3000).result
  19. if res == None:
  20. os.system('clear')
  21. sys.stdout.write("no fix")
  22. sys.stdout.flush()
  23. continue
  24. elif res['name'] == "location":
  25. # It's a GPS message!
  26. try:
  27. loc = res['data']['gps']
  28. except (KeyError, TypeError):
  29. #print("Location message, but no GPS data")
  30. continue
  31. else:
  32. rgps = str(loc['latitude']) +','+ str(loc['longitude'])
  33. time.sleep(1)
  34. if __name__ == '__main__':
  35. print "Chargement..."
  36. droid = androidhelper.Android()
  37. droid.startLocating(0, 0)
  38. thread.start_new_thread(Gps,())
  39. while True:
  40. try:
  41. if rgps:
  42. res, ecart = pkfieur.coord2pk(rgps, table_name)
  43. gar1, gar2 = pkfieur.gares(res['ligne'], res['pk'], table_name)
  44. os.system('clear')
  45. sys.stdout.write("GPS : {0}\n\n".format(rgps))
  46. sys.stdout.write("Ligne : {0}\n".format(res['ligne']))
  47. sys.stdout.write("PK : {0}\n\n".format(res['pk']))
  48. sys.stdout.write("Distance du PK : {0} m\n\n".format(ecart).encode('utf8'))
  49. sys.stdout.write(u"Gares encadrantes :\n{0}\n{1}\n".format(gar1, gar2).encode('utf8'))
  50. sys.stdout.flush()
  51. time.sleep(1)
  52. except KeyboardInterrupt:
  53. print "\nQuit"
  54. droid.stopLocating()
  55. thread.exit()