0
|
1 |
#
|
|
2 |
# positioning.py
|
|
3 |
#
|
|
4 |
# python interface for Location Acquisition API
|
|
5 |
#
|
|
6 |
# Copyright (c) 2007-2009 Nokia Corporation
|
|
7 |
#
|
|
8 |
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
9 |
# you may not use this file except in compliance with the License.
|
|
10 |
# You may obtain a copy of the License at
|
|
11 |
#
|
|
12 |
# http://www.apache.org/licenses/LICENSE-2.0
|
|
13 |
#
|
|
14 |
# Unless required by applicable law or agreed to in writing, software
|
|
15 |
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
16 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
17 |
# See the License for the specific language governing permissions and
|
|
18 |
# limitations under the License.
|
|
19 |
#
|
|
20 |
|
|
21 |
import e32
|
|
22 |
import copy
|
|
23 |
import thread
|
|
24 |
|
|
25 |
import _locationacq
|
|
26 |
|
|
27 |
class ThreadLocalStore(object):
|
|
28 |
def __init__(self, constructor):
|
|
29 |
self.objects = {}
|
|
30 |
self.constructor = constructor
|
|
31 |
def get(self):
|
|
32 |
id = thread.get_ident()
|
|
33 |
if id not in self.objects:
|
|
34 |
self.objects[id] = self.constructor()
|
|
35 |
return self.objects[id]
|
|
36 |
def set(self, newobject):
|
|
37 |
self.objects[thread.get_ident()] = newobject
|
|
38 |
|
|
39 |
_pos_serv_tls = ThreadLocalStore(_locationacq.position_server)
|
|
40 |
_get_pos_serv = _pos_serv_tls.get
|
|
41 |
_positioner_tls = ThreadLocalStore(lambda:_get_pos_serv().positioner())
|
|
42 |
_get_positioner = _positioner_tls.get
|
|
43 |
_set_positioner = _positioner_tls.set
|
|
44 |
_request_ongoing_tls = ThreadLocalStore(lambda:False)
|
|
45 |
_get_request_ongoing = _request_ongoing_tls.get
|
|
46 |
_set_request_ongoing = _request_ongoing_tls.set
|
|
47 |
_current_pos_tls = ThreadLocalStore(lambda:None)
|
|
48 |
_get_current_pos = _current_pos_tls.get
|
|
49 |
_set_current_pos = _current_pos_tls.set
|
|
50 |
|
|
51 |
POSITION_INTERVAL=1000000
|
|
52 |
|
|
53 |
def revdict(d):
|
|
54 |
return dict([(d[k],k) for k in d.keys()])
|
|
55 |
|
|
56 |
_requestor_types={"service":_locationacq.req_type_service,
|
|
57 |
"contact":_locationacq.req_type_contact}
|
|
58 |
_reverse_requestor_types=revdict(_requestor_types)
|
|
59 |
|
|
60 |
_requestor_formats={"application":_locationacq.req_format_app,
|
|
61 |
"telephone":_locationacq.req_format_tel,
|
|
62 |
"url":_locationacq.req_format_url,
|
|
63 |
"email":_locationacq.req_format_mail}
|
|
64 |
_reverse_requestor_formats=revdict(_requestor_formats)
|
|
65 |
|
|
66 |
# get information about available positioning modules
|
|
67 |
def modules():
|
|
68 |
return _get_pos_serv().modules()
|
|
69 |
|
|
70 |
# get default module id
|
|
71 |
def default_module():
|
|
72 |
return _get_pos_serv().default_module()
|
|
73 |
|
|
74 |
# get detailed information about the specified module
|
|
75 |
def module_info(module_id):
|
|
76 |
return _get_pos_serv().module_info(module_id)
|
|
77 |
|
|
78 |
# select a module
|
|
79 |
def select_module(module_id):
|
|
80 |
_set_positioner(_get_pos_serv().positioner(module_id))
|
|
81 |
|
|
82 |
# set requestors of the service (at least one must be set)
|
|
83 |
def set_requestors(requestors):
|
|
84 |
for item in requestors:
|
|
85 |
item["type"]=_requestor_types[item["type"]]
|
|
86 |
item["format"]=_requestor_formats[item["format"]]
|
|
87 |
item["data"]=unicode(item["data"])
|
|
88 |
_get_positioner().set_requestors(requestors)
|
|
89 |
|
|
90 |
# stop the feed
|
|
91 |
def stop_position():
|
|
92 |
if _get_request_ongoing():
|
|
93 |
_get_positioner().stop_position()
|
|
94 |
_set_request_ongoing(False)
|
|
95 |
|
|
96 |
# get the position information
|
|
97 |
def position(course=0,satellites=0,
|
|
98 |
callback=None,interval=POSITION_INTERVAL,
|
|
99 |
partial=0):
|
|
100 |
def cb(event):
|
|
101 |
_set_current_pos(copy.deepcopy(event))
|
|
102 |
_lock.signal()
|
|
103 |
|
|
104 |
if _get_request_ongoing():
|
|
105 |
raise RuntimeError, "Position request ongoing"
|
|
106 |
|
|
107 |
flags=0
|
|
108 |
if(course):
|
|
109 |
flags|=_locationacq.info_course
|
|
110 |
if(satellites):
|
|
111 |
flags|=_locationacq.info_satellites
|
|
112 |
|
|
113 |
if callback!=None:
|
|
114 |
if not callable(callback):
|
|
115 |
raise TypeError("'%s' is not callable"%callback)
|
|
116 |
_set_request_ongoing(True)
|
|
117 |
return _get_positioner().position(flags, callback, interval, partial)
|
|
118 |
else:
|
|
119 |
_lock=e32.Ao_lock()
|
|
120 |
_set_request_ongoing(True)
|
|
121 |
_get_positioner().position(flags, cb, interval, partial)
|
|
122 |
_lock.wait()
|
|
123 |
stop_position()
|
|
124 |
return _get_current_pos()
|
|
125 |
|
|
126 |
# get the last position information
|
|
127 |
def last_position():
|
|
128 |
if _get_request_ongoing():
|
|
129 |
raise RuntimeError, "Position request ongoing"
|
|
130 |
_set_request_ongoing(True)
|
|
131 |
last_pos=_get_positioner().last_position()
|
|
132 |
_get_positioner().stop_position()
|
|
133 |
_set_request_ongoing(False)
|
|
134 |
return last_pos
|
|
135 |
|