예제 #1
0
def evsensor():
    try:
        # process command line arguments
        debug = False
        args = arg_parse()
        if args.debug:
            debug = True

        # reset bluetooth functionality
        try:
            if debug:
                print "-- reseting bluetooth device"
            ble.reset_hci()
            if debug:
                print "-- reseting bluetooth device : success"
        except Exception as e:
            print "error enabling bluetooth device"
            print str(e)
            sys.exit(1)

        # initialize bluetooth socket
        try:
            if debug:
                print "-- open bluetooth device"
            sock = ble.bluez.hci_open_dev(conf.BT_DEV_ID)
            if debug:
                print "-- ble thread started"
        except Exception as e:
            print "error accessing bluetooth device: ", str(conf.BT_DEV_ID)
            print str(e)
            sys.exit(1)

        # set ble scan parameters
        try:
            if debug:
                print "-- set ble scan parameters"
            ble.hci_le_set_scan_parameters(sock)
            if debug:
                print "-- set ble scan parameters : success"
        except Exception as e:
            print "failed to set scan parameter!!"
            print str(e)
            sys.exit(1)

        # start ble scan
        try:
            if debug:
                print "-- enable ble scan"
            ble.hci_le_enable_scan(sock)
            if debug:
                print "-- ble scan started"
        except Exception as e:
            print "failed to activate scan!!"
            print str(e)
            sys.exit(1)
        global flag_scanning_started
        flag_scanning_started = True
        print("envsensor_observer : complete initialization")
        print ""

        # preserve old filter setting
        old_filter = sock.getsockopt(ble.bluez.SOL_HCI, ble.bluez.HCI_FILTER,
                                     14)
        # perform a device inquiry on bluetooth device #0
        # The inquiry should last 8 * 1.28 = 10.24 seconds
        # before the inquiry is performed, bluez should flush its cache of
        # previously discovered devices
        flt = ble.bluez.hci_filter_new()
        ble.bluez.hci_filter_all_events(flt)
        ble.bluez.hci_filter_set_ptype(flt, ble.bluez.HCI_EVENT_PKT)
        sock.setsockopt(ble.bluez.SOL_HCI, ble.bluez.HCI_FILTER, flt)

        # activate timer for sensor status evaluation
        timer = threading.Timer(conf.CHECK_SENSOR_STATE_INTERVAL_SECONDS,
                                eval_sensor_state)

        timer.setDaemon(True)
        timer.start()

        global flag_update_sensor_status
        while True:
            # parse ble event
            parse_events(sock)
            if flag_update_sensor_status:
                print_sensor_state()
                flag_update_sensor_status = False

    except Exception as e:
        print "Exception: " + str(e)
        import traceback
        traceback.print_exc()
        sys.exit(1)

    finally:
        if flag_scanning_started:
            # restore old filter setting
            sock.setsockopt(ble.bluez.SOL_HCI, ble.bluez.HCI_FILTER,
                            old_filter)
            ble.hci_le_disable_scan(sock)
        print "Exit"
예제 #2
0
# main function
if __name__ == "__main__":
    try:
        flag_scanning_started = False

        # process command line arguments
        debug = False
        args = arg_parse()
        if args.debug:
            debug = True

        # reset bluetooth functionality
        try:
            if debug:
                print "-- reseting bluetooth device"
            ble.reset_hci()
            if debug:
                print "-- reseting bluetooth device : success"
        except Exception as e:
            print "error enabling bluetooth device"
            print str(e)
            sys.exit(1)

        # initialize cloud (influxDB) output interface
        try:
            if conf.INFLUXDB_OUTPUT:
                if debug:
                    print "-- initialize influxDB interface"
                influx_client = InfluxDBClient(conf.INFLUXDB_ADDRESS,
                                               conf.INFLUXDB_PORT,
                                               conf.INFLUXDB_USER,
예제 #3
0
def evsensor():
    # print('called')
    sys.path.append("..")
    import util
    try:
        # process command line arguments
        debug = False
        args = arg_parse()
        if args.debug:
            debug = False

        # reset bluetooth functionality
        try:
            if debug:
                print "-- reseting bluetooth device"
            ble.reset_hci()
            if debug:
                print "-- reseting bluetooth device : success"
        except Exception as e:
            util.Logprint(4, "error enabling bluetooth device")
            # print "error enabling bluetooth device"
            # print str(e)
            sys.exit(1)

        # initialize bluetooth socket
        try:
            if debug:
                print "-- open bluetooth device"
            sock = ble.bluez.hci_open_dev(conf.BT_DEV_ID)
            if debug:
                print "-- ble thread started"
        except Exception as e:
            util.Logprint(4, "error accessing bluetooth device")
            # print "error accessing bluetooth device: ", str(conf.BT_DEV_ID)
            # print str(e)
            sys.exit(1)

        # set ble scan parameters
        try:
            if debug:
                print "-- set ble scan parameters"
            ble.hci_le_set_scan_parameters(sock)
            if debug:
                print "-- set ble scan parameters : success"
        except Exception as e:
            util.Logprint(4, "failed to set scan parameter!!")
            # print "failed to set scan parameter!!"
            # print str(e)
            sys.exit(1)

        # start ble scan
        try:
            if debug:
                print "-- enable ble scan"
            ble.hci_le_enable_scan(sock)
            if debug:
                print "-- ble scan started"
        except Exception as e:
            util.Logprint(4, "failed to activate scan!!")
            # print "failed to activate scan!!"
            # print str(e)
            sys.exit(1)

        global flag_scanning_started
        flag_scanning_started = True

        util.Logprint(1, "envsensor_observer : complete initialization")
        # print ("envsensor_observer : complete initialization")
        # print ""

        # preserve old filter setting
        old_filter = sock.getsockopt(ble.bluez.SOL_HCI, ble.bluez.HCI_FILTER,
                                     14)
        # perform a device inquiry on bluetooth device #0
        # The inquiry should last 8 * 1.28 = 10.24 seconds
        # before the inquiry is performed, bluez should flush its cache of
        # previously discovered devices
        flt = ble.bluez.hci_filter_new()
        ble.bluez.hci_filter_all_events(flt)
        ble.bluez.hci_filter_set_ptype(flt, ble.bluez.HCI_EVENT_PKT)
        sock.setsockopt(ble.bluez.SOL_HCI, ble.bluez.HCI_FILTER, flt)

        # activate timer for sensor status evaluation
        with open('/home/nvidia/Horus/config.cnf') as f:
            cnf = json.load(f)
        timer = threading.Timer(
            int(cnf['check_sensor_state_interval_seconds']), eval_sensor_state)

        timer.setDaemon(True)
        timer.start()

        global flag_update_sensor_status
        while True:
            # parse ble event
            parse_events(sock)
            if flag_update_sensor_status:
                # print_sensor_state()
                flag_update_sensor_status = False

    except Exception as e:
        # print "Exception: " + str(e)
        import traceback
        traceback.print_exc()
        sys.exit(1)

    finally:
        if flag_scanning_started:
            # restore old filter setting
            sock.setsockopt(ble.bluez.SOL_HCI, ble.bluez.HCI_FILTER,
                            old_filter)
            ble.hci_le_disable_scan(sock)