source: subversion/applications/routing/pyroute/geoPosition.py @ 8327

Last change on this file since 8327 was 5574, checked in by ojw, 12 years ago

return the current position as a hash instead of an array, so that things can refer to ownposlat? instead of ownpos[2]

File size: 3.1 KB
Line 
1#!/usr/bin/python
2#----------------------------------------------------------------
3# report "current position" (currently accepts replayed GPX
4# files from follow.py)
5#------------------------------------------------------
6# Usage:
7#   pos = geoPosition(filename_of_latlong_file)
8#   lat,lon = pos.get()
9#------------------------------------------------------
10# Copyright 2007, Oliver White
11#
12# This program is free software: you can redistribute it and/or modify
13# it under the terms of the GNU General Public License as published by
14# the Free Software Foundation, either version 3 of the License, or
15# (at your option) any later version.
16#
17# This program is distributed in the hope that it will be useful,
18# but WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20# GNU General Public License for more details.
21#
22# You should have received a copy of the GNU General Public License
23# along with this program.  If not, see <http://www.gnu.org/licenses/>.
24#------------------------------------------------------
25import sys
26import os
27import socket
28from time import sleep
29import re
30
31class geoPosition:
32  def __init__(self):
33    self.mode = "none"
34    try:
35      self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
36      self.s.connect(("127.0.0.1", 2947))
37      self.mode = "socket"
38    except socket.error:
39      print "No GPSD detected, position information will not be available"
40      pass # leave mode at "none"
41   
42    self.filename = "pos.txt"
43    if(os.path.exists(self.filename)):
44      self.mode = "file"
45      print "Using %s as position reference" % self.filename
46 
47  def get(self):
48    try:
49        function = getattr(self, 'get_%s' % self.mode)
50    except AttributeError:
51        return({'valid':False,'message':"function for %s not defined" % self.mode})
52    return(function())
53   
54  def get_none(self):
55    return({'valid':False,'message':"no gps"})
56 
57  def socket_cmd(self, cmd):
58    self.s.send("%s\r\n" % cmd)
59    return self.s.recv(8192)
60   
61  def test_socket(self):
62    for i in ('i','p','p','p','p'):
63      print "%s = %s" % (i, self.socket_cmd(i))
64      sleep(1)
65   
66 
67  def get_socket(self):
68    pos = self.socket_cmd("p")
69    p = re.compile('P=(.*?)\s*$')
70    match = p.search(pos)
71    if(match == None):
72      return({'valid':False,'message':"GPS returned %s" % pos})
73    text = match.group(1)
74    if(text == '?'):
75      return({'valid':False,'message':"GPS returned P=?"})
76    lat,lon = [float(ll) for ll in text.split(' ')]
77    #print "matched '%s', '%s'"%(lat,lon)
78    return({'valid':True, 'lat':lat, 'lon':lon, 'source':'GPS'})
79 
80  def get_file(self):
81    file = open(self.filename, 'r')
82    text = file.readline(50)
83    file.close()
84    try:
85      lat,lon = [float(i) for i in text.rstrip().split(",")]
86      return({'valid':True, 'lat':lat, 'lon':lon, 'source':'textfile'})
87    except ValueError:
88      return({'valid':False, 'message':'Invalid textfile'})
89   
90if __name__ == "__main__":
91  #print dir(socket)
92  pos = geoPosition()
93  #print pos.test_socket()
94  for i in range(5):
95    print pos.get()
96    sleep(1)
Note: See TracBrowser for help on using the repository browser.