using Aitex.Core.Common.DeviceData; using Aitex.Core.RT.Event; using Aitex.Core.UI.Control; using Aitex.Core.UI.Dialog; using Aitex.Core.UI.MVVM; using Aitex.Core.Util; using Aitex.Core.Utilities; using Aitex.Core.WCF; using Aitex.Sorter.Common; using Hardcodet.Wpf.TaskbarNotification; using MECF.Framework.Common.CommonData; using MECF.Framework.Common.OperationCenter; using MECF.Framework.UI.Core.Accounts; using MECF.Framework.UI.Core.Applications; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; namespace Aitex.Sorter.UI.ViewModel { public class TerminalMessageItem : NotifiableItem { public string MessageTime { get; set; } public string Type { get; set; } public string Content { get; set; } public string Title { get { return MessageTime + " (" + Type + ")"; } } } public class TopViewSubModel : UIViewModelBase { private int loadPortCount = 10; public string SoftwareVersion { get; set; } private string companyName; public string CompanyName { get { return companyName; } set { companyName=value; InvokePropertyChanged("CompanyName"); } } private string showDateTime; public string ShowDateTime { get { return showDateTime; } set { showDateTime = value; InvokePropertyChanged("ShowDateTime"); } } [Subscription(ParamName.ControlStatus, SystemStateModule)] public string HostControlStatus { get; set; } public string HostControlStatusBackground { get { switch (HostControlStatus) { case "Unknown": return "Yellow"; case "EquipmentOffline": case "AttemptOnline": case "HostOffline": return "Transparent"; case "OnlineLocal": case "OnlineRemote": return "LawnGreen"; default: return "Yellow"; } } } [Subscription(ParamName.CommunicationStatus, SystemStateModule)] public string HostCommunicationStatus { get; set; } /// Disabled = 0, /// Enabled = 1, /// EnabledNotCommunicating = 2, /// EnabledCommunicating = 3, /// WaitCRA = 4, /// WaitDelay = 5, /// WaitCRFromHost = 6, 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"; } } } [Subscription(ParamName.RTStatus, SystemStateModule)] public int RoutManagerState { get; set; } public string SystemStatus { get { return ((RtState)RoutManagerState).ToString(); } } public string SystemStatusBackground { get { switch (RoutManagerState) { case (int)RtState.Init: return "Yellow"; case (int)RtState.Initializing: case (int)RtState.ManualTransfer: case (int)RtState.Transfer: case (int)RtState.Cycle: case (int)RtState.PrepareCycle: case (int)RtState.PostCycle: case (int)RtState.Reset: case (int)RtState.Processing: case (int)RtState.PostProcess: case (int)RtState.PauseProcess: case (int)RtState.SetSpeed: return "LawnGreen"; case (int)RtState.Error: case (int)RtState.Maintenance: return "Red"; case (int)RtState.Idle: return "Transparent"; default: return "Goldenrod"; } } } protected override void Poll() { for (int i = 0; i < loadPortCount; i++) { Subscribe($"LP{i+1}.LoadportState"); } ShowDateTime = DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss"); base.Poll(); } public string TerminalBackground { get { return _notifyTerminalMessageArrive ? "LawnGreen" : "#0086cd"; } } [Subscription(ParamName.AlignerState, DeviceName.Aligner)] public string AlignerStatus { get; set; } public string AlignerStatusBackground { get { return GetUnitStatusBackground(AlignerStatus); } } [Subscription(ParamName.RobotState, DeviceName.Robot)] public string RobotStatus { get; set; } public string RobotStatusBackground { get { return GetUnitStatusBackground(RobotStatus); } } [Subscription("System.TurnoverStation.State")] public string TurnoverStatus { get; set; } public string TurnoverStatusBackground { get { return GetUnitStatusBackground(TurnoverStatus); } } [Subscription("System.SignalTower.DeviceData")] public AITSignalTowerData SignalTower { get; set; } [Subscription(ParamName.IsAutoMode, SystemStateModule)] public bool IsAutoMode { get; set; } private bool _simulatorMode = false; public bool IsManualButtonEnabled { get { return IsAutoMode; } } public bool IsAutoButtonEnabled { get { return !IsAutoMode; } } public string GetUnitStatusBackground(string status) { if (status != null) status = status.Trim().ToLower(); switch (status) { case "unknown": return "Yellow"; case "idle": return "Transparent"; case "busy": return "LawnGreen"; default: return "Yellow"; } } public string TopViewBackground { get { return _simulatorMode ? "Goldenrod" : "Transparent"; } } public string User { get { return AccountClient.Instance.CurrentUser == null ? "" : AccountClient.Instance.CurrentUser.AccountId; } } public string Role { get { return AccountClient.Instance.CurrentUser == null ? "" : AccountClient.Instance.CurrentUser.Role; } } public ICommand SwitchOffBuzzerCommand { get; set; } public ICommand ResetCommand { get; set; } public ICommand LogoffCommand { get; set; } public ICommand TerminalCommand { get; set; } public ICommand TerminalSendCommand { get; set; } public ICommand TerminalClearCommand { get; set; } public bool IsManualMode { get { return !IsAutoMode; } } public FACommunicationState FACommunicationState { get { return string.IsNullOrEmpty(HostCommunicationStatus) ? FACommunicationState.Disabled : (FACommunicationState)Enum.Parse(typeof(FACommunicationState), HostCommunicationStatus); } } public bool IsFAEnableButtonEnabled { get { return FACommunicationState == FACommunicationState.Disabled; } } public bool IsFADisableButtonEnabled { get { return FACommunicationState != FACommunicationState.Disabled; } } MessageControl _balloonControl; private bool _logoff; public ObservableCollection EventLogList { get; set; } public ObservableCollection TerminalMessageList { get; set; } public object _lockMessage = new object(); private TerminalDialog _terminalDialog; [IgnorePropertyChange] public string TerminalMessageSetPoint { get; set; } private bool _notifyTerminalMessageArrive = false; public TopViewSubModel() : base("TopViewModelSub") { //this.loadPortCount = loadPortCount; SwitchOffBuzzerCommand = new DelegateCommand(OnSwitchOffBuzzer); ResetCommand = new DelegateCommand(DoReset); LogoffCommand = new DelegateCommand(DoLogoff); TerminalCommand = new DelegateCommand(ShowTerminalWindow); TerminalClearCommand = new DelegateCommand(ClearTerminal); TerminalSendCommand = new DelegateCommand(SendTerminal); SoftwareVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(); InvokePropertyChanged("SoftwareVersion"); EventLogList = new ObservableCollection(); TerminalMessageList = new ObservableCollection(); _balloonControl = new MessageControl(); LPStatus = new ObservableCollection(); //LPStatus = new ObservableCollection(Enumerable.Range(1, 4).Select(x => new TopViewLPStatus() //{ // LPName = $"LP{x}", // LoadPortStatus = "unknown", //}).ToList()); EventClient.Instance.OnEvent += Instance_OnEvent; EventClient.Instance.Start(); } private ObservableCollection lpStatus; public ObservableCollection LPStatus { get { return lpStatus; } set { lpStatus = value; InvokePropertyChanged("LPStatus"); } } private void SendTerminal(object obj) { if (string.IsNullOrEmpty(TerminalMessageSetPoint)) return; InvokeClient.Instance.Service.DoOperation("FACommand", "FASendTerminalMessage", TerminalMessageSetPoint); lock (_lockMessage) { while (TerminalMessageList.Count > 200) { TerminalMessageList.RemoveAt(0); } TerminalMessageItem item = new TerminalMessageItem { Content = TerminalMessageSetPoint, Type = "E->H", MessageTime = DateTime.Now.ToString("yyyy-MM-dd HH: mm:ss") }; TerminalMessageList.Add(item); } TerminalMessageSetPoint = ""; InvokePropertyChanged("TerminalMessageSetPoint"); } private void ClearTerminal(object obj) { lock (_lockMessage) { TerminalMessageList.Clear(); } } private void ShowTerminalWindow(object obj) { _notifyTerminalMessageArrive = false; _terminalDialog = new TerminalDialog() { DataContext = this, }; _terminalDialog.Topmost = true; _terminalDialog.WindowStartupLocation = WindowStartupLocation.CenterScreen; _terminalDialog.ShowDialog(); _terminalDialog = null; } private void Instance_OnEvent(EventItem obj) { if (_logoff) return; Application.Current.Dispatcher.Invoke(new Action(() => { if (obj.Source == "Host" && obj.EventEnum == "TerminalMessage") { lock (_lockMessage) { while (TerminalMessageList.Count > 200) { TerminalMessageList.RemoveAt(0); } TerminalMessageItem item = new TerminalMessageItem { Content = obj.Description, Type = "H->E", MessageTime = obj.OccuringTime.ToString("yyyy-MM-dd HH: mm:ss") }; TerminalMessageList.Add(item); } if (_terminalDialog == null) _notifyTerminalMessageArrive = true; } })); switch (obj.Type) { case EventType.EventUI_Notify: LogEvent(obj); 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 DoLogoff(object obj) { _logoff = true; UiApplication.Instance.Logoff(); } private void DoReset(object obj) { InvokeClient.Instance.Service.DoOperation("System.Reset"); EventLogList.Clear(); } public void Reset() { InvokeClient.Instance.Service.DoOperation(OperationName.ResetAlarm); InvokeClient.Instance.Service.DoOperation(OperationName.ResetDevice); InvokeClient.Instance.Service.DoOperation(OperationName.ResetRouteManager); } void OnSwitchOffBuzzer(object param) { InvokeClient.Instance.Service.DoOperation(OperationName.DeviceOperation.ToString(), new object[] { DeviceName.SignalTower.ToString(), AITSignalTowerOperation.SwitchOffBuzzer.ToString() }); } void PopUIMessage(EventItem obj) { Application.Current.Dispatcher.Invoke(new Action(() => { _balloonControl.SetMessage(MessageType.Info, obj.Description); ShowNotifyIcon(2000); })); } void LogEvent(EventItem obj) { if (obj.Type != EventType.EventUI_Notify) return; Application.Current.Dispatcher.Invoke(new Action(() => { while (EventLogList.Count > 100) { EventLogList.RemoveAt(0); } EventLogList.Add(obj); })); } void ShowNotifyIcon(int time) { TaskbarIcon icon = new TaskbarIcon(); icon.ShowCustomBalloon(_balloonControl, global::System.Windows.Controls.Primitives.PopupAnimation.Slide, time); } void PopDialog(EventItem item) { Application.Current.Dispatcher.Invoke(new Action(() => { switch (item.Level) { case EventLevel.Alarm: MessageBoxEx.ShowError(item.Description, item.Explaination); break; case EventLevel.Warning: MessageBoxEx.ShowWarning(item.Description, item.Explaination); break; case EventLevel.Information: MessageBoxEx.ShowInfo(item.Description, item.Explaination); break; } })); } protected override void InvokeBeforeUpdateProperty(Dictionary data) { Application.Current.Dispatcher.Invoke(new Action(() => { var tempLPStatus = new List(); foreach (var item in data.Keys) { if (item.Substring(0, 2) == "LP") { tempLPStatus.Add(new TopViewLPStatus() { LPName = item.Split('.')[0], LoadPortStatus = data[item].ToString(), }); //.Substring(0, 3), } } lpStatus.Clear(); tempLPStatus = tempLPStatus.OrderBy(x => x.LPName).ToList(); foreach (var item in tempLPStatus) { lpStatus.Add(item); } })); } } public class TopViewLPStatus : INotifyPropertyChanged { public string LPName { get; set; } public string LoadPortStatus { get; set; } public event PropertyChangedEventHandler PropertyChanged; } }