ShiftWatcherService.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using Prism.Events;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using TeamAAS_VP.Core;
  9. using TeamAAS_VP.Events;
  10. using TeamAAS_VP.Models;
  11. namespace TeamAAS_VP.Services
  12. {
  13. public class ShiftWatcherService
  14. {
  15. private readonly IEventAggregator _eventAggregator;
  16. private Timer _timer;
  17. private string _lastShift;
  18. private WorkShift workShift;
  19. public ShiftWatcherService(IEventAggregator eventAggregator)
  20. {
  21. this._eventAggregator = eventAggregator;
  22. }
  23. public void Start()
  24. {
  25. // 中文注释:每秒检测一次
  26. //_timer = new Timer(_ => CheckShift(), null, 0, 1000);
  27. //try
  28. //{
  29. // this.workShift = FileHelper.ReadJsonFile<WorkShift>(FilePath.WorkShiftParamPath);
  30. //}
  31. //catch (Exception)
  32. //{ }
  33. //_eventAggregator.GetEvent<WorkShiftUpdateNotification>().Subscribe((p) =>
  34. //{
  35. // this.workShift = p;
  36. //});
  37. }
  38. private void CheckShift()
  39. {
  40. DateTime dateTimeDayShift = DateTime.Now;
  41. DateTime dateTimeNightShift = DateTime.Now;
  42. try
  43. {
  44. dateTimeDayShift = DateTime.Parse(workShift.DayShift);
  45. dateTimeNightShift = DateTime.Parse(workShift.NightShift);
  46. }
  47. catch
  48. {
  49. }
  50. string currentShift =
  51. (DateTime.Now.Hour >= 8 && DateTime.Now.Hour < 20) ? "Day" : "Night";
  52. if (_lastShift == null)
  53. {
  54. _lastShift = currentShift;
  55. return;
  56. }
  57. if (_lastShift != currentShift)
  58. {
  59. _lastShift = currentShift;
  60. // 只负责“通知”,不要弹窗
  61. _ = App.Current.Dispatcher.BeginInvoke(new Action(() =>
  62. {
  63. _eventAggregator.GetEvent<WorkShiftChangedNotification>().Publish();
  64. }));
  65. }
  66. }
  67. }
  68. }