1
0
Эх сурвалжийг харах

feat: 调整tk的文件结构

SongZihuan 3 жил өмнө
parent
commit
a35150fa17

+ 14 - 194
control/admin.py

@@ -2,14 +2,13 @@ import tkinter as tk
 from tkinter import ttk
 from tkinter import messagebox as msg
 import abc
-import time
 
 import conf
 from tool.type_ import *
-from tool.tk import set_button_disable_from_list, make_font
+from tool.tk import make_font
 
 from sql.db import DB
-from sql.user import creat_new_user, del_user, find_user_by_name
+from sql.user import creat_new_user, del_user
 from sql.garbage import creat_new_garbage, del_garbage_not_use, del_garbage_not_use_many
 
 from equipment.scan_user import write_uid_qr, write_all_uid_qr
@@ -18,7 +17,7 @@ from equipment.scan_garbage import write_gid_qr
 from core.user import User
 from core.garbage import GarbageBag
 
-from tk_event import TkEventBase, TkEventMain, TkThreading
+from tk_event import TkEventMain
 
 
 class AdminStationException(Exception):
@@ -33,22 +32,6 @@ class CreatUserError(AdminStationException):
     ...
 
 
-class AdminEventBase(TkEventBase):
-    def __init__(self, station, title: str = 'unknown'):
-        self.station: AdminStationBase = station
-        self._db: DB = station.get_db()
-        self._title = title
-
-    def get_title(self) -> str:
-        return self._title
-
-    def is_end(self) -> bool:
-        raise AdminStationException
-
-    def done_after_event(self):
-        raise AdminStationException
-
-
 class AdminStationBase(TkEventMain, metaclass=abc.ABCMeta):
     def __init__(self, db: DB):
         self._admin: Optional[User] = None
@@ -128,6 +111,11 @@ class AdminStationBase(TkEventMain, metaclass=abc.ABCMeta):
         ...
 
 
+import admin_program as tk_program
+import admin_menu as tk_menu
+import admin_event as tk_event
+
+
 class AdminStation(AdminStationBase):
     def set_after_run(self, ms, func, *args):  # super.__init__可能会调用
         self.init_after_run_list.append((ms, func, args))
@@ -157,7 +145,7 @@ class AdminStation(AdminStationBase):
         self._full_screen = False
         self._is_loading = False
         self._disable_all_btn = False
-        self._now_program: Optional[Tuple[str, tk.Frame, AdminProgram]] = None
+        self._now_program: Optional[Tuple[str, tk.Frame, tk_program.AdminProgram]] = None
 
         self.__conf_font_size()
         self.__conf_creak_tk()
@@ -185,12 +173,12 @@ class AdminStation(AdminStationBase):
         self._menu_back = tk.Frame(self._window)
         self._menu_line = tk.Label(self._menu_back)  # 下划线
         self._menu_title: Tuple[tk.Label, tk.Variable] = tk.Label(self._menu_back), tk.StringVar()
-        self._menu_dict: Dict[str, AdminMenu] = {}
+        self._menu_dict: Dict[str, tk_menu.AdminMenu] = {}
         self._menu_list: List[str] = []  # 菜单回溯
 
         self._program_back = tk.Frame(self._window)
         self._program_title: Tuple[tk.Label, tk.Variable] = tk.Label(self._program_back), tk.StringVar()
-        self._program_dict: Dict[str, AdminProgram] = {}
+        self._program_dict: Dict[str, tk_program.AdminProgram] = {}
 
         self._msg_frame = tk.Frame(self._window)
         self._msg_label = tk.Label(self._msg_frame), tk.Label(self._msg_frame), tk.StringVar(), tk.StringVar()
@@ -254,7 +242,7 @@ class AdminStation(AdminStationBase):
 
     def __conf_creak_menu(self):
         frame_list = [
-            MainMenu(self, self._menu_back, '#fffffb')
+            tk_menu.MainMenu(self, self._menu_back, '#fffffb')
         ]
 
         for i in frame_list:
