fishyboteso/fishy/engine/common/IEngine.py

82 lines
2.2 KiB
Python
Raw Normal View History

import logging
2020-06-01 12:58:18 +00:00
import typing
from threading import Thread
from typing import Callable
import cv2
from fishy.engine.common.window import WindowClient
2020-10-17 19:06:07 +00:00
from fishy.gui.funcs import GUIFuncsMock
from fishy.helper.helper import print_exc
2020-06-01 12:58:18 +00:00
if typing.TYPE_CHECKING:
from fishy.gui import GUI
class IEngine:
2020-06-01 12:58:18 +00:00
2020-10-17 19:06:07 +00:00
def __init__(self, gui_ref: 'Callable[[], GUI]'):
2020-06-01 12:58:18 +00:00
self.get_gui = gui_ref
# 0 - off, 1 - running, 2 - quitting
self.state = 0
self.window = None
2022-02-02 23:59:10 +00:00
self.thread = None
2022-02-03 00:21:31 +00:00
self.name = "default"
2020-06-01 12:58:18 +00:00
@property
def gui(self):
if self.get_gui is None:
return GUIFuncsMock()
2020-06-01 12:58:18 +00:00
return self.get_gui().funcs
@property
def start(self):
return self.state == 1
2020-06-01 12:58:18 +00:00
def toggle_start(self):
if self.state == 0:
self.turn_on()
else:
self.turn_off()
def turn_on(self):
self.state = 1
self.thread = Thread(target=self._crash_safe)
self.thread.start()
def join(self):
if self.thread:
2022-02-03 00:21:31 +00:00
logging.debug(f"waiting for {self.name} engine")
self.thread.join()
def turn_off(self):
"""
this method only signals the thread to close using start flag,
its the responsibility of the thread to shut turn itself off
"""
if self.state == 1:
2022-02-03 00:21:31 +00:00
logging.debug(f"sending turn off signal to {self.name} engine")
self.state = 2
else:
2022-02-03 00:21:31 +00:00
logging.debug(f"{self.name} engine already signaled to turn off ")
# todo: implement force turn off on repeated calls
# noinspection PyBroadException
def _crash_safe(self):
2022-02-03 00:21:31 +00:00
logging.debug(f"starting {self.name} engine thread")
self.window = WindowClient(color=cv2.COLOR_RGB2GRAY, show_name=f"{self.name} debug")
self.gui.bot_started(True)
try:
self.run()
except Exception:
2022-02-03 00:21:31 +00:00
logging.error(f"Unhandled exception occurred while running {self.name} engine")
print_exc()
self.state = 0
self.gui.bot_started(False)
self.window.destroy()
2022-02-03 00:21:31 +00:00
logging.debug(f"{self.name} engine thread safely exiting")
2020-06-01 12:58:18 +00:00
def run(self):
raise NotImplementedError