| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- using Prism.Events;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using TeamAAS_VP.Core;
- using TeamAAS_VP.Events;
- using TeamAAS_VP.Models;
- namespace TeamAAS_VP.Services
- {
- public class ShiftWatcherService
- {
- private readonly IEventAggregator _eventAggregator;
- private Timer _timer;
- private string _lastShift;
- private WorkShift workShift;
- public ShiftWatcherService(IEventAggregator eventAggregator)
- {
- this._eventAggregator = eventAggregator;
- }
- public void Start()
- {
- // 中文注释:每秒检测一次
- //_timer = new Timer(_ => CheckShift(), null, 0, 1000);
- //try
- //{
- // this.workShift = FileHelper.ReadJsonFile<WorkShift>(FilePath.WorkShiftParamPath);
- //}
- //catch (Exception)
- //{ }
- //_eventAggregator.GetEvent<WorkShiftUpdateNotification>().Subscribe((p) =>
- //{
- // this.workShift = p;
- //});
- }
- private void CheckShift()
- {
- DateTime dateTimeDayShift = DateTime.Now;
- DateTime dateTimeNightShift = DateTime.Now;
- if (workShift==null)
- {
- return;
- }
- try
- {
- dateTimeDayShift = DateTime.Parse(workShift.DayShift);
- dateTimeNightShift = DateTime.Parse(workShift.NightShift);
- }
- catch
- {
- }
- string currentShift =
- (DateTime.Now.Hour >= 8 && DateTime.Now.Hour < 20) ? "Day" : "Night";
- if (_lastShift == null)
- {
- _lastShift = currentShift;
- return;
- }
- if (_lastShift != currentShift)
- {
- _lastShift = currentShift;
- // 只负责“通知”,不要弹窗
- _ = App.Current.Dispatcher.BeginInvoke(new Action(() =>
- {
- _eventAggregator.GetEvent<WorkShiftChangedNotification>().Publish();
- }));
- }
- }
- }
- }
|