@@ -309,7 +297,7 @@ class AdminStation(AdminStationBase):
 
     def __conf_creak_program(self):
         program_list = [
-            WelcomeProgram(self, self._program_back, '#fffffb')
+            tk_program.WelcomeProgram(self, self._program_back, '#fffffb')
         ]
 
         for i in program_list:
@@ -487,7 +475,7 @@ class AdminStation(AdminStationBase):
         self._login_btn[1].place(relx=0.70, rely=0.70, relwidth=0.16, relheight=0.15)
 
     def login_call(self):
-        event = LoginEvent(self).start(self._login_name[2].get(), self._login_passwd[2].get())
+        event = tk_event.LoginEvent(self).start(self._login_name[2].get(), self._login_passwd[2].get())
         self.push_event(event)
 
     def login(self, user: User):
@@ -526,174 +514,6 @@ class AdminStation(AdminStationBase):
         self._window.destroy()
 
 
-class LoginEvent(AdminEventBase):
-    def __init__(self, station: AdminStationBase):
-        super().__init__(station, "Ranking")
-        self.thread: Optional[TkThreading] = None
-
-    def login(self, name, passwd):
-        return find_user_by_name(name, passwd, self._db)
-
-    def start(self, name, passwd):
-        self.thread = TkThreading(self.login, name, passwd)
-        return self
-
-    def is_end(self) -> bool:
-        return not self.thread.is_alive()
-
-    def done_after_event(self):
-        self.station.login(self.thread.wait_event())
-
-
-class TestProgressEvent(AdminEventBase):
-    @staticmethod
-    def func(sleep_time):
-        time.sleep(sleep_time)
-
-    def __init__(self, station, db):
-        super(TestProgressEvent, self).__init__(station, db)
-        self.thread = TkThreading(self.func, 5)
-
-    def is_end(self) -> bool:
-        return not self.thread.is_alive()
-
-    def done_after_event(self):
-        ...
-
-
-class AdminMenu(metaclass=abc.ABCMeta):
-    def __init__(self, station: AdminStation, win: Union[tk.Frame, tk.Toplevel, tk.Tk], color: str):
-        self.station = station
-        self.win = win
-        self.color = color
-
-    @abc.abstractmethod
-    def set_disable(self):
-        ...
-
-    @abc.abstractmethod
-    def reset_disable(self):
-        ...
-
-    @abc.abstractmethod
-    def conf_gui(self, color: str, n: int = 1):
-        ...
-
-    @abc.abstractmethod
-    def get_menu_frame(self) -> Tuple[str, tk.Frame]:
-        ...
-
-
-class AdminProgram(metaclass=abc.ABCMeta):
-    def __init__(self, station: AdminStation, win: Union[tk.Frame, tk.Toplevel, tk.Tk], color: str):
-        self.station = station
-        self.win = win
-        self.color = color
-
-    @abc.abstractmethod
-    def set_disable(self):
-        ...
-
-    @abc.abstractmethod
-    def reset_disable(self):
-        ...
-
-    @abc.abstractmethod
-    def conf_gui(self, n: int = 1):
-        ...
-
-    @abc.abstractmethod
-    def get_program_frame(self) -> tk.Frame:
-        ...
-
-
-class MainMenu(AdminMenu):
-    def __init__(self, station, win, color):
-        super(MainMenu, self).__init__(station, win, color)
-        self.frame = tk.Frame(self.win)
-        self.frame['bg'] = color
-
-        self.btn: List[tk.Button] = [tk.Button(self.frame) for _ in range(5)]
-        self.__conf_font()
-
-    def __conf_font(self, n: int = 1):
-        self.btn_font_size = int(16 * n)
-
-    def conf_gui(self, color: str, n: int = 1):
-        self.__conf_font(n)
-
-        btn_font = make_font(size=self.btn_font_size, weight="bold")
-        height = 0.02
-        for btn, text in zip(self.btn, ["Creat", "Delete", "Search", "Update", "Logout"]):
-            btn['font'] = btn_font
-            btn['text'] = text
-            btn['bg'] = color
-            btn.place(relx=0.02, rely=height, relwidth=0.96, relheight=0.1)
-            height += 0.1 + 0.02
-
-    def get_menu_frame(self) -> Tuple[str, tk.Frame]:
-        return "Main", self.frame
-
-    def set_disable(self):
-        set_button_disable_from_list(self.btn)
-
-    def reset_disable(self):
-        set_button_disable_from_list(self.btn, flat='normal')
-
-
-class WelcomeProgram(AdminProgram):
-    def __init__(self, station, win, color):
-        super(WelcomeProgram, self).__init__(station, win, color)
-        self.frame = tk.Frame(self.win)
-        self.frame['bg'] = color
-
-        self.title = tk.Label(self.frame)
-        self.btn: List[tk.Button] = [tk.Button(self.frame) for _ in range(2)]
-        self.__conf_font()
-
-    def __conf_font(self, n: int = 1):
-        self.title_font_size = int(25 * n)
-        self.btn_font_size = int(14 * n)
-
-    def conf_gui(self, n: int = 1):
-        self.__conf_font(n)
-
-        title_font = make_font(size=self.title_font_size, weight="bold")
-        btn_font = make_font(size=self.btn_font_size)
-
-        for btn, text in zip(self.btn, ["TestMSG", "TestProgress"]):
-            btn['font'] = btn_font
-            btn['text'] = text
-            btn['bg'] = '#d3d7d4'
-
-        self.title['text'] = 'Welcome to HGSSystem Manager'
-        self.title['font'] = title_font
-        self.title['bg'] = self.color
-
-        self.btn[0]['command'] = lambda: self.test_msg()
-        self.btn[1]['command'] = lambda: self.test_progress()
-
-        self.title.place(relx=0.1, rely=0.3, relwidth=0.8, relheight=0.2)
-        self.btn[0].place(relx=0.2, rely=0.7, relwidth=0.2, relheight=0.1)
-        self.btn[1].place(relx=0.6, rely=0.7, relwidth=0.2, relheight=0.1)
-
-    def test_progress(self):
-        event = TestProgressEvent(self.station, "Sleep(5)")
-        self.station.push_event(event)
-
-    def test_msg(self):
-        self.station.show_msg("Test Msg", "test msg")
-
-    def get_program_frame(self) -> Tuple[str, tk.Frame]:
-        return "Welcome", self.frame
-
-    def set_disable(self):
-        set_button_disable_from_list(self.btn)
-
-    def reset_disable(self):
-        set_button_disable_from_list(self.btn, flat='normal')
-
-
 if __name__ == '__main__':
     mysql_db = DB()
     station_ = AdminStation(mysql_db)

