Guest User

Untitled

a guest
Nov 19th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.23 KB | None | 0 0
  1. namespace Luna.Shifts.Presenters
  2. {
  3. [PerRequest(typeof(ISeatMonitoringPresenter))]
  4. public class SeatMonitoringPresenter : DockingPresenter, ISeatMonitoringPresenter, IClozeContainer
  5. {
  6. private readonly Area _openedArea;
  7. private readonly AutoResetEvent _autoResetEvent = new AutoResetEvent(false);
  8.  
  9.  
  10. private readonly ManualResetEvent _manualResetEvent = new ManualResetEvent(true);
  11. private readonly Thread _dataThread;
  12.  
  13. private readonly IRealTimeSeatAdherenceModel _realTimeSeatAdherenceModel;
  14. private readonly IEntityFactory _entityFactory;
  15. private TimeSpan _refreshInterval = TimeSpan.FromSeconds(10);
  16. private bool _runningThread = true;
  17.  
  18. private IEnumerable<AgentStatusType> _agentStatusTypes;
  19. private IDictionary<AgentStatusType, AgentStatusAlertTime> _customagentStatusTypes;
  20. private Action<int> _agentInfoColumnChanged;
  21. private Action<int> _seatInfoColumnChanged;
  22. public event Action<DateTime> UpdateEvent;
  23. //private readonly IShiftDispatcherModel _shiftDispatcherModel;
  24. private readonly IAgentStatusTypeModel _agentStatusTypeModel;
  25. //private IEnumerable<TermStyle> _allEventTypes;
  26. private IEnumerable<IAgentSeatModel> _agentSeats;
  27.  
  28. private Dictionary<string, AgentStatusType> _agentStatusTypesDic;
  29.  
  30. public SeatMonitoringPresenter(Area area, IEntityFactory entityFactory, IRealTimeSeatAdherenceModel realTimeSeatAdherenceModel, IAgentStatusTypeModel agentStatusTypeModel)
  31. {
  32. _entityFactory = entityFactory;
  33. _realTimeSeatAdherenceModel = realTimeSeatAdherenceModel;
  34. _agentStatusTypeModel = agentStatusTypeModel;
  35. _dimension = area.Dimension;
  36. _openedArea = area;
  37.  
  38. _dataThread = new Thread(Fetch);
  39.  
  40. DisplayName = string.Format(LanguageReader.GetValue("Adherence_SeatMonitoring_Title"), _openedArea.Name);
  41.  
  42. _fullAgenInfoColumnList = (string[])Application.Current.Resources["AgentInfoColumnFullList"];
  43. _fullSeatInfoColumnList = (string[])Application.Current.Resources["SeatInfoColumnFullList"];
  44. }
  45.  
  46.  
  47. private bool _refreshing;
  48. public bool Refreshing
  49. {
  50. get { return _refreshing; }
  51. set
  52. {
  53. _refreshing = value;
  54. NotifyOfPropertyChange(() => Refreshing);
  55. }
  56. }
  57.  
  58. public void Refresh()
  59. {
  60. _realTimeSeatAdherenceModel.ClearCache();
  61.  
  62. var thread = new Thread(() =>
  63. {
  64. var model = ServiceLocator.Current.GetInstance<IRealTimeSeatAdherenceModel>();
  65. CurrentTime = ServiceLocator.Current.GetInstance<IBackendModel>().GetUniversialTime();
  66. Refreshing = true;
  67. model.SetMonitoringArea(_openedArea, CurrentTime, _agentStatusTypesDic);
  68.  
  69. foreach (var agentSeat in AgentSeats)
  70. {
  71. model.LoadData(agentSeat);
  72. agentSeat.UpdateStatus(CurrentTime, GetAgentStatusTimeoutAlert);
  73. }
  74. Refreshing = false;
  75. });
  76.  
  77. thread.IsBackground = true;
  78. thread.Start();
  79. }
  80.  
  81. private void Fetch()
  82. {
  83.  
  84. #region refresh agentStatus process
  85. //int count = -1;
  86. var model = _realTimeSeatAdherenceModel;//ServiceLocator.Current.GetInstance<IRealTimeSeatAdherenceModel>();
  87.  
  88. var runningWait = new AutoResetEvent(true);
  89. //_runningWait.Set();
  90. while (_runningThread)
  91. {
  92. //count++;
  93. runningWait.WaitOne();
  94. try
  95. {
  96.  
  97. if (AutoRefresh)
  98. {
  99. CurrentTime = ServiceLocator.Current.GetInstance<IBackendModel>().GetUniversialTime();
  100. Refreshing = true;
  101. }
  102.  
  103. model.SetMonitoringArea(_openedArea, CurrentTime, _agentStatusTypesDic);
  104. Refreshing = false;
  105.  
  106. foreach (var agentSeat in AgentSeats)
  107. {
  108. model.LoadData(agentSeat);
  109. agentSeat.UpdateStatus(CurrentTime, GetAgentStatusTimeoutAlert);
  110. }
  111.  
  112. if (UpdateEvent != null)
  113. UpdateEvent(CurrentTime);
  114. }
  115. catch (Exception ex)
  116. {
  117. //Log it
  118. var log = log4net.LogManager.GetLogger("SeatMonitoringLogger");
  119. if (log.IsErrorEnabled)
  120. log.Error(ex);
  121.  
  122. }
  123.  
  124. runningWait.Set();
  125.  
  126.  
  127. if (AutoRefresh)
  128. {
  129. Thread.Sleep(_refreshInterval);
  130. }
  131. else //手动拉的时候间隔
  132. {
  133. Thread.Sleep(500);
  134. _autoResetEvent.WaitOne();
  135. }
  136.  
  137. _manualResetEvent.WaitOne(); //全局控制线程状态
  138. }
  139.  
  140. Debug.WriteLine("Thread Close");
  141. model.As<IDisposable>().Dispose();
  142.  
  143. #endregion
  144. }
  145.  
  146.  
  147.  
  148.  
  149. protected override void OnInitialize()
  150. {
  151. _agentStatusTypes = _agentStatusTypeModel.GetAll();
  152. _agentStatusTypesDic = _agentStatusTypes.ToDictionary(o => o.StatusCode.Trim());
  153. _customagentStatusTypes = _agentStatusTypeModel.GetCustomAlterTimes(_openedArea);
  154.  
  155. _realTimeSeatAdherenceModel.BuildSeats(_openedArea, seats =>
  156. {
  157. _agentSeats = new List<IAgentSeatModel>(seats.Select(o =>
  158. {
  159. var entity = _entityFactory.Create<IAgentSeatModel>(new Dictionary<string, object> { { "Profile", o } });
  160. _agentInfoColumnChanged += entity.UpdateAgentInfo;
  161. _seatInfoColumnChanged += entity.UpdateSeatInfo;
  162. return entity;
  163. }).ToList());
  164.  
  165. });
  166.  
  167. CurrentTime = ServiceLocator.Current.GetInstance<IBackendModel>().GetUniversialTime();
  168. _dataThread.IsBackground = true;
  169. _dataThread.Start();
  170.  
  171. AgentInfoColumnItem = ((string[])Application.Current.Resources["AgentInfoColumnList"]).FirstOrDefault();
  172. SeatInfoColumnItem = ((string[])Application.Current.Resources["SeatInfoColumnList"]).FirstOrDefault();
  173.  
  174. var seatMonitoringRefreshIntervalsList = Application.Current.Resources["SeatMonitoringRefreshIntervalsList"] as IEnumerable<object>;
  175. if (seatMonitoringRefreshIntervalsList != null)
  176. RefreshInterval = seatMonitoringRefreshIntervalsList.OfType<int>().Max();
  177.  
  178. base.OnInitialize();
  179. }
  180.  
  181. protected override void OnDeactivate()
  182. {
  183. //_manualResetEvent.Reset();
  184. base.OnDeactivate();
  185. }
  186.  
  187. public override void Close()
  188. {
  189. _manualResetEvent.Reset();
  190. base.Close();
  191. }
  192.  
  193. protected override void OnActivate()
  194. {
  195. _manualResetEvent.Set();
  196.  
  197. NotifyOfPropertyChange("AgentStatusTypes");
  198. base.OnActivate();
  199. }
  200.  
  201. private readonly string[] _fullAgenInfoColumnList;
  202.  
  203. private string _agentInfoColumnItem;
  204. public string AgentInfoColumnItem
  205. {
  206. get { return _agentInfoColumnItem; }
  207. set
  208. {
  209. if (value == null)
  210. return;
  211. _agentInfoColumnItem = value;
  212. if (_agentInfoColumnChanged != null)
  213. _agentInfoColumnChanged(_fullAgenInfoColumnList.IndexOf(value));
  214. NotifyOfPropertyChange(() => AgentInfoColumnItem);
  215. }
  216. }
  217.  
  218. private readonly string[] _fullSeatInfoColumnList;
  219.  
  220. private string _seatInfoColumnItem;
  221. public string SeatInfoColumnItem
  222. {
  223. get { return _seatInfoColumnItem; }
  224. set
  225. {
  226. if (value == null)
  227. return;
  228. _seatInfoColumnItem = value;
  229. if (_seatInfoColumnChanged != null)
  230. _seatInfoColumnChanged(_fullSeatInfoColumnList.IndexOf(value));
  231. NotifyOfPropertyChange(() => SeatInfoColumnItem);
  232. }
  233. }
  234.  
  235. public IEnumerable<AgentStatusType> AgentStatusTypes
  236. {
  237. get { return _agentStatusTypes; }
  238. }
  239.  
  240. public IEnumerable<IAgentSeatModel> AgentSeats { get { return _agentSeats; } }
  241.  
  242. private DateTime _currentTime;
  243. public DateTime CurrentTime
  244. {
  245. get { return _currentTime.Date.AddSeconds(CurrentValue); }
  246. set
  247. {
  248. CurrentValue = (int)value.Subtract(value.Date).TotalSeconds;
  249. _currentTime = value;
  250. this.NotifyOfPropertyChange(o => o.CurrentTime);
  251. }
  252. }
  253.  
  254. private int _currentValue;
  255. public int CurrentValue
  256. {
  257. get { return _currentValue; }
  258. set
  259. {
  260. _currentValue = value;
  261. this.NotifyOfPropertyChange(o => o.CurrentValue);
  262. this.NotifyOfPropertyChange(o => o.CurrentTime);
  263. }
  264. }
  265.  
  266. public void ManuallyQuery()
  267. {
  268. if (!AutoRefresh)
  269. _autoResetEvent.Set();
  270. }
  271.  
  272. private bool _autoRefresh = true;
  273. public bool AutoRefresh
  274. {
  275. get { return _autoRefresh; }
  276. set
  277. {
  278. _autoRefresh = value;
  279. if (_autoRefresh)
  280. {
  281. CurrentTime = ServiceLocator.Current.GetInstance<IBackendModel>().GetUniversialTime();
  282. _autoResetEvent.Set();
  283. }
  284. this.NotifyOfPropertyChange(o => o.AutoRefresh);
  285. }
  286. }
  287.  
  288. public int RefreshInterval
  289. {
  290. get { return (int)_refreshInterval.TotalSeconds; }
  291. set
  292. {
  293. _refreshInterval = TimeSpan.FromSeconds(value);
  294. }
  295. }
  296.  
  297. private bool _showRtsaPanel = true;
  298. public bool ShowRtsaPanel
  299. {
  300. get { return _showRtsaPanel; }
  301. set
  302. {
  303. _showRtsaPanel = value;
  304. this.NotifyOfPropertyChange(o => o.ShowRtsaPanel);
  305. }
  306. }
  307.  
  308. private bool _showRtaaPanel = true;
  309. public bool ShowRtaaPanel
  310. {
  311. get { return _showRtaaPanel; }
  312. set
  313. {
  314. _showRtaaPanel = value;
  315. this.NotifyOfPropertyChange(o => o.ShowRtaaPanel);
  316. }
  317. }
  318.  
  319. private bool _showFilter;
  320. public bool ShowFilter
  321. {
  322. get { return _showFilter; }
  323. set
  324. {
  325. _showFilter = value;
  326. this.NotifyOfPropertyChange(o => o.ShowFilter);
  327. }
  328. }
  329.  
  330. private AgentStatusAlertTime _selctedCustomAgentStatusType;
  331.  
  332. public AgentStatusAlertTime SelctedCustomAgentStatusType
  333. {
  334. get { return _selctedCustomAgentStatusType; }
  335. set
  336. {
  337. _selctedCustomAgentStatusType = value;
  338. NotifyOfPropertyChange("SelctedCustomAgentStatusType");
  339. }
  340. }
  341.  
  342. private AgentStatusAlertTime RetrieveOrSaveStatusTimeoutAlert(AgentStatusType baseType)
  343. {
  344. AgentStatusAlertTime result;
  345.  
  346. if (!_customagentStatusTypes.ContainsKey(baseType))
  347. {
  348. result = new AgentStatusAlertTime(_openedArea, baseType);
  349. _customagentStatusTypes[baseType] = result;
  350. _agentStatusTypeModel.SaveCustomAgentStatusType(result);
  351. }
  352. else
  353. result = _customagentStatusTypes[baseType];
  354.  
  355. return result;
  356. }
  357.  
  358. private int GetAgentStatusTimeoutAlert(AgentStatusType baseType, bool secondary)
  359. {
  360. var result = RetrieveOrSaveStatusTimeoutAlert(baseType);
  361. return secondary ? result.AlertTimeOutSecond2 : result.AlertTimeOutSecond;
  362. }
  363.  
  364. public void AgentStatusTypeSelectionChange(AgentStatusType item)
  365. {
  366. SelctedCustomAgentStatusType = RetrieveOrSaveStatusTimeoutAlert(item);
  367. }
  368.  
  369. public void OpendFilterDialog()
  370. {
  371. OpenDialog<IAgentSeatFilterPresenter>(new Dictionary<string, object> { { "seatMonitoringPresenter", this } });
  372. }
  373.  
  374.  
  375. public bool CanOpenAgentStatusHistory()
  376. {
  377. return _selectedAgentSeatModel != null;
  378. }
  379. [Preview("CanOpenAgentStatusHistory")]
  380. public void OpenAgentStatusHistory()
  381. {
  382.  
  383. var targetSeat = _selectedAgentSeatModel.FirstOrDefault();
  384. OnDeactivate();
  385. OpenDialog<IAgentStatusHistoryPresenter>(new Dictionary<string, object>
  386. {
  387. {"queryDelegate", new Func<DateTime, DateTime, IList<AgentStatus>>((s, e)=>
  388. {
  389. var model = ServiceLocator.Current.GetInstance<IRealTimeSeatAdherenceModel>();
  390. return model.GetAgentStatusHistory(targetSeat.Profile.ExtNo, s, e);
  391. })},
  392. {"targetSeat", targetSeat},
  393. {"monitoringTime", CurrentTime}
  394. });
  395. OnActivate();
  396. }
  397.  
  398. public bool CanOpenAddingEventDialog()
  399. {
  400. return !AutoRefresh;
  401. }
  402.  
  403. [Preview("CanOpenAddingEventDialog")]
  404. [Dependencies("AutoRefresh")]
  405. public void OpenAddingEventDialog()
  406. {
  407. //TODO:Henry refactor this part
  408. //var reloadAgentDelegate = new Func<IList<TimeBox>>(() => _realTimeSeatAdherenceModel.Save(
  409. // AgentSeats.Where(
  410. // o =>
  411. // o.IsSelected == true &&
  412. // o.AgentStatus != null).
  413. // Select(o => o.CurrentAgent).ToList()));
  414.  
  415. //if (_allEventTypes == null)
  416. // _allEventTypes = _realTimeSeatAdherenceModel.GetAllEventTypes();
  417.  
  418. //OpenDialog<IAddingEventPresenter>(new Dictionary<string, object> {
  419. // {"SelectedAgents", reloadAgentDelegate() },
  420. // {"AvailableTypes", _allEventTypes},
  421. // {"SupportTwoWayAdding" , false},
  422. // {"EventStart", CurrentTime},
  423. // {"ReloadAgentDelegate", reloadAgentDelegate},
  424. // {"RefreshDelegate", new Action<IAddingTermPresenter>((p)=> AcceptAllChanges(p.SelectedAgents.ToList())) }});
  425.  
  426. }
  427.  
  428. [SuperRescue("ReloadAgents", "Shifts_ShiftDispatcher_SubmitChangesFail", "Shifts_AlterFail_Title")]
  429. public void AcceptAllChanges(IList affectedAgents)
  430. {
  431. }
  432.  
  433. public override void Shutdown()
  434. {
  435. _manualResetEvent.Set();
  436. _autoResetEvent.Set();
  437. _runningThread = false;
  438. //_manualWait.Close();
  439. //_autoWait.Close();
  440. _agentStatusTypeModel.AcceptAllChanges();
  441. base.Shutdown();
  442. }
  443.  
  444. #region IClozeContainer Members
  445.  
  446. public int Capacity
  447. {
  448. get { return Dimension.Count; }
  449. }
  450.  
  451.  
  452.  
  453. public System.Collections.IEnumerator GetSeatsEnumerator()
  454. {
  455. if (_agentSeats == null)
  456. return null;
  457. return _agentSeats.GetEnumerator();
  458. }
  459.  
  460. public ICloze NewItem(int index)
  461. {
  462. return NewItem(index, _entityFactory);
  463. }
  464.  
  465. public ICloze NewItem(int index, IEntityFactory entityFactory)
  466. {
  467. var container = (IClozeContainer)_openedArea;
  468.  
  469. return entityFactory.Create<IAgentSeatModel>(new Dictionary<string, object> { { "Profile", container.NewItem(index) } });
  470. }
  471.  
  472. private readonly Dimension _dimension;
  473. public Dimension Dimension
  474. {
  475. get { return _dimension; }
  476. }
  477.  
  478. #endregion
  479.  
  480. #region IShiftDispatcherPresenter Members
  481.  
  482. private IList _bindableAgents;
  483.  
  484. public IList BindableAgents
  485. {
  486. get { return _bindableAgents; }
  487. set
  488. {
  489. _bindableAgents = value;
  490. this.NotifyOfPropertyChange("BindableAgents");
  491. }
  492. }
  493.  
  494. public void ReloadAgents(IList affectedAgents, Exception ex)
  495. {
  496. }
  497.  
  498. public Dictionary<string, object> GetOperationParams()
  499. {
  500. throw new NotImplementedException();
  501. }
  502.  
  503. #endregion
  504.  
  505. private IList<IAgentSeatModel> _selectedAgentSeatModel;
  506.  
  507. public void AddOrRemoveAgent(IList added, IList removed)
  508. {
  509. _selectedAgentSeatModel = new List<IAgentSeatModel>();
  510.  
  511. foreach (var item in added.Cast<IAgentSeatModel>())
  512. {
  513. if (_selectedAgentSeatModel.Contains(item)) continue;
  514. _selectedAgentSeatModel.Add(item);
  515. }
  516.  
  517. foreach (var item in removed.Cast<IAgentSeatModel>())
  518. {
  519. _selectedAgentSeatModel.Remove(item);
  520. }
  521.  
  522. BindableAgents = _selectedAgentSeatModel.Where(o => o.CurrentAgent != null).Select(o => o.CurrentAgent).ToList();
  523. }
  524.  
  525. [SuperRescue("OpenExternalCommandFail", "ApplicationFramework_MenuPresenter_ExDialogTitle_LanguageKey", "null")]
  526. public void OpenExternalCommand(object param)
  527. {
  528. var fileName = string.Format("{0}{1}", Application.Current.FindResource("AgentSeatParameter"), param);
  529. Process.Start(fileName);
  530. }
  531.  
  532. public void OpenExternalCommandFail()
  533. {
  534. }
  535. }
  536. }
Add Comment
Please, Sign In to add comment