TopViewModel2.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Windows;
  5. using Aitex.Core.Common.DeviceData;
  6. using Aitex.Core.UI.MVVM;
  7. using System.Windows.Input;
  8. using System.Windows.Media;
  9. using Aitex.Core.RT.Event;
  10. using Aitex.Core.UI.Control;
  11. using Aitex.Core.UI.Dialog;
  12. using Aitex.Core.Util;
  13. using Aitex.Core.WCF;
  14. using Aitex.Sorter.Common;
  15. using Aitex.Sorter.UI.ViewModel;
  16. using Hardcodet.Wpf.TaskbarNotification;
  17. using MECF.Framework.Common.OperationCenter;
  18. using MECF.Framework.UI.Core.Accounts;
  19. using MECF.Framework.UI.Core.Applications;
  20. using Aitex.Core.RT.OperationCenter;
  21. namespace Sorter.UI.ViewModel
  22. {
  23. public class TopViewModel2 : UIViewModelBase
  24. {
  25. public string SoftwareVersion
  26. {
  27. get;
  28. set;
  29. }
  30. [Subscription(ParamName.RTStatus, SystemStateModule)]
  31. public int RoutManagerState
  32. {
  33. get;
  34. set;
  35. }
  36. public string SystemStatus
  37. {
  38. get { return ((RtState)RoutManagerState).ToString(); }
  39. }
  40. public string SystemStatusBackground
  41. {
  42. get
  43. {
  44. switch (RoutManagerState)
  45. {
  46. case (int)RtState.Init:
  47. return "Yellow";
  48. case (int)RtState.Initializing:
  49. case (int)RtState.ManualTransfer:
  50. case (int)RtState.Transfer:
  51. case (int)RtState.Cycle:
  52. case (int)RtState.PrepareCycle:
  53. case (int)RtState.PostCycle:
  54. case (int)RtState.Reset:
  55. case (int)RtState.Processing:
  56. case (int)RtState.PostProcess:
  57. case (int)RtState.PauseProcess:
  58. case (int)RtState.SetSpeed:
  59. return "LawnGreen";
  60. case (int)RtState.Error:
  61. case (int)RtState.Maintenance:
  62. return "Red";
  63. case (int)RtState.Idle:
  64. return "Transparent";
  65. default:
  66. return "Goldenrod";
  67. }
  68. }
  69. }
  70. [Subscription("Status", "LP1")]
  71. public string LoadPort1Status
  72. {
  73. get;
  74. set;
  75. }
  76. public string LoadPort1StatusBackground
  77. {
  78. get { return GetUnitStatusBackground(LoadPort1Status); }
  79. }
  80. [Subscription("Status", "LP2")]
  81. public string LoadPort2Status
  82. {
  83. get;
  84. set;
  85. }
  86. public string LoadPort2StatusBackground
  87. {
  88. get { return GetUnitStatusBackground(LoadPort2Status); }
  89. }
  90. [Subscription("LoadportState", "LP3")]
  91. public string LoadPort3Status
  92. {
  93. get;
  94. set;
  95. }
  96. public string LoadPort3StatusBackground
  97. {
  98. get { return GetUnitStatusBackground(LoadPort3Status); }
  99. }
  100. [Subscription("LoadportState", "LP4")]
  101. public string LoadPort4Status
  102. {
  103. get;
  104. set;
  105. }
  106. public string LoadPort4StatusBackground
  107. {
  108. get { return GetUnitStatusBackground(LoadPort4Status); }
  109. }
  110. [Subscription("Robot.State")]
  111. public string RobotStatus
  112. {
  113. get;
  114. set;
  115. }
  116. public string RobotStatusBackground
  117. {
  118. get { return GetUnitStatusBackground(RobotStatus); }
  119. }
  120. [Subscription("System.SignalTower.DeviceData")]
  121. public AITSignalTowerData SignalTower
  122. {
  123. get;
  124. set;
  125. }
  126. [Subscription("System.IsOnlineMode")]
  127. public bool IsOnlineMode { get; set; }
  128. public string OnlineStatus
  129. {
  130. get { return IsOnlineMode ? "Online" : "Offline"; }
  131. }
  132. private bool _simulatorMode = false;
  133. [Subscription("System.ServerStatus")]
  134. public string ServerStatus
  135. {
  136. get; set;
  137. }
  138. public R_TRIG _trig = new R_TRIG();
  139. public bool _clearEventLogEnable;
  140. [Subscription("System.ClearEventLogEnable")]
  141. public bool ClearEventLogEnable
  142. {
  143. get;
  144. set;
  145. //get
  146. // {
  147. // return _clearEventLogEnable;
  148. //}
  149. //set
  150. //{
  151. // _clearEventLogEnable = value;
  152. // if (value)
  153. // {
  154. // _trig.CLK = value;
  155. // if (_trig.Q)
  156. // EventLogList.Clear();
  157. // }
  158. // else
  159. // {
  160. // _trig.RST = true;
  161. // }
  162. //}
  163. }
  164. [Subscription("CoolingBuffer1.CoolingBuffer1.Status")]
  165. public string CoolingBuffer1Status
  166. {
  167. get; set;
  168. }
  169. public string _CoolingBuffer1Status => CoolingBuffer1Status=="Disable"?"": CoolingBuffer1Status;
  170. public string CoolingBuffer1Background
  171. {
  172. get { return GetUnitStatusBackground(CoolingBuffer1Status); }
  173. }
  174. [Subscription("CoolingBuffer2.CoolingBuffer2.Status")]
  175. public string CoolingBuffer2Status
  176. {
  177. get; set;
  178. }
  179. public string _CoolingBuffer2Status => CoolingBuffer2Status == "Disable" ? "" : CoolingBuffer2Status;
  180. public string CoolingBuffer2Background
  181. {
  182. get { return GetUnitStatusBackground(CoolingBuffer2Status); }
  183. }
  184. [Subscription("Aligner1.Aligner1.Status")]
  185. public string Aligner1Status
  186. {
  187. get; set;
  188. }
  189. public string _Aligner1Status => Aligner1Status == "Disable" ? "" : Aligner1Status;
  190. public string Aligner1Background
  191. {
  192. get { return GetUnitStatusBackground(Aligner1Status); }
  193. }
  194. [Subscription("Aligner2.Aligner2.Status")]
  195. public string Aligner2Status
  196. {
  197. get; set;
  198. }
  199. public string _Aligner2Status => Aligner2Status == "Disable" ? "" : Aligner2Status;
  200. public string Aligner2Background
  201. {
  202. get { return GetUnitStatusBackground(Aligner2Status); }
  203. }
  204. public string ServerStatusBackground
  205. {
  206. get { return GetServerStatusBackground(ServerStatus); }
  207. }
  208. public string GetServerStatusBackground(string status)
  209. {
  210. return "Transparent";
  211. // if (status != null)
  212. // status = status.Trim().ToLower();
  213. //switch (status)
  214. //{
  215. // case "unknown":
  216. // return "Yellow";
  217. // case "idle":
  218. // return "Transparent";
  219. // case "busy":
  220. // return "LawnGreen";
  221. // default:
  222. // return "Yellow";
  223. //}
  224. }
  225. public string GetUnitStatusBackground(string status)
  226. {
  227. if (status!=null)
  228. status = status.Trim().ToLower();
  229. switch (status)
  230. {
  231. case "unknown":
  232. return "Yellow";
  233. case "idle":
  234. return "Transparent";
  235. case "busy":
  236. return "LawnGreen";
  237. //case "disable":
  238. // return "DarkOrange";
  239. default:
  240. return "Yellow";
  241. }
  242. }
  243. public string TopBackground
  244. {
  245. get
  246. {
  247. return ServerStatus=="Connected" ? "ForestGreen" : "Yellow";
  248. }
  249. }
  250. public string OnlineStatusBackground
  251. {
  252. get
  253. {
  254. return IsOnlineMode ? "Transparent" : "Yellow";
  255. }
  256. }
  257. public string TopViewBackground
  258. {
  259. get
  260. {
  261. return _simulatorMode ? "Goldenrod" : "Transparent";
  262. }
  263. }
  264. public string User
  265. {
  266. get
  267. {
  268. return AccountClient.Instance.CurrentUser== null ? "" : AccountClient.Instance.CurrentUser.AccountId;
  269. }
  270. }
  271. public string Role
  272. {
  273. get
  274. {
  275. return AccountClient.Instance.CurrentUser == null ? "" : AccountClient.Instance.CurrentUser.Role;
  276. }
  277. }
  278. public ICommand SwitchOffBuzzerCommand { get; set; }
  279. public ICommand ResetCommand { get; set; }
  280. public ICommand LogoffCommand { get; set; }
  281. public bool IsEnableOnline
  282. {
  283. get { return !IsOnlineMode; }
  284. }
  285. public bool IsEnableOffline
  286. {
  287. get { return IsOnlineMode; }
  288. }
  289. MessageControl _balloonControl;
  290. private bool _logoff;
  291. public ObservableCollection<EventItem> EventLogList { get; set; }
  292. public TopViewModel2()
  293. : base("TopViewModel2")
  294. {
  295. SwitchOffBuzzerCommand = new DelegateCommand<object>(OnSwitchOffBuzzer);
  296. ResetCommand = new DelegateCommand<object>(DoReset);
  297. LogoffCommand = new DelegateCommand<object>(DoLogoff);
  298. SoftwareVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
  299. InvokePropertyChanged("SoftwareVersion");
  300. EventLogList = new ObservableCollection<EventItem>();
  301. _balloonControl = new MessageControl();
  302. EventClient.Instance.OnEvent += Instance_OnEvent;
  303. EventClient.Instance.Start();
  304. }
  305. private void Instance_OnEvent(EventItem obj)
  306. {
  307. if (_logoff)
  308. return;
  309. switch (obj.Type)
  310. {
  311. case EventType.EventUI_Notify:
  312. LogEvent(obj);
  313. break;
  314. case EventType.Dialog_Nofity:
  315. PopDialog(obj);
  316. break;
  317. case EventType.KickOut_Notify:
  318. break;
  319. case EventType.Sound_Notify:
  320. break;
  321. case EventType.UIMessage_Notify:
  322. PopUIMessage(obj);
  323. break;
  324. }
  325. }
  326. private void DoLogoff(object obj)
  327. {
  328. _logoff = true;
  329. UiApplication.Instance.Logoff();
  330. }
  331. private void DoReset(object obj)
  332. {
  333. InvokeClient.Instance.Service.DoOperation("System.Reset");
  334. EventLogList.Clear();
  335. InvokeClient.Instance.Service.DoOperation("LP1.LPReset");
  336. InvokeClient.Instance.Service.DoOperation("LP2.LPReset");
  337. }
  338. public void Reset()
  339. {
  340. InvokeClient.Instance.Service.DoOperation(OperationName.ResetAlarm);
  341. InvokeClient.Instance.Service.DoOperation(OperationName.ResetDevice);
  342. InvokeClient.Instance.Service.DoOperation(OperationName.ResetRouteManager);
  343. }
  344. void OnSwitchOffBuzzer(object param)
  345. {
  346. InvokeClient.Instance.Service.DoOperation("System.SignalTower.SwitchOffBuzzer1");
  347. InvokeClient.Instance.Service.DoOperation("System.SignalTower.SwitchOffBuzzer2");
  348. }
  349. void PopUIMessage(EventItem obj)
  350. {
  351. Application.Current.Dispatcher.Invoke(new Action(() =>
  352. {
  353. _balloonControl.SetMessage(MessageType.Info, obj.Description);
  354. ShowNotifyIcon(2000);
  355. }));
  356. }
  357. void LogEvent(EventItem obj)
  358. {
  359. if (obj.Type != EventType.EventUI_Notify)
  360. return;
  361. Application.Current.Dispatcher.Invoke(new Action(() =>
  362. {
  363. while (EventLogList.Count > 100)
  364. {
  365. EventLogList.RemoveAt(0);
  366. }
  367. EventLogList.Add(obj);
  368. }));
  369. }
  370. void ShowNotifyIcon(int time)
  371. {
  372. TaskbarIcon icon = new TaskbarIcon();
  373. icon.ShowCustomBalloon(_balloonControl, global::System.Windows.Controls.Primitives.PopupAnimation.Slide, time);
  374. }
  375. void PopDialog(EventItem item)
  376. {
  377. Application.Current.Dispatcher.Invoke(new Action(() =>
  378. {
  379. switch (item.Level)
  380. {
  381. case EventLevel.Alarm:
  382. MessageBoxEx.ShowError(item.Description, item.Explaination);
  383. break;
  384. case EventLevel.Warning:
  385. MessageBoxEx.ShowWarning(item.Description, item.Explaination);
  386. break;
  387. case EventLevel.Information:
  388. MessageBoxEx.ShowInfo(item.Description, item.Explaination);
  389. break;
  390. }
  391. }));
  392. }
  393. protected override void InvokeBeforeUpdateProperty(Dictionary<string, object> data)
  394. {
  395. Application.Current.Dispatcher.Invoke(new Action(() =>
  396. {
  397. _trig.CLK = ClearEventLogEnable;
  398. if (_trig.Q)
  399. {
  400. InvokeClient.Instance.Service.DoOperation("System.Reset");
  401. EventLogList.Clear();
  402. InvokeClient.Instance.Service.DoOperation("LP1.LPReset");
  403. InvokeClient.Instance.Service.DoOperation("LP2.LPReset");
  404. InvokeClient.Instance.Service.DoOperation("System.ClearEventLogEnable");
  405. }
  406. }));
  407. }
  408. }
  409. }