+ 59 - 0
control/admin_event.py

@@ -0,0 +1,59 @@
+import time
+
+from tool.type_ import *
+from sql.db import DB
+from sql.user import find_user_by_name
+
+from tk_event import TkEventBase, TkThreading, TkEventException
+import admin
+
+
+class AdminEventBase(TkEventBase):
+    def __init__(self, station):
+        super(AdminEventBase, self).__init__()
+        self.station: admin.AdminStationBase = station
+        self._db: DB = station.get_db()
+
+    def get_title(self) -> str:
+        return "AdminEvent"
+
+    def is_end(self) -> bool:
+        raise TkEventException
+
+    def done_after_event(self):
+        raise TkEventException
+
+
+class LoginEvent(AdminEventBase):
+    def __init__(self, station):
+        super().__init__(station)
+        self.thread: Optional[TkThreading] = None
+
+    def login(self, name, passwd):
+        return find_user_by_name(name, passwd, self._db)
+
+    def start(self, name, passwd):
+        self.thread = TkThreading(self.login, name, passwd)
+        return self
+
+    def is_end(self) -> bool:
+        return not self.thread.is_alive()
+
+    def done_after_event(self):
+        self.station.login(self.thread.wait_event())
+
+
+class TestProgressEvent(AdminEventBase):
+    @staticmethod
+    def func(sleep_time):
+        time.sleep(sleep_time)
+
+    def __init__(self, station):
+        super(TestProgressEvent, self).__init__(station)
+        self.thread = TkThreading(self.func, 5)
+
+    def is_end(self) -> bool:
+        return not self.thread.is_alive()
+
+    def done_after_event(self):
+        ...

