@@ -30,6 +30,17 @@ public class SecRandomNotification : NotificationProviderBase
3030 WriteIndented = true ,
3131 Encoder = System . Text . Encodings . Web . JavaScriptEncoder . UnsafeRelaxedJsonEscaping
3232 } ;
33+ private DateTime _lastStatusMessageTime = DateTime . MinValue ;
34+ private static readonly TimeSpan _breakingMessageInterval = TimeSpan . FromMilliseconds ( 800 ) ; // Breaking状态下0.8秒发送一次状态消息
35+ private static readonly TimeSpan _onClassMessageInterval = TimeSpan . FromSeconds ( 10 ) ; // OnClass状态下10秒发送一次状态消息
36+ private string _lastCurrentSubject = "" ;
37+ private string _lastNextSubject = "" ;
38+ private string _lastCurrentState = "" ;
39+ private bool _lastIsClassPlanEnabled = false ;
40+ private bool _lastIsClassPlanLoaded = false ;
41+ private bool _lastIsLessonConfirmed = false ;
42+ private double _lastOnClassLeftTime = 0 ;
43+ private double _lastOnBreakingTimeLeft = 0 ;
3344
3445 public SecRandomNotification ( ILessonsService lessonsService )
3546 {
@@ -39,8 +50,25 @@ public SecRandomNotification(ILessonsService lessonsService)
3950
4051 private void LessonsServiceOnOnTicked ( object ? sender , EventArgs e )
4152 {
42- // 发送课程状态消息
43- SendClassStatusMessage ( ) ;
53+ // 发送课程状态消息,但根据课程状态使用不同的发送频率
54+ TimeSpan currentInterval ;
55+ string currentState = LessonsService . CurrentState . ToString ( ) ;
56+
57+ // 根据课程状态选择不同的时间间隔
58+ if ( currentState == "Breaking" )
59+ {
60+ currentInterval = _breakingMessageInterval ; // Breaking状态下0.8秒发送一次
61+ }
62+ else
63+ {
64+ currentInterval = _onClassMessageInterval ; // 其他状态下10秒发送一次
65+ }
66+
67+ if ( DateTime . Now - _lastStatusMessageTime >= currentInterval )
68+ {
69+ SendClassStatusMessage ( ) ;
70+ _lastStatusMessageTime = DateTime . Now ;
71+ }
4472
4573 // 接收并处理消息
4674 string result = Path . GetTempPath ( ) ;
@@ -309,10 +337,6 @@ private void SendClassStatusMessage()
309337 {
310338 try
311339 {
312- string tempPath = Path . GetTempPath ( ) ;
313- string messageFile = Path . Combine ( tempPath , "SecRandom_message_received.json" ) ;
314- string unreadFile = Path . Combine ( tempPath , "SecRandom_unread_received" ) ;
315-
316340 // 获取当前课程状态信息
317341 // 课程服务具有以下属性:
318342 string currentSubject = LessonsService . CurrentSubject ? . Name ?? "无" ; // Subject? 当前所处时间点的科目。如果没有加载课表,则为 null。
@@ -324,6 +348,37 @@ private void SendClassStatusMessage()
324348 double onClassLeftTime = LessonsService . OnClassLeftTime . TotalSeconds ; // TimeSpan 距离上课剩余时间(秒)。
325349 double onBreakingTimeLeft = LessonsService . OnBreakingTimeLeftTime . TotalSeconds ; // TimeSpan 距下课剩余时间(秒)。
326350
351+ // 检查课程状态是否发生变化
352+ bool statusChanged =
353+ _lastCurrentSubject != currentSubject ||
354+ _lastNextSubject != nextSubject ||
355+ _lastCurrentState != currentState ||
356+ _lastIsClassPlanEnabled != isClassPlanEnabled ||
357+ _lastIsClassPlanLoaded != isClassPlanLoaded ||
358+ _lastIsLessonConfirmed != isLessonConfirmed ||
359+ Math . Abs ( _lastOnClassLeftTime - onClassLeftTime ) > 1 || // 时间变化超过1秒才认为有变化
360+ Math . Abs ( _lastOnBreakingTimeLeft - onBreakingTimeLeft ) > 1 ; // 时间变化超过1秒才认为有变化
361+
362+ // 如果状态没有变化,则不发送消息
363+ if ( ! statusChanged )
364+ {
365+ return ;
366+ }
367+
368+ // 更新最后的状态值
369+ _lastCurrentSubject = currentSubject ;
370+ _lastNextSubject = nextSubject ;
371+ _lastCurrentState = currentState ;
372+ _lastIsClassPlanEnabled = isClassPlanEnabled ;
373+ _lastIsClassPlanLoaded = isClassPlanLoaded ;
374+ _lastIsLessonConfirmed = isLessonConfirmed ;
375+ _lastOnClassLeftTime = onClassLeftTime ;
376+ _lastOnBreakingTimeLeft = onBreakingTimeLeft ;
377+
378+ string tempPath = Path . GetTempPath ( ) ;
379+ string messageFile = Path . Combine ( tempPath , "SecRandom_message_received.json" ) ;
380+ string unreadFile = Path . Combine ( tempPath , "SecRandom_unread_received" ) ;
381+
327382 // 创建课程状态消息
328383 var classStatus = new
329384 {
0 commit comments