提交记录修改Core/Servicers/Instances/DateTimeObserver.cs+22 −12看文件
修改Core/Servicers/Instances/Main.cs+30 −11看文件
修改Core/Servicers/Instances/Sleepdiscover.cs+49 −16看文件
修改Core/Servicers/Interfaces/IDateTimeObserver.cs+4 −0看文件
修改Core/Servicers/Interfaces/IMain.cs+9 −1看文件
| @@ -1,53 +1,63 @@ | ||
| 1 | 1 | using Core.Event; |
| 2 | 新增:using Core.Librarys; | |
| 2 | 3 | using Core.Servicers.Interfaces; |
| 3 | 4 | using System; |
| 4 | 5 | using System.Collections.Generic; |
| 5 | 6 | using System.Diagnostics; |
| 6 | 7 | using System.Linq; |
| 7 | 8 | using System.Text; |
| 9 | 新增:using System.Threading; | |
| 8 | 10 | using System.Threading.Tasks; |
| 9 | 删除:using System.Windows.Threading; | |
| 11 | 新增:using System.Timers; | |
| 12 | 新增:using Timer = System.Timers.Timer; | |
| 10 | 13 | |
| 11 | 14 | namespace Core.Servicers.Instances |
| 12 | 15 | { |
| 13 | 16 | public class DateTimeObserver : IDateTimeObserver |
| 14 | 17 | { |
| 15 | 18 | public event DateTimeObserverEventHandler OnDateTimeChanging; |
| 16 | 删除: private DispatcherTimer timer; | |
| 19 | 新增: private Timer timer; | |
| 17 | 20 | |
| 18 | 21 | public void Start() |
| 19 | 22 | { |
| 23 | 新增: Stop(); | |
| 20 | 24 | SetTimer(); |
| 21 | 25 | } |
| 22 | 26 | |
| 23 | 27 | private void SetTimer() |
| 24 | 28 | { |
| 25 | 删除: timer = new DispatcherTimer(); | |
| 26 | 删除: timer.Tick += Timer_Tick; | |
| 29 | 新增: timer = new Timer(); | |
| 30 | 新增: timer.Elapsed += Timer_Tick; | |
| 27 | 31 | DateTime now = DateTime.Now; |
| 28 | 32 | DateTime updateTime = new DateTime(now.Year, now.Month, now.Day, now.Hour, 59, 59); |
| 29 | 删除: int diffMilliseconds = (int)updateTime.Subtract(now).TotalMilliseconds; | |
| 33 | 新增: double diffMilliseconds = updateTime.Subtract(now).TotalMilliseconds; | |
| 30 | 34 | if (diffMilliseconds < 0) |
| 31 | 35 | { |
| 32 | 36 | diffMilliseconds = 1; |
| 33 | 37 | } |
| 34 | 删除: timer.Interval = new TimeSpan(0, 0, 0, 0, diffMilliseconds); | |
| 38 | 新增: timer.Interval = diffMilliseconds; | |
| 35 | 39 | timer.Start(); |
| 40 | 新增: Logger.Info("datetime observer start,interval(s):" + updateTime.Subtract(now).TotalSeconds); | |
| 36 | 41 | } |
| 37 | 42 | |
| 38 | 删除: private async void Timer_Tick(object sender, EventArgs e) | |
| 43 | 新增: private void Timer_Tick(object sender, EventArgs e) | |
| 39 | 44 | { |
| 40 | 45 | timer.Stop(); |
| 41 | 46 | |
| 42 | 47 | DateTime nowTime = DateTime.Now; |
| 43 | 删除: | |
| 44 | 48 | OnDateTimeChanging?.Invoke(this, nowTime); |
| 45 | 49 | |
| 46 | 删除: await Task.Run(async () => | |
| 50 | 新增: Thread.Sleep(2000); | |
| 51 | 新增: SetTimer(); | |
| 52 | 新增: } | |
| 53 | 新增: | |
| 54 | 新增: public void Stop() | |
| 55 | 新增: { | |
| 56 | 新增: if (timer != null && timer.Enabled) | |
| 47 | 57 | { |
| 48 | 删除: await Task.Delay(1000); | |
| 49 | 删除: SetTimer(); | |
| 50 | 删除: }); | |
| 58 | 新增: timer.Stop(); | |
| 59 | 新增: timer.Dispose(); | |
| 60 | 新增: } | |
| 51 | 61 | } |
| 52 | 62 | } |
| 53 | 63 | } |
| @@ -38,6 +38,7 @@ namespace Core.Servicers.Instances | ||
| 38 | 38 | "OpenWith", |
| 39 | 39 | "Updater", |
| 40 | 40 | "LockApp", |
| 41 | 新增: "dwm", | |
| 41 | 42 | }; |
| 42 | 43 | |
| 43 | 44 | /// <summary> |
| @@ -155,18 +156,27 @@ namespace Core.Servicers.Instances | ||
| 155 | 156 | config = appConfig.GetConfig(); |
| 156 | 157 | UpdateConfigIgnoreProcess(); |
| 157 | 158 | |
| 158 | 删除: // 日期变化观察 | |
| 159 | 删除: dateTimeObserver.Start(); | |
| 160 | 159 | |
| 161 | 删除: // 启动进程观察 | |
| 162 | 删除: observer.Start(); | |
| 163 | 160 | |
| 164 | 161 | // 启动睡眠监测 |
| 165 | 162 | sleepdiscover.Start(); |
| 163 | 新增: Start(); | |
| 166 | 164 | |
| 167 | 165 | OnStarted?.Invoke(this, EventArgs.Empty); |
| 168 | 166 | } |
| 167 | 新增: public void Start() | |
| 168 | 新增: { | |
| 169 | 新增: activeStartTime = DateTime.Now; | |
| 170 | 新增: activeProcess = null; | |
| 169 | 171 | |
| 172 | 新增: dateTimeObserver.Start(); | |
| 173 | 新增: observer.Start(); | |
| 174 | 新增: } | |
| 175 | 新增: public void Stop() | |
| 176 | 新增: { | |
| 177 | 新增: dateTimeObserver.Stop(); | |
| 178 | 新增: observer.Stop(); | |
| 179 | 新增: } | |
| 170 | 180 | public void Exit() |
| 171 | 181 | { |
| 172 | 182 | observer?.Stop(); |
| @@ -199,13 +209,20 @@ namespace Core.Servicers.Instances | ||
| 199 | 209 | Logger.Info($"[{sleepStatus}] process:{activeProcess},start:{activeStartTime}"); |
| 200 | 210 | if (sleepStatus == SleepStatus.Sleep) |
| 201 | 211 | { |
| 212 | 新增: // 进入睡眠状态 | |
| 213 | 新增: Debug.WriteLine("进入睡眠状态"); | |
| 214 | 新增: | |
| 215 | 新增: // 停止服务 | |
| 216 | 新增: Stop(); | |
| 217 | 新增: | |
| 202 | 218 | // 更新时间 |
| 203 | 219 | UpdateTime(); |
| 204 | 220 | } |
| 205 | 删除: else if (sleepStatus == SleepStatus.Wake) | |
| 221 | 新增: else | |
| 206 | 222 | { |
| 207 | 删除: // 唤醒时重置计时开始时间 | |
| 208 | 删除: activeStartTime = DateTime.Now; | |
| 223 | 新增: // 从睡眠状态唤醒 | |
| 224 | 新增: Debug.WriteLine("从睡眠状态唤醒"); | |
| 225 | 新增: Start(); | |
| 209 | 226 | } |
| 210 | 227 | } |
| 211 | 228 | |
| @@ -339,7 +356,9 @@ namespace Core.Servicers.Instances | ||
| 339 | 356 | |
| 340 | 357 | private void UpdateTime() |
| 341 | 358 | { |
| 342 | 删除: if (!string.IsNullOrEmpty(activeProcess)) | |
| 359 | 新增: string app = activeProcess != null ? activeProcess.ToString() : ""; | |
| 360 | 新增: | |
| 361 | 新增: if (!string.IsNullOrEmpty(app)) | |
| 343 | 362 | { |
| 344 | 363 | var time = activeStartTime; |
| 345 | 364 | |
| @@ -358,12 +377,12 @@ namespace Core.Servicers.Instances | ||
| 358 | 377 | { |
| 359 | 378 | Task.Run(() => |
| 360 | 379 | { |
| 361 | 删除: data.Set(activeProcess, seconds, time); | |
| 380 | 新增: data.Set(app, seconds, time); | |
| 362 | 381 | |
| 363 | 382 | // 关联进程更新 |
| 364 | 删除: HandleLinks(activeProcess, seconds, time); | |
| 383 | 新增: HandleLinks(app, seconds, time); | |
| 365 | 384 | |
| 366 | 删除: Logger.Info("status:" + sleepStatus + ",process:" + activeProcess + ",seconds:" + seconds + ",start:" + activeStartTime.ToString() + ",end:" + DateTime.Now.ToString() + ",time:" + time.ToString()); | |
| 385 | 新增: Logger.Info("status:" + sleepStatus + ",process:" + app + ",seconds:" + seconds + ",start:" + activeStartTime.ToString() + ",end:" + DateTime.Now.ToString() + ",time:" + time.ToString()); | |
| 367 | 386 | }); |
| 368 | 387 | } |
| 369 | 388 | |
| @@ -60,9 +60,9 @@ namespace Core.Servicers.Instances | ||
| 60 | 60 | { |
| 61 | 61 | timer = new DispatcherTimer(); |
| 62 | 62 | timer.Interval = new TimeSpan(0, 5, 0); |
| 63 | 删除: //#if DEBUG | |
| 64 | 删除: // timer.Interval = new TimeSpan(0, 0, 10); | |
| 65 | 删除: //#endif | |
| 63 | 新增:#if DEBUG | |
| 64 | 新增: //timer.Interval = new TimeSpan(0, 0, 10); | |
| 65 | 新增:#endif | |
| 66 | 66 | timer.Tick += Timer_Tick; |
| 67 | 67 | timer.Start(); |
| 68 | 68 | |
| @@ -158,7 +158,6 @@ namespace Core.Servicers.Instances | ||
| 158 | 158 | /// <returns>睡眠返回true</returns> |
| 159 | 159 | private async Task<bool> IsSleepAsync() |
| 160 | 160 | { |
| 161 | 删除: // 1.先判断鼠标是否有过移动 | |
| 162 | 161 | Point point = Win32API.GetCursorPosition(); |
| 163 | 162 | |
| 164 | 163 | if (point.X + point.Y == 0) |
| @@ -172,21 +171,13 @@ namespace Core.Servicers.Instances | ||
| 172 | 171 | } |
| 173 | 172 | } |
| 174 | 173 | |
| 175 | 删除: if (lastPoint.ToString() != point.ToString()) | |
| 174 | 新增: bool isActive = await IsActiveAsync(); | |
| 175 | 新增: if (isActive) | |
| 176 | 176 | { |
| 177 | 删除: // 鼠标在移动,更新坐标 | |
| 178 | 删除: lastPoint = point; | |
| 179 | 177 | return false; |
| 180 | 178 | } |
| 181 | 179 | |
| 182 | 删除: // 2.判断是否键盘超时 | |
| 183 | 删除: if (!IsKeyboardOuttime()) | |
| 184 | 删除: { | |
| 185 | 删除: // 十分钟内有键盘操作 | |
| 186 | 删除: return false; | |
| 187 | 删除: } | |
| 188 | 删除: | |
| 189 | 删除: // 3.鼠标和键盘都没有操作时判断是否在播放声音 | |
| 180 | 新增: // 鼠标和键盘都没有操作时判断是否在播放声音 | |
| 190 | 181 | |
| 191 | 182 | // 持续30秒检测当前是否在播放声音 |
| 192 | 183 | bool isPlaySound = await IsPlaySoundAsync(); |
| @@ -279,6 +270,10 @@ namespace Core.Servicers.Instances | ||
| 279 | 270 | |
| 280 | 271 | } |
| 281 | 272 | |
| 273 | 新增: /// <summary> | |
| 274 | 新增: /// 指示当前是否在播放声音(持续30秒检测) | |
| 275 | 新增: /// </summary> | |
| 276 | 新增: /// <returns>播放返回true</returns> | |
| 282 | 277 | private async Task<bool> IsPlaySoundAsync() |
| 283 | 278 | { |
| 284 | 279 | bool result = false; |
| @@ -307,6 +302,42 @@ namespace Core.Servicers.Instances | ||
| 307 | 302 | return result; |
| 308 | 303 | } |
| 309 | 304 | |
| 305 | 新增: /// <summary> | |
| 306 | 新增: /// 指示当前是否处于活跃状态(持续30秒检测) | |
| 307 | 新增: /// </summary> | |
| 308 | 新增: /// <returns>活跃返回true</returns> | |
| 309 | 新增: private async Task<bool> IsActiveAsync() | |
| 310 | 新增: { | |
| 311 | 新增: bool result = false; | |
| 312 | 新增: | |
| 313 | 新增: await Task.Run(() => | |
| 314 | 新增: { | |
| 315 | 新增: // 持续30秒 | |
| 316 | 新增: int time = 30; | |
| 317 | 新增: | |
| 318 | 新增: while (time > 0) | |
| 319 | 新增: { | |
| 320 | 新增: Point point = Win32API.GetCursorPosition(); | |
| 321 | 新增: bool isMouseMove = lastPoint.ToString() != point.ToString(); | |
| 322 | 新增: bool isKeyboardActive = !IsKeyboardOuttime(); | |
| 323 | 新增: | |
| 324 | 新增: if (isMouseMove || isKeyboardActive) | |
| 325 | 新增: { | |
| 326 | 新增: lastPoint = Win32API.GetCursorPosition(); | |
| 327 | 新增: result = true; | |
| 328 | 新增: break; | |
| 329 | 新增: } | |
| 330 | 新增: else | |
| 331 | 新增: { | |
| 332 | 新增: time--; | |
| 333 | 新增: Debug.WriteLine(time); | |
| 334 | 新增: Thread.Sleep(1000); | |
| 335 | 新增: } | |
| 336 | 新增: } | |
| 337 | 新增: }); | |
| 338 | 新增: return result; | |
| 339 | 新增: } | |
| 340 | 新增: | |
| 310 | 341 | /// <summary> |
| 311 | 342 | /// 指示是否键盘行为超时(超过10分钟) |
| 312 | 343 | /// </summary> |
| @@ -314,7 +345,9 @@ namespace Core.Servicers.Instances | ||
| 314 | 345 | private bool IsKeyboardOuttime() |
| 315 | 346 | { |
| 316 | 347 | TimeSpan timeSpan = DateTime.Now - pressKeyboardLastTime; |
| 317 | 删除: | |
| 348 | 新增:#if DEBUG | |
| 349 | 新增: //return timeSpan.TotalSeconds >= 10; | |
| 350 | 新增:#endif | |
| 318 | 351 | return timeSpan.TotalMinutes >= 10; |
| 319 | 352 | } |
| 320 | 353 | } |
| @@ -17,6 +17,10 @@ namespace Core.Servicers.Interfaces | ||
| 17 | 17 | /// </summary> |
| 18 | 18 | void Start(); |
| 19 | 19 | /// <summary> |
| 20 | 新增: /// 停止 | |
| 21 | 新增: /// </summary> | |
| 22 | 新增: void Stop(); | |
| 23 | 新增: /// <summary> | |
| 20 | 24 | /// 日期即将变化时发生(前1秒) |
| 21 | 25 | /// </summary> |
| 22 | 26 | event DateTimeObserverEventHandler OnDateTimeChanging; |
| @@ -12,13 +12,21 @@ namespace Core.Servicers.Interfaces | ||
| 12 | 12 | public interface IMain |
| 13 | 13 | { |
| 14 | 14 | /// <summary> |
| 15 | 删除: /// 启动服务 | |
| 15 | 新增: /// 运行服务(仅首次启动调用) | |
| 16 | 16 | /// </summary> |
| 17 | 17 | void Run(); |
| 18 | 18 | /// <summary> |
| 19 | 19 | /// 退出服务 |
| 20 | 20 | /// </summary> |
| 21 | 21 | void Exit(); |
| 22 | 新增: /// <summary> | |
| 23 | 新增: /// 启动服务 | |
| 24 | 新增: /// </summary> | |
| 25 | 新增: void Start(); | |
| 26 | 新增: /// <summary> | |
| 27 | 新增: /// 停止服务 | |
| 28 | 新增: /// </summary> | |
| 29 | 新增: void Stop(); | |
| 22 | 30 | |
| 23 | 31 | /// <summary> |
| 24 | 32 | /// 更新某个进程时间时发生 |