+ 64 - 0
control/admin_menu.py

@@ -0,0 +1,64 @@
+import abc
+import tkinter as tk
+
+from tool.type_ import *
+from tool.tk import make_font, set_button_disable_from_list
+
+import admin
+
+
+class AdminMenu(metaclass=abc.ABCMeta):
+    def __init__(self, station: admin.AdminStationBase, win: Union[tk.Frame, tk.Toplevel, tk.Tk], color: str):
+        self.station = station
+        self.win = win
+        self.color = color
+
+    @abc.abstractmethod
+    def set_disable(self):
+        ...
+
+    @abc.abstractmethod
+    def reset_disable(self):
+        ...
+
+    @abc.abstractmethod
+    def conf_gui(self, color: str, n: int = 1):
+        ...
+
+    @abc.abstractmethod
+    def get_menu_frame(self) -> Tuple[str, tk.Frame]:
+        ...
+
+
+class MainMenu(AdminMenu):
+    def __init__(self, station, win, color):
+        super(MainMenu, self).__init__(station, win, color)
+        self.frame = tk.Frame(self.win)
+        self.frame['bg'] = color
+
+        self.btn: List[tk.Button] = [tk.Button(self.frame) for _ in range(5)]
+        self.__conf_font()
+
+    def __conf_font(self, n: int = 1):
+        self.btn_font_size = int(16 * n)
+
+    def conf_gui(self, color: str, n: int = 1):
+        self.__conf_font(n)
+
+        btn_font = make_font(size=self.btn_font_size, weight="bold")
+        height = 0.02
+        for btn, text in zip(self.btn, ["Creat", "Delete", "Search", "Update", "Logout"]):
+            btn['font'] = btn_font
+            btn['text'] = text
+            btn['bg'] = color
+            btn.place(relx=0.02, rely=height, relwidth=0.96, relheight=0.1)
+            height += 0.1 + 0.02
+
+    def get_menu_frame(self) -> Tuple[str, tk.Frame]:
+        return "Main", self.frame
+
+    def set_disable(self):
+        set_button_disable_from_list(self.btn)
+
+    def reset_disable(self):
+        set_button_disable_from_list(self.btn, flat='normal')

+ 84 - 0
control/admin_program.py

