From 77abb74a971acff6ea5d13127d9f0ebd024b7d5d Mon Sep 17 00:00:00 2001 From: Bruce Schubert Date: Fri, 28 Aug 2020 06:42:40 -0700 Subject: [PATCH 1/2] Refactored voice mail message count- Message count query cannot occur in a separate thread. --- callattendant/messaging/message.py | 49 +++++++++++++++------------- callattendant/messaging/voicemail.py | 18 +++++----- 2 files changed, 34 insertions(+), 33 deletions(-) diff --git a/callattendant/messaging/message.py b/callattendant/messaging/message.py index a703be4..86188ad 100644 --- a/callattendant/messaging/message.py +++ b/callattendant/messaging/message.py @@ -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 = """ @@ -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, @@ -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): @@ -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 @@ -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() diff --git a/callattendant/messaging/voicemail.py b/callattendant/messaging/voicemail.py index a0214c2..5e466ee 100644 --- a/callattendant/messaging/voicemail.py +++ b/callattendant/messaging/voicemail.py @@ -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): @@ -48,31 +49,28 @@ 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(self.messages.unplayed_count) 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(self.messages.unplayed_count) def voice_messaging_menu(self, call_no, caller): """ @@ -108,7 +106,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(self.messages.unplayed_count) def record_message(self, call_no, caller): """ @@ -135,7 +133,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(self.messages.unplayed_count) # Return failure return None From 3077389e99415d3a4fff1b2355bab7acd8e53b79 Mon Sep 17 00:00:00 2001 From: Bruce Schubert Date: Fri, 28 Aug 2020 06:54:32 -0700 Subject: [PATCH 2/2] Refactored reseting the message indicator --- callattendant/messaging/voicemail.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/callattendant/messaging/voicemail.py b/callattendant/messaging/voicemail.py index 5e466ee..964c90b 100644 --- a/callattendant/messaging/voicemail.py +++ b/callattendant/messaging/voicemail.py @@ -55,7 +55,7 @@ def __init__(self, db, config, modem): self.event_thread.start() # Pulse the indicator if an unplayed msg is waiting - self.reset_message_indicator(self.messages.unplayed_count) + self.reset_message_indicator() if self.config["DEBUG"]: print("VoiceMail initialized") @@ -68,9 +68,7 @@ def _event_handler(self): while 1: # 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(self.messages.unplayed_count) + self.reset_message_indicator() def voice_messaging_menu(self, call_no, caller): """ @@ -106,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.unplayed_count) + self.reset_message_indicator() def record_message(self, call_no, caller): """ @@ -133,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.unplayed_count) + self.reset_message_indicator() # Return failure return None @@ -144,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: