Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #4 from myoung34/threading
Browse files Browse the repository at this point in the history
Use threading properly
  • Loading branch information
myoung34 authored Feb 17, 2020
2 parents a49d7c4 + 50db681 commit e5a0dbd
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 12 deletions.
13 changes: 13 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@
from tilty import cli


@mock.patch('tilty.tilt_device')
@mock.patch('tilty.cli.sys')
def test_terminate_process(
mock_tilt_device,
mock_sys,
):
cli.terminate_process(mock_tilt_device, None, None)
assert mock_tilt_device.mock_calls == [
mock.call.stop(),
mock.call.exit()
]


def test_cli_invalid_params():
runner = CliRunner()
result = runner.invoke(cli.run, ["--foo"])
Expand Down
13 changes: 13 additions & 0 deletions tests/test_tilt_device.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# -*- coding: utf-8 -*-
from unittest import mock

from tilty import tilt_device


@mock.patch('tilty.blescan.hci_disable_le_scan')
def test_scan_for_tilt_data(
mock_disable_le_scan,
):
t = tilt_device.TiltDevice()
t.stop()
mock_disable_le_scan.assert_called()
1 change: 1 addition & 0 deletions tests/test_tilty.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def test_scan_for_tilt_data(
):
t = tilt_device.TiltDevice()
t.scan_for_tilt_data()
bt_events.assert_called()


@mock.patch('tilty.emitters.webhook.Webhook')
Expand Down
45 changes: 36 additions & 9 deletions tilty/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

import configparser
import logging
import signal
import sys
import threading
from functools import partial
from time import sleep

import click
Expand All @@ -16,6 +20,29 @@
CONFIG = configparser.ConfigParser()


def terminate_process(device, signal_number, frame): # noqa # pylint: disable=unused-argument
""" handle SIGTERM """
device.stop()
sys.exit()


def scan_and_emit(device, config):
""" method that does the needful
"""
tilt_data = device.scan_for_tilt_data()
click.echo(tilt_data)
emit(config=config, tilt_data=tilt_data)


def scan_and_emit_thread(device, config, keep_running=False):
""" method that calls the needful
"""
scan_and_emit(device, config)
while keep_running:
scan_and_emit(device, config)
sleep(int(config['general']['sleep_interval']))


@click.command()
@click.option(
'--keep-running',
Expand All @@ -37,14 +64,14 @@ def run(
"""
CONFIG.read(config_file)
click.echo('Scanning for Tilt data...')
t = tilt_device.TiltDevice()
t.start()
device = tilt_device.TiltDevice()
signal.signal(signal.SIGINT, partial(terminate_process, device))
device.start()
threading.Thread(
target=scan_and_emit_thread,
name='tilty_daemon',
args=(device, CONFIG, keep_running)
).start()
if keep_running:
while True:
tilt_data = t.scan_for_tilt_data()
emit(config=CONFIG, tilt_data=tilt_data)
sleep(int(CONFIG['general']['sleep_interval']))
else:
tilt_data = t.scan_for_tilt_data()
click.echo(tilt_data)
emit(config=CONFIG, tilt_data=tilt_data)
pass
11 changes: 8 additions & 3 deletions tilty/tilt_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,20 @@ def __init__(self, device_id=0):
self.sock = bluez.hci_open_dev(device_id)

def start(self):
""" Initializer
""" Start scanning and device
Args:
device_id: (int) represents the device id for HCI
sock: the socket to open
"""
blescan.hci_le_set_scan_parameters(self.sock)
blescan.hci_enable_le_scan(self.sock)

def stop(self):
""" Stop scanning and device
Args:
"""
blescan.hci_disable_le_scan(self.sock)

def scan_for_tilt_data(self):
""" scan for tilt and return data if found """

Expand Down

0 comments on commit e5a0dbd

Please sign in to comment.