@@ -0,0 +1,84 @@
+import abc
+import tkinter as tk
+
+from tool.type_ import *
+from tool.tk import make_font, set_button_disable_from_list
+
+import admin
+import admin_event as tk_event
+
+
+class AdminProgram(metaclass=abc.ABCMeta):
+    def __init__(self, station: admin.AdminStation, win: Union[tk.Frame, tk.Toplevel, tk.Tk], color: str):
+        self.station = station
+        self.win = win
+        self.color = color
+
+    @abc.abstractmethod
+    def set_disable(self):
+        ...
+
+    @abc.abstractmethod
+    def reset_disable(self):
+        ...
+
+    @abc.abstractmethod
+    def conf_gui(self, n: int = 1):
+        ...
+
+    @abc.abstractmethod
+    def get_program_frame(self) -> tk.Frame:
+        ...
+
+
+class WelcomeProgram(AdminProgram):
+    def __init__(self, station, win, color):
+        super(WelcomeProgram, self).__init__(station, win, color)
+        self.frame = tk.Frame(self.win)
+        self.frame['bg'] = color
+
+        self.title = tk.Label(self.frame)
+        self.btn: List[tk.Button] = [tk.Button(self.frame) for _ in range(2)]
+        self.__conf_font()
+
+    def __conf_font(self, n: int = 1):
+        self.title_font_size = int(25 * n)
+        self.btn_font_size = int(14 * n)
+
+    def conf_gui(self, n: int = 1):
+        self.__conf_font(n)
+
+        title_font = make_font(size=self.title_font_size, weight="bold")
+        btn_font = make_font(size=self.btn_font_size)
+
+        for btn, text in zip(self.btn, ["TestMSG", "TestProgress"]):
+            btn['font'] = btn_font
+            btn['text'] = text
+            btn['bg'] = '#d3d7d4'
+
+        self.title['text'] = 'Welcome to HGSSystem Manager'
+        self.title['font'] = title_font
+        self.title['bg'] = self.color
+
+        self.btn[0]['command'] = lambda: self.test_msg()
+        self.btn[1]['command'] = lambda: self.test_progress()
+
+        self.title.place(relx=0.1, rely=0.3, relwidth=0.8, relheight=0.2)
+        self.btn[0].place(relx=0.2, rely=0.7, relwidth=0.2, relheight=0.1)
+        self.btn[1].place(relx=0.6, rely=0.7, relwidth=0.2, relheight=0.1)
+
+    def test_progress(self):
+        event = tk_event.TestProgressEvent(self.station)
+        self.station.push_event(event)
+
+    def test_msg(self):
+        self.station.show_msg("Test Msg", "test msg")
+
+    def get_program_frame(self) -> Tuple[str, tk.Frame]:
+        return "Welcome", self.frame
+
+    def set_disable(self):
+        set_button_disable_from_list(self.btn)
+
+    def reset_disable(self):
+        set_button_disable_from_list(self.btn, flat='normal')

+ 9 - 170
control/station.py

@@ -20,11 +20,9 @@ from sql.db import DB
 from sql.user import update_user, find_user_by_id
 from sql.garbage import update_garbage
 
-from equipment.scan import HGSCapture, HGSQRCoder, QRCode
-from equipment.scan_user import scan_user
-from equipment.scan_garbage import scan_garbage
+from equipment.scan import HGSCapture, HGSQRCoder
 
-from tk_event import TkEventBase, TkEventMain, TkThreading
+from tk_event import TkEventMain
 
 
 class GarbageStationException(Exception):
@@ -47,22 +45,6 @@ class RankingUserError(GarbageStationException):
     ...
 
 
-class StationEventBase(TkEventBase):
-    def __init__(self, gb_station, title: str = 'unknown'):
-        self.station: GarbageStationBase = gb_station
-        self._db: DB = gb_station.get_db()
-        self._title = title
-
-    def get_title(self) -> str:
-        return self._title
-
-    def is_end(self) -> bool:
-        raise GarbageStationException
-
-    def done_after_event(self):
-        raise GarbageStationException
-
-
 class GarbageStationBase(TkEventMain, metaclass=abc.ABCMeta):
     status_normal = 1
     status_get_garbage_type = 2
@@ -226,7 +208,7 @@ class GarbageStationBase(TkEventMain, metaclass=abc.ABCMeta):
             self.show_warning("Operation Fail", "You should login first and scan the QR code of the trash bag")
             return
 
-        event = ThrowGarbageEvent(self).start(self._garbage, garbage_type)
+        event = tk_event.ThrowGarbageEvent(self).start(self._garbage, garbage_type)
         self.push_event(event)
         self._flat = GarbageStationBase.status_normal
         self._garbage = None
@@ -237,7 +219,7 @@ class GarbageStationBase(TkEventMain, metaclass=abc.ABCMeta):
             self.show_warning("Operation Fail", "You should login first and scan the QR code of the trash bag")
             return
 
-        event = CheckGarbageEvent(self).start(self._garbage, check)
+        event = tk_event.CheckGarbageEvent(self).start(self._garbage, check)
         self.push_event(event)
         self._flat = GarbageStationBase.status_normal
         self._garbage = None
@@ -386,11 +368,11 @@ The function has not yet been implemented.
         if qr_code is None:
             return GarbageStationBase.scan_no_to_done, None
 
-        user_scan_event = ScanUserEvent(self).start(qr_code)
+        user_scan_event = tk_event.ScanUserEvent(self).start(qr_code)
         self.push_event(user_scan_event)
 
     def get_show_rank(self):
