Ejemplo n.º 1
0
def test_insert_alarms():
    repo = AlarmRepository()
    alarm = Alarm(name="Alarm 1", hour=6, minute=0)
    repo.insert(alarm)
    result = repo.find_all()
    assert len(result) is 1
    assert result[0].id is 1
Ejemplo n.º 2
0
    def delete_alarm(self, id):
        if "id" is None:
            return "Invalid Update Request: Entitiy id is missing"

        entity = AlarmRepository().find_by_id(id)
        AlarmRepository().delete(entity)

        return "200"
Ejemplo n.º 3
0
    def __init__(self, testing=False):
        threading.Thread.__init__(self)
        self.running = True
        self.alarm_repo = AlarmRepository()

        if testing is False:
            from Tassimo import Tassimo
            self.tassimo = Tassimo()
        else:
            self.tassimo = None
Ejemplo n.º 4
0
def setup_alarm():
    ConnectorFactory().build_connector().clear_db()
    now = datetime.now()
    yesterday = now - timedelta(1)

    repo = AlarmRepository()
    alarm = Alarm(name="Alarm 1",
                  hour=now.hour,
                  minute=now.minute,
                  last_activated=yesterday)
    repo.insert(alarm)
Ejemplo n.º 5
0
def test_find_alarms():
    repo = AlarmRepository()

    for i in range(10):
        repo.insert(Alarm("Alarm {}".format(i), hour=i + 1, minute=i))

    result = repo.find_all()
    assert len(result) is 10

    for i in range(len(result)):
        assert result[i].id is i + 1
Ejemplo n.º 6
0
def test_exectuing_alarm():
    repo = AlarmRepository()
    alarm = repo.find_by_id(1)
    alarm.last_activated = datetime.strptime(alarm.last_activated,
                                             "%Y-%m-%d %H:%M:%S.%f")
    expected = alarm.last_activated + timedelta(1)

    alarm_thread = AlarmThread(testing=True)
    alarm_thread.start()

    sleep(1)

    alarm_thread.running = False
    alarm_thread.join(10)

    alarm = repo.find_by_id(1)
    alarm.last_activated = datetime.strptime(alarm.last_activated,
                                             "%Y-%m-%d %H:%M:%S.%f")

    assert alarm.last_activated.day is expected.day
Ejemplo n.º 7
0
def test_delete():
    repo = AlarmRepository()

    for i in range(10):
        repo.update(Alarm(name="Alarm {}".format(i), hour=i + 1, minute=i))

    result = repo.find_all()
    assert len(result) is 10

    for i in range(len(result)):
        assert result[i].id is i + 1
        repo.delete(result[i])

    result = repo.find_all()
    assert len(result) is 0
Ejemplo n.º 8
0
    def update_alarm(self, alarm_as_dict):
        if "id" not in alarm_as_dict:
            return "Invalid Update Request: Entitiy id is missing"

        repo = AlarmRepository()

        alarm = repo.find_by_id(alarm_as_dict["id"])

        if alarm is None:
            return "400: Alarm not found"

        if "hour" in alarm_as_dict:
            alarm.hour = alarm_as_dict["hour"]
        if "minute" in alarm_as_dict:
            alarm.minute = alarm_as_dict["minute"]
        if "name" in alarm_as_dict:
            alarm.name = alarm_as_dict["name"]

        alarm = AlarmRepository().update(alarm)

        if alarm is not None:
            return jsonify(AlarmJSONConverter().alarm_to_json(alarm))
        else:
            return "400: Alarm not found"
Ejemplo n.º 9
0
class AlarmThread(threading.Thread):
    def __init__(self, testing=False):
        threading.Thread.__init__(self)
        self.running = True
        self.alarm_repo = AlarmRepository()

        if testing is False:
            from Tassimo import Tassimo
            self.tassimo = Tassimo()
        else:
            self.tassimo = None

    def run(self):
        while self.running is True:
            alarms = self.alarm_repo.find_all()
            now = datetime.now()

            for alarm in alarms:
                datetime_obj = datetime.strptime(alarm.last_activated,
                                                 "%Y-%m-%d %H:%M:%S.%f")

                if alarm.hour == now.hour and alarm.minute == now.minute:
                    if datetime_obj.day != now.day or \
                            datetime_obj.month != now.month or \
                            datetime_obj.year != now.year or \
                            datetime_obj.hour != now.hour or \
                            datetime_obj.minute != now.minute:
                        print("Activated Alarm " + str(alarm.id) + " : " +
                              alarm.name)
                        alarm.last_activated = now
                        self.alarm_repo.update(alarm)

                        if self.tassimo is not None:
                            self.tassimo.make_coffee()

            sleep(0.5)
Ejemplo n.º 10
0
 def post_alarm(self, alarm_as_dict):
     alarm = self.convert_dict_to_alarm(alarm_as_dict)
     alarm = AlarmRepository().insert(alarm)
     return jsonify(AlarmJSONConverter().alarm_to_json(alarm))
Ejemplo n.º 11
0
 def get_alarms(self):
     alarms = AlarmRepository().find_all()
     return jsonify(AlarmJSONConverter().alarm_list_to_json(alarms))