Example #1
0
def found_action_upload(remote, record = None, line=None):
    from openproximity.rpc.server import OpenProximityService
    
    uploader = OpenProximityService.getUploader()

    if uploader is None:
        line.content+=" no uploaders, can't handle"
        line.save()
        return True

    logger.info("found uploader")

    camps = getMatchingCampaigns(
            remote, 
            enabled=True, 
            record=record, 
            classes=[MarketingCampaign,]
    )


    if len(camps)==0:
        line.content+=" no matching campaings, not handling"
        line.save()
        logger.info("no campaigns")
        return True

    channel = None

    files=list()
    name=None
    service='opp'
    use_same = False

    for camp in camps:
        files.extend( list(get_files_from_campaign(camp, record)) )
        if camp.dongle_name:
            name = camp.dongle_name
        service = camp.get_service_display()
        if camp.fixed_channel and camp.fixed_channel > -1:
          channel = camp.fixed_channel
        if camp.upload_on_discovered:
            use_same = True

    sdp = RemoteBluetoothDeviceSDP.objects.filter(remote__pk=record.remote.pk)
    if sdp.count() > 0:
        channel = sdp.order_by("-time")[0].channel
        line.content += "using previous resolved channel %i" % channel

    logger.info("going to upload %s files" % len(files))
    if len(files) > 0:
        OpenProximityService.addPending(record.remote.address, uploader)
        do_upload(uploader, files, record.remote.address, service, name, 
                channel=channel, dongle=dongle if use_same else None)
        line.content+=" uploading files"
    else:
        line.content+=" no files to upload"
    
    line.save()
Example #2
0
def handle_sdp_timeout(dongle, remote):
    logger.info("SDP timeout: %s" % remote )

    from openproximity.rpc.server import OpenProximityService
    OpenProximityService.removePending(remote)

    record = RemoteBluetoothDeviceSDPTimeout()
    record.dongle = UploaderBluetoothDongle.objects.get(address=dongle)
    record.setRemoteDevice(remote)
    record.save()
Example #3
0
def handle_sdp_timeout(dongle, remote):
    logger.info("SDP timeout: %s" % remote)

    from openproximity.rpc.server import OpenProximityService

    OpenProximityService.removePending(remote)

    record = RemoteBluetoothDeviceSDPTimeout()
    record.dongle = UploaderBluetoothDongle.objects.get(address=dongle)
    record.setRemoteDevice(remote)
    record.save()
Example #4
0
def handle_sdp_norecord(dongle, remote):
    logger.info("No SDP: %s" % remote)
    
    from openproximity.rpc.server import OpenProximityService
    OpenProximityService.removePending(remote)

    remote=RemoteDevice.objects.filter(address=remote).get()
    if RemoteBluetoothDeviceNoSDP.objects.filter(remote=remote).count() == 0:
        record = RemoteBluetoothDeviceNoSDP()
        record.dongle = UploaderBluetoothDongle.objects.get(address=dongle)
        record.remote = remote
        record.save()
Example #5
0
def handle_file_uploaded(dongle, remote, channel, files):
    logger.info("files uploaded: %s[%s]: %s" % ( remote, channel, files) )

    from openproximity.rpc.server import OpenProximityService
    OpenProximityService.removePending(remote)

    for camp in get_campaign_rule(files):
        record = RemoteBluetoothDeviceFilesSuccess()
        record.dongle = UploaderBluetoothDongle.objects.get(address=dongle)
        record.campaign = camp
        record.setRemoteDevice(remote)
        record.save()
Example #6
0
def handle_file_uploaded(dongle, remote, channel, files):
    logger.info("files uploaded: %s[%s]: %s" % (remote, channel, files))

    from openproximity.rpc.server import OpenProximityService

    OpenProximityService.removePending(remote)

    for camp in get_campaign_rule(files):
        record = RemoteBluetoothDeviceFilesSuccess()
        record.dongle = UploaderBluetoothDongle.objects.get(address=dongle)
        record.campaign = camp
        record.setRemoteDevice(remote)
        record.save()
Example #7
0
def handle_sdp_norecord(dongle, remote):
    logger.info("No SDP: %s" % remote)

    from openproximity.rpc.server import OpenProximityService

    OpenProximityService.removePending(remote)

    remote = RemoteDevice.objects.filter(address=remote).get()
    if RemoteBluetoothDeviceNoSDP.objects.filter(remote=remote).count() == 0:
        record = RemoteBluetoothDeviceNoSDP()
        record.dongle = UploaderBluetoothDongle.objects.get(address=dongle)
        record.remote = remote
        record.save()
