| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 | using System;using System.Collections.Generic;using System.Collections.ObjectModel;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows;using System.Windows.Input;using Aitex.Core.UI.MVVM;using MECF.Framework.Simulator.Core.Driver;namespace MECF.Framework.Simulator.Core.Commons{    public class SocketDeviceViewModel:TimerViewModelBase    {        public ICommand ClearLogCommand { get; set; }        public ICommand EnableCommand { get; set; }        public ICommand DisableCommand { get; set; }        public bool IsEnableEnable        {            get { return !_simulator.IsEnabled; }        }        public bool IsEnableDisable        {            get { return _simulator.IsEnabled; }        }        public bool IsConnected        {            get { return _simulator.IsConnected; }        }         public int LocalPort        {            get { return _simulator.LocalPort; }        }        public string RemoteConnection        {            get { return _simulator.RemoteConnection; }        }        public string ConnectionStatus        {            get            {                if (_simulator.IsConnected)                    return "Connected";                if (_simulator.IsEnabled)                    return "Listening";                return "Disable";            }        }        public string LocalPortSetPoint { get; set; }        public ObservableCollection<TransactionLogItem> TransactionLogItems { get; set; }        protected SocketDeviceSimulator _simulator;        public SocketDeviceViewModel(string name) : base(name)        {            ClearLogCommand = new DelegateCommand<string>(ClearLog);            EnableCommand = new DelegateCommand<string>(Enable);            DisableCommand = new DelegateCommand<string>(Disable);            TransactionLogItems = new ObservableCollection<TransactionLogItem>();            LocalPortSetPoint = "";        }        protected void Init(SocketDeviceSimulator sim, bool enable=true)        {            LocalPortSetPoint = sim.LocalPort.ToString();            _simulator = sim;            _simulator.MessageOut += _simulator_MessageOut;            _simulator.MessageIn += _simulator_MessageIn;            if (enable)                _simulator.Enable();        }        private void Disable(string obj)        {            _simulator.Disable();        }        private void Enable(string obj)        {            int port;            if (int.TryParse(LocalPortSetPoint, out port))                _simulator.LocalPort = port;            _simulator.Enable();        }        private void ClearLog(string obj)        {            TransactionLogItems.Clear();        }        private void _simulator_MessageIn(string obj)        {            if(!string.IsNullOrEmpty(obj))            {                Application.Current.Dispatcher.Invoke(new Action(() =>                {                    TransactionLogItems.Add(new TransactionLogItem() { Incoming = obj, OccurTime = DateTime.Now.ToString("HH:mm:ss.fff") });                }));            }        }        private void _simulator_MessageOut(string obj)        {            if (!string.IsNullOrEmpty(obj))            {                Application.Current.Dispatcher.Invoke(new Action(() =>                {                    TransactionLogItems.Add(new TransactionLogItem() { Outgoing = obj, OccurTime = DateTime.Now.ToString("HH:mm:ss.fff") });                }));            }                    }        protected override void Poll()        {            if (Application.Current != null)            {                Application.Current.Dispatcher.Invoke(new Action(() =>                {                    InvokeAllPropertyChanged();                }));            }        }    }    public class SimpleSocketDeviceViewModel : TimerViewModelBase    {        public ICommand ClearLogCommand { get; set; }        public ICommand EnableCommand { get; set; }        public ICommand DisableCommand { get; set; }        public bool IsEnableEnable        {            get { return !_simulator.IsEnabled; }        }        public bool IsEnableDisable        {            get { return _simulator.IsEnabled; }        }        public bool IsConnected        {            get { return _simulator.IsConnected; }        }        public int LocalPort        {            get { return _simulator.LocalPort; }        }        //public string RemoteConnection        //{        //    get { return _simulator.RemoteConnection; }        //}        public string ConnectionStatus        {            get            {                if (_simulator.IsConnected)                    return "Connected";                if (_simulator.IsEnabled)                    return "Listening";                return "Disable";            }        }        public string LocalPortSetPoint { get; set; }        public ObservableCollection<TransactionLogItem> TransactionLogItems { get; set; }        protected SimpleSocketDeviceSimulator _simulator;        public SimpleSocketDeviceViewModel(string name) : base(name)        {            ClearLogCommand = new DelegateCommand<string>(ClearLog);            EnableCommand = new DelegateCommand<string>(Enable);            DisableCommand = new DelegateCommand<string>(Disable);            TransactionLogItems = new ObservableCollection<TransactionLogItem>();            LocalPortSetPoint = "";        }        protected void Init(SimpleSocketDeviceSimulator sim, bool enable = true)        {            LocalPortSetPoint = sim.LocalPort.ToString();            _simulator = sim;            _simulator.MessageOut += _simulator_MessageOut;            _simulator.MessageIn += _simulator_MessageIn;            if (enable)                _simulator.Enable();        }        private void Disable(string obj)        {            _simulator.Disable();        }        private void Enable(string obj)        {            int port;            if (int.TryParse(LocalPortSetPoint, out port))                _simulator.LocalPort = port;            _simulator.Enable();        }        private void ClearLog(string obj)        {            TransactionLogItems.Clear();        }        private void _simulator_MessageIn(string obj)        {            if(!string.IsNullOrEmpty(obj))            {                Application.Current.Dispatcher.Invoke(new Action(() =>                {                    TransactionLogItems.Add(new TransactionLogItem() { Incoming = obj, OccurTime = DateTime.Now.ToString("HH:mm:ss.fff") });                }));            }        }        private void _simulator_MessageOut(string obj)        {            if (!string.IsNullOrEmpty(obj))            {                Application.Current.Dispatcher.Invoke(new Action(() =>                {                    TransactionLogItems.Add(new TransactionLogItem() { Outgoing = obj, OccurTime = DateTime.Now.ToString("HH:mm:ss.fff") });                }));            }        }        protected override void Poll()        {            if (Application.Current != null)            {                Application.Current.Dispatcher.Invoke(new Action(() =>                {                    InvokeAllPropertyChanged();                }));            }        }    }}
 |