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

Implement alert message handler #57

Merged
merged 2 commits into from
Apr 10, 2024
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
11 changes: 11 additions & 0 deletions neon_iris/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ def handle_neon_response(self, channel, method, _, body):
self.handle_error_response(message)
elif message.msg_type == "neon.languages.get.response":
self._handle_supported_languages(message)
elif message.msg_type == "neon.alert_expired":
self.handle_alert(message)
elif message.msg_type.endswith(".response"):
self.handle_api_response(message)
else:
Expand Down Expand Up @@ -231,6 +233,12 @@ def clear_media(self, message: Message):
Override this method to handle requests to clear media (photos, etc)
"""

@abstractmethod
def handle_alert(self, message: Message):
"""
Override this method to handle alerts (timers, alarms, reminders)
"""

def _handle_profile_update(self, message: Message):
updated_profile = message.data["profile"]
if updated_profile['user']['username'] == \
Expand Down Expand Up @@ -469,6 +477,9 @@ def handle_api_response(self, message: Message):
def clear_caches(self, message: Message):
print("Cached Responses Cleared")

def handle_alert(self, message: Message):
print(f"\nAlert Expired: {message.data.get('alert_name')}")

def clear_media(self, message: Message):
pass

Expand Down
28 changes: 26 additions & 2 deletions neon_iris/web_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def __init__(self, lang: str = None):
self.config = config.get('iris') or dict()
NeonAIClient.__init__(self, config.get("MQ"))
self._await_response = Event()
self._alerts = dict()
self._response = None
self._transcribed = None
self._current_tts = dict()
Expand Down Expand Up @@ -80,6 +81,14 @@ def _start_session(self):
self._profiles[sid]['user']['username'] = sid
return sid

def check_alerts(self, session_id: str):
if not self._alerts.get(session_id):
gradio.Info("No Alerts")
return session_id
while self._alerts.get(session_id):
gradio.Info(self._alerts[session_id].pop())
return session_id

def update_profile(self, stt_lang: str, tts_lang: str, tts_lang_2: str,
time: int, date: str, uom: str, city: str, state: str,
country: str, first: str, middle: str, last: str,
Expand Down Expand Up @@ -214,6 +223,9 @@ def run(self):
# inputs=[client_session],
# outputs=[tts_audio, client_session])
# Define settings UI
with gradio.Row():
submit = gradio.Button("Update User Settings")
check_alerts = gradio.Button("Check for Alerts")
with gradio.Row():
with gradio.Column():
lang = self.get_lang(client_session.value).split('-')[0]
Expand Down Expand Up @@ -253,14 +265,15 @@ def run(self):
pref_name = gradio.Textbox(label="Preferred Name")
email_addr = gradio.Textbox(label="Email Address")
# TODO: DoB, pic, about, phone?
submit = gradio.Button("Update User Settings")
submit.click(self.update_profile,
inputs=[stt_lang, tts_lang, tts_lang_2, time_format,
date_format, unit_of_measure, city, state,
country, first_name, middle_name, last_name,
pref_name, email_addr, client_session],
outputs=[client_session])
blocks.launch(server_name=address, server_port=port)
check_alerts.click(self.check_alerts, inputs=[client_session],
outputs=[client_session])
blocks.queue().launch(server_name=address, server_port=port)

def handle_klat_response(self, message: Message):
"""
Expand Down Expand Up @@ -307,6 +320,17 @@ def handle_api_response(self, message: Message):
if message.msg_type == "neon.audio_input.response":
self._transcribed = message.data.get("transcripts", [""])[0]

def handle_alert(self, message: Message):
"""
Handle an expired alert that was previously set by this session.
@param message: neon.alert_expired Message
"""
user = message.context['username']
LOG.info(f"Alert expired for user: {user}")
alert = f"Alert Expired: {message.data.get('alert_name')}"
self._alerts.setdefault(user, list())
self._alerts[user].append(alert)

def _handle_profile_update(self, message: Message):
updated_profile = message.data["profile"]
session_id = updated_profile['user']['username']
Expand Down
Loading