TopViewModel.cs 21 KB

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