| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340 | using Aitex.Core.UI.MVVM;using Aitex.Core.Utilities;using CyberX8_Simulator.Devices;using MECF.Framework.Simulator.Core.Commons;using MECF.Framework.Simulator.Core.Driver;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.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;namespace CyberX8_Simulator.Views{    /// <summary>    /// WagoView.xaml 的交互逻辑    /// </summary>    public partial class WagoView : UserControl    {        public WagoView()        {            InitializeComponent();            this.Loaded += OnViewLoaded;        }        private void OnViewLoaded(object sender, RoutedEventArgs e)        {            (DataContext as TimerViewModelBase)?.Start();        }    }    class WagoViewModel : SocketDeviceViewModel    {        #region 属性        public string Title        {            get { return "Wago Simulator"; }        }        private string _DOSelectedItem;        [IgnorePropertyChange]        public string DOSelectedItem        {            get            {                return _DOSelectedItem;            }            set            {                _DOSelectedItem = value;            }        }        private int _DOInputValue;        [IgnorePropertyChange]        public int DOInputValue        {            get            {                return _DOInputValue;            }            set            {                _DOInputValue = value;            }        }        private int _DOCurrentValue;        [IgnorePropertyChange]        public int DOCurrentValue        {            get            {                return _DOCurrentValue;            }            set            {                _DOCurrentValue = value;            }        }        private string _DISelectedItem;        [IgnorePropertyChange]        public string DISelectedItem        {            get            {                return _DISelectedItem;            }            set            {                _DISelectedItem = value;            }        }        private int _DIInputValue;        [IgnorePropertyChange]        public int DIInputValue        {            get            {                return _DIInputValue;            }            set            {                _DIInputValue = value;            }        }        private int _DICurrentValue;        [IgnorePropertyChange]        public int DICurrentValue        {            get            {                return _DICurrentValue;            }            set            {                _DICurrentValue = value;            }        }        private string _AOSelectedItem;        [IgnorePropertyChange]        public string AOSelectedItem        {            get            {                return _AOSelectedItem;            }            set            {                _AOSelectedItem = value;            }        }        private short _AOInputValue;        [IgnorePropertyChange]        public short AOInputValue        {            get            {                return _AOInputValue;            }            set            {                _AOInputValue = value;            }        }        private int _AOCurrentValue;        [IgnorePropertyChange]        public int AOCurrentValue        {            get            {                return _AOCurrentValue;            }            set            {                _AOCurrentValue = value;            }        }        private string _AISelectedItem;        [IgnorePropertyChange]        public string AISelectedItem        {            get            {                return _AISelectedItem;            }            set            {                _AISelectedItem = value;            }        }        private short _AIInputValue;        [IgnorePropertyChange]        public short AIInputValue        {            get            {                return _AIInputValue;            }            set            {                _AIInputValue = value;            }        }        private int _AICurrentValue;        [IgnorePropertyChange]        public int AICurrentValue        {            get            {                return _AICurrentValue;            }            set            {                _AICurrentValue = value;            }        }        #endregion        public ObservableCollection<string> DONameItems { get; set; }        public ObservableCollection<string> DINameItems { get; set; }        public ObservableCollection<string> AONameItems { get; set; }        public ObservableCollection<string> AINameItems { get; set; }        public ObservableCollection<int> DigitalInputSelected { get; set; }                        private WagoSocketSimulator _sim;        public ICommand SetDICommand { get; set; }        public ICommand SetDOCommand { get; set; }        public ICommand SetAICommand { get; set; }        public ICommand SetAOCommand { get; set; }        public ICommand DOSelectionChangedCommand { get; set; }        public ICommand DISelectionChangedCommand { get; set; }        public ICommand AOSelectionChangedCommand { get; set; }        public ICommand AISelectionChangedCommand { get; set; }        public WagoViewModel(string str) : base("WagoViewModel")        {            DOSelectionChangedCommand = new DelegateCommand<object>(DOSelectionChangedAction);            DISelectionChangedCommand = new DelegateCommand<object>(DISelectionChangedAction);            AOSelectionChangedCommand = new DelegateCommand<object>(AOSelectionChangedAction);            AISelectionChangedCommand = new DelegateCommand<object>(AISelectionChangedAction);                        SetDOCommand = new DelegateCommand<object>(SetDOAction);            SetDICommand = new DelegateCommand<object>(SetDIAction);            SetAOCommand = new DelegateCommand<object>(SetAOAction);            SetAICommand = new DelegateCommand<object>(SetAIAction);                        int.TryParse(str, out int port);            _sim = new WagoSocketSimulator(port);            Init(_sim);            InitData(port);            _sim.OnDIVariableValueChanged += DISelectionChangedAction;            _sim.OnAIVariableValueChanged += AISelectionChangedAction;            _sim.OnAOVariableValueChanged += AOSelectionChangedAction;            _sim.OnDOVariableValueChanged += DOSelectionChangedAction;        }        private void SetDIAction(object obj)        {            _sim.UpdataDIBytes(DISelectedItem, DIInputValue);            DISelectionChangedAction(null);        }        private void SetDOAction(object obj)        {            _sim.UpdataDOBytes(DOSelectedItem, DOInputValue);            DOSelectionChangedAction(null);        }        private void SetAIAction(object obj)        {            _sim.UpdataAIShorts(AISelectedItem, AIInputValue);            AISelectionChangedAction(null);        }        private void SetAOAction(object obj)        {            _sim.UpdataAOShorts(AOSelectedItem, AOInputValue);            AOSelectionChangedAction(null);        }        private void DOSelectionChangedAction(object obj)        {            if (DOSelectedItem == null)             {                return;            }            DOCurrentValue = _sim.DOBytes[_sim.DONameIndexDic[DOSelectedItem]];        }        private void DISelectionChangedAction(object obj)        {            if (DISelectedItem == null)            {                return;            }            DICurrentValue = _sim.DIBytes[_sim.DINameIndexDic[DISelectedItem]];        }        private void AOSelectionChangedAction(object obj)        {            if (AOSelectedItem == null)            {                return;            }            AOCurrentValue = _sim.AOShorts[_sim.AONameIndexDic[AOSelectedItem]];        }        private void AISelectionChangedAction(object obj)        {            if(AISelectedItem == null) //若来自其它的模块更新了AI的值,且当前选中的是该项,实时更新该项的值            {               return;            }            AICurrentValue = _sim.AIShorts[_sim.AINameIndexDic[AISelectedItem]];        }        private void InitData(int port) //端口用于初始化不同Wago设备的字典        {            DigitalInputSelected = new ObservableCollection<int> { 0, 1 };            DONameItems = new ObservableCollection<string>();            foreach (var item in _sim.DONameIndexDic.Keys)            {                DONameItems.Add(item);            }            DINameItems = new ObservableCollection<string>();            foreach (var item in _sim.DINameIndexDic.Keys)            {                DINameItems.Add(item);            }            AONameItems = new ObservableCollection<string>();            foreach (var item in _sim.AONameIndexDic.Keys)            {                AONameItems.Add(item);            }            AINameItems = new ObservableCollection<string>();            foreach (var item in _sim.AINameIndexDic.Keys)            {                AINameItems.Add(item);            }        }           }}
 |