Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored voice mail message count #93

Merged
merged 2 commits into from
Aug 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 26 additions & 23 deletions callattendant/messaging/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,22 @@

class Message:

def __init__(self, db, config, message_indicator):
def __init__(self, db, config):
"""
Initialize the database tables for voice messages.
:param db:
The database used within a single thread.
:config:
The applicaiton-wide config object.
"""
if config["DEBUG"]:
print("Initializing Message")

self.db = db
self.config = config
self.message_indicator = message_indicator
self.unplayted_count = 0
self.message_event = threading.Event()

# Create the message table if it does not exist
if self.db:
sql = """
Expand All @@ -52,18 +57,26 @@ def __init__(self, db, config, message_indicator):
Filename TEXT,
DateTime TEXT,
FOREIGN KEY(CallLogID) REFERENCES CallLog(CallLogID));"""

curs = self.db.cursor()
curs.executescript(sql)
curs.close()

self._update_unplayed_count()

if config["DEBUG"]:
print("Message initialized")

def add(self, call_no, filepath):
"""
Adds a message to the table
Adds a message to the table.
:param call_no:
The unique ID of the call this message is associated with.
:param filepath:
The name and path for the message .wav file that was recorded.
:return:
The unique ID of the new row
"""

sql = """
INSERT INTO Message(
CallLogID,
Expand All @@ -86,9 +99,8 @@ def add(self, call_no, filepath):
msg_no = curs.fetchone()[0]
curs.close()

self.message_event.set()
self.message_event.clear()
# ~ self.reset_message_indicator()
self._update_unplayed_count()

return msg_no

def delete(self, msg_no):
Expand Down Expand Up @@ -130,9 +142,7 @@ def delete(self, msg_no):
print("Message entry removed")
pprint(arguments)

self.message_event.set()
self.message_event.clear()
# ~ self.reset_message_indicator()
self._update_unplayed_count()

return success

Expand All @@ -150,22 +160,15 @@ def update_played(self, msg_no, played=1):
pprint(e)
return False

self.message_event.set()
self.message_event.clear()
# ~ self.reset_message_indicator()
self._update_unplayed_count()
return True

def get_unplayed_count(self):
def _update_unplayed_count(self):
# Get the number of unread messages
sql = "SELECT COUNT(*) FROM Message WHERE Played = 0"
curs = self.db.execute(sql)
unplayed_count = curs.fetchone()[0]
self.unplayed_count = curs.fetchone()[0]
if self.config["DEBUG"]:
print("Unplayed message count is {}".format(unplayed_count))
return unplayed_count

def reset_message_indicator(self):
if self.get_unplayed_count() > 0:
self.message_indicator.pulse()
else:
self.message_indicator.turn_off()
print("Unplayed message count is {}".format(self.unplayed_count))
self.message_event.set()
self.message_event.clear()
23 changes: 10 additions & 13 deletions callattendant/messaging/voicemail.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from hardware.indicators import MessageIndicator, MessageCountIndicator, \
GPIO_MESSAGE, GPIO_MESSAGE_COUNT_PINS, GPIO_MESSAGE_COUNT_KWARGS


class VoiceMail:

def __init__(self, db, config, modem):
Expand All @@ -48,31 +49,26 @@ def __init__(self, db, config, modem):
kwargs = config.get("GPIO_LED_MESSAGE_COUNT_KWARGS", GPIO_MESSAGE_COUNT_KWARGS)
self.message_count_indicator = MessageCountIndicator(*pins, **kwargs)

self.messages = Message(db, config, self.message_indicator)
self.messages = Message(db, config)
self.event_thread = threading.Thread(target=self._event_handler)
self.event_thread.name = "voice_mail_event_handler"
self.event_thread.start()

# Pulse the indicator if an unplayed msg is waiting
self.reset_message_indicator(self.messages.get_unplayed_count())
self.reset_message_indicator()

if self.config["DEBUG"]:
print("VoiceMail initialized")


def _event_handler(self):
"""
Thread function
Thread function that updates the message indicators upon a message event.
"""
db = sqlite3.connect(self.config['DB_FILE'])
sql = "SELECT COUNT(*) FROM Message WHERE Played = 0"

while 1:
# Get the number of unread messages
# Get the number of unread messages
if self.messages.message_event.wait():
curs = self.db.execute(sql)
unplayed_count = curs.fetchone()[0]
self.reset_message_indicator(unplayed_count)
self.reset_message_indicator()

def voice_messaging_menu(self, call_no, caller):
"""
Expand Down Expand Up @@ -108,7 +104,7 @@ def voice_messaging_menu(self, call_no, caller):
tries += 1
self.modem.play_audio(goodbye_file)
if not rec_msg:
self.reset_message_indicator(self.messages.get_unplayed_count())
self.reset_message_indicator()

def record_message(self, call_no, caller):
"""
Expand All @@ -135,7 +131,7 @@ def record_message(self, call_no, caller):
# Return the messageID on success
return msg_no
else:
self.reset_message_indicator(self.messages.get_unplayed_count())
self.reset_message_indicator()
# Return failure
return None

Expand All @@ -146,7 +142,8 @@ def delete_message(self, msg_no):
# Remove message and file (message.delete will update the indicator)
return self.messages.delete(msg_no)

def reset_message_indicator(self, unplayed_count):
def reset_message_indicator(self):
unplayed_count = self.messages.unplayed_count
if unplayed_count > 0:
self.message_indicator.pulse()
if unplayed_count < 10:
Expand Down