using Aitex.Core.RT.Event; using Aitex.Core.WCF; using MECF.Framework.Common.DataCenter; 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; namespace Venus_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; //public ObservableCollection m_WarnEventLogList=new ObservableCollection (); 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; #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 Dictionary RtDataValues { get { return m_RtDataValues; } set { SetProperty(ref m_RtDataValues, value); } } //public ObservableCollection WarnEventLogList //{ // get { return m_WarnEventLogList; } // set { SetProperty(ref m_WarnEventLogList, 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); } } #endregion #region 命令 private DelegateCommand _SwichLanguageCommand; public DelegateCommand SwichLanguageCommand => _SwichLanguageCommand ?? (_SwichLanguageCommand = new DelegateCommand(OnSwitchLanguage)); #endregion #region 构造函数 public TopViewModel() { Title = "Venus"; m_SoftwareVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(); ModuleName = "PMA"; addDataKeys(); DispatcherTimer timer = new DispatcherTimer(); timer.Interval = TimeSpan.FromSeconds(1); timer.Tick += timer_Tick; timer.Start(); EventClient.Instance.OnEvent += Instance_OnEvent; EventClient.Instance.Start(); } #endregion #region 方法 void timer_Tick(object sender, EventArgs e) { RtDataValues = QueryDataClient.Instance.Service.PollData(m_RtDataKeys); } private void OnSwitchLanguage() { List dictionaryList = new List(); foreach (ResourceDictionary dictionary in Application.Current.Resources.MergedDictionaries) { if (dictionary.Source != null) { dictionaryList.Add(dictionary); } } string requestedCulture1 = @"/Venus_Themes;component/Languages/StringResources.en-US.xaml"; string requestedCulture2 = @"/Venus_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 addDataKeys() { m_RtDataKeys.Add($"{ModuleName}.FsmState"); m_RtDataKeys.Add($"{ModuleName}.SignalTower.IsRedLightOn"); m_RtDataKeys.Add($"{ModuleName}.SignalTower.IsYellowLightOn"); m_RtDataKeys.Add($"{ModuleName}.SignalTower.IsGreenLightOn"); m_RtDataKeys.Add($"{ModuleName}.SignalTower.IsBlueLightOn"); m_RtDataKeys.Add($"{ModuleName}.SignalTower.IsBuzzerOn"); } private void Instance_OnEvent(EventItem eventItem) { //lock (_lockObj) //{ // ThreadPool.QueueUserWorkItem(delegate // { // CurrentEventItem = eventItem; // }); //} Application.Current.Dispatcher.Invoke(delegate { EventLogList.Add(eventItem); CurrentEventItem = eventItem; EventLogListSelectedIndex = EventLogList.Count - 1; //CurrentEventItemValue = eventItem.Description; }); } #endregion } }