From 6e1d8c2672e1281091161762f0cf370c9b479f0f Mon Sep 17 00:00:00 2001
From: SurajSSingh <surajss@uci.edu>
Date: Sun, 8 May 2022 22:37:19 -0700
Subject: [PATCH] Added Simple Alarm

---
 .gitignore                                    |  2 +-
 .idea/.gitignore                              |  3 +
 .idea/Final Project.iml                       | 10 +++
 .../inspectionProfiles/profiles_settings.xml  |  6 ++
 .idea/misc.xml                                |  4 +
 .idea/modules.xml                             |  8 ++
 .idea/vcs.xml                                 |  6 ++
 alarm.py                                      | 37 ++++++--
 simple_alarm_clock.py                         | 84 +++++++++++++++++++
 vlc_mock.py                                   |  2 +
 10 files changed, 156 insertions(+), 6 deletions(-)
 create mode 100644 .idea/.gitignore
 create mode 100644 .idea/Final Project.iml
 create mode 100644 .idea/inspectionProfiles/profiles_settings.xml
 create mode 100644 .idea/misc.xml
 create mode 100644 .idea/modules.xml
 create mode 100644 .idea/vcs.xml
 create mode 100644 simple_alarm_clock.py
 create mode 100644 vlc_mock.py

diff --git a/.gitignore b/.gitignore
index a136337..f08278d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1 @@
-*.pdf
+*.pdf
\ No newline at end of file
diff --git a/.idea/.gitignore b/.idea/.gitignore
new file mode 100644
index 0000000..26d3352
--- /dev/null
+++ b/.idea/.gitignore
@@ -0,0 +1,3 @@
+# Default ignored files
+/shelf/
+/workspace.xml
diff --git a/.idea/Final Project.iml b/.idea/Final Project.iml
new file mode 100644
index 0000000..e4149dd
--- /dev/null
+++ b/.idea/Final Project.iml	
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="PYTHON_MODULE" version="4">
+  <component name="NewModuleRootManager">
+    <content url="file://$MODULE_DIR$">
+      <excludeFolder url="file://$MODULE_DIR$/venv" />
+    </content>
+    <orderEntry type="jdk" jdkName="Python 3.10 (Final Project)" jdkType="Python SDK" />
+    <orderEntry type="sourceFolder" forTests="false" />
+  </component>
+</module>
\ No newline at end of file
diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml
new file mode 100644
index 0000000..105ce2d
--- /dev/null
+++ b/.idea/inspectionProfiles/profiles_settings.xml
@@ -0,0 +1,6 @@
+<component name="InspectionProjectProfileManager">
+  <settings>
+    <option name="USE_PROJECT_PROFILE" value="false" />
+    <version value="1.0" />
+  </settings>
+</component>
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
new file mode 100644
index 0000000..92f766b
--- /dev/null
+++ b/.idea/misc.xml
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.10 (Final Project)" project-jdk-type="Python SDK" />
+</project>
\ No newline at end of file
diff --git a/.idea/modules.xml b/.idea/modules.xml
new file mode 100644
index 0000000..d4560ab
--- /dev/null
+++ b/.idea/modules.xml
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ProjectModuleManager">
+    <modules>
+      <module fileurl="file://$PROJECT_DIR$/.idea/Final Project.iml" filepath="$PROJECT_DIR$/.idea/Final Project.iml" />
+    </modules>
+  </component>
+</project>
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000..94a25f7
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="VcsDirectoryMappings">
+    <mapping directory="$PROJECT_DIR$" vcs="Git" />
+  </component>
+</project>
\ No newline at end of file
diff --git a/alarm.py b/alarm.py
index eb50245..49034fe 100644
--- a/alarm.py
+++ b/alarm.py
@@ -1,5 +1,32 @@
-# Code adapted from https://raspberrydiy.com/raspberry-pi-alarm-clock/
-import vlc
-from time import sleep
-
-
+# # Code adapted from https://raspberrydiy.com/raspberry-pi-alarm-clock/
+# import vlc
+# from time import sleep
+# # from dataclasses import dataclass, field
+#
+# # @dataclass
+# # class AlarmClock:
+#
+# #     sound: str
+#
+#
+# # Constants
+# VLC_INSTANCE = vlc.Instance("--input-repeat=999")
+# PLAYER = VLC_INSTANCE.media_player_new()
+# SONG = VLC_INSTANCE.media_new("/home/pi/Music/song.mp3")
+# VOLUME = 100
+#
+# PLAYER.set_media(SONG)
+# PLAYER.audio_set_volume(VOLUME)
+#
+# def play_alarm(volume: int = VOLUME):
+#     PLAYER.set_media(SONG)
+#     PLAYER.audio_set_volume(volume)
+#
+# def stop_alarm():
+#     pass
+#
+# def snooze_alarm(snooze_time: int = 60):
+#     pass
+#
+#
+#
diff --git a/simple_alarm_clock.py b/simple_alarm_clock.py
new file mode 100644
index 0000000..306dde8
--- /dev/null
+++ b/simple_alarm_clock.py
@@ -0,0 +1,84 @@
+from dataclasses import dataclass
+from enum import Enum, auto
+from datetime import time, datetime
+from typing import Optional, Any
+from time import sleep
+import vlc
+
+# Simple Alarm Clock has 5 functionalities:
+# 1. Set Alarm Time - Set the time for the alarm to sound, does NOT make the alarm active
+# 2. Start Alarm    - Make the alarm active, which will run the timer until it reaches the specified time
+# 3. Sound Alarm    - Play the sound for the alarm
+# 4. Snooze Alarm   - Stop the alarm sound and wait for some time to play the alarm again
+# 5. Stop Alarm     - If the alarm is active or is playing, stop the alarm (deactivate state)
+
+DEFAULT_VOLUME = 50
+
+
+class AlarmState(Enum):
+    SET = auto(),
+    STARTED = auto(),
+    PLAYING = auto(),
+    SNOOZED = auto(),
+    DEACTIVATED = auto(),
+
+
+@dataclass
+class AlarmClock:
+    player: Optional
+    wake_time: time
+    current_state: AlarmState = AlarmState.DEACTIVATED
+
+    def set_alarm(self, wake_time: time) -> time:
+        print(f"SETTING ALARM to: {wake_time}")
+        self.current_state = AlarmState.SET
+        self.wake_time = wake_time
+        return self.wake_time
+
+    def start_alarm(self, check_frequency: int = 1) -> bool:
+        print(f"STARTING ALARM")
+        self.current_state = AlarmState.STARTED
+        now = datetime.now().time()
+        if now >= self.wake_time:
+            return False
+        sleep_time_amount = check_frequency if check_frequency > 1 else 1
+        while now < self.wake_time:
+            # Sleep for some time
+            sleep(sleep_time_amount)
+            now = datetime.now().time()
+        self.sound_alarm()
+        return True
+
+    def sound_alarm(self, sound: Optional = None, volume: int = DEFAULT_VOLUME):
+        print(f"SOUNDING ALARM")
+        self.current_state = AlarmState.PLAYING
+        if sound is not None:
+            self.player.set_media(sound)
+        self.player.audio_set_volume(volume)
+        self.player.play()
+
+    def snooze_alarm(self, snooze_time: int = 60):
+        print(f"SNOOZING ALARM")
+        self.current_state = AlarmState.SNOOZED
+        self.player.pause()
+        # Maybe make this async later
+        sleep(snooze_time)
+        self.player.play()
+        if self.current_state is AlarmState.SNOOZED:
+            self.sound_alarm()
+
+    def stop_alarm(self, deactivate: bool = True):
+        print(f"STOPPING ALARM")
+        if self.current_state is AlarmState.PLAYING:
+            self.player.stop()
+        self.current_state = AlarmState.DEACTIVATED if deactivate else AlarmState.SET
+
+
+def simple_alarm_mode():
+    alarm_clock = AlarmClock(math, ALARM_TIME)
+
+
+ALARM_TIME = time(7, 0, 0)
+
+if __name__ == '__main__':
+    simple_alarm_mode()
\ No newline at end of file
diff --git a/vlc_mock.py b/vlc_mock.py
new file mode 100644
index 0000000..7cd0ee4
--- /dev/null
+++ b/vlc_mock.py
@@ -0,0 +1,2 @@
+class VlcTestInstance:
+    pass
\ No newline at end of file
-- 
GitLab