-
Notifications
You must be signed in to change notification settings - Fork 0
/
notify_user.py
32 lines (28 loc) · 1.17 KB
/
notify_user.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import os
from time import sleep
from gtts import gTTS
from playsound import playsound
try: # Beep module enables to produce beep sound in Windows systems
from winsound import Beep
except:
pass
def notify_message(message, flag):
"""Notify user whether to start/stop charging the device based on battery percent using Google Text To Speech"""
try: # Google text to speech to convert message into audio
tts = gTTS(text=message, lang="en")
audio_file_name = "Battery_Monitor.mp3"
tts.save(audio_file_name)
playsound(audio_file_name)
os.remove(audio_file_name)
except ConnectionError: # In case of no internet connection
try: # Produce beep sound in Windows systems
if flag == "optimal":
Beep(950, 730)
elif flag == "low":
[Beep(800, 750) for _ in range(2)]
except OSError: # Produce beep sound in Linux systems
if flag == "optimal":
os.system(f"play -nq -t alsa synth {0.5} sine {320}")
elif flag == "low":
[os.system(
f"play -nq -t alsa synth {0.5} sine {250}") for _ in range(2)]