TopViewModel.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. using Aitex.Core.Common.DeviceData;
  2. using Aitex.Core.RT.Event;
  3. using Aitex.Core.Util;
  4. using Aitex.Core.WCF;
  5. using MECF.Framework.Common.DataCenter;
  6. using MECF.Framework.Common.Equipment;
  7. using MECF.Framework.Common.OperationCenter;
  8. using Prism.Commands;
  9. using Prism.Mvvm;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Collections.ObjectModel;
  13. using System.Linq;
  14. using System.Text;
  15. using System.Threading;
  16. using System.Threading.Tasks;
  17. using System.Windows;
  18. using System.Windows.Threading;
  19. using PunkHPX8_Core;
  20. using PunkHPX8_MainPages.Unity;
  21. using PunkHPX8_Themes.UserControls;
  22. using MECF.Framework.Common.Utilities;
  23. using PunkHPX8_MainPages.Views;
  24. using System.Net.Sockets;
  25. using System.Net;
  26. namespace PunkHPX8_MainPages.ViewModels
  27. {
  28. internal class TopViewModel : BindableBase
  29. {
  30. #region 私有字段
  31. private string m_Title;
  32. private string m_SoftwareVersion;
  33. private List<string> m_RtDataKeys = new List<string>();
  34. private Dictionary<string, object> m_RtDataValues;
  35. //private string ModuleName;
  36. private ObservableCollection<EventItem> m_EventLogList = new ObservableCollection<EventItem>();
  37. private EventItem m_CurrentEventItem = new EventItem();
  38. private string m_CurrentEventItemValue;
  39. private object _lockObj = new object();
  40. private int m_EventLogListSelectedIndex;
  41. private Queue<EventItem> alarmQuery = new Queue<EventItem>();//控制alarm log 在 top UI显示
  42. private int logMaxCount = 50;//log在ui最多显示数量
  43. private AITSignalTowerData m_SignalTowerData;
  44. private string m_HostCommunicationStatus;
  45. private string m_TimeTick;
  46. //private JetChamber m_SelectedJetChamber = JetChamber.None;
  47. private bool _EFEMIsInstalled;
  48. private bool _vpw1Installed;
  49. private bool _vpw2Installed;
  50. private bool _platingCell1Installed;
  51. private bool _platingCell2Installed;
  52. private bool _platingCell3Installed;
  53. private bool _platingCell4Installed;
  54. private bool _srd1Installed;
  55. private bool _srd2Installed;
  56. private List<int> stopEntityId = new List<int>() { 17 };
  57. private string _systemControlIP;
  58. private string _toolID;
  59. private List<string> localIPs = new List<string>();
  60. private bool _isControlPermission;
  61. /// <summary>
  62. /// 用户名
  63. /// </summary>
  64. private string _userName;
  65. #endregion
  66. #region 属性
  67. public string Title
  68. {
  69. get { return m_Title; }
  70. set { SetProperty(ref m_Title, value); }
  71. }
  72. public string SoftwareVersion
  73. {
  74. get { return m_SoftwareVersion; }
  75. set { SetProperty(ref m_SoftwareVersion, value); }
  76. }
  77. public string SystemControlIP
  78. {
  79. get { return _systemControlIP; }
  80. set { SetProperty(ref _systemControlIP, value); }
  81. }
  82. public string ToolID
  83. {
  84. get { return _toolID; }
  85. set { SetProperty(ref _toolID, value); }
  86. }
  87. public string TimeTick
  88. {
  89. get { return m_TimeTick; }
  90. set { SetProperty(ref m_TimeTick, value); }
  91. }
  92. public Dictionary<string, object> RtDataValues
  93. {
  94. get { return m_RtDataValues; }
  95. set { SetProperty(ref m_RtDataValues, value); }
  96. }
  97. public ObservableCollection<EventItem> EventLogList
  98. {
  99. get { return m_EventLogList; }
  100. set { SetProperty(ref m_EventLogList, value); }
  101. }
  102. public EventItem CurrentEventItem
  103. {
  104. get { return m_CurrentEventItem; }
  105. set { SetProperty(ref m_CurrentEventItem, value); }
  106. }
  107. public int EventLogListSelectedIndex
  108. {
  109. get { return m_EventLogListSelectedIndex; }
  110. set { SetProperty(ref m_EventLogListSelectedIndex, value); }
  111. }
  112. public string CurrentEventItemValue
  113. {
  114. get { return m_CurrentEventItemValue; }
  115. set { SetProperty(ref m_CurrentEventItemValue, value); }
  116. }
  117. public AITSignalTowerData SignalTowerData
  118. {
  119. get { return m_SignalTowerData; }
  120. set { SetProperty(ref m_SignalTowerData, value); }
  121. }
  122. public string HostCommunicationStatusBackground
  123. {
  124. get
  125. {
  126. switch (HostCommunicationStatus)
  127. {
  128. case "Disabled":
  129. return "Yellow";
  130. case "Enabled":
  131. case "EnabledNotCommunicating":
  132. case "WaitCRA":
  133. case "WaitDelay":
  134. case "WaitCRFromHost":
  135. return "Transparent";
  136. case "EnabledCommunicating":
  137. return "LawnGreen";
  138. default:
  139. return "Yellow";
  140. }
  141. }
  142. }
  143. public string m_HostBack;
  144. public string HostBack
  145. {
  146. get { return m_HostBack; }
  147. set { SetProperty(ref m_HostBack, value); }
  148. }
  149. [Subscription("System.CommunicationStatus")]
  150. public string HostCommunicationStatus
  151. {
  152. get { return m_HostCommunicationStatus; }
  153. set
  154. {
  155. SetProperty(ref m_HostCommunicationStatus, value);
  156. }
  157. }
  158. public bool IsEnableFAEnable
  159. {
  160. get
  161. {
  162. return HostCommunicationStatus == "Disabled";
  163. }
  164. }
  165. public bool _IsEnableEnable;
  166. public bool IsEnableEnable
  167. {
  168. get { return _IsEnableEnable; }
  169. set { SetProperty(ref _IsEnableEnable, value); }
  170. }
  171. public bool _IsEnableDisable;
  172. public bool IsEnableDisable
  173. {
  174. get { return _IsEnableDisable; }
  175. set { SetProperty(ref _IsEnableDisable, value); }
  176. }
  177. public FACommunicationState FACommunicationState
  178. {
  179. get
  180. {
  181. return string.IsNullOrEmpty(HostCommunicationStatus) ? FACommunicationState.Disabled
  182. : (FACommunicationState)Enum.Parse(typeof(FACommunicationState), HostCommunicationStatus);
  183. }
  184. }
  185. public bool IsControlPermission
  186. {
  187. get { return _isControlPermission; }
  188. set { SetProperty(ref _isControlPermission, value); }
  189. }
  190. public bool EFEMIsInstalled
  191. {
  192. get { return _EFEMIsInstalled; }
  193. set { SetProperty(ref _EFEMIsInstalled, value); }
  194. }
  195. public bool VPW1IsInstalled
  196. {
  197. get { return _vpw1Installed; }
  198. set { SetProperty(ref _vpw1Installed, value); }
  199. }
  200. public bool VPW2IsInstalled
  201. {
  202. get { return _vpw2Installed; }
  203. set { SetProperty(ref _vpw2Installed, value); }
  204. }
  205. public bool PlatingCell1IsInstalled
  206. {
  207. get { return _platingCell1Installed; }
  208. set { SetProperty(ref _platingCell1Installed, value); }
  209. }
  210. public bool PlatingCell2IsInstalled
  211. {
  212. get { return _platingCell2Installed; }
  213. set { SetProperty(ref _platingCell2Installed, value); }
  214. }
  215. public bool PlatingCell3IsInstalled
  216. {
  217. get { return _platingCell3Installed; }
  218. set { SetProperty(ref _platingCell3Installed, value); }
  219. }
  220. public bool PlatingCell4IsInstalled
  221. {
  222. get { return _platingCell4Installed; }
  223. set { SetProperty(ref _platingCell4Installed, value); }
  224. }
  225. public bool Srd1IsInstalled
  226. {
  227. get { return _srd1Installed; }
  228. set { SetProperty(ref _srd1Installed, value); }
  229. }
  230. public bool Srd2IsInstalled
  231. {
  232. get { return _srd2Installed; }
  233. set { SetProperty(ref _srd2Installed, value); }
  234. }
  235. /// <summary>
  236. /// 用户名
  237. /// </summary>
  238. public string UserName
  239. {
  240. get { return _userName; }
  241. set { SetProperty(ref _userName, value); }
  242. }
  243. #endregion
  244. #region 命令
  245. private DelegateCommand _SwichLanguageCommand;
  246. public DelegateCommand SwichLanguageCommand =>
  247. _SwichLanguageCommand ?? (_SwichLanguageCommand = new DelegateCommand(OnSwitchLanguage));
  248. private DelegateCommand _ResetCommand;
  249. public DelegateCommand ResetCommand =>
  250. _ResetCommand ?? (_ResetCommand = new DelegateCommand(OnReset));
  251. private DelegateCommand _ClearCommand;
  252. public DelegateCommand ClearCommand =>
  253. _ClearCommand ?? (_ClearCommand = new DelegateCommand(OnClear));
  254. private DelegateCommand _SkipCommand;
  255. public DelegateCommand SkipCommand =>
  256. _SkipCommand ?? (_SkipCommand = new DelegateCommand(OnSkip));
  257. //private DelegateCommand _BuzzerOffCommand;
  258. //public DelegateCommand BuzzerOffCommand =>
  259. // _BuzzerOffCommand ?? (_BuzzerOffCommand = new DelegateCommand(OnBuzzerOff));
  260. private DelegateCommand _FADisableCommand;
  261. public DelegateCommand FADisableCommand =>
  262. _FADisableCommand ?? (_FADisableCommand = new DelegateCommand(FaDisable));
  263. private DelegateCommand _FAEnableCommand;
  264. public DelegateCommand FAEnableCommand =>
  265. _FAEnableCommand ?? (_FAEnableCommand = new DelegateCommand(FaEnable));
  266. private DelegateCommand _LogoutCommand;
  267. public DelegateCommand LogoutCommand =>
  268. _LogoutCommand ?? (_LogoutCommand = new DelegateCommand(OnLogout));
  269. private DelegateCommand _ControlPermissionCommand;
  270. public DelegateCommand ControlPermissionCommand =>
  271. _ControlPermissionCommand ?? (_ControlPermissionCommand = new DelegateCommand(ControlPermissionAction));
  272. private DelegateCommand _ControlReleaseCommand;
  273. public DelegateCommand ControlReleaseCommand =>
  274. _ControlReleaseCommand ?? (_ControlReleaseCommand = new DelegateCommand(ControlReleaseAction));
  275. #endregion
  276. #region 构造函数
  277. public TopViewModel()
  278. {
  279. string ipConfigFilter = QueryDataClient.Instance.Service.GetConfig($"System.ControlIPFilter").ToString();
  280. string hostName = Dns.GetHostName();
  281. IPHostEntry host = Dns.GetHostEntry(hostName);
  282. foreach (IPAddress ip in host.AddressList)
  283. {
  284. if (ip.AddressFamily == AddressFamily.InterNetwork && ip.ToString().Trim().StartsWith($"{ipConfigFilter}"))
  285. {
  286. localIPs.Add(ip.ToString());
  287. }
  288. }
  289. ToolID = QueryDataClient.Instance.Service.GetConfig($"System.ToolID").ToString();
  290. m_SoftwareVersion = System.Reflection.Assembly.GetEntryAssembly().GetName().Version.ToString();
  291. Dictionary<string,object> allModulesDictionary = QueryDataClient.Instance.Service.PollData(new List<string>() { "System.InstalledModules" });
  292. if (allModulesDictionary != null)
  293. {
  294. List<string> allModules = CommonFunction.GetValue<List<string>>(allModulesDictionary, "System.InstalledModules");
  295. EFEMIsInstalled = allModules.Contains("EFEM");
  296. VPW1IsInstalled = allModules.Contains("VPW1");
  297. VPW2IsInstalled = allModules.Contains("VPW2");
  298. PlatingCell1IsInstalled = allModules.Contains("PlatingCell1");
  299. PlatingCell2IsInstalled = allModules.Contains("PlatingCell2");
  300. PlatingCell3IsInstalled = allModules.Contains("PlatingCell3");
  301. PlatingCell4IsInstalled = allModules.Contains("PlatingCell4");
  302. Srd1IsInstalled = allModules.Contains("SRD1");
  303. Srd2IsInstalled = allModules.Contains("SRD2");
  304. }
  305. //ModuleName = "PMA";
  306. AddDataKeys();
  307. DispatcherTimer timer = new DispatcherTimer();
  308. timer.Interval = TimeSpan.FromSeconds(0.5);
  309. timer.Tick += timer_Tick;
  310. timer.Start();
  311. EventClient.Instance.OnEvent += Instance_OnEvent;
  312. EventClient.Instance.Start();
  313. Title = QueryDataClient.Instance.Service.GetConfig($"System.Name").ToString();
  314. UserName = GlobalUser.Instance.User.Name;
  315. }
  316. #endregion
  317. #region 方法
  318. void timer_Tick(object sender, EventArgs e)
  319. {
  320. TimeTick = DateTime.Now.ToString();
  321. RtDataValues = QueryDataClient.Instance.Service.PollData(m_RtDataKeys);
  322. if (RtDataValues == null)
  323. {
  324. return;
  325. }
  326. if( RtDataValues != null )
  327. {
  328. SignalTowerData = CommonFunction.GetValue<AITSignalTowerData>(RtDataValues, $"System.SignalTower.DeviceData");
  329. HostCommunicationStatus = CommonFunction.GetValue<String>(RtDataValues, "System.CommunicationStatus");
  330. }
  331. //IsEnableEnable = FACommunicationState == CommunicationState.Disabled;
  332. //IsEnableDisable = FACommunicationState != CommunicationState.Disabled;
  333. HostBack = HostCommunicationStatusBackground;
  334. IsEnableEnable = (HostCommunicationStatus == FACommunicationState.Disabled.ToString());
  335. IsEnableDisable = (HostCommunicationStatus != FACommunicationState.Disabled.ToString());
  336. SystemControlIP = CommonFunction.GetValue<string>(RtDataValues, $"System.SystemControlIp");
  337. if (localIPs.Count > 0 && localIPs[0].Equals(SystemControlIP))
  338. {
  339. ControlPermission.Instance.Permission = true;
  340. IsControlPermission = true;
  341. }
  342. else
  343. {
  344. ControlPermission.Instance.Permission = false;
  345. IsControlPermission = false;
  346. }
  347. }
  348. private void OnSwitchLanguage()
  349. {
  350. List<ResourceDictionary> dictionaryList = new List<ResourceDictionary>();
  351. foreach (ResourceDictionary dictionary in Application.Current.Resources.MergedDictionaries)
  352. {
  353. if (dictionary.Source != null)
  354. {
  355. dictionaryList.Add(dictionary);
  356. }
  357. }
  358. string requestedCulture1 = @"/PunkHPX8_Themes;component/Languages/StringResources.en-US.xaml";
  359. string requestedCulture2 = @"/PunkHPX8_Themes;component/Languages/StringResources.zh-CN.xaml";
  360. ResourceDictionary resourceDictionary1 = dictionaryList.FirstOrDefault(d => d.Source.OriginalString.Equals(requestedCulture1));
  361. ResourceDictionary resourceDictionary2 = dictionaryList.FirstOrDefault(d => d.Source.OriginalString.Equals(requestedCulture2));
  362. if (dictionaryList.IndexOf(resourceDictionary1) < dictionaryList.IndexOf(resourceDictionary2))
  363. {
  364. Application.Current.Resources.MergedDictionaries.Remove(resourceDictionary1);
  365. Application.Current.Resources.MergedDictionaries.Add(resourceDictionary1);
  366. }
  367. else
  368. {
  369. Application.Current.Resources.MergedDictionaries.Remove(resourceDictionary2);
  370. Application.Current.Resources.MergedDictionaries.Add(resourceDictionary2);
  371. }
  372. }
  373. private void OnReset()
  374. {
  375. InvokeClient.Instance.Service.DoOperation("System.Reset");
  376. }
  377. private void AddDataKeys()
  378. {
  379. if (EFEMIsInstalled == true)
  380. {
  381. m_RtDataKeys.Add("EFEM.FsmState");
  382. }
  383. if(VPW1IsInstalled)
  384. {
  385. m_RtDataKeys.Add("VPW1.FsmState");
  386. }
  387. if(VPW2IsInstalled)
  388. {
  389. m_RtDataKeys.Add("VPW2.FsmState");
  390. }
  391. if (PlatingCell1IsInstalled)
  392. {
  393. m_RtDataKeys.Add("PlatingCell1.FsmState");
  394. }
  395. if (PlatingCell2IsInstalled)
  396. {
  397. m_RtDataKeys.Add("PlatingCell2.FsmState");
  398. }
  399. if (PlatingCell3IsInstalled)
  400. {
  401. m_RtDataKeys.Add("PlatingCell3.FsmState");
  402. }
  403. if (PlatingCell4IsInstalled)
  404. {
  405. m_RtDataKeys.Add("PlatingCell4.FsmState");
  406. }
  407. if (Srd1IsInstalled)
  408. {
  409. m_RtDataKeys.Add("SRD1.FsmState");
  410. }
  411. if(Srd2IsInstalled)
  412. {
  413. m_RtDataKeys.Add("SRD2.FsmState");
  414. }
  415. m_RtDataKeys.Add("SYSTEM.FsmState");
  416. m_RtDataKeys.Add("System.IsAutoMode");
  417. m_RtDataKeys.Add($"System.SignalTower.DeviceData");
  418. m_RtDataKeys.Add("System.CommunicationStatus");
  419. m_RtDataKeys.Add("System.SystemControlIp");
  420. }
  421. private void Instance_OnEvent(EventItem eventItem)
  422. {
  423. Unity.GlobalEvents.Instance.OnEventItemRaiseChanged(eventItem);
  424. switch (eventItem.Type)
  425. {
  426. case EventType.EventUI_Notify:
  427. Application.Current.Dispatcher.Invoke(delegate
  428. {
  429. EventLogList.Insert(0, eventItem);
  430. if (EventLogList.Count > logMaxCount)
  431. {
  432. EventLogList.RemoveAt(EventLogList.Count - 1);
  433. }
  434. if (eventItem.Level == EventLevel.Alarm)
  435. {
  436. alarmQuery.Enqueue(eventItem);
  437. //if (stopEntityId.Contains(eventItem.Id) && eventItem.Source.Contains("PM"))
  438. //{
  439. // InvokeClient.Instance.Service.DoOperation($"{eventItem.Source}.PmError");
  440. //}
  441. }
  442. if (alarmQuery.Count > 0)
  443. {
  444. CurrentEventItem = alarmQuery.Last();
  445. }
  446. else
  447. {
  448. CurrentEventItem = eventItem;
  449. }
  450. EventLogListSelectedIndex = 0;
  451. });
  452. break;
  453. case EventType.Dialog_Nofity:
  454. //PopDialog(obj);
  455. break;
  456. case EventType.KickOut_Notify:
  457. break;
  458. case EventType.Sound_Notify:
  459. break;
  460. case EventType.UIMessage_Notify:
  461. //PopUIMessage(obj);
  462. break;
  463. }
  464. }
  465. private void OnClear()
  466. {
  467. CurrentEventItem = null;
  468. alarmQuery.Clear();
  469. EventLogList.Clear();
  470. }
  471. private void OnSkip()
  472. {
  473. if (alarmQuery.Count > 0)
  474. {
  475. alarmQuery.Dequeue();
  476. if (alarmQuery.Count > 0)
  477. {
  478. CurrentEventItem = alarmQuery.First();
  479. }
  480. else
  481. {
  482. CurrentEventItem = null;
  483. }
  484. }
  485. }
  486. //private void OnBuzzerOff()
  487. //{
  488. // InvokeClient.Instance.Service.DoOperation($"{ModuleName}.SignalTower.SwitchOffBuzzer");
  489. //}
  490. public void FaDisable()
  491. {
  492. InvokeClient.Instance.Service.DoOperation($"FACommand", "FADisable");
  493. }
  494. public void FaEnable()
  495. {
  496. InvokeClient.Instance.Service.DoOperation($"FACommand", "FAEnable");
  497. }
  498. /// <summary>
  499. /// Logout
  500. /// </summary>
  501. private void OnLogout()
  502. {
  503. var _mainWindow = Application.Current.Windows.Cast<Window>().FirstOrDefault(window => window is Window) as Window;
  504. LogoutView heaterView = new LogoutView();
  505. heaterView.WindowStartupLocation = WindowStartupLocation.CenterScreen;
  506. heaterView.Owner = _mainWindow;
  507. heaterView.Show();
  508. }
  509. /// <summary>
  510. /// ControPermissionAction
  511. /// </summary>
  512. private void ControlPermissionAction()
  513. {
  514. if (localIPs.Count > 0)
  515. {
  516. InvokeClient.Instance.Service.DoOperation($"ApplySystemControl", localIPs[0]);
  517. }
  518. else
  519. {
  520. InvokeClient.Instance.Service.DoOperation($"ApplySystemControl","");
  521. }
  522. }
  523. /// <summary>
  524. /// ControPermissionRelease
  525. /// </summary>
  526. private void ControlReleaseAction()
  527. {
  528. if (localIPs.Count > 0)
  529. {
  530. InvokeClient.Instance.Service.DoOperation($"ReleaseSystemControl", localIPs[0]);
  531. }
  532. }
  533. #endregion
  534. }
  535. }