-        event = RankingEvent(self)
+        event = tk_event.RankingEvent(self)
         self.push_event(event)
 
     @abc.abstractmethod
@@ -444,6 +426,9 @@ The function has not yet been implemented.
         ...
 
 
+import station_event as tk_event
+
+
 class GarbageStation(GarbageStationBase):
     def set_after_run(self, ms, func, *args):  # super.__init__可能会调用
         self.init_after_run_list.append((ms, func, args))
@@ -1162,152 +1147,6 @@ class GarbageStation(GarbageStationBase):
         self._window.destroy()
 
 
-class ScanUserEvent(StationEventBase):
-    @staticmethod
-    def func(qr: QRCode, db: DB):
-        return scan_user(qr, db)
-
-    def __init__(self, gb_station: GarbageStationBase):
-        super().__init__(gb_station, "Scan User")
-
-        self._user: User = gb_station.get_user()
-        self._qr_code: Optional[QRCode] = None
-        self.thread = None
-
-    def start(self, qr_code: QRCode):
-        self._qr_code = qr_code
-        self.thread = TkThreading(self.func, qr_code, self._db)
-        return self
-
-    def is_end(self) -> bool:
-        return self.thread is not None and not self.thread.is_alive()
-
-    def done_after_event(self):
-        self.thread.join()
-        if self.thread.result is not None:
-            self.station.switch_user(self.thread.result)
-            self.station.update_control()
-        else:
-            event = ScanGarbageEvent(self.station).start(self._qr_code)
-            self.station.push_event(event)
-
-
-class ScanGarbageEvent(StationEventBase):
-    @staticmethod
-    def func(qr: QRCode, db: DB):
-        return scan_garbage(qr, db)
-
-    def __init__(self, gb_station: GarbageStationBase):
-        super().__init__(gb_station, "Scan Garbage")
-
-        self._user: User = gb_station.get_user()
-        self._qr_code: Optional[QRCode] = None
-        self.thread = None
-
-    def start(self, qr_code: QRCode):
-        self._qr_code = qr_code
-        self.thread = TkThreading(self.func, qr_code, self._db)
-        return self
-
-    def is_end(self) -> bool:
-        return self.thread is not None and not self.thread.is_alive()
-
-    def done_after_event(self):
-        self.thread.join()
-        if self.thread.result is not None:
-            if self._user is None:
-                self.station.show_warning("Operation Fail", "The garbage bags have been used.")
-            elif self._user.is_manager():
-                self.station.to_get_garbage_check(self.thread.result)
-                self.station.show_garbage_info()  # 显示信息
-                self.station.update_control()
-            else:
-                self.station.to_get_garbage_type(self.thread.result)
-                self.station.hide_msg_rank()  # 如果有msg也马上隐藏
-                self.station.update_control()
-
-
-class RankingEvent(StationEventBase):
-    @staticmethod
-    def func(db: DB):
-        cur = db.search((f"SELECT uid, name, score, reputation "
-                         f"FROM user "
-                         f"WHERE manager = 0 "
-                         f"ORDER BY reputation DESC, score DESC "
-                         f"LIMIT 20;"))
-        if cur is None:
-            return []
-        return list(cur.fetchall())
-
-    def __init__(self, gb_station: GarbageStationBase):
-        super().__init__(gb_station, "Ranking")
-        self.thread = TkThreading(self.func, self._db)
-
-    def is_end(self) -> bool:
-        return not self.thread.is_alive()
-
-    def done_after_event(self):
-        self.thread.join()
-        if self.thread.result is not None:
-            self.station.thread_show_rank(self.thread.result)
-
-
-class ThrowGarbageEvent(StationEventBase):
-    def func(self, garbage: GarbageBag, garbage_type: enum):
-        try:
-            self.station.throw_garbage_core(garbage, garbage_type)
-        except (ThrowGarbageError, UserNotSupportError, ControlNotLogin):
-            self.station.show_warning("Operation Fail", "The garbage bags have been used.")
-            return False
-        else:
-            return True
-
-    def __init__(self, gb_station: GarbageStationBase):
-        super().__init__(gb_station, "ThrowGarbage")
-
-        self.thread = None
-
-    def start(self, garbage: GarbageBag, garbage_type: enum):
-        self.thread = TkThreading(self.func, garbage, garbage_type)
-        self.thread.start()
-        return self
-
-    def is_end(self) -> bool:
-        return not self.thread.is_alive()
-
-    def done_after_event(self):
-        self.thread.join()
-        if not self.thread.result:
-            self.station.show_warning("Operation Fail", "The garbage bag throw error")
-
-
-class CheckGarbageEvent(StationEventBase):
-    def func(self, garbage: GarbageBag, check: bool):
-        try:
-            self.station.check_garbage_core(garbage, check)
-        except (ThrowGarbageError, UserNotSupportError, ControlNotLogin, CheckGarbageError):
-            self.station.show_warning("Operation Fail", "The garbage bag has been checked")
-            return False
-        else:
-            return True
-
-    def __init__(self, gb_station: GarbageStationBase):
-        super().__init__(gb_station, "CheckGarbage")
-        self.thread = None
-
-    def start(self, garbage: GarbageBag, garbage_check: bool):
-        self.thread = TkThreading(self.func, garbage, garbage_check)
-        return self
-
-    def is_end(self) -> bool:
-        return not self.thread.is_alive()
-
-    def done_after_event(self):
-        self.thread.join()
-        if not self.thread.result:
-            self.station.show_warning("Operation Fail", "The garbage bag check error")
-
-
 if __name__ == '__main__':
     mysql_db = DB()
     capture = HGSCapture()

