Beispiel #1
0
class Cafeteira:
    def __init__(self):
        self.twitter = TwitterManager()
        self.scheduled_times = self.setup_schedule()
        self.schedule = ScheduleManager(self.scheduled_times)
        self.machine = MachineManager()
        self.machine.listen_button(self.button_callback)
        self.start_schedule_process()
        self.start_api_process()

    def get_machine_status(self):
        return self.machine.machine_status

    def get_schedule_times(self):
        return self.scheduled_times

    def button_callback(self, pin):
        logger.debug("Button panic activaded!")
        if self.machine.machine_status is MachineStatus.stand_by:
            self.start_coffee_routine_async()
        else:
            self.twitter.tweet_turn_off_machine()
            self.machine.go_back_stand_by()

    def setup_schedule(self):
        FILE_PATH = "schedule_coffee.txt"
        reader = ScheduleReader(FILE_PATH)
        times = reader.read_scheduled_times()
        return times

    def check_schedule_are_ok(self):
        while True:
            if self.schedule.its_time() and self.machine.machine_status is MachineStatus.stand_by:
                logger.debug("Schedule time is Ok!")
                self.start_coffee_routine_async()
            time.sleep(30)

    def start_api_process(self):
        logger.debug("Create process for API")
        self.api = ApiManager(self)
        self.api.start()
        thread_api = Thread(target=self.api.start)
        thread_api.start()

    def start_schedule_process(self):
        logger.debug("Create process for Schedule Reader")
        thread_reading_schedule = Thread(target=self.check_schedule_are_ok)
        thread_reading_schedule.start()

    def start_coffee_routine_async(self):
        if self.machine.machine_status is MachineStatus.stand_by:
            logger.debug("Create thread for coffee")
            self.twitter.tweet_turn_on_machine()
            thread_making_coffee = Thread(target=self.machine.start_coffee_routine)
            thread_making_coffee.start()
class TwitterManagerTest(unittest.TestCase):

    def setUp(self):
        self.target = TwitterManager()
        self.target.update_status = MagicMock()

    def test_twitter_turn_on_machine(self):
        self.target.transform_message = MagicMock()
        self.target.tweet_turn_on_machine()
        self.assertTrue(self.target.update_status.called)
        self.assertTrue(self.target.transform_message.called)

    def test_twitter_turn_off_machine(self):
        self.target.transform_message = MagicMock()
        self.target.tweet_turn_off_machine()
        self.assertTrue(self.target.update_status.called)
        self.assertTrue(self.target.transform_message.called)

    @freeze_time("2015-10-10 10:00:00")
    def test_twitter_message_transform(self):
        message = self.target.transform_message("%02d:%02d teste!!")
        self.assertEquals('10:00 teste!!', message)