1010try :
1111 # 导入 Python.NET
1212 from pythonnet import load
13+
1314 load ("coreclr" , runtime_config = get_data_path ("dlls" , "dotnet.runtimeconfig.json" ))
1415
1516 # 加载 .NET CoreCLR 程序集
1617 import clr
18+
1719 clr .AddReference ("ClassIsland.Shared.IPC" )
1820 clr .AddReference ("SecRandom4Ci.Interface" )
1921
3234
3335
3436if CSHARP_AVAILABLE :
37+
3538 class CSharpIPCHandler :
3639 """C# dotnetCampus.Ipc 处理器,用于连接 ClassIsland 实例"""
40+
3741 _instance : Optional ["CSharpIPCHandler" ] = None
3842
3943 def __new__ (cls ):
@@ -56,7 +60,7 @@ def __init__(self):
5660 self .ipc_client : Optional [IpcClient ] = None
5761 self .client_thread : Optional [threading .Thread ] = None
5862 self .is_running = False
59-
63+
6064 def start_ipc_client (self ) -> bool :
6165 """
6266 启动 C# IPC 客户端
@@ -68,7 +72,9 @@ def start_ipc_client(self) -> bool:
6872 return True
6973
7074 try :
71- self .client_thread = threading .Thread (target = self ._run_client , daemon = False )
75+ self .client_thread = threading .Thread (
76+ target = self ._run_client , daemon = False
77+ )
7278 self .client_thread .start ()
7379 self .is_running = True
7480 return True
@@ -81,39 +87,52 @@ def stop_ipc_client(self):
8187 self .is_running = False
8288 if self .client_thread and self .client_thread .is_alive ():
8389 self .client_thread .join (timeout = 1 )
84-
90+
8591 def send_notification (
86- self ,
87- class_name ,
88- selected_students ,
89- draw_count = 1 ,
90- settings = None ,
91- settings_group = None
92- ) -> bool :
92+ self ,
93+ class_name ,
94+ selected_students ,
95+ draw_count = 1 ,
96+ settings = None ,
97+ settings_group = None ,
98+ ) -> bool :
9399 """发送提醒"""
94100
95101 if settings :
96102 display_duration = settings .get ("notification_display_duration" , 5 )
97103 else :
98104 display_duration = 5
99-
105+
100106 randomService = GeneratedIpcFactory .CreateIpcProxy [ISecRandomService ](
101- self .ipc_client .Provider , self .ipc_client .PeerProxy )
102- result = self .convert_to_call_result (class_name , selected_students , draw_count , display_duration )
107+ self .ipc_client .Provider , self .ipc_client .PeerProxy
108+ )
109+ result = self .convert_to_call_result (
110+ class_name , selected_students , draw_count , display_duration
111+ )
103112 randomService .NotifyResult (result )
104113
105114 return True
106115
107116 def is_breaking (self ) -> bool :
108117 """是否处于下课时间"""
109118 lessonSc = GeneratedIpcFactory .CreateIpcProxy [IPublicLessonsService ](
110- self .ipc_client .Provider , self .ipc_client .PeerProxy )
111- state = lessonSc .CurrentState in [getattr (TimeState , "None" ), TimeState .PrepareOnClass , TimeState .Breaking , TimeState .AfterSchool ]
112- logger .debug (f"获取到的 ClassIsland 时间状态: { lessonSc .CurrentState } 是否下课: { state } " )
119+ self .ipc_client .Provider , self .ipc_client .PeerProxy
120+ )
121+ state = lessonSc .CurrentState in [
122+ getattr (TimeState , "None" ),
123+ TimeState .PrepareOnClass ,
124+ TimeState .Breaking ,
125+ TimeState .AfterSchool ,
126+ ]
127+ logger .debug (
128+ f"获取到的 ClassIsland 时间状态: { lessonSc .CurrentState } 是否下课: { state } "
129+ )
113130 return state
114131
115132 @staticmethod
116- def convert_to_call_result (class_name : str , selected_students , draw_count : int , display_duration = 5.0 ) -> CallResult :
133+ def convert_to_call_result (
134+ class_name : str , selected_students , draw_count : int , display_duration = 5.0
135+ ) -> CallResult :
117136 result = CallResult ()
118137 result .ClassName = class_name
119138 result .DrawCount = draw_count
@@ -128,8 +147,11 @@ def convert_to_call_result(class_name: str, selected_students, draw_count: int,
128147
129148 def _on_class_test (self ):
130149 lessonSc = GeneratedIpcFactory .CreateIpcProxy [IPublicLessonsService ](
131- self .ipc_client .Provider , self .ipc_client .PeerProxy )
132- logger .debug (f"上课 { lessonSc .CurrentSubject .Name } 时间: { lessonSc .CurrentTimeLayoutItem } " )
150+ self .ipc_client .Provider , self .ipc_client .PeerProxy
151+ )
152+ logger .debug (
153+ f"上课 { lessonSc .CurrentSubject .Name } 时间: { lessonSc .CurrentTimeLayoutItem } "
154+ )
133155
134156 def _run_client (self ):
135157 """运行 C# IPC 客户端"""
@@ -138,24 +160,29 @@ async def client():
138160 """异步客户端"""
139161
140162 self .ipc_client = IpcClient ()
141- self .ipc_client .JsonIpcProvider .AddNotifyHandler (IpcRoutedNotifyIds .OnClassNotifyId , Action (lambda : self ._on_class_test ()))
163+ self .ipc_client .JsonIpcProvider .AddNotifyHandler (
164+ IpcRoutedNotifyIds .OnClassNotifyId ,
165+ Action (lambda : self ._on_class_test ()),
166+ )
142167
143168 task = self .ipc_client .Connect ()
144169 await loop .run_in_executor (None , lambda : task .Wait ())
145-
170+
146171 while self .is_running :
147172 await asyncio .sleep (1 )
148-
173+
149174 self .ipc_client = None
150-
175+
151176 # 启动新的 asyncio 事件循环
152177 loop = asyncio .new_event_loop ()
153178 asyncio .set_event_loop (loop )
154179 loop .run_until_complete (client ())
155180 loop .close ()
156181else :
182+
157183 class CSharpIPCHandler :
158184 """C# dotnetCampus.Ipc 处理器,用于连接 ClassIsland 实例"""
185+
159186 _instance : Optional ["CSharpIPCHandler" ] = None
160187
161188 def __new__ (cls ):
@@ -170,15 +197,15 @@ def instance(cls):
170197 if cls ._instance is None :
171198 cls ._instance = cls ()
172199 return cls ._instance
173-
200+
174201 def __init__ (self ):
175202 """
176203 初始化 C# IPC 处理器
177204 """
178205 self .ipc_client = None
179206 self .client_thread = None
180207 self .is_running = False
181-
208+
182209 def start_ipc_client (self ) -> bool :
183210 """
184211 启动 C# IPC 客户端
@@ -187,33 +214,35 @@ def start_ipc_client(self) -> bool:
187214 启动成功返回True,失败返回False
188215 """
189216 return False
190-
217+
191218 def stop_ipc_client (self ):
192219 """停止 C# IPC 客户端"""
193220 pass
194-
221+
195222 def send_notification (
196- self ,
197- class_name ,
198- selected_students ,
199- draw_count = 1 ,
200- settings = None ,
201- settings_group = None
202- ) -> bool :
223+ self ,
224+ class_name ,
225+ selected_students ,
226+ draw_count = 1 ,
227+ settings = None ,
228+ settings_group = None ,
229+ ) -> bool :
203230 """发送提醒"""
204231 return False
205232
206233 def is_breaking (self ) -> bool :
207234 """是否处于下课时间"""
208235 return False
209-
236+
210237 @staticmethod
211- def convert_to_call_result (class_name : str , selected_students , draw_count : int , display_duration = 5.0 ) -> object :
238+ def convert_to_call_result (
239+ class_name : str , selected_students , draw_count : int , display_duration = 5.0
240+ ) -> object :
212241 return object
213242
214243 def _on_class_test (self ):
215244 pass
216-
245+
217246 def _run_client (self ):
218247 """运行 C# IPC 客户端"""
219248 pass
0 commit comments