+ 177 - 0
control/station_event.py

@@ -0,0 +1,177 @@
+from equipment.scan import QRCode
+from equipment.scan_user import scan_user
+from equipment.scan_garbage import scan_garbage
+
+from tool.type_ import *
+
+from core.user import User, UserNotSupportError
+from core.garbage import GarbageBag
+
+from sql.db import DB
+
+from tk_event import TkThreading, TkEventBase
+import station as tk_station
+
+
+class StationEventBase(TkEventBase):
+    def __init__(self, station: tk_station.GarbageStationBase, title: str = 'unknown'):
+        super(StationEventBase, self).__init__()
+        self.station: tk_station.GarbageStationBase = station
+        self._db: DB = station.get_db()
+        self._title = title
+
+    def get_title(self) -> str:
+        return self._title
+
+    def is_end(self) -> bool:
+        raise tk_station.GarbageStationException
+
+    def done_after_event(self):
+        raise tk_station.GarbageStationException
+
+
+class ScanUserEvent(StationEventBase):
+    @staticmethod
+    def func(qr: QRCode, db: DB):
+        return scan_user(qr, db)
+
+    def __init__(self, gb_station):
+        super().__init__(gb_station, "Scan User")
+
+        self._user: User = gb_station.get_user()
+        self._qr_code: Optional[QRCode] = None
+        self.thread = None
+
+    def start(self, qr_code: QRCode):
+        self._qr_code = qr_code
+        self.thread = TkThreading(self.func, qr_code, self._db)
+        return self
+
+    def is_end(self) -> bool:
+        return self.thread is not None and not self.thread.is_alive()
+
+    def done_after_event(self):
+        self.thread.join()
+        if self.thread.result is not None:
+            self.station.switch_user(self.thread.result)
+            self.station.update_control()
+        else:
+            event = ScanGarbageEvent(self.station).start(self._qr_code)
+            self.station.push_event(event)
+
+
+class ScanGarbageEvent(StationEventBase):
+    @staticmethod
+    def func(qr: QRCode, db: DB):
+        return scan_garbage(qr, db)
+
+    def __init__(self, gb_station):
+        super().__init__(gb_station, "Scan Garbage")
+
+        self._user: User = gb_station.get_user()
+        self._qr_code: Optional[QRCode] = None
+        self.thread = None
+
+    def start(self, qr_code: QRCode):
+        self._qr_code = qr_code
+        self.thread = TkThreading(self.func, qr_code, self._db)
+        return self
+
+    def is_end(self) -> bool:
+        return self.thread is not None and not self.thread.is_alive()
+
+    def done_after_event(self):
+        self.thread.join()
+        if self.thread.result is not None:
+            if self._user is None:
+                self.station.show_warning("Operation Fail", "The garbage bags have been used.")
+            elif self._user.is_manager():
+                self.station.to_get_garbage_check(self.thread.result)
+                self.station.show_garbage_info()  # 显示信息
+                self.station.update_control()
+            else:
+                self.station.to_get_garbage_type(self.thread.result)
+                self.station.hide_msg_rank()  # 如果有msg也马上隐藏
+                self.station.update_control()
+
+
+class RankingEvent(StationEventBase):
+    @staticmethod
+    def func(db: DB):
+        cur = db.search((f"SELECT uid, name, score, reputation "
+                         f"FROM user "
+                         f"WHERE manager = 0 "
+                         f"ORDER BY reputation DESC, score DESC "
+                         f"LIMIT 20;"))
+        if cur is None:
+            return []
+        return list(cur.fetchall())
+
+    def __init__(self, gb_station):
+        super().__init__(gb_station, "Ranking")
+        self.thread = TkThreading(self.func, self._db)
+
+    def is_end(self) -> bool:
+        return not self.thread.is_alive()
+
+    def done_after_event(self):
+        self.thread.join()
+        if self.thread.result is not None:
+            self.station.thread_show_rank(self.thread.result)
+
+
+class ThrowGarbageEvent(StationEventBase):
+    def func(self, garbage: GarbageBag, garbage_type: enum):
+        try:
+            self.station.throw_garbage_core(garbage, garbage_type)
+        except (tk_station.ThrowGarbageError, UserNotSupportError, tk_station.ControlNotLogin):
+            self.station.show_warning("Operation Fail", "The garbage bags have been used.")
+            return False
+        else:
+            return True
+
+    def __init__(self, gb_station):
+        super().__init__(gb_station, "ThrowGarbage")
+
+        self.thread = None
+
+    def start(self, garbage: GarbageBag, garbage_type: enum):
+        self.thread = TkThreading(self.func, garbage, garbage_type)
+        self.thread.start()
+        return self
+
+    def is_end(self) -> bool:
+        return not self.thread.is_alive()
+
+    def done_after_event(self):
+        self.thread.join()
+        if not self.thread.result:
+            self.station.show_warning("Operation Fail", "The garbage bag throw error")
+
+
+class CheckGarbageEvent(StationEventBase):
+    def func(self, garbage: GarbageBag, check: bool):
+        try:
+            self.station.check_garbage_core(garbage, check)
+        except (tk_station.ThrowGarbageError, UserNotSupportError,
+                tk_station.ControlNotLogin, tk_station.CheckGarbageError):
+            self.station.show_warning("Operation Fail", "The garbage bag has been checked")
+            return False
+        else:
+            return True
+
+    def __init__(self, gb_station):
+        super().__init__(gb_station, "CheckGarbage")
+        self.thread = None
+
+    def start(self, garbage: GarbageBag, garbage_check: bool):
+        self.thread = TkThreading(self.func, garbage, garbage_check)
+        return self
+
+    def is_end(self) -> bool:
+        return not self.thread.is_alive()
+
+    def done_after_event(self):
+        self.thread.join()
+        if not self.thread.result:
+            self.station.show_warning("Operation Fail", "The garbage bag check error")

+ 7 - 0
control/tk_event.py

@@ -6,7 +6,14 @@ import conf
 from tool.type_ import *
 
 
+class TkEventException(Exception):
+    ...
+
+
 class TkEventBase(metaclass=abc.ABCMeta):
+    def __init__(self):
+        self.thread: Optional[TkThreading] = None
+
     @abc.abstractmethod
     def is_end(self) -> bool:
         ...