using Aitex.Core.UI.MVVM; using Aitex.Core.Util; using Aitex.Core.Utilities; using MECF.Framework.Common.DataCenter; using MECF.Framework.Common.Equipment; using MECF.Framework.Common.OperationCenter; using OpenSEMI.ClientBase; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; using Aitex.Core.RT.Log; using MessageBox = Xceed.Wpf.Toolkit.MessageBox; using PageSCValue = MECF.Framework.UI.Core.View.Common.PageSCValue; using Prism.Mvvm; namespace CyberX8_MainPages.ViewModels { public enum FACommunicationState { Disabled, Enabled, EnabledNotCommunicating, EnabledCommunicating, WaitCRA, WaitDelay, WaitCRFromHost, } public enum FAControlState { Unknown, EquipmentOffline, AttemptOnline, HostOffline, OnlineLocal, OnlineRemote, } public enum FAControlSubState { Local, Remote, } public enum FASpoolingState { Active = 1, Inactive } public class FACommandName { public const string FAEnable = "FAEnable"; public const string FADisable = "FADisable"; public const string FAOnline = "FAOnline"; public const string FAOffline = "FAOffline"; public const string FALocal = "FALocal"; public const string FARemote = "FARemote"; public const string FAEnableSpooling = "FAEnableSpooling"; public const string FADisableSpooling = "FADisableSpooling"; public const string FASendTerminalMessage = "FASendTerminalMessage"; } public class FAViewModel : BindableBase { protected ConcurrentBag _subscribedKeys = new ConcurrentBag(); protected Func _isSubscriptionAttribute; protected Func _hasSubscriptionAttribute; private class DataName { public const string CommunicationStatus = "CommunicationStatus"; public const string ControlStatus = "ControlStatus"; public const string ControlSubStatus = "ControlSubStatus"; public const string SpoolingState = "SpoolingState"; public const string SpoolingActual = "SpoolingActual"; public const string SpoolingTotal = "SpoolingTotal"; public const string SpoolingFullTime = "SpoolingFullTime"; public const string SpoolingStartTime = "SpoolingStartTime"; public const string IsSpoolingEnable = "IsSpoolingEnable"; //EFEM } public FAViewModel() { ConfigFeedback = new PageSCFA(); ConfigSetPoint = new PageSCFA(); SetConfigCommand = new DelegateCommand(SetConfig); InvokeCommand = new DelegateCommand(Invoke); Initialize(); } protected void Initialize() { _isSubscriptionAttribute = attribute => attribute is SubscriptionAttribute; _hasSubscriptionAttribute = mi => mi.GetCustomAttributes(false).Any(_isSubscriptionAttribute); Parallel.ForEach(this.GetType().GetProperties().Where(_hasSubscriptionAttribute), property => { SubscriptionAttribute subscription = property.GetCustomAttributes(false).First(_isSubscriptionAttribute) as SubscriptionAttribute; string key = subscription.ModuleKey; if (!_subscribedKeys.Contains(key)) _subscribedKeys.Add(key); }); _timer = new PeriodicJob(200, this.OnTimer, "UIUpdaterThread - " + GetType().Name, true); ConfigSetPoint.Update(QueryDataClient.Instance.Service.PollConfig(ConfigSetPoint.GetKeys())); } #region subscrib [Subscription(DataName.IsSpoolingEnable, ModuleNameString.System)] public bool IsSpoolingEnable { get { return _isSpoolingEnable; } set { IsEnableSpoolingEnableButton = !value; IsEnableSpoolingDisableButton = value; _isSpoolingEnable = value; } } private bool _isSpoolingEnable = false; [Subscription("System.SpoolingState")] public string SpoolingState { get; set; } [Subscription(DataName.SpoolingActual, ModuleNameString.System)] public string SpoolingActual { get; set; } [Subscription(DataName.SpoolingTotal, ModuleNameString.System)] public string SpoolingTotal { get; set; } [Subscription(DataName.SpoolingFullTime, ModuleNameString.System)] public string SpoolingFullTime { get; set; } [Subscription(DataName.SpoolingStartTime, ModuleNameString.System)] public string SpoolingStartTime { get; set; } [Subscription(DataName.ControlStatus, ModuleNameString.System)] public string HostControlStatus { get { return _hostControlStatus; } set { IsEnableOnlineButton = FACommunicationState == FACommunicationState.EnabledCommunicating && (value == FAControlState.Unknown.ToString() || value == FAControlState.EquipmentOffline.ToString()); IsEnableOfflineButton = FACommunicationState == FACommunicationState.EnabledCommunicating && (value == FAControlState.Unknown.ToString() || value != FAControlState.EquipmentOffline.ToString()); IsEnableRemoteButton = FACommunicationState == FACommunicationState.EnabledCommunicating&&value==FAControlState.OnlineLocal.ToString(); IsEnableLocalButton = FACommunicationState == FACommunicationState.EnabledCommunicating && value == FAControlState.OnlineRemote.ToString(); _hostControlStatus = value; } } private string _hostControlStatus; [Subscription(DataName.CommunicationStatus, ModuleNameString.System)] public string HostCommunicationStatus { get { return _hostCommunicationStatus; } set { if(_hostCommunicationStatus == FACommunicationState.Disabled.ToString()) { IsEnableEnableButton = true; IsEnableDisableButton = false; } else { IsEnableEnableButton = false; IsEnableDisableButton = true; } _hostCommunicationStatus = value; } } private string _hostCommunicationStatus; #endregion #region logic public FACommunicationState FACommunicationState { get { return string.IsNullOrEmpty(HostCommunicationStatus) ? FACommunicationState.Disabled : (FACommunicationState)Enum.Parse(typeof(FACommunicationState), HostCommunicationStatus); } } public FAControlState FAControlState { get { return string.IsNullOrEmpty(HostControlStatus) ? FAControlState.Unknown : (FAControlState)Enum.Parse(typeof(FAControlState), HostControlStatus); } } //Disabled, //Enabled, //EnabledNotCommunicating, //EnabledCommunicating, //WaitCRA, //WaitDelay, //WaitCRFromHost, public bool IsEnableEnableButton { get { return _isEnableEnableButton; } set { SetProperty(ref _isEnableEnableButton, value); } } private bool _isEnableEnableButton = false; public bool IsEnableDisableButton { get { return _isEnableDisableButton; } set { SetProperty(ref _isEnableDisableButton, value); } } private bool _isEnableDisableButton; //Unknown, //EquipmentOffline, //AttemptOnline, //HostOffline, //OnlineLocal, //OnlineRemote, public bool IsEnableOnlineButton { get { return _isEnableOnlineButton; } set { SetProperty(ref _isEnableOnlineButton, value); } } private bool _isEnableOnlineButton; public bool IsEnableOfflineButton { get { return _isEnableOfflineButton; } set { SetProperty(ref _isEnableOfflineButton, value); } } private bool _isEnableOfflineButton; public bool IsEnableLocalButton { get { return _isEnableLocalButton; } set { SetProperty(ref _isEnableLocalButton, value); } } private bool _isEnableLocalButton; public bool IsEnableRemoteButton { get { return _isEnableRemoteButton; } set { SetProperty(ref _isEnableRemoteButton,value); } } private bool _isEnableRemoteButton; public bool IsEnableSpoolingEnableButton { get { return _isEnableSpoolingEnableButton;//SpoolingState == FASpoolingState.Inactive.ToString(); } set { SetProperty(ref _isEnableSpoolingEnableButton, value); } } private bool _isEnableSpoolingEnableButton; public bool IsEnableSpoolingDisableButton { get { return _isEnableSpoolingDisableButton;// SpoolingState == FASpoolingState.Active.ToString(); } set { SetProperty(ref _isEnableSpoolingDisableButton, value); } } private bool _isEnableSpoolingDisableButton; #endregion [IgnorePropertyChange] public PageSCFA ConfigFeedback { get; set; } [IgnorePropertyChange] public PageSCFA ConfigSetPoint { get; set; } [IgnorePropertyChange] public ICommand SetConfigCommand { get; private set; } [IgnorePropertyChange] public ICommand InvokeCommand { get; private set; } PeriodicJob _timer; //protected ConcurrentBag _subscribedKeys = new ConcurrentBag(); //protected Func _isSubscriptionAttribute; //protected Func _hasSubscriptionAttribute; private void Invoke(object param) { InvokeClient.Instance.Service.DoOperation("FACommand", ((string)param).Split(',')[1]); } void SetConfig(object param) { object[] sc = (object[])param; InvokeClient.Instance.Service.DoOperation("System.SetConfig", sc[0].ToString(), sc[1].ToString()); UpdateConfig(); } public void UpdateConfig() { ConfigFeedback.Update(QueryDataClient.Instance.Service.PollConfig(ConfigFeedback.GetKeys())); this.RaisePropertyChanged("ConfigFeedback"); } private bool OnTimer() { try { Poll(); UpdateConfig(); } catch { } return true; } private void Poll() { if (_subscribedKeys.Count > 0) { Dictionary result = QueryDataClient.Instance.Service.PollData(_subscribedKeys); if (result == null) { return; } if (result.Count != _subscribedKeys.Count) { string unknowKeys = string.Empty; foreach (string key in _subscribedKeys) { if (!result.ContainsKey(key)) { unknowKeys += key + "\r\n"; } } //System.Diagnostics.Debug.Assert(false, unknowKeys); } UpdateValue(result); } } void UpdateValue(Dictionary data) { if (data == null) return; UpdateSubscribe(data, this); } void UpdateSubscribe(Dictionary data, object target, string module = null) { Parallel.ForEach(target.GetType().GetProperties().Where(_hasSubscriptionAttribute), property => { PropertyInfo pi = (PropertyInfo)property; SubscriptionAttribute subscription = property.GetCustomAttributes(false).First(_isSubscriptionAttribute) as SubscriptionAttribute; string key = subscription.ModuleKey; key = module == null ? key : string.Format("{0}.{1}", module, key); if (_subscribedKeys.Contains(key) && data.ContainsKey(key)) { try { var convertedValue = Convert.ChangeType(data[key], pi.PropertyType); var originValue = Convert.ChangeType(pi.GetValue(target, null), pi.PropertyType); if (originValue != convertedValue) { pi.SetValue(target, convertedValue, null); RaisePropertyChanged(pi.Name); } } catch { } } }); } } public class PageSCFA : PageSCValue { public string Fa_ConnectionMode { get; set; } public string FA_LocalIpAddress { get; set; } public int FA_LocalPortNumber { get; set; } public string FA_RemoteIpAddress { get; set; } public int FA_RemotePortNumber { get; set; } public int FA_T3Timeout { get; set; } public int FA_T5Timeout { get; set; } public int FA_T6Timeout { get; set; } public int FA_T7Timeout { get; set; } public int FA_T8Timeout { get; set; } public bool FA_EnableSpooling { get; set; } public int FA_LinkTestInterval { get; set; } public string FA_DefaultCommunicationState { get; set; } public string FA_DefaultControlState { get; set; } public string FA_DefaultControlSubState { get; set; } public string FA_DeviceId { get; set; } public int FA_SpoolingCapability { get; set; } public int FA_SpoolingMaxTransmit { get; set; } public string FA_SpoolingFullOverWrite { get; set; } public List FA_SpoolingFullOverWriteLst { get { return new List() { "True", "False" }; } } public List FA_CommunicateStateLst { get { return new List() {"Enabled","Disabled" };} } public List FA_ControlStateLst { get { return new List() { "Online", "Offline" }; } } public List FA_ControlSubStateLst { get { return new List() { "Local", "Remote" }; } } public PageSCFA() { UpdateKeys(typeof(PageSCFA).GetProperties()); } } }