def _scan_position(self): ut.report("_scan_position") def f(): if self.positioner: self.positioner.close() self.positioner = None try: from pytwink import Positioner modlist = positioning.modules() module_id = None for entry in modlist: if entry["name"] == u"Network based" and \ entry["available"]: module_id = entry["id"] break if not module_id: raise "no network positioning module" updatetimeout = 15000000 maxupdateage = 30000000 self.positioner = Positioner(module_id, updatetimeout, maxupdateage) self.positioner.ask_position(self._positioner_done) return except: self._send_card() self.active = True self.cb("progress", u"Querying position") self._via_immediate(f)
def _scan_gsm(self): ut.report("_scan_gsm") def f(): self.gsm_scanner.scan(self._gsm_done) self.active = True self.cb("progress", u"Scanning context") self._via_immediate(f)
def _timeout(self): """ We use a timer to restrict the scan duration to something reasonable. """ self.active = False ut.report("btprox scan timeout") self.resolver.cancel() self.cb(self.list) # anything so far
def _gsm_done(self, gsm): """ Note that it is important to set values to None when scanning fails so that we never end up sending a card with an old value. """ ut.report("_gsm_done") self.active = False self.card.set_gsm(gsm) self._scan_btprox()
def context_start_scanning(self): """ Starts any constant context scanning that is enabled. It is safe to call this even when already scanning. """ if self.config.get_noscan(): return if self.config.get_gps_scan(): self._gps_start_scanning() ut.report("context scanning started")
def _send_done(self, serr, equ): self.active = False if serr: ut.report("send error (Symbian %d)" % serr) self.cb("fail", u"Sending failed") elif equ == "refused": self.cb("fail", u"Server did not accept card") elif equ == "accepted": self.cb("ok", u"Card sent") else: self.cb("fail", u"Unexpected response from server")
def use_module(self, module_id): if module_id is None: raise "must specify valid module ID" if self.module_id != module_id: isact = self.active if isact: self.stop() try: positioning.select_module(module_id) self.module_id = module_id ut.report("selected GPS module %d (%s)" % (self.module_id, self.module_gui_name())) finally: if isact: self.start()
def _read(self, err, data, udata): ut.report((err, data, udata)) if err != 0: self.cb(err, None) else: self.data += data self.len += len(data) if self.len == self.max: self.cb(err, self.data) elif self.len > self.max: self.cb(err, self.data[0:self.max]) else: self.read()
def message_arrived(self, id): # At the time we get a message arrival event, it is not yet in # Inbox, and many of the Inbox methods fail to work on that # message. So we just record the ID, and handle it after the # callback has returned. if self.inbox: ut.report("got message %d" % id) if self.is_bt_message(id): # xxx yes, there is a period of time where a message could be lost self.later_do(lambda: self._examine(id)) else: ut.report("ignoring non-BT message")
def _btprox_done(self, btdata): """ btdata:: Has value None if there was a scanning error. """ ut.report("_btprox_done") self.active = False if btdata is None: self.btprox_scanner = None self.card.set_btprox(btdata) if self.card.gps is None: # Our positioning hack no longer appears to work, so disabling for now. #self._scan_position() self._send_card() else: self._send_card()
def _cb(self, error, mac, name, dummy): ut.report([error, mac, name, dummy]) self.active = False if error == -25: # KErrEof (no more devices) self.timer.cancel() self.cb(self.list) elif error: self.timer.cancel() ut.report("BT scan error %d" % error) appuifw.note(u"Bluetooth scan failed", "error") self.cb(None) else: self.list.append({"mac": add_colons(mac), "name": name}) self.resolver.next() self.active = True
def _scan_btprox(self): ut.report("_scan_btprox") try: if not self.config.get_btprox_scan(): self._btprox_done(None) return if not self.btprox_scanner: self.init_btprox_scanner() if not self.btprox_scanner: if not self.btprox_scan_error_shown: self.btprox_scan_error_shown = True appuifw.note(u"Could not scan proximity: Is Bluetooth enabled?", "error") self._btprox_done(None) return self.btprox_scanner.scan(self._btprox_done) self.active = True except: ut.print_exception() self._btprox_done(None)
def _init_gps_scanner(self): """ It is valid for "self.gps_scanner" to be None, since the capabilities required to access GPS may not be available. If this method fails, then "self.gps_scanner" will be set to None, and will stay that way. To change the used positioning module, invoke the appropriate methods on the scanner. """ self.gps_scanner = None if gps_avail: try: omid = self.config.db.get("gps_module_id", None) self.gps_scanner = GpsScanner(self._gps_scanned, omid) except: if ut.get_debug_on(): ut.print_exception() ut.report("GPS scanner init failure") self.gps_scanner = None else: ut.report("GPS not available")
def context_stop_scanning(self): """ Stops all constant context scanning. """ self._gps_stop_scanning() ut.report("context scanning stopped")
def _examine(self, id): ut.report("examining message %d" % id) try: ut.report("message size is %d" % self.inbox.size(id)) desc = self.inbox.description(id) if self.is_filedata_message(id, desc): data = inbox_read_data(self.inbox, id) ut.report("message fetched") self.inbox.delete(id) ut.report("message deleted") self.cb(desc, data) else: ut.report("ignoring non-Filedata message") except: ut.report("failed to handle message") ut.print_exception()
def stop_observing(self): if self.inbox is not None: self.inbox = None ut.report("not observing Inbox")
def start_observing(self): if self.inbox is None: self.inbox = pyinbox.Inbox(KUidMsgTypeBt) self.inbox.bind(self.message_arrived) ut.report("observing Inbox")
def _positioner_done(self, code): ut.report("_positioner_done (%d)" % code) self.active = False if not code: self.card.gps = {"position": self.positioner.get_position()} self._send_card()