diff --git a/src/Assistant.py b/src/Assistant.py index 75b6e09..a5d2e7f 100644 --- a/src/Assistant.py +++ b/src/Assistant.py @@ -1,6 +1,6 @@ import re from datetime import datetime - +import threading import Support from VoiceInterface import VoiceInterface @@ -11,7 +11,7 @@ def __init__(self): """Creates an Assistant instance consisting of an VoiceInterface instance""" self.__voiceInterface = VoiceInterface() - + def wish_user(self): """Wishes user based on the hour of the day""" hour = int (datetime.now().hour) @@ -38,17 +38,15 @@ def listen_for_query(self) -> str: self.__voiceInterface.speak(LISTENING_ERROR) return query - + def execute_query(self, query: str) -> None: """Processes the query string and runs the corresponding tasks - Args: query (str): the query string obtained from speech input """ - # if any( text in query for text in ['exit', 'quit', 'close'] ): - # return - - if 'what can you do' in query: + if query is None: + print("No query detected. Please provide an input.") + elif 'what can you do' in query: Support.explain_features(self.__voiceInterface) elif re.match(r'(what|all|list).*apps.*open', query): @@ -78,7 +76,28 @@ def execute_query(self, query: str) -> None: elif any(text in query for text in ["the time", "time please"]): Support.tell_time(self.__voiceInterface) - + + elif 'scroll' in query: + if re.search(r'start scrolling (up|down|left|right|top|bottom)', query): + direction = re.findall(r'start scrolling (up|down|left|right|top|bottom)', query)[0] + scroll_thread,stop_scroll_event=Support.setup_scrolling() + if scroll_thread is None: # Only start if not already scrolling + Support.start_scrolling(direction) + + elif 'stop scrolling' in query: + scroll_thread,stop_scroll_event=Support.setup_scrolling() + if scroll_thread is not None: + Support.stop_scrolling() + elif re.search(r'scroll to (up|down|left|right|top|bottom)', query): + match = re.search(r'scroll to (up|down|left|right|top|bottom)', query) + direction = match.group(1) + Support.scroll_to(direction) + elif re.search(r'scroll (up|down|left|right)', query): + match = re.search(r'scroll (up|down|left|right)', query) + direction = match.group(1) + Support.simple_scroll(direction) + else: + print("Scroll command not recognized") else: self.__voiceInterface.speak("could not interpret the query") diff --git a/src/Support.py b/src/Support.py index 69182cf..5cacfb0 100644 --- a/src/Support.py +++ b/src/Support.py @@ -1,12 +1,19 @@ import os from datetime import datetime +import threading +import time +import pyautogui as pag +import pygetwindow as gw +from PIL import ImageGrab import googlesearch import wikipedia from ExternalPaths import AppPath, WebPath, features from VoiceInterface import VoiceInterface - +#include the actual code to gradual score +scroll_thread = None +stop_scroll_event = threading.Event() def clear_screen(): if os.name == "posix": @@ -120,4 +127,98 @@ def tell_time(vi: VoiceInterface) -> None: hour, minute, second = date_time.hour, date_time.minute, date_time.second tmz = date_time.tzname() - vi.speak(f"Current time is {hour}:{minute}:{second} {tmz}") \ No newline at end of file + vi.speak(f"Current time is {hour}:{minute}:{second} {tmz}") + +def setup_scrolling(): + if not hasattr(setup_scrolling, "scroll_thread"): + setup_scrolling.scroll_thread = None + if not hasattr(setup_scrolling, "stop_scroll_event"): + setup_scrolling.stop_scroll_event = threading.Event() + + return setup_scrolling.scroll_thread, setup_scrolling.stop_scroll_event + +def start_gradual_scroll(direction: str, stop_event: threading.Event) -> None: + """Gradually scroll in the given direction until stop_event is set.""" + time.sleep(2) + active_window = pag.getActiveWindow() + if active_window: + + left, top, width, height = active_window.left, active_window.top, active_window.width, active_window.height + + previous_image = ImageGrab.grab(bbox=(left, top, left + width, top + height)) # Capture the entire window + + while True: + if stop_event.is_set(): + break + pag.press(direction) + time.sleep(1) + current_image = ImageGrab.grab(bbox=(left, top, left + width, top + height)) + + if list(current_image.getdata()) == list(previous_image.getdata()): + print("Reached to extreme") + stop_event.set() + setup_scrolling.scroll_thread = None + break + previous_image = current_image + + print(f"Scrolling {direction}...") # Simulate scrolling action + # Simulate delay between scroll actions + print(f"Stopped scrolling {direction}.") + +def start_scrolling(direction: str) -> None: + """Start a new scroll thread.""" + setup_scrolling.stop_scroll_event.clear() + setup_scrolling.scroll_thread = threading.Thread(target=start_gradual_scroll, args=(direction, setup_scrolling.stop_scroll_event)) + setup_scrolling.scroll_thread.start() + +def stop_scrolling() -> None: + """Stop the current scrolling thread.""" + setup_scrolling.stop_scroll_event.set() + if setup_scrolling.scroll_thread is not None: + setup_scrolling.scroll_thread.join() + setup_scrolling.scroll_thread = None + print("Scrolling has stopped.") + + +def scroll_to(direction:str)->None: + active_window = gw.getActiveWindow() + if active_window: + # Bring the active window to the front + active_window.activate() + time.sleep(0.5) + if direction=='top': + pag.press('home') + + elif direction=='bottom': + pag.press('end') + + elif direction=='right': + pag.press('right', presses=9999) + + elif direction=='left': + pag.press('left', presses=9999) + + else: + print("Invalid Command") + + +#pygetwindow and implement +def simple_scroll(direction:str)->None: + active_window = gw.getActiveWindow() + if active_window: + # Bring the active window to the front + active_window.activate() + time.sleep(0.5) + if direction=='up': + pag.press('up', presses=100) + elif direction=='down': + pag.press('down', presses=100) + elif direction=='right': + pag.press('right', presses=500) + elif direction=='left': + pag.press('left', presses=500) + + else: + print("Invalid direction") + + diff --git a/src/VoiceInterface.py b/src/VoiceInterface.py index 27d7844..63163c0 100644 --- a/src/VoiceInterface.py +++ b/src/VoiceInterface.py @@ -57,7 +57,7 @@ def listen(self, print_statement: bool = False) -> Any | None: try: # language = English(en)-India(in) if print_statement: print("Recognizing...\n") - query = self.__recognizer.recoginize_google(audio_data=audio, language="en-in") + query = self.__recognizer.recognize_google(audio_data=audio, language="en-in") return query except: return None