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

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

Fix return values (should be (valid,lat,lon))

File size: 2.9 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        print "Error: position mode %s not defined" % self.mode
52        self.mode = 'none'
53        return(0,0)
54    return(function())
55   
56  def get_none(self):
57    return((False,0,0))
58 
59  def socket_cmd(self, cmd):
60    self.s.send("%s\r\n" % cmd)
61    return self.s.recv(8192)
62   
63  def test_socket(self):
64    for i in ('i','p','p','p','p'):
65      print "%s = %s" % (i, self.socket_cmd(i))
66      sleep(1)
67    pos = self.socket_cmd("p")
68    p = re.compile('P=(.*?)\s+(.*?)\s*')
69    match = p.search(pos)
70    print (pos, dir(match), match.group(1))
71 
72  def get_socket(self):
73    pos = self.socket_cmd("p")
74    p = re.compile('P=([0-9.]+?)\s+([0-9.]+?)\s*')
75    match = p.search(pos)
76    if(match != None):
77      text1 = match.group(1)
78      text2 = match.group(2)
79      print "matched '%s', '%s'"%(text1,text2)
80      return((True, float(text1), float(text2)))
81    return((False, 0,0))
82 
83  def get_file(self):
84    file = open("pos.txt", 'r')
85    text = file.readline(50)
86    file.close()
87    try:
88      lat,lon = [float(i) for i in text.rstrip().split(",")]
89      return((True, lat,lon))
90    except ValueError:
91      return((False, 0,0))
92   
93if __name__ == "__main__":
94  #print dir(socket)
95  pos = geoPosition()
96  print pos.get()
Note: See TracBrowser for help on using the repository browser.