Example #8
0
def handle_file_failed(dongle, remote, channel, files, ret, err):
    logger.info("handle file failed %s[%s]: %s" % (remote, channel, files))
    logger.debug(err)
    try:
        rules = get_campaign_rule(files)
        if rules is None:
            raise Exception("No matching rule for files %s!" % files)
        record = None
        try_again = False

        for rule in get_campaign_rule(files):
            record = RemoteBluetoothDeviceFilesRejected()
            record.dongle = UploaderBluetoothDongle.objects.get(address=dongle)
            record.campaign = rule
            record.ret_value = ret
            record.setRemoteDevice(remote)
            record.save()
            # from here we try again either on timeout or if rejected count is
            # smaller than filter, for any of the matching rules
            # if only 1 campaign matches we do it all over!
            try_again = try_again | rule.tryAgain(record)

        logger.info("try again: %s" % try_again)
        if try_again:
            from openproximity.rpc.server import OpenProximityService

            uploader = OpenProximityService.getUploader()
            if uploader:
                logger.info("trying again")
                return do_upload(uploader, files, remote)
            else:
                logger.info("no uploader registered")
    except Exception, err:
        logger.error("OOOPS!!!")
        logger.exception(err)
Example #9
0
def handle_file_failed(dongle, remote, channel, files, ret, err):
    logger.info("handle file failed %s[%s]: %s" % (remote, channel, files))
    logger.debug(err)
    try:
        rules = get_campaign_rule(files)
        if rules is None:
            raise Exception("No matching rule for files %s!" % files)
        record = None
        try_again = False

        for rule in get_campaign_rule(files):
            record = RemoteBluetoothDeviceFilesRejected()
            record.dongle = UploaderBluetoothDongle.objects.get(address=dongle)
            record.campaign = rule
            record.ret_value = ret
            record.setRemoteDevice(remote)
            record.save()
            # from here we try again either on timeout or if rejected count is 
            # smaller than filter, for any of the matching rules
            # if only 1 campaign matches we do it all over!
            try_again = try_again | rule.tryAgain(record)

        logger.info("try again: %s" % try_again)
        if try_again:
            from openproximity.rpc.server import OpenProximityService
            uploader = OpenProximityService.getUploader()
            if uploader:
                logger.info("trying again")
                return do_upload(uploader, files, remote)
            else:
                logger.info("no uploader registered")
    except Exception, err:
        logger.error("OOOPS!!!")
        logger.exception(err)
Example #10
0
def found_action(address, record, dongle):
    line = LogLine()
    line.content = "Found action for: %s" % address
    try:
        for plugin in pluginsystem.get_plugins("found_action"):
            logger.info("found action trying with %s" % plugin.name)
            service = plugin.rpc["found_action"](record=record, dongle=dongle)
            if service:
                logger.info("plugin has handled")
                line.content += " %s is handling" % getattr(plugin, "name", "plugin")
                line.save()
                OpenProximityService.addPending(address, service)
                return True
    except Exception, err:
        logger.error("plugin do_action")
        logger.exception(err)
Example #11
0
def found_action(address, record, dongle):
    line = LogLine()
    line.content="Found action for: %s" % address
    try:
        for plugin in pluginsystem.get_plugins('found_action'):
            logger.info("found action trying with %s" % plugin.name)
            service = plugin.rpc['found_action'](record=record, dongle=dongle)
            if service:
                logger.info("plugin has handled")
                line.content+=" %s is handling" % getattr(plugin, 'name', 
                                                                    'plugin')
                line.save()
                OpenProximityService.addPending(address, service)
                return True
    except Exception, err:
        logger.error("plugin do_action")
        logger.exception(err)
Example #12
0
def handle_pair_error(dongle, remote, msg, exception):
    logger.info("Paired failed %s on %s, %s:%s" % (dongle, remote, msg, exception))

    record = RemoteBluetoothDevicePairing()
    record.dongle = UploaderBluetoothDongle.objects.get(address=dongle)
    record.setRemoteDevice(remote)
    exc = exception.lower()
    if "reject" in exc or "failed" in exc:
        record.state = RemoteBluetoothDevicePairing.STATES["Rejected"]
    else:
        record.state = RemoteBluetoothDevicePairing.STATES["Timeout"]
    record.msg = msg
    record.exception = exception
    record.save()

    from openproximity.rpc.server import OpenProximityService

    OpenProximityService.removePending(remote)
