-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget-location.py
162 lines (139 loc) · 4.85 KB
/
get-location.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# This file is part of barrioSquare.
#
# v0.1.20
#
# barrioSquare is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# barrioSquare is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with barrioSquare. If not, see <http://www.gnu.org/licenses/>.
# or write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# this file should not be called directly.
# called from barriosq.py
#
import location
import gobject
import os
import time
import sys
import socket
print sys.argv
getLocationChildPID = 0
currentLatitude = 0
currentLongitude = 0
lockCount = 0
partialLockCount = 0
locationMethodType = int(sys.argv[1])
serverPort = int(sys.argv[2])
maxPartialLockCount = 0
maxLockCount = 0
userPreferencesDir = os.path.expanduser('~') + os.sep + '.barriosquare' + os.sep
# print 'loaded locationMethodType: %d' % locationMethodType
# create the socket to back to the server
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('127.0.0.1', serverPort))
sock.send('INITIALIZING\n\r')
if (not os.path.exists(userPreferencesDir)):
print 'Creating Pref Dir'
os.mkdir(userPreferencesDir)
def on_error(control, error, data):
print "location error: %d... quitting" % error
data.quit()
def on_changed(device, data):
if not device:
return
if device.status == location.GPS_DEVICE_STATUS_NO_FIX:
return
if device.fix:
if device.fix[1] & location.GPS_DEVICE_LATLONG_SET:
global sock
global currentLatitude
global currentLongitude
global lockCount
currentLatitude = device.fix[4]
currentLongitude = device.fix[5]
horizAcc = str(device.fix[6])
vertAcc = str(device.fix[7])
# print device.fix[6]
# print "lat = %f" % currentLatitude
# print "long = %f" % currentLongitude
# print "horizAcc = " + horizAcc
if horizAcc == 'nan':
realHorizAcc = -1
else:
realHorizAcc = float(horizAcc) / 100
if vertAcc == 'nan':
realVertAcc = -1
else:
realVertAcc = float(vertAcc) / 100
# print 'Full lock acquired'
lockCount += 1
print 'Fl|' + str(lockCount) + '|' + str(currentLatitude) + '|' + str(currentLongitude) + '|' + str(realHorizAcc) + '|' + str(realVertAcc)
# print 'lock count: %d' % lockCount
strCurrentLatitude = "%f" % currentLatitude
strCurrentLongitude = "%f" % currentLongitude
sockCommand = 'UPDATING_LOCATION|' + str(currentLatitude) + '|' + str(currentLongitude) + '|' + str(realHorizAcc) + '|' + str(realVertAcc)
sock.send(sockCommand + '\n\r')
#
# locationFile = open(userPreferencesDir + 'CurrentLocation.txt','w')
# locationFile.write(strCurrentLatitude + '\n')
# locationFile.write(strCurrentLongitude + '\n')
# locationFile.write(str(realHorizAcc) + '\n')
# locationFile.write(str(realVertAcc) + '\n')
# locationFile.close()
# time.sleep(2)
# data.stop()
def on_stop(control, data):
print "quitting"
global sock
sock.close()
data.quit()
def start_location(data):
data.start()
return False
loop = gobject.MainLoop()
control = location.GPSDControl.get_default()
device = location.GPSDevice()
interval = location.INTERVAL_120S
print '(get-location.py) LocationMethodType: %d' % locationMethodType
if locationMethodType == 0:
maxPartialLockCount = 4
maxLockCount = 1
control.set_properties(preferred_method=location.METHOD_USER_SELECTED,
preferred_interval=interval)
elif locationMethodType == 1:
maxPartialLockCount = 4
maxLockCount = 1
control.set_properties(preferred_method=location.METHOD_CWP,
preferred_interval=interval)
elif locationMethodType == 2:
maxPartialLockCount = 4
maxLockCount = 1
control.set_properties(preferred_method=location.METHOD_ACWP,
preferred_interval=interval)
elif locationMethodType == 3:
maxPartialLockCount = 6
maxLockCount = 1
control.set_properties(preferred_method=location.METHOD_GNSS,
preferred_interval=interval)
elif locationMethodType == 4:
maxPartialLockCount = 6
maxLockCount = 1
control.set_properties(preferred_method=location.METHOD_AGNSS,
preferred_interval=interval)
# control.set_properties(preferred_method=location.METHOD_USER_SELECTED,
# preferred_interval=location.INTERVAL_DEFAULT)
control.connect("error-verbose", on_error, loop)
device.connect("changed", on_changed, control)
control.connect("gpsd-stopped", on_stop, loop)
gobject.idle_add(start_location, control)
loop.run()