TopViewSubModel.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. using Aitex.Core.Common.DeviceData;
  2. using Aitex.Core.RT.Event;
  3. using Aitex.Core.UI.Control;
  4. using Aitex.Core.UI.Dialog;
  5. using Aitex.Core.UI.MVVM;
  6. using Aitex.Core.Util;
  7. using Aitex.Core.Utilities;
  8. using Aitex.Core.WCF;
  9. using Aitex.Sorter.Common;
  10. using Hardcodet.Wpf.TaskbarNotification;
  11. using MECF.Framework.Common.CommonData;
  12. using MECF.Framework.Common.OperationCenter;
  13. using MECF.Framework.UI.Core.Accounts;
  14. using MECF.Framework.UI.Core.Applications;
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Collections.ObjectModel;
  18. using System.ComponentModel;
  19. using System.Linq;
  20. using System.Text;
  21. using System.Threading.Tasks;
  22. using System.Windows;
  23. using System.Windows.Input;
  24. namespace Aitex.Sorter.UI.ViewModel
  25. {
  26. public class TerminalMessageItem : NotifiableItem
  27. {
  28. public string MessageTime { get; set; }
  29. public string Type { get; set; }
  30. public string Content { get; set; }
  31. public string Title
  32. {
  33. get { return MessageTime + " (" + Type + ")"; }
  34. }
  35. }
  36. public class TopViewSubModel : UIViewModelBase
  37. {
  38. private int loadPortCount = 10;
  39. public string SoftwareVersion
  40. {
  41. get;
  42. set;
  43. }
  44. private string companyName;
  45. public string CompanyName
  46. {
  47. get { return companyName; }
  48. set {
  49. companyName=value;
  50. InvokePropertyChanged("CompanyName");
  51. }
  52. }
  53. private string showDateTime;
  54. public string ShowDateTime
  55. {
  56. get { return showDateTime; }
  57. set
  58. {
  59. showDateTime = value;
  60. InvokePropertyChanged("ShowDateTime");
  61. }
  62. }
  63. [Subscription(ParamName.ControlStatus, SystemStateModule)]
  64. public string HostControlStatus
  65. {
  66. get;
  67. set;
  68. }
  69. public string HostControlStatusBackground
  70. {
  71. get
  72. {
  73. switch (HostControlStatus)
  74. {
  75. case "Unknown":
  76. return "Yellow";
  77. case "EquipmentOffline":
  78. case "AttemptOnline":
  79. case "HostOffline":
  80. return "Transparent";
  81. case "OnlineLocal":
  82. case "OnlineRemote":
  83. return "LawnGreen";
  84. default:
  85. return "Yellow";
  86. }
  87. }
  88. }
  89. [Subscription(ParamName.CommunicationStatus, SystemStateModule)]
  90. public string HostCommunicationStatus
  91. {
  92. get;
  93. set;
  94. }
  95. /// Disabled = 0,
  96. /// Enabled = 1,
  97. /// EnabledNotCommunicating = 2,
  98. /// EnabledCommunicating = 3,
  99. /// WaitCRA = 4,
  100. /// WaitDelay = 5,
  101. /// WaitCRFromHost = 6,
  102. public string HostCommunicationStatusBackground
  103. {
  104. get
  105. {
  106. switch (HostCommunicationStatus)
  107. {
  108. case "Disabled":
  109. return "Yellow";
  110. case "Enabled":
  111. case "EnabledNotCommunicating":
  112. case "WaitCRA":
  113. case "WaitDelay":
  114. case "WaitCRFromHost":
  115. return "Transparent";
  116. case "EnabledCommunicating":
  117. return "LawnGreen";
  118. default:
  119. return "Yellow";
  120. }
  121. }
  122. }
  123. [Subscription(ParamName.RTStatus, SystemStateModule)]
  124. public int RoutManagerState
  125. {
  126. get;
  127. set;
  128. }
  129. public string SystemStatus
  130. {
  131. get { return ((RtState)RoutManagerState).ToString(); }
  132. }
  133. public string SystemStatusBackground
  134. {
  135. get
  136. {
  137. switch (RoutManagerState)
  138. {
  139. case (int)RtState.Init:
  140. return "Yellow";
  141. case (int)RtState.Initializing:
  142. case (int)RtState.ManualTransfer:
  143. case (int)RtState.Transfer:
  144. case (int)RtState.Cycle:
  145. case (int)RtState.PrepareCycle:
  146. case (int)RtState.PostCycle:
  147. case (int)RtState.Reset:
  148. case (int)RtState.Processing:
  149. case (int)RtState.PostProcess:
  150. case (int)RtState.PauseProcess:
  151. case (int)RtState.SetSpeed:
  152. return "LawnGreen";
  153. case (int)RtState.Error:
  154. case (int)RtState.Maintenance:
  155. return "Red";
  156. case (int)RtState.Idle:
  157. return "Transparent";
  158. default:
  159. return "Goldenrod";
  160. }
  161. }
  162. }
  163. protected override void Poll()
  164. {
  165. for (int i = 0; i < loadPortCount; i++)
  166. {
  167. Subscribe($"LP{i+1}.LoadportState");
  168. }
  169. ShowDateTime = DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss");
  170. base.Poll();
  171. }
  172. public string TerminalBackground
  173. {
  174. get { return _notifyTerminalMessageArrive ? "LawnGreen" : "#0086cd"; }
  175. }
  176. [Subscription(ParamName.AlignerState, DeviceName.Aligner)]
  177. public string AlignerStatus
  178. {
  179. get;
  180. set;
  181. }
  182. public string AlignerStatusBackground
  183. {
  184. get { return GetUnitStatusBackground(AlignerStatus); }
  185. }
  186. [Subscription(ParamName.RobotState, DeviceName.Robot)]
  187. public string RobotStatus
  188. {
  189. get;
  190. set;
  191. }
  192. public string RobotStatusBackground
  193. {
  194. get { return GetUnitStatusBackground(RobotStatus); }
  195. }
  196. [Subscription("System.TurnoverStation.State")]
  197. public string TurnoverStatus
  198. {
  199. get;
  200. set;
  201. }
  202. public string TurnoverStatusBackground
  203. {
  204. get { return GetUnitStatusBackground(TurnoverStatus); }
  205. }
  206. [Subscription("System.SignalTower.DeviceData")]
  207. public AITSignalTowerData SignalTower
  208. {
  209. get;
  210. set;
  211. }
  212. [Subscription(ParamName.IsAutoMode, SystemStateModule)]
  213. public bool IsAutoMode { get; set; }
  214. private bool _simulatorMode = false;
  215. public bool IsManualButtonEnabled
  216. {
  217. get
  218. {
  219. return IsAutoMode;
  220. }
  221. }
  222. public bool IsAutoButtonEnabled
  223. {
  224. get
  225. {
  226. return !IsAutoMode;
  227. }
  228. }
  229. public string GetUnitStatusBackground(string status)
  230. {
  231. if (status != null)
  232. status = status.Trim().ToLower();
  233. switch (status)
  234. {
  235. case "unknown":
  236. return "Yellow";
  237. case "idle":
  238. return "Transparent";
  239. case "busy":
  240. return "LawnGreen";
  241. default:
  242. return "Yellow";
  243. }
  244. }
  245. public string TopViewBackground
  246. {
  247. get
  248. {
  249. return _simulatorMode ? "Goldenrod" : "Transparent";
  250. }
  251. }
  252. public string User
  253. {
  254. get
  255. {
  256. return AccountClient.Instance.CurrentUser == null ? "" : AccountClient.Instance.CurrentUser.AccountId;
  257. }
  258. }
  259. public string Role
  260. {
  261. get
  262. {
  263. return AccountClient.Instance.CurrentUser == null ? "" : AccountClient.Instance.CurrentUser.Role;
  264. }
  265. }
  266. public ICommand SwitchOffBuzzerCommand { get; set; }
  267. public ICommand ResetCommand { get; set; }
  268. public ICommand LogoffCommand { get; set; }
  269. public ICommand TerminalCommand { get; set; }
  270. public ICommand TerminalSendCommand { get; set; }
  271. public ICommand TerminalClearCommand { get; set; }
  272. public bool IsManualMode
  273. {
  274. get
  275. {
  276. return !IsAutoMode;
  277. }
  278. }
  279. public FACommunicationState FACommunicationState
  280. {
  281. get
  282. {
  283. return string.IsNullOrEmpty(HostCommunicationStatus) ? FACommunicationState.Disabled
  284. : (FACommunicationState)Enum.Parse(typeof(FACommunicationState), HostCommunicationStatus);
  285. }
  286. }
  287. public bool IsFAEnableButtonEnabled
  288. {
  289. get { return FACommunicationState == FACommunicationState.Disabled; }
  290. }
  291. public bool IsFADisableButtonEnabled
  292. {
  293. get { return FACommunicationState != FACommunicationState.Disabled; }
  294. }
  295. MessageControl _balloonControl;
  296. private bool _logoff;
  297. public ObservableCollection<EventItem> EventLogList { get; set; }
  298. public ObservableCollection<TerminalMessageItem> TerminalMessageList { get; set; }
  299. public object _lockMessage = new object();
  300. private TerminalDialog _terminalDialog;
  301. [IgnorePropertyChange]
  302. public string TerminalMessageSetPoint { get; set; }
  303. private bool _notifyTerminalMessageArrive = false;
  304. public TopViewSubModel()
  305. : base("TopViewModelSub")
  306. {
  307. //this.loadPortCount = loadPortCount;
  308. SwitchOffBuzzerCommand = new DelegateCommand<object>(OnSwitchOffBuzzer);
  309. ResetCommand = new DelegateCommand<object>(DoReset);
  310. LogoffCommand = new DelegateCommand<object>(DoLogoff);
  311. TerminalCommand = new DelegateCommand<object>(ShowTerminalWindow);
  312. TerminalClearCommand = new DelegateCommand<object>(ClearTerminal);
  313. TerminalSendCommand = new DelegateCommand<object>(SendTerminal);
  314. SoftwareVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
  315. InvokePropertyChanged("SoftwareVersion");
  316. EventLogList = new ObservableCollection<EventItem>();
  317. TerminalMessageList = new ObservableCollection<TerminalMessageItem>();
  318. _balloonControl = new MessageControl();
  319. LPStatus = new ObservableCollection<TopViewLPStatus>();
  320. //LPStatus = new ObservableCollection<TopViewLPStatus>(Enumerable.Range(1, 4).Select(x => new TopViewLPStatus()
  321. //{
  322. // LPName = $"LP{x}",
  323. // LoadPortStatus = "unknown",
  324. //}).ToList());
  325. EventClient.Instance.OnEvent += Instance_OnEvent;
  326. EventClient.Instance.Start();
  327. }
  328. private ObservableCollection<TopViewLPStatus> lpStatus;
  329. public ObservableCollection<TopViewLPStatus> LPStatus
  330. {
  331. get { return lpStatus; }
  332. set
  333. {
  334. lpStatus = value;
  335. InvokePropertyChanged("LPStatus");
  336. }
  337. }
  338. private void SendTerminal(object obj)
  339. {
  340. if (string.IsNullOrEmpty(TerminalMessageSetPoint))
  341. return;
  342. InvokeClient.Instance.Service.DoOperation("FACommand", "FASendTerminalMessage", TerminalMessageSetPoint);
  343. lock (_lockMessage)
  344. {
  345. while (TerminalMessageList.Count > 200)
  346. {
  347. TerminalMessageList.RemoveAt(0);
  348. }
  349. TerminalMessageItem item = new TerminalMessageItem
  350. {
  351. Content = TerminalMessageSetPoint,
  352. Type = "E->H",
  353. MessageTime = DateTime.Now.ToString("yyyy-MM-dd HH: mm:ss")
  354. };
  355. TerminalMessageList.Add(item);
  356. }
  357. TerminalMessageSetPoint = "";
  358. InvokePropertyChanged("TerminalMessageSetPoint");
  359. }
  360. private void ClearTerminal(object obj)
  361. {
  362. lock (_lockMessage)
  363. {
  364. TerminalMessageList.Clear();
  365. }
  366. }
  367. private void ShowTerminalWindow(object obj)
  368. {
  369. _notifyTerminalMessageArrive = false;
  370. _terminalDialog = new TerminalDialog()
  371. {
  372. DataContext = this,
  373. };
  374. _terminalDialog.Topmost = true;
  375. _terminalDialog.WindowStartupLocation = WindowStartupLocation.CenterScreen;
  376. _terminalDialog.ShowDialog();
  377. _terminalDialog = null;
  378. }
  379. private void Instance_OnEvent(EventItem obj)
  380. {
  381. if (_logoff)
  382. return;
  383. Application.Current.Dispatcher.Invoke(new Action(() =>
  384. {
  385. if (obj.Source == "Host" && obj.EventEnum == "TerminalMessage")
  386. {
  387. lock (_lockMessage)
  388. {
  389. while (TerminalMessageList.Count > 200)
  390. {
  391. TerminalMessageList.RemoveAt(0);
  392. }
  393. TerminalMessageItem item = new TerminalMessageItem
  394. {
  395. Content = obj.Description,
  396. Type = "H->E",
  397. MessageTime = obj.OccuringTime.ToString("yyyy-MM-dd HH: mm:ss")
  398. };
  399. TerminalMessageList.Add(item);
  400. }
  401. if (_terminalDialog == null)
  402. _notifyTerminalMessageArrive = true;
  403. }
  404. }));
  405. switch (obj.Type)
  406. {
  407. case EventType.EventUI_Notify:
  408. LogEvent(obj);
  409. break;
  410. case EventType.Dialog_Nofity:
  411. PopDialog(obj);
  412. break;
  413. case EventType.KickOut_Notify:
  414. break;
  415. case EventType.Sound_Notify:
  416. break;
  417. case EventType.UIMessage_Notify:
  418. PopUIMessage(obj);
  419. break;
  420. }
  421. }
  422. private void DoLogoff(object obj)
  423. {
  424. _logoff = true;
  425. UiApplication.Instance.Logoff();
  426. }
  427. private void DoReset(object obj)
  428. {
  429. InvokeClient.Instance.Service.DoOperation("System.Reset");
  430. EventLogList.Clear();
  431. }
  432. public void Reset()
  433. {
  434. InvokeClient.Instance.Service.DoOperation(OperationName.ResetAlarm);
  435. InvokeClient.Instance.Service.DoOperation(OperationName.ResetDevice);
  436. InvokeClient.Instance.Service.DoOperation(OperationName.ResetRouteManager);
  437. }
  438. void OnSwitchOffBuzzer(object param)
  439. {
  440. InvokeClient.Instance.Service.DoOperation(OperationName.DeviceOperation.ToString(), new object[] { DeviceName.SignalTower.ToString(), AITSignalTowerOperation.SwitchOffBuzzer.ToString() });
  441. }
  442. void PopUIMessage(EventItem obj)
  443. {
  444. Application.Current.Dispatcher.Invoke(new Action(() =>
  445. {
  446. _balloonControl.SetMessage(MessageType.Info, obj.Description);
  447. ShowNotifyIcon(2000);
  448. }));
  449. }
  450. void LogEvent(EventItem obj)
  451. {
  452. if (obj.Type != EventType.EventUI_Notify)
  453. return;
  454. Application.Current.Dispatcher.Invoke(new Action(() =>
  455. {
  456. while (EventLogList.Count > 100)
  457. {
  458. EventLogList.RemoveAt(0);
  459. }
  460. EventLogList.Add(obj);
  461. }));
  462. }
  463. void ShowNotifyIcon(int time)
  464. {
  465. TaskbarIcon icon = new TaskbarIcon();
  466. icon.ShowCustomBalloon(_balloonControl, global::System.Windows.Controls.Primitives.PopupAnimation.Slide, time);
  467. }
  468. void PopDialog(EventItem item)
  469. {
  470. Application.Current.Dispatcher.Invoke(new Action(() =>
  471. {
  472. switch (item.Level)
  473. {
  474. case EventLevel.Alarm:
  475. MessageBoxEx.ShowError(item.Description, item.Explaination);
  476. break;
  477. case EventLevel.Warning:
  478. MessageBoxEx.ShowWarning(item.Description, item.Explaination);
  479. break;
  480. case EventLevel.Information:
  481. MessageBoxEx.ShowInfo(item.Description, item.Explaination);
  482. break;
  483. }
  484. }));
  485. }
  486. protected override void InvokeBeforeUpdateProperty(Dictionary<string, object> data)
  487. {
  488. Application.Current.Dispatcher.Invoke(new Action(() =>
  489. {
  490. var tempLPStatus = new List<TopViewLPStatus>();
  491. foreach (var item in data.Keys)
  492. {
  493. if (item.Substring(0, 2) == "LP")
  494. {
  495. tempLPStatus.Add(new TopViewLPStatus()
  496. {
  497. LPName = item.Split('.')[0],
  498. LoadPortStatus = data[item].ToString(),
  499. }); //.Substring(0, 3),
  500. }
  501. }
  502. lpStatus.Clear();
  503. tempLPStatus = tempLPStatus.OrderBy(x => x.LPName).ToList();
  504. foreach (var item in tempLPStatus)
  505. {
  506. lpStatus.Add(item);
  507. }
  508. }));
  509. }
  510. }
  511. public class TopViewLPStatus : INotifyPropertyChanged
  512. {
  513. public string LPName { get; set; }
  514. public string LoadPortStatus { get; set; }
  515. public event PropertyChangedEventHandler PropertyChanged;
  516. }
  517. }