Example #13
0
def handle_pair_error(dongle, remote, msg, exception):
    logger.info("Paired failed %s on %s, %s:%s" % (dongle, remote, msg,
        exception))
    
    record = RemoteBluetoothDevicePairing()
    record.dongle = UploaderBluetoothDongle.objects.get(address=dongle)
    record.setRemoteDevice(remote)
    exc = exception.lower()
    if 'reject' in exc or 'failed' in exc:
        record.state = RemoteBluetoothDevicePairing.STATES["Rejected"]
    else:
        record.state = RemoteBluetoothDevicePairing.STATES["Timeout"]
    record.msg = msg
    record.exception = exception
    record.save()
    
    from openproximity.rpc.server import OpenProximityService
    OpenProximityService.removePending(remote)
Example #14
0
def handle_sdp_resolved(dongle, remote, channel):
    from openproximity.rpc.server import OpenProximityService
    logger.info("Valid SDP: %s %s" % (remote, channel) )
    remote=RemoteDevice.objects.filter(address=remote).get()
    if RemoteBluetoothDeviceSDP.objects.filter(remote=remote).count() == 0:
        logger.info("New SDP result")
        record = RemoteBluetoothDeviceSDP()
        record.dongle = UploaderBluetoothDongle.objects.get(address=dongle)
        record.channel = channel
        record.remote = remote
        record.save()
    logger.info("Starting bonding process")
    uploader = OpenProximityService.getUploader()
    uploader.start_pairing(remote.address)
Example #15
0
def handle_sdp_resolved(dongle, remote, channel):
    from openproximity.rpc.server import OpenProximityService

    logger.info("Valid SDP: %s %s" % (remote, channel))
    remote = RemoteDevice.objects.filter(address=remote).get()
    if RemoteBluetoothDeviceSDP.objects.filter(remote=remote).count() == 0:
        logger.info("New SDP result")
        record = RemoteBluetoothDeviceSDP()
        record.dongle = UploaderBluetoothDongle.objects.get(address=dongle)
        record.channel = channel
        record.remote = remote
        record.save()
    logger.info("Starting bonding process")
    uploader = OpenProximityService.getUploader()
    uploader.start_pairing(remote.address)
Example #16
0
def handle_addrecord(remote_, dongle):
    from openproximity.rpc.server import OpenProximityService
    address = remote_['address']
    name = smart_unicode(remote_['name']) if remote_['name'] else None

    logger.info("handle_addrecord %s" % address)
    remote=RemoteDevice.getRemoteDevice(
        address=address, 
        name=name, 
        devclass=remote_['devclass']
    )
    record = RemoteBluetoothDeviceFoundRecord()
    record.action = signals.FOUND_DEVICE
    record.dongle = dongle
    record.remote = remote
    record.remote.save() # update last seen
    record.setRSSI(remote_['rssi'])
    
    if not isinstance(dongle, RemoteScannerBluetoothDongle):
        if name is not None and record.remote.name!=name:
            record.remote.name = name
            record.remote.save()
        if remote_['devclass'] != -1 and \
                    record.remote.devclass!=remote_['devclass']:
            record.remote.devclass = remote_['devclass']
            record.remote.save()

    logger.debug(record)
    logger.debug(record.remote)

    record.save() # this will set the time field to now
    
    logl = LogLine()
    logl.content="Found: %s %s %s" % (
        record.remote.address, 
        record.remote.name,
        record.getRSSI())
    logl.save()
    
    if not OpenProximityService.isPending(address):
        return found_action(address, record, dongle=dongle)

    return True
Example #17
0
def handle_addrecord(remote_, dongle):
    from openproximity.rpc.server import OpenProximityService
    address = remote_['address']
    name = smart_unicode(remote_['name']) if remote_['name'] else None

    logger.info("handle_addrecord %s" % address)
    remote = RemoteDevice.getRemoteDevice(address=address,
                                          name=name,
                                          devclass=remote_['devclass'])
    record = RemoteBluetoothDeviceFoundRecord()
    record.action = signals.FOUND_DEVICE
    record.dongle = dongle
    record.remote = remote
    record.remote.save()  # update last seen
    record.setRSSI(remote_['rssi'])

    if not isinstance(dongle, RemoteScannerBluetoothDongle):
        if name is not None and record.remote.name != name:
            record.remote.name = name
            record.remote.save()
        if remote_['devclass'] != -1 and \
                    record.remote.devclass!=remote_['devclass']:
            record.remote.devclass = remote_['devclass']
            record.remote.save()

    logger.debug(record)
    logger.debug(record.remote)

    record.save()  # this will set the time field to now

    logl = LogLine()
    logl.content = "Found: %s %s %s" % (record.remote.address,
                                        record.remote.name, record.getRSSI())
    logl.save()

    if not OpenProximityService.isPending(address):
        return found_action(address, record, dongle=dongle)

    return True
