class InteractionManager(ButtonObserver):
    def __init__(self, GPIO):
        self.__current_job = None

        self.light_indicator = LightIndicator(GPIO)
        self.job_runner = JobRunner()
        self.job_factory = JobFactory()

    def update(self, press_time):
        if self.job_runner.is_idle():
            self.__current_job = self.__get_job_by_press_time(press_time)
            self.__start_job()
        else:
            self.__stop_job()
            self.__current_job = None

    def __start_job(self):
        self.light_indicator.turn_on()
        self.job_runner.start(self.__current_job)

    def __stop_job(self):
        self.light_indicator.turn_off()
        self.job_runner.stop()

    def __get_job_by_press_time(self, press_time):
        return self.job_factory.create_reading_job() \
            if press_time < 3 \
            else self.job_factory.create_auto_assignment_job()
Пример #2
0
class App:
    def __init__(self, GPIO):
        self.config = Config()
        self.job_factory = JobFactory()
        self.job_runner = JobRunner()
        self.button_handler = ButtonHandler(GPIO)
        self.interaction_manager = InteractionManager(GPIO)

    def run(self):
        if self.config.start_immediately:
            self.job_runner.start(self.job_factory.create_reading_job())
        elif self.config.start_auto_assignment:
            self.job_runner.start(
                self.job_factory.create_auto_assignment_job())
        else:
            self.button_handler.add_observer(self.interaction_manager)
            self.button_handler.start_monitoring()
Пример #3
0
    def __init__(self, GPIO):
        self.gpio = GPIO()
        self.job_runner = JobRunner()

        self.__dispose_callback = None
        self.__bounce_time = 500

        self.observers = list()
        self.pressed = False
        self.initial_press_time = 0
Пример #4
0
class TestJobRunner(unittest.TestCase):
    @patch('gripit.jobs.job.Job', autospec=True)
    @patch('threading.Thread', autospec=True)
    def setUp(self, MockJob, MockThread):
        self.mock_job = MockJob
        self.mock_thread = MockThread

        self.job_runner = JobRunner()

    def test_start_will_raise_exception_if_thread_already_started(self):
        self.job_runner.start(self.mock_job)

        with self.assertRaises(Exception) as cm:
            self.job_runner.start(self.mock_job)

        self.assertIsNotNone(cm.exception)

    @patch.object(ThreadFactory, 'create')
    def test_start_will_create_a_new_thread(self, MockThreadFactoryCreate):
        self.job_runner.start(self.mock_job)

        MockThreadFactoryCreate.assert_called_with(self.mock_job.run)

    @patch.object(ThreadFactory, 'create')
    def test_start_will_start_the_new_thread(self, MockThreadFactoryCreate):
        MockThreadFactoryCreate.return_value = self.mock_job

        self.job_runner.start(self.mock_job)

        self.mock_job.start.assert_called()

    @patch.object(ThreadFactory, 'create')
    def test_stop_will_stop_the_thread(self, MockThreadFactoryCreate):
        MockThreadFactoryCreate.return_value = self.mock_job
        self.job_runner.start(self.mock_thread)

        self.job_runner.stop()

        self.mock_thread.stop.assert_called()

    def test_is_idle_will_return_true_by_default(self):
        self.assertTrue(self.job_runner.is_idle())

    def test_is_idle_will_return_false_if_job_was_started(self):
        self.job_runner.start(self.mock_job)

        self.assertFalse(self.job_runner.is_idle())

    def test_is_busy_will_return_false_by_default(self):
        self.assertFalse(self.job_runner.is_busy())

    def test_is_busy_will_return_true_if_job_was_started(self):
        self.job_runner.start(self.mock_job)

        self.assertTrue(self.job_runner.is_busy())
Пример #5
0
    def setUp(self, MockJob, MockThread):
        self.mock_job = MockJob
        self.mock_thread = MockThread

        self.job_runner = JobRunner()
Пример #6
0
 def __init__(self, GPIO):
     self.config = Config()
     self.job_factory = JobFactory()
     self.job_runner = JobRunner()
     self.button_handler = ButtonHandler(GPIO)
     self.interaction_manager = InteractionManager(GPIO)
    def __init__(self, GPIO):
        self.__current_job = None

        self.light_indicator = LightIndicator(GPIO)
        self.job_runner = JobRunner()
        self.job_factory = JobFactory()