using Aitex.Core.Common.DeviceData; using Aitex.Core.RT.Event; using Aitex.Core.Util; using Aitex.Core.WCF; using MECF.Framework.Common.DataCenter; using MECF.Framework.Common.Equipment; using MECF.Framework.Common.OperationCenter; using Prism.Commands; using Prism.Mvvm; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Threading; using CyberX8_Core; using CyberX8_MainPages.Unity; using CyberX8_Themes.UserControls; using MECF.Framework.Common.Utilities; using CyberX8_MainPages.Views; using System.Net.Sockets; using System.Net; namespace CyberX8_MainPages.ViewModels { internal class TopViewModel : BindableBase { #region 私有字段 private string m_Title; private string m_SoftwareVersion; private List m_RtDataKeys = new List(); private Dictionary m_RtDataValues; //private string ModuleName; private ObservableCollection m_EventLogList = new ObservableCollection(); private EventItem m_CurrentEventItem = new EventItem(); private string m_CurrentEventItemValue; private object _lockObj = new object(); private int m_EventLogListSelectedIndex; private Queue alarmQuery = new Queue();//控制alarm log 在 top UI显示 private int logMaxCount = 50;//log在ui最多显示数量 private AITSignalTowerData m_SignalTowerData; private string m_HostCommunicationStatus; private string m_TimeTick; //private JetChamber m_SelectedJetChamber = JetChamber.None; private bool _EFEMIsInstalled; private bool _PUF1IsInstalled; private bool _PUF2IsInstalled; private bool _loader1Installed; private bool _transporter2Installed; private bool _transporter1Installed; private bool _srd1Installed; private bool _srd2Installed; private List stopEntityId = new List() { 17 }; private string _systemControlIP; private string _toolID; private List localIPs = new List(); private bool _isControlPermission; /// /// 用户名 /// private string _userName; #endregion #region 属性 public string Title { get { return m_Title; } set { SetProperty(ref m_Title, value); } } public string SoftwareVersion { get { return m_SoftwareVersion; } set { SetProperty(ref m_SoftwareVersion, value); } } public string SystemControlIP { get { return _systemControlIP; } set { SetProperty(ref _systemControlIP, value); } } public string ToolID { get { return _toolID; } set { SetProperty(ref _toolID, value); } } public string TimeTick { get { return m_TimeTick; } set { SetProperty(ref m_TimeTick, value); } } public Dictionary RtDataValues { get { return m_RtDataValues; } set { SetProperty(ref m_RtDataValues, value); } } public ObservableCollection EventLogList { get { return m_EventLogList; } set { SetProperty(ref m_EventLogList, value); } } public EventItem CurrentEventItem { get { return m_CurrentEventItem; } set { SetProperty(ref m_CurrentEventItem, value); } } public int EventLogListSelectedIndex { get { return m_EventLogListSelectedIndex; } set { SetProperty(ref m_EventLogListSelectedIndex, value); } } public string CurrentEventItemValue { get { return m_CurrentEventItemValue; } set { SetProperty(ref m_CurrentEventItemValue, value); } } public AITSignalTowerData SignalTowerData { get { return m_SignalTowerData; } set { SetProperty(ref m_SignalTowerData, value); } } public string HostCommunicationStatusBackground { get { switch (HostCommunicationStatus) { case "Disabled": return "Yellow"; case "Enabled": case "EnabledNotCommunicating": case "WaitCRA": case "WaitDelay": case "WaitCRFromHost": return "Transparent"; case "EnabledCommunicating": return "LawnGreen"; default: return "Yellow"; } } } public string m_HostBack; public string HostBack { get { return m_HostBack; } set { SetProperty(ref m_HostBack, value); } } [Subscription("System.CommunicationStatus")] public string HostCommunicationStatus { get { return m_HostCommunicationStatus; } set { SetProperty(ref m_HostCommunicationStatus, value); } } public bool IsEnableFAEnable { get { return HostCommunicationStatus == "Disabled"; } } public bool _IsEnableEnable; public bool IsEnableEnable { get { return _IsEnableEnable; } set { SetProperty(ref _IsEnableEnable, value); } } public bool _IsEnableDisable; public bool IsEnableDisable { get { return _IsEnableDisable; } set { SetProperty(ref _IsEnableDisable, value); } } public FACommunicationState FACommunicationState { get { return string.IsNullOrEmpty(HostCommunicationStatus) ? FACommunicationState.Disabled : (FACommunicationState)Enum.Parse(typeof(FACommunicationState), HostCommunicationStatus); } } public bool IsControlPermission { get { return _isControlPermission; } set { SetProperty(ref _isControlPermission, value); } } public bool EFEMIsInstalled { get { return _EFEMIsInstalled; } set { SetProperty(ref _EFEMIsInstalled, value); } } public bool PUF1IsInstalled { get { return _PUF1IsInstalled; } set { SetProperty(ref _PUF1IsInstalled, value); } } public bool PUF2IsInstalled { get { return _PUF2IsInstalled; } set { SetProperty(ref _PUF2IsInstalled, value); } } public bool Loader1IsInstalled { get { return _loader1Installed; } set { SetProperty(ref _loader1Installed, value); } } public bool Transporter2IsInstalled { get { return _transporter2Installed; } set { SetProperty(ref _transporter2Installed, value); } } public bool Transporter1IsInstalled { get { return _transporter1Installed; } set { SetProperty(ref _transporter1Installed, value); } } public bool Srd1IsInstalled { get { return _srd1Installed; } set { SetProperty(ref _srd1Installed, value); } } public bool Srd2IsInstalled { get { return _srd2Installed; } set { SetProperty(ref _srd2Installed, value); } } /// /// 用户名 /// public string UserName { get { return _userName; } set { SetProperty(ref _userName, value); } } #endregion #region 命令 private DelegateCommand _SwichLanguageCommand; public DelegateCommand SwichLanguageCommand => _SwichLanguageCommand ?? (_SwichLanguageCommand = new DelegateCommand(OnSwitchLanguage)); private DelegateCommand _ResetCommand; public DelegateCommand ResetCommand => _ResetCommand ?? (_ResetCommand = new DelegateCommand(OnReset)); private DelegateCommand _ClearCommand; public DelegateCommand ClearCommand => _ClearCommand ?? (_ClearCommand = new DelegateCommand(OnClear)); private DelegateCommand _SkipCommand; public DelegateCommand SkipCommand => _SkipCommand ?? (_SkipCommand = new DelegateCommand(OnSkip)); //private DelegateCommand _BuzzerOffCommand; //public DelegateCommand BuzzerOffCommand => // _BuzzerOffCommand ?? (_BuzzerOffCommand = new DelegateCommand(OnBuzzerOff)); private DelegateCommand _FADisableCommand; public DelegateCommand FADisableCommand => _FADisableCommand ?? (_FADisableCommand = new DelegateCommand(FaDisable)); private DelegateCommand _FAEnableCommand; public DelegateCommand FAEnableCommand => _FAEnableCommand ?? (_FAEnableCommand = new DelegateCommand(FaEnable)); private DelegateCommand _LogoutCommand; public DelegateCommand LogoutCommand => _LogoutCommand ?? (_LogoutCommand = new DelegateCommand(OnLogout)); private DelegateCommand _ControlPermissionCommand; public DelegateCommand ControlPermissionCommand => _ControlPermissionCommand ?? (_ControlPermissionCommand = new DelegateCommand(ControlPermissionAction)); private DelegateCommand _ControlReleaseCommand; public DelegateCommand ControlReleaseCommand => _ControlReleaseCommand ?? (_ControlReleaseCommand = new DelegateCommand(ControlReleaseAction)); #endregion #region 构造函数 public TopViewModel() { string ipConfigFilter = QueryDataClient.Instance.Service.GetConfig($"System.ControlIPFilter").ToString(); string hostName = Dns.GetHostName(); IPHostEntry host = Dns.GetHostEntry(hostName); foreach (IPAddress ip in host.AddressList) { if (ip.AddressFamily == AddressFamily.InterNetwork && ip.ToString().Trim().StartsWith($"{ipConfigFilter}")) { localIPs.Add(ip.ToString()); } } ToolID = QueryDataClient.Instance.Service.GetConfig($"System.ToolID").ToString(); m_SoftwareVersion = System.Reflection.Assembly.GetEntryAssembly().GetName().Version.ToString(); Dictionary allModulesDictionary = QueryDataClient.Instance.Service.PollData(new List() { "System.InstalledModules" }); if (allModulesDictionary != null) { List allModules = CommonFunction.GetValue>(allModulesDictionary, "System.InstalledModules"); EFEMIsInstalled = allModules.Contains("EFEM"); PUF1IsInstalled = allModules.Contains("PUF1"); PUF2IsInstalled = allModules.Contains("PUF2"); Loader1IsInstalled = allModules.Contains("Loader1"); Transporter1IsInstalled = allModules.Contains("Transporter1"); Transporter2IsInstalled = allModules.Contains("Transporter2"); Srd1IsInstalled = allModules.Contains("SRD1"); Srd2IsInstalled = allModules.Contains("SRD2"); } //ModuleName = "PMA"; AddDataKeys(); DispatcherTimer timer = new DispatcherTimer(); timer.Interval = TimeSpan.FromSeconds(0.5); timer.Tick += timer_Tick; timer.Start(); EventClient.Instance.OnEvent += Instance_OnEvent; EventClient.Instance.Start(); Title = QueryDataClient.Instance.Service.GetConfig($"System.Name").ToString(); UserName = GlobalUser.Instance.User.Name; } #endregion #region 方法 void timer_Tick(object sender, EventArgs e) { TimeTick = DateTime.Now.ToString(); RtDataValues = QueryDataClient.Instance.Service.PollData(m_RtDataKeys); if( RtDataValues != null ) { SignalTowerData = CommonFunction.GetValue(RtDataValues, $"System.SignalTower.DeviceData"); HostCommunicationStatus = CommonFunction.GetValue(RtDataValues, "System.CommunicationStatus"); } //IsEnableEnable = FACommunicationState == CommunicationState.Disabled; //IsEnableDisable = FACommunicationState != CommunicationState.Disabled; HostBack = HostCommunicationStatusBackground; IsEnableEnable = (HostCommunicationStatus == FACommunicationState.Disabled.ToString()); IsEnableDisable = (HostCommunicationStatus != FACommunicationState.Disabled.ToString()); SystemControlIP = CommonFunction.GetValue(RtDataValues, $"System.SystemControlIp"); if (localIPs.Count > 0 && localIPs[0].Equals(SystemControlIP)) { ControlPermission.Instance.Permission = true; IsControlPermission = true; } else { ControlPermission.Instance.Permission = false; IsControlPermission = false; } } private void OnSwitchLanguage() { List dictionaryList = new List(); foreach (ResourceDictionary dictionary in Application.Current.Resources.MergedDictionaries) { if (dictionary.Source != null) { dictionaryList.Add(dictionary); } } string requestedCulture1 = @"/CyberX8_Themes;component/Languages/StringResources.en-US.xaml"; string requestedCulture2 = @"/CyberX8_Themes;component/Languages/StringResources.zh-CN.xaml"; ResourceDictionary resourceDictionary1 = dictionaryList.FirstOrDefault(d => d.Source.OriginalString.Equals(requestedCulture1)); ResourceDictionary resourceDictionary2 = dictionaryList.FirstOrDefault(d => d.Source.OriginalString.Equals(requestedCulture2)); if (dictionaryList.IndexOf(resourceDictionary1) < dictionaryList.IndexOf(resourceDictionary2)) { Application.Current.Resources.MergedDictionaries.Remove(resourceDictionary1); Application.Current.Resources.MergedDictionaries.Add(resourceDictionary1); } else { Application.Current.Resources.MergedDictionaries.Remove(resourceDictionary2); Application.Current.Resources.MergedDictionaries.Add(resourceDictionary2); } } private void OnReset() { InvokeClient.Instance.Service.DoOperation("System.Reset"); } private void AddDataKeys() { if (EFEMIsInstalled == true) { m_RtDataKeys.Add("EFEM.FsmState"); } if(PUF1IsInstalled) { m_RtDataKeys.Add("PUF1.FsmState"); } if (PUF2IsInstalled) { m_RtDataKeys.Add("PUF2.FsmState"); } if(Loader1IsInstalled) { m_RtDataKeys.Add("Loader1.FsmState"); } if(Transporter1IsInstalled) { m_RtDataKeys.Add("Transporter1.FsmState"); } if(Transporter2IsInstalled) { m_RtDataKeys.Add("Transporter2.FsmState"); } if(Srd1IsInstalled) { m_RtDataKeys.Add("SRD1.FsmState"); } if(Srd2IsInstalled) { m_RtDataKeys.Add("SRD2.FsmState"); } m_RtDataKeys.Add("SYSTEM.FsmState"); m_RtDataKeys.Add("System.IsAutoMode"); m_RtDataKeys.Add($"System.SignalTower.DeviceData"); m_RtDataKeys.Add("System.CommunicationStatus"); m_RtDataKeys.Add("System.SystemControlIp"); } private void Instance_OnEvent(EventItem eventItem) { Unity.GlobalEvents.Instance.OnEventItemRaiseChanged(eventItem); switch (eventItem.Type) { case EventType.EventUI_Notify: Application.Current.Dispatcher.Invoke(delegate { EventLogList.Insert(0, eventItem); if (EventLogList.Count > logMaxCount) { EventLogList.RemoveAt(EventLogList.Count - 1); } if (eventItem.Level == EventLevel.Alarm) { alarmQuery.Enqueue(eventItem); //if (stopEntityId.Contains(eventItem.Id) && eventItem.Source.Contains("PM")) //{ // InvokeClient.Instance.Service.DoOperation($"{eventItem.Source}.PmError"); //} } if (alarmQuery.Count > 0) { CurrentEventItem = alarmQuery.Last(); } else { CurrentEventItem = eventItem; } EventLogListSelectedIndex = 0; }); break; case EventType.Dialog_Nofity: //PopDialog(obj); break; case EventType.KickOut_Notify: break; case EventType.Sound_Notify: break; case EventType.UIMessage_Notify: //PopUIMessage(obj); break; } } private void OnClear() { CurrentEventItem = null; alarmQuery.Clear(); EventLogList.Clear(); } private void OnSkip() { if (alarmQuery.Count > 0) { alarmQuery.Dequeue(); if (alarmQuery.Count > 0) { CurrentEventItem = alarmQuery.First(); } else { CurrentEventItem = null; } } } //private void OnBuzzerOff() //{ // InvokeClient.Instance.Service.DoOperation($"{ModuleName}.SignalTower.SwitchOffBuzzer"); //} public void FaDisable() { InvokeClient.Instance.Service.DoOperation($"FACommand", "FADisable"); } public void FaEnable() { InvokeClient.Instance.Service.DoOperation($"FACommand", "FAEnable"); } /// /// Logout /// private void OnLogout() { var _mainWindow = Application.Current.Windows.Cast().FirstOrDefault(window => window is Window) as Window; LogoutView heaterView = new LogoutView(); heaterView.WindowStartupLocation = WindowStartupLocation.CenterScreen; heaterView.Owner = _mainWindow; heaterView.Show(); } /// /// ControPermissionAction /// private void ControlPermissionAction() { if (localIPs.Count > 0) { InvokeClient.Instance.Service.DoOperation($"ApplySystemControl", localIPs[0]); } else { InvokeClient.Instance.Service.DoOperation($"ApplySystemControl",""); } } /// /// ControPermissionRelease /// private void ControlReleaseAction() { if (localIPs.Count > 0) { InvokeClient.Instance.Service.DoOperation($"ReleaseSystemControl", localIPs[0]); } } #endregion } }