Example #18
0
def cycle_completed():
    from openproximity.rpc.server import OpenProximityService
    logger.info("scanner_cycle_complete")
    camps = getMatchingCampaigns(enabled=True)
    scanner = OpenProximityService.getScanner()
    if len(camps) == 0:
        logger.info("no campaigns, no more scanning")
        # tell the rpc scanner to ping us periodically until there's
        # a new campaign available
        scanner.noCampaigns()
        return

    concurrent = False
    for camp in camps:
        if camp.concurrent_scanning:
            concurrent = True
            break

    logger.info("starting scan cycle")
    scanner.setConcurrent(concurrent)
    scanner.startScanningCycle()
    scanner.doScan()
Example #19
0
def cycle_completed():
    from openproximity.rpc.server import OpenProximityService
    logger.info("scanner_cycle_complete")
    camps = getMatchingCampaigns(enabled=True)
    scanner = OpenProximityService.getScanner()
    if len(camps)==0:
        logger.info("no campaigns, no more scanning")
        # tell the rpc scanner to ping us periodically until there's
        # a new campaign available
        scanner.noCampaigns()
        return

    concurrent = False
    for camp in camps:
        if camp.concurrent_scanning:
            concurrent = True
            break

    logger.info("starting scan cycle")
    scanner.setConcurrent(concurrent)
    scanner.startScanningCycle()
    scanner.doScan()
Example #20
0
        for rule in get_campaign_rule(files):
            record = RemoteBluetoothDeviceFilesRejected()
            record.dongle = UploaderBluetoothDongle.objects.get(address=dongle)
            record.campaign = rule
            record.ret_value = ret
            record.setRemoteDevice(remote)
            record.save()
            # from here we try again either on timeout or if rejected count is
            # smaller than filter, for any of the matching rules
            # if only 1 campaign matches we do it all over!
            try_again = try_again | rule.tryAgain(record)

        logger.info("try again: %s" % try_again)
        if try_again:
            from openproximity.rpc.server import OpenProximityService

            uploader = OpenProximityService.getUploader()
            if uploader:
                logger.info("trying again")
                return do_upload(uploader, files, remote)
            else:
                logger.info("no uploader registered")
    except Exception, err:
        logger.error("OOOPS!!!")
        logger.exception(err)
    logger.info("taking out of pending list")

    from openproximity.rpc.server import OpenProximityService

    OpenProximityService.removePending(remote)
Example #21
0
def do_scan():
    from openproximity.rpc.server import OpenProximityService
    logger.info("start scan")
    scanner = OpenProximityService.getScanner()
    scanner.doScan()
Example #22
0
        try_again = False

        for rule in get_campaign_rule(files):
            record = RemoteBluetoothDeviceFilesRejected()
            record.dongle = UploaderBluetoothDongle.objects.get(address=dongle)
            record.campaign = rule
            record.ret_value = ret
            record.setRemoteDevice(remote)
            record.save()
            # from here we try again either on timeout or if rejected count is 
            # smaller than filter, for any of the matching rules
            # if only 1 campaign matches we do it all over!
            try_again = try_again | rule.tryAgain(record)

        logger.info("try again: %s" % try_again)
        if try_again:
            from openproximity.rpc.server import OpenProximityService
            uploader = OpenProximityService.getUploader()
            if uploader:
                logger.info("trying again")
                return do_upload(uploader, files, remote)
            else:
                logger.info("no uploader registered")
    except Exception, err:
        logger.error("OOOPS!!!")
        logger.exception(err)
    logger.info("taking out of pending list")

    from openproximity.rpc.server import OpenProximityService
    OpenProximityService.removePending(remote)
Example #23
0
def do_scan():
    from openproximity.rpc.server import OpenProximityService
    logger.info("start scan")
    scanner = OpenProximityService.getScanner()
    scanner.doScan()