def scan_and_emit_thread(device: tilt_device.TiltDevice, config: configparser.ConfigParser, keep_running: bool = False) -> None: """ method that calls the needful Args: device (TiltDevice): The bluetooth device to operate on. config (dict): The parsed configuration keep_running (bool): Whether or not to keep running. Default: False """ emitters = parse_config(config) click.echo('Scanning for Tilt data...') gravity_offset = float( safe_get_key(CONFIG, 'general', {}).get('gravity_offset', '0')) LOGGER.debug('Gravity offset: %f', gravity_offset) temperature_offset = float( safe_get_key(CONFIG, 'general', {}).get('temperature_offset', '0')) LOGGER.debug('Temperature offset: %f', temperature_offset) scan_and_emit(device, emitters, gravity_offset, temperature_offset) while keep_running: LOGGER.debug('Scanning for Tilt data...') try: scan_and_emit(device, emitters, gravity_offset, temperature_offset) except Exception as exception: # pylint: disable=broad-except LOGGER.error("%s\n%s", str(exception), traceback.format_tb(exception.__traceback__)) sleep_time = int(CONFIG['general'].get('sleep_interval', '1')) LOGGER.debug('Sleeping for %s....', sleep_time) sleep(sleep_time)
def __init__(self, config: dict) -> None: """ Initializer Args: config: (dict) represents the configuration for the emitter """ # [prometheus] # url = localhost:80 # gravity_gauge_name = tilty_gravity_g # temp_gauge_name = tilty_temperature_f # labels = {"color": "{{ color }}", "mac": "{{ mac }}"} # job_name = tilty self.job_name = safe_get_key(config, 'job_name', 'tilty') self.label_template = Template(config['labels']) self.url = config['url'] self.registry = CollectorRegistry() label_dict = json.loads(self.label_template.render()) self.gravity_gauge = Gauge( safe_get_key(config, 'gravity_gauge_name', 'tilty_gravity_g'), 'The currently measured gravity', label_dict.keys(), registry=self.registry, ) self.temp_gauge = Gauge( safe_get_key(config, 'temp_gauge_name', 'tilty_temperature_f'), 'The currently measured temperature', label_dict.keys(), registry=self.registry, )
def __init__(self, config): """ Initializer Args: config: (dict) represents the configuration for the emitter """ self.temperature_payload = config['temperature_payload'] self.gravity_payload = config['gravity_payload'] self.client = InfluxDBClient(config['url'], safe_get_key(config, 'port', 80), safe_get_key(config, 'user'), safe_get_key(config, 'password'), config['database'])
def __init__(self, config: dict) -> None: """ Initializer Args: config: (dict) represents the configuration for the emitter """ # [influxdb] # url = www.foo.com # port = 80 # database = tilty # gravity_payload_template = {"measurement": "gravity", "tags": {"color": "{{ color }}"}, "fields": {"value": {{ gravity }}}} # noqa # pylint: disable=line-too-long # temperature_payload_template = {"measurement": "temperature", "tags": {"color": "{{ color }}"}, "fields": {"value": {{ temp }}}} # noqa # pylint: disable=line-too-long self.gravity_template = Template(config['gravity_payload_template']) # noqa self.temperature_template = Template(config['temperature_payload_template']) # noqa self.client = InfluxDBClient( config['url'], safe_get_key(config, 'port', 80), safe_get_key(config, 'user'), safe_get_key(config, 'password'), config['database'] )
def __init__(self, config: dict) -> None: """ Initializer Args: config: (dict) represents the configuration for the emitter """ self.gravity_template = Template(config['gravity_payload_template']) # noqa self.temperature_template = Template(config['temperature_payload_template']) # noqa self.bucket = safe_get_key(config, 'bucket') verify_ssl = bool(distutil.strtobool( safe_get_key(config, 'verify_ssl', 'False') )) self.org = safe_get_key(config, 'org') client = InfluxDBClient( url=config['url'], org=self.org, token=safe_get_key(config, 'token'), verify_ssl=verify_ssl ) self.write_api = client.write_api(write_options=SYNCHRONOUS)
def __init__(self, config: dict) -> None: """ Initializer Args: config: (dict) represents the configuration for the emitter """ # [datadog] # host = 'host' # port = 'port' options = { 'statsd_host': config['host'], 'statsd_port': safe_get_key(config, 'port', 8125), } initialize(**options)
def __init__(self, config): """ Initializer Args: config: (dict) represents the configuration for the emitter """ self.temperature = config['temperature'] self.gravity = config['gravity'] self.color = config['color'] options = { 'statsd_host': config['host'], 'statsd_port': safe_get_key(config, 'port', 8125), } initialize(**options)
def test_safe_get_key_fallback(): assert common.safe_get_key({}, 'foo', 'wut') == 'wut'
def test_safe_get_key_no_fallback(): assert common.safe_get_key({}, 'foo') is None
def test_safe_get_key_valid(): assert common.safe_get_key({'foo': 'asdf'}, 'foo', 'wut') == 'asdf'