Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Luna.Shifts.Presenters
- {
- [PerRequest(typeof(ISeatMonitoringPresenter))]
- public class SeatMonitoringPresenter : DockingPresenter, ISeatMonitoringPresenter, IClozeContainer
- {
- private readonly Area _openedArea;
- private readonly AutoResetEvent _autoResetEvent = new AutoResetEvent(false);
- private readonly ManualResetEvent _manualResetEvent = new ManualResetEvent(true);
- private readonly Thread _dataThread;
- private readonly IRealTimeSeatAdherenceModel _realTimeSeatAdherenceModel;
- private readonly IEntityFactory _entityFactory;
- private TimeSpan _refreshInterval = TimeSpan.FromSeconds(10);
- private bool _runningThread = true;
- private IEnumerable<AgentStatusType> _agentStatusTypes;
- private IDictionary<AgentStatusType, AgentStatusAlertTime> _customagentStatusTypes;
- private Action<int> _agentInfoColumnChanged;
- private Action<int> _seatInfoColumnChanged;
- public event Action<DateTime> UpdateEvent;
- //private readonly IShiftDispatcherModel _shiftDispatcherModel;
- private readonly IAgentStatusTypeModel _agentStatusTypeModel;
- //private IEnumerable<TermStyle> _allEventTypes;
- private IEnumerable<IAgentSeatModel> _agentSeats;
- private Dictionary<string, AgentStatusType> _agentStatusTypesDic;
- public SeatMonitoringPresenter(Area area, IEntityFactory entityFactory, IRealTimeSeatAdherenceModel realTimeSeatAdherenceModel, IAgentStatusTypeModel agentStatusTypeModel)
- {
- _entityFactory = entityFactory;
- _realTimeSeatAdherenceModel = realTimeSeatAdherenceModel;
- _agentStatusTypeModel = agentStatusTypeModel;
- _dimension = area.Dimension;
- _openedArea = area;
- _dataThread = new Thread(Fetch);
- DisplayName = string.Format(LanguageReader.GetValue("Adherence_SeatMonitoring_Title"), _openedArea.Name);
- _fullAgenInfoColumnList = (string[])Application.Current.Resources["AgentInfoColumnFullList"];
- _fullSeatInfoColumnList = (string[])Application.Current.Resources["SeatInfoColumnFullList"];
- }
- private bool _refreshing;
- public bool Refreshing
- {
- get { return _refreshing; }
- set
- {
- _refreshing = value;
- NotifyOfPropertyChange(() => Refreshing);
- }
- }
- public void Refresh()
- {
- _realTimeSeatAdherenceModel.ClearCache();
- var thread = new Thread(() =>
- {
- var model = ServiceLocator.Current.GetInstance<IRealTimeSeatAdherenceModel>();
- CurrentTime = ServiceLocator.Current.GetInstance<IBackendModel>().GetUniversialTime();
- Refreshing = true;
- model.SetMonitoringArea(_openedArea, CurrentTime, _agentStatusTypesDic);
- foreach (var agentSeat in AgentSeats)
- {
- model.LoadData(agentSeat);
- agentSeat.UpdateStatus(CurrentTime, GetAgentStatusTimeoutAlert);
- }
- Refreshing = false;
- });
- thread.IsBackground = true;
- thread.Start();
- }
- private void Fetch()
- {
- #region refresh agentStatus process
- //int count = -1;
- var model = _realTimeSeatAdherenceModel;//ServiceLocator.Current.GetInstance<IRealTimeSeatAdherenceModel>();
- var runningWait = new AutoResetEvent(true);
- //_runningWait.Set();
- while (_runningThread)
- {
- //count++;
- runningWait.WaitOne();
- try
- {
- if (AutoRefresh)
- {
- CurrentTime = ServiceLocator.Current.GetInstance<IBackendModel>().GetUniversialTime();
- Refreshing = true;
- }
- model.SetMonitoringArea(_openedArea, CurrentTime, _agentStatusTypesDic);
- Refreshing = false;
- foreach (var agentSeat in AgentSeats)
- {
- model.LoadData(agentSeat);
- agentSeat.UpdateStatus(CurrentTime, GetAgentStatusTimeoutAlert);
- }
- if (UpdateEvent != null)
- UpdateEvent(CurrentTime);
- }
- catch (Exception ex)
- {
- //Log it
- var log = log4net.LogManager.GetLogger("SeatMonitoringLogger");
- if (log.IsErrorEnabled)
- log.Error(ex);
- }
- runningWait.Set();
- if (AutoRefresh)
- {
- Thread.Sleep(_refreshInterval);
- }
- else //手动拉的时候间隔
- {
- Thread.Sleep(500);
- _autoResetEvent.WaitOne();
- }
- _manualResetEvent.WaitOne(); //全局控制线程状态
- }
- Debug.WriteLine("Thread Close");
- model.As<IDisposable>().Dispose();
- #endregion
- }
- protected override void OnInitialize()
- {
- _agentStatusTypes = _agentStatusTypeModel.GetAll();
- _agentStatusTypesDic = _agentStatusTypes.ToDictionary(o => o.StatusCode.Trim());
- _customagentStatusTypes = _agentStatusTypeModel.GetCustomAlterTimes(_openedArea);
- _realTimeSeatAdherenceModel.BuildSeats(_openedArea, seats =>
- {
- _agentSeats = new List<IAgentSeatModel>(seats.Select(o =>
- {
- var entity = _entityFactory.Create<IAgentSeatModel>(new Dictionary<string, object> { { "Profile", o } });
- _agentInfoColumnChanged += entity.UpdateAgentInfo;
- _seatInfoColumnChanged += entity.UpdateSeatInfo;
- return entity;
- }).ToList());
- });
- CurrentTime = ServiceLocator.Current.GetInstance<IBackendModel>().GetUniversialTime();
- _dataThread.IsBackground = true;
- _dataThread.Start();
- AgentInfoColumnItem = ((string[])Application.Current.Resources["AgentInfoColumnList"]).FirstOrDefault();
- SeatInfoColumnItem = ((string[])Application.Current.Resources["SeatInfoColumnList"]).FirstOrDefault();
- var seatMonitoringRefreshIntervalsList = Application.Current.Resources["SeatMonitoringRefreshIntervalsList"] as IEnumerable<object>;
- if (seatMonitoringRefreshIntervalsList != null)
- RefreshInterval = seatMonitoringRefreshIntervalsList.OfType<int>().Max();
- base.OnInitialize();
- }
- protected override void OnDeactivate()
- {
- //_manualResetEvent.Reset();
- base.OnDeactivate();
- }
- public override void Close()
- {
- _manualResetEvent.Reset();
- base.Close();
- }
- protected override void OnActivate()
- {
- _manualResetEvent.Set();
- NotifyOfPropertyChange("AgentStatusTypes");
- base.OnActivate();
- }
- private readonly string[] _fullAgenInfoColumnList;
- private string _agentInfoColumnItem;
- public string AgentInfoColumnItem
- {
- get { return _agentInfoColumnItem; }
- set
- {
- if (value == null)
- return;
- _agentInfoColumnItem = value;
- if (_agentInfoColumnChanged != null)
- _agentInfoColumnChanged(_fullAgenInfoColumnList.IndexOf(value));
- NotifyOfPropertyChange(() => AgentInfoColumnItem);
- }
- }
- private readonly string[] _fullSeatInfoColumnList;
- private string _seatInfoColumnItem;
- public string SeatInfoColumnItem
- {
- get { return _seatInfoColumnItem; }
- set
- {
- if (value == null)
- return;
- _seatInfoColumnItem = value;
- if (_seatInfoColumnChanged != null)
- _seatInfoColumnChanged(_fullSeatInfoColumnList.IndexOf(value));
- NotifyOfPropertyChange(() => SeatInfoColumnItem);
- }
- }
- public IEnumerable<AgentStatusType> AgentStatusTypes
- {
- get { return _agentStatusTypes; }
- }
- public IEnumerable<IAgentSeatModel> AgentSeats { get { return _agentSeats; } }
- private DateTime _currentTime;
- public DateTime CurrentTime
- {
- get { return _currentTime.Date.AddSeconds(CurrentValue); }
- set
- {
- CurrentValue = (int)value.Subtract(value.Date).TotalSeconds;
- _currentTime = value;
- this.NotifyOfPropertyChange(o => o.CurrentTime);
- }
- }
- private int _currentValue;
- public int CurrentValue
- {
- get { return _currentValue; }
- set
- {
- _currentValue = value;
- this.NotifyOfPropertyChange(o => o.CurrentValue);
- this.NotifyOfPropertyChange(o => o.CurrentTime);
- }
- }
- public void ManuallyQuery()
- {
- if (!AutoRefresh)
- _autoResetEvent.Set();
- }
- private bool _autoRefresh = true;
- public bool AutoRefresh
- {
- get { return _autoRefresh; }
- set
- {
- _autoRefresh = value;
- if (_autoRefresh)
- {
- CurrentTime = ServiceLocator.Current.GetInstance<IBackendModel>().GetUniversialTime();
- _autoResetEvent.Set();
- }
- this.NotifyOfPropertyChange(o => o.AutoRefresh);
- }
- }
- public int RefreshInterval
- {
- get { return (int)_refreshInterval.TotalSeconds; }
- set
- {
- _refreshInterval = TimeSpan.FromSeconds(value);
- }
- }
- private bool _showRtsaPanel = true;
- public bool ShowRtsaPanel
- {
- get { return _showRtsaPanel; }
- set
- {
- _showRtsaPanel = value;
- this.NotifyOfPropertyChange(o => o.ShowRtsaPanel);
- }
- }
- private bool _showRtaaPanel = true;
- public bool ShowRtaaPanel
- {
- get { return _showRtaaPanel; }
- set
- {
- _showRtaaPanel = value;
- this.NotifyOfPropertyChange(o => o.ShowRtaaPanel);
- }
- }
- private bool _showFilter;
- public bool ShowFilter
- {
- get { return _showFilter; }
- set
- {
- _showFilter = value;
- this.NotifyOfPropertyChange(o => o.ShowFilter);
- }
- }
- private AgentStatusAlertTime _selctedCustomAgentStatusType;
- public AgentStatusAlertTime SelctedCustomAgentStatusType
- {
- get { return _selctedCustomAgentStatusType; }
- set
- {
- _selctedCustomAgentStatusType = value;
- NotifyOfPropertyChange("SelctedCustomAgentStatusType");
- }
- }
- private AgentStatusAlertTime RetrieveOrSaveStatusTimeoutAlert(AgentStatusType baseType)
- {
- AgentStatusAlertTime result;
- if (!_customagentStatusTypes.ContainsKey(baseType))
- {
- result = new AgentStatusAlertTime(_openedArea, baseType);
- _customagentStatusTypes[baseType] = result;
- _agentStatusTypeModel.SaveCustomAgentStatusType(result);
- }
- else
- result = _customagentStatusTypes[baseType];
- return result;
- }
- private int GetAgentStatusTimeoutAlert(AgentStatusType baseType, bool secondary)
- {
- var result = RetrieveOrSaveStatusTimeoutAlert(baseType);
- return secondary ? result.AlertTimeOutSecond2 : result.AlertTimeOutSecond;
- }
- public void AgentStatusTypeSelectionChange(AgentStatusType item)
- {
- SelctedCustomAgentStatusType = RetrieveOrSaveStatusTimeoutAlert(item);
- }
- public void OpendFilterDialog()
- {
- OpenDialog<IAgentSeatFilterPresenter>(new Dictionary<string, object> { { "seatMonitoringPresenter", this } });
- }
- public bool CanOpenAgentStatusHistory()
- {
- return _selectedAgentSeatModel != null;
- }
- [Preview("CanOpenAgentStatusHistory")]
- public void OpenAgentStatusHistory()
- {
- var targetSeat = _selectedAgentSeatModel.FirstOrDefault();
- OnDeactivate();
- OpenDialog<IAgentStatusHistoryPresenter>(new Dictionary<string, object>
- {
- {"queryDelegate", new Func<DateTime, DateTime, IList<AgentStatus>>((s, e)=>
- {
- var model = ServiceLocator.Current.GetInstance<IRealTimeSeatAdherenceModel>();
- return model.GetAgentStatusHistory(targetSeat.Profile.ExtNo, s, e);
- })},
- {"targetSeat", targetSeat},
- {"monitoringTime", CurrentTime}
- });
- OnActivate();
- }
- public bool CanOpenAddingEventDialog()
- {
- return !AutoRefresh;
- }
- [Preview("CanOpenAddingEventDialog")]
- [Dependencies("AutoRefresh")]
- public void OpenAddingEventDialog()
- {
- //TODO:Henry refactor this part
- //var reloadAgentDelegate = new Func<IList<TimeBox>>(() => _realTimeSeatAdherenceModel.Save(
- // AgentSeats.Where(
- // o =>
- // o.IsSelected == true &&
- // o.AgentStatus != null).
- // Select(o => o.CurrentAgent).ToList()));
- //if (_allEventTypes == null)
- // _allEventTypes = _realTimeSeatAdherenceModel.GetAllEventTypes();
- //OpenDialog<IAddingEventPresenter>(new Dictionary<string, object> {
- // {"SelectedAgents", reloadAgentDelegate() },
- // {"AvailableTypes", _allEventTypes},
- // {"SupportTwoWayAdding" , false},
- // {"EventStart", CurrentTime},
- // {"ReloadAgentDelegate", reloadAgentDelegate},
- // {"RefreshDelegate", new Action<IAddingTermPresenter>((p)=> AcceptAllChanges(p.SelectedAgents.ToList())) }});
- }
- [SuperRescue("ReloadAgents", "Shifts_ShiftDispatcher_SubmitChangesFail", "Shifts_AlterFail_Title")]
- public void AcceptAllChanges(IList affectedAgents)
- {
- }
- public override void Shutdown()
- {
- _manualResetEvent.Set();
- _autoResetEvent.Set();
- _runningThread = false;
- //_manualWait.Close();
- //_autoWait.Close();
- _agentStatusTypeModel.AcceptAllChanges();
- base.Shutdown();
- }
- #region IClozeContainer Members
- public int Capacity
- {
- get { return Dimension.Count; }
- }
- public System.Collections.IEnumerator GetSeatsEnumerator()
- {
- if (_agentSeats == null)
- return null;
- return _agentSeats.GetEnumerator();
- }
- public ICloze NewItem(int index)
- {
- return NewItem(index, _entityFactory);
- }
- public ICloze NewItem(int index, IEntityFactory entityFactory)
- {
- var container = (IClozeContainer)_openedArea;
- return entityFactory.Create<IAgentSeatModel>(new Dictionary<string, object> { { "Profile", container.NewItem(index) } });
- }
- private readonly Dimension _dimension;
- public Dimension Dimension
- {
- get { return _dimension; }
- }
- #endregion
- #region IShiftDispatcherPresenter Members
- private IList _bindableAgents;
- public IList BindableAgents
- {
- get { return _bindableAgents; }
- set
- {
- _bindableAgents = value;
- this.NotifyOfPropertyChange("BindableAgents");
- }
- }
- public void ReloadAgents(IList affectedAgents, Exception ex)
- {
- }
- public Dictionary<string, object> GetOperationParams()
- {
- throw new NotImplementedException();
- }
- #endregion
- private IList<IAgentSeatModel> _selectedAgentSeatModel;
- public void AddOrRemoveAgent(IList added, IList removed)
- {
- _selectedAgentSeatModel = new List<IAgentSeatModel>();
- foreach (var item in added.Cast<IAgentSeatModel>())
- {
- if (_selectedAgentSeatModel.Contains(item)) continue;
- _selectedAgentSeatModel.Add(item);
- }
- foreach (var item in removed.Cast<IAgentSeatModel>())
- {
- _selectedAgentSeatModel.Remove(item);
- }
- BindableAgents = _selectedAgentSeatModel.Where(o => o.CurrentAgent != null).Select(o => o.CurrentAgent).ToList();
- }
- [SuperRescue("OpenExternalCommandFail", "ApplicationFramework_MenuPresenter_ExDialogTitle_LanguageKey", "null")]
- public void OpenExternalCommand(object param)
- {
- var fileName = string.Format("{0}{1}", Application.Current.FindResource("AgentSeatParameter"), param);
- Process.Start(fileName);
- }
- public void OpenExternalCommandFail()
- {
- }
- }
- }
Add Comment
Please, Sign In to add comment