def deliverfile(self, eventargs, ruleargs): # examples of arguments: # eventargs = Sighting() # ruleargs = {'url':'http://img705.imageshack.us/img705/3679/lolface.gif', # 'mac':'aa:bb:cc:dd:ee:ff', # 'rememberfor':'3600', # 'retrytimes':'3'} # state variables: # sentfiles = (address, url, sendtime) sighting = eventargs if ruleargs.has_key('rememberfor'): rememberfor = ruleargs['rememberfor'] else: rememberfor = 3600 if self.state.get('sentfiles') == None: self.state.set('sentfiles', {}) for device in sighting.devices: if not ruleargs['mac'] or ruleargs[ 'mac'] == 'ANY' or device.address == ruleargs['mac']: if self.state.get('sentfiles').has_key( (device.address, ruleargs['url'])): timeOfLastSent = self.state.get('sentfiles')[( device.address, ruleargs['url'])] else: timeOfLastSent = 0 if timeOfLastSent < basics.Clock().getTimestamp( ) - rememberfor: path = self.modules['resources'].get(ruleargs['url']) if device.getServiceChannel('OBEX Object Push'): channel = device.getServiceChannel('OBEX Object Push') elif device.getServiceChannel('OBEX File Transfer'): channel = device.getServiceChannel( 'OBEX File Transfer') elif device.getServiceChannel('Object Push'): channel = device.getServiceChannel('Object Push') else: channel = 0 if device.address and channel and path: if ruleargs.has_key('retrytimes'): retrytimes = ruleargs['retrytimes'] else: retrytimes = 3 status = self.modules['obexsend'].sendFile( address=device.address, channel=channel, filepath=path, retrytimes=retrytimes) if status > 0: self.state.variables['sentfiles'][( device.address, ruleargs['url'] )] = basics.Clock().getTimestamp() self.state.stored = False print "[ACT] file %s sent to device %s" % ( path, device.address) else: print "[ACT] sending error or device rejected the file" else: print "[ACT] not possible to send file. service channel unkown" else: print "[ACT] device %s already have file %s. will not send again." % ( device.address, ruleargs['url'])
def serialize(self): rootText = "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"urn:Mynamespace\"" serial = "<?xml version=\"1.0\"?>" serial += "<sightings %s>" % rootText serial += "<clientReportId>%s</clientReportId>" % self.reportId serial += "<scanningBeginTime>%s</scanningBeginTime>" % basics.Clock().getIsoTime() serial += "<clientReportTime>%s</clientReportTime>" % basics.Clock().getIsoTime() serial += "<sightingItemsList>" for device in self.devices: serial += device.serialize(tagname='sightingItem') serial += "</sightingItemsList>" serial += "</sightings>" return serial
def __init__(self, address="", name="", devclass="Unknown", services={}, lastSeen=0): self.address = address self.name = name self.devclass = devclass self.services = services self.lastSeen = lastSeen if self.name: self.lastNameUpdate = basics.Clock().getTimestamp() else: self.lastNameUpdate = 0 if self.services: self.lastServicesUpdate = basics.Clock().getTimestamp() else: self.lastServicesUpdate = 0
def setInfo( self, address, name="", devclass=None, services={}, lastSeen=0): # if device is not on the list, this method adds it if not self._listOfDevices.has_key(address): self._listOfDevices[address] = Device(address=address) if name: self._listOfDevices[address].name = name self._listOfDevices[address].lastNameUpdate = basics.Clock( ).getTimestamp() if devclass: self._listOfDevices[address].devclass = devclass if services: self._listOfDevices[address].services = services if lastSeen: self._listOfDevices[address].lastseen = lastSeen
def run(self): print "[OXR] ObexReceive thread running" clock = basics.Clock() while not self.kill_received: if self.advertising: addr = None filename = "/tmp/bluebox-" + (hashlib.md5("11:11:11"+str(clock.getMicroTimestamp())).hexdigest()) print "[OXR] Ready to receive a file (%s) on interface hci%d" % (filename, self.devid) addr = lightblue.obex.recvfile(self.obexsocket, filename) print "[OXR] File",filename,"received from device", addr, ":)" self.sched.pushEvent("obexin",[filename,addr]) time.sleep(1) else: self.cv.acquire() self.cv.wait() self.cv.release() print "[OXR] ObexReceiver thread ended"
def addService(self, channel, name): self.services[channel] = name self.lastServicesUpdate = basics.Clock().getTimestamp()
def run(self): print "[SCN] Scanner thread running" namegetter = NameGetter(self.knownDevices, self.intrf) servicegetter = ServiceGetter(self.knownDevices, self.intrf) namegetter.start() servicegetter.start() while not self.kill_received: if self.scannerOn: devid = self.intrf.request(bandwidth=8) (status,seenaddresses) = self._doScan(devid=devid) addressesToGetName = [] addressesToGetServices = [] for addr in seenaddresses: self.knownDevices.add(addr) dev = self.knownDevices.getDeviceByAddress(addr) # run the getter threads: if self.namechache and self.namechache > 0: timeWithNameInCache = self.namechache else: timeWithNameInCache = 0 if self.getnames and dev and (not dev.name or dev.lastNameUpdate < basics.Clock().getTimestamp() - timeWithNameInCache): addressesToGetName.append(addr) if self.getservices and dev and not dev.services: addressesToGetServices.append(addr) self.intrf.release(interface=devid, bandwidth=8) # create the sighting structure and register event seendevices2 = [] for addr in seenaddresses: seendevices2.append(self.knownDevices.getDeviceByAddress(addr)) sighting = Sighting(seendevices2) if(status>0): self.sched.pushEvent("newscan",sighting) namegetter.addAddresses(addressesToGetName) #namegetter.join() servicegetter.addAddresses(addressesToGetServices) #servicegetter.join() wait = self.scaninterval - (time.time() - self.lastScanTimestamp) if wait < -100000 or wait > self.scaninterval: wait = self.scaninterval if wait>0: wait = int(round(wait)) print "[SCN] waiting %d seconds until scanning again (scaninterval=%s)" % (wait, self.scaninterval) for i in range(wait): if self.kill_received: break time.sleep(1) else: self.cv.acquire() self.cv.wait() self.cv.release() namegetter.kill() servicegetter.kill() print "[SCN] Scanner thread ended"