| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394 | using Aitex.Core.RT.DataCenter;using Caliburn.Micro.Core;using MECF.Framework.Common.Device.LinMot;using MECF.Framework.Common.OperationCenter;using MECF.Framework.Common.Persistent.SRD;using System;using System.Collections.Generic;using System.Collections.ObjectModel;using System.ComponentModel;using System.Linq;using System.Reflection;using System.Runtime.CompilerServices;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_Themes.UserControls{    /// <summary>    /// LinMotOtherControl.xaml 的交互逻辑    /// </summary>    public partial class LinMotOtherControl : UserControl    {        #region 属性        public static readonly DependencyProperty ModuleNameProperty = DependencyProperty.Register("ModuleName", typeof(string), typeof(LinMotOtherControl),          new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));        /// <summary>        /// 模块名称        /// </summary>        public string ModuleName        {            get            {                return (string)this.GetValue(ModuleNameProperty);            }            set            {                this.SetValue(ModuleNameProperty, value);            }        }        public static readonly DependencyProperty ModuleTitleProperty = DependencyProperty.Register("ModuleTitle", typeof(string), typeof(LinMotOtherControl),            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));        /// <summary>        /// 标题        /// </summary>        public string ModuleTitle        {            get            {                return (string)this.GetValue(ModuleTitleProperty);            }            set            {                this.SetValue(ModuleTitleProperty, value);            }        }        public static readonly DependencyProperty ParentModuleProperty = DependencyProperty.Register("ParentModule", typeof(string), typeof(LinMotOtherControl),          new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));        /// <summary>        /// ParentModule        /// </summary>        public string ParentModule        {            get            {                return (string)this.GetValue(ParentModuleProperty);            }            set            {                this.SetValue(ParentModuleProperty, value);            }        }        public static readonly DependencyProperty IsConnectedProperty = DependencyProperty.Register("IsConnected", typeof(bool), typeof(LinMotOtherControl),           new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));        /// <summary>        /// 连接状态        /// </summary>        public bool IsConnected        {            get            {                return (bool)this.GetValue(IsConnectedProperty);            }            set            {                this.SetValue(IsConnectedProperty, value);            }        }        public static readonly DependencyProperty StatusWordProperty = DependencyProperty.Register("StatusWord", typeof(short), typeof(LinMotOtherControl),         new FrameworkPropertyMetadata((short)0, new PropertyChangedCallback(StatusWordPropertyChangedCallBack)));        /// <summary>        /// 状态字        /// </summary>        public short StatusWord        {            get { return (short)this.GetValue(StatusWordProperty); }            set            {                this.SetValue(StatusWordProperty, value);            }        }        private static void StatusWordPropertyChangedCallBack(DependencyObject d, DependencyPropertyChangedEventArgs e)        {            short statusWord = (short)e.NewValue;            d.SetValue(ReadyProperty, (statusWord & 0x01) == 1);            d.SetValue(DisableProperty, (statusWord & 0b0100) >> 2 == 0);            d.SetValue(ErrorProperty, ((statusWord & 0b01000) >> 3 == 1) || ((statusWord & 0b01000000000000) >> 12 == 1));            d.SetValue(InitProperty, (statusWord & 0b0100000000000) >> 11 == 1);            d.SetValue(RunProperty, (statusWord & 0b010000000000000) >> 13 == 1);        }        public static readonly DependencyProperty ReadyProperty = DependencyProperty.Register("Ready", typeof(bool), typeof(LinMotOtherControl),        new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));        public bool Ready        {            get { return (bool)this.GetValue(ReadyProperty); }            set { SetValue(ReadyProperty, value); }        }        public static readonly DependencyProperty DisableProperty = DependencyProperty.Register("Disable", typeof(bool), typeof(LinMotOtherControl),        new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));        public bool Disable        {            get { return (bool)this.GetValue(DisableProperty); }            set { SetValue(DisableProperty, value); }        }        public static readonly DependencyProperty RunProperty = DependencyProperty.Register("Run", typeof(bool), typeof(LinMotOtherControl),        new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));        public bool Run        {            get { return (bool)this.GetValue(RunProperty); }            set { SetValue(RunProperty, value); }        }        public static readonly DependencyProperty ErrorProperty = DependencyProperty.Register("Error", typeof(bool), typeof(LinMotOtherControl),        new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));        public bool Error        {            get { return (bool)this.GetValue(ErrorProperty); }            set { SetValue(ErrorProperty, value); }        }        public static readonly DependencyProperty InitProperty = DependencyProperty.Register("Init", typeof(bool), typeof(LinMotOtherControl),        new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));        public bool Init        {            get { return (bool)this.GetValue(InitProperty); }            set { SetValue(InitProperty, value); }        }        public static readonly DependencyProperty DirectionProperty = DependencyProperty.Register("Direction", typeof(string), typeof(LinMotOtherControl),       new FrameworkPropertyMetadata("",new PropertyChangedCallback(DirectionChangedCallBack)));        public string Direction        {            get { return (string)this.GetValue(DirectionProperty); }            set { SetValue(DirectionProperty, value); }        }        public static readonly DependencyProperty LinMotDeviceDataProperty = DependencyProperty.Register("LinMotDeviceData", typeof(LinMotDeviceData), typeof(LinMotOtherControl)            ,new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));        public LinMotDeviceData LinMotDeviceData        {            get { return (LinMotDeviceData)this.GetValue(LinMotDeviceDataProperty); }            set { SetValue(LinMotDeviceDataProperty, value); }        }        public static readonly DependencyProperty PositionProperty = DependencyProperty.Register("Position", typeof(double), typeof(LinMotOtherControl));        public double Position        {            get { return (double)this.GetValue(PositionProperty); }            set { SetValue(PositionProperty, value); }        }        public static readonly DependencyProperty CycleProperty = DependencyProperty.Register("Cycle", typeof(int), typeof(LinMotOtherControl),             new FrameworkPropertyMetadata(10));        public int Cycle        {            get { return (int)this.GetValue(CycleProperty); }            set { SetValue(CycleProperty, value); }        }        public static readonly DependencyProperty CurrentScanProperty = DependencyProperty.Register("CurrentScan", typeof(int), typeof(LinMotOtherControl),            new FrameworkPropertyMetadata(0));        public int CurrentScan        {            get { return (int)this.GetValue(CurrentScanProperty); }            set { SetValue(CurrentScanProperty, value); }        }        public static readonly DependencyProperty AccelProperty = DependencyProperty.Register("Accel", typeof(string), typeof(LinMotOtherControl)            , new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));        public string Accel        {            get { return (string)this.GetValue(AccelProperty); }            set { SetValue(AccelProperty, value); }        }        public static readonly DependencyProperty DecelProperty = DependencyProperty.Register("Decel", typeof(string), typeof(LinMotOtherControl)            , new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));        public string Decel        {            get { return (string)this.GetValue(DecelProperty); }            set { SetValue(DecelProperty, value); }        }        public static readonly DependencyProperty SpeedProperty = DependencyProperty.Register("Speed", typeof(string), typeof(LinMotOtherControl)            , new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));        public string Speed        {            get { return (string)this.GetValue(SpeedProperty); }            set { SetValue(SpeedProperty, value); }        }        public static readonly DependencyProperty InputSpeedProperty = DependencyProperty.Register("InputSpeed", typeof(double), typeof(LinMotOtherControl)            , new FrameworkPropertyMetadata(0.00, FrameworkPropertyMetadataOptions.AffectsRender));        public double InputSpeed        {            get { return (double)this.GetValue(InputSpeedProperty); }            set { SetValue(InputSpeedProperty, value); }        }        public static readonly DependencyProperty InputDecelProperty = DependencyProperty.Register("InputDecel", typeof(double), typeof(LinMotOtherControl)            , new FrameworkPropertyMetadata(0.00, FrameworkPropertyMetadataOptions.AffectsRender));        public double InputDecel        {            get { return (double)this.GetValue(InputDecelProperty); }            set { SetValue(InputDecelProperty, value); }        }        public static readonly DependencyProperty InputAccelProperty = DependencyProperty.Register("InputAccel", typeof(double), typeof(LinMotOtherControl)            , new FrameworkPropertyMetadata(0.00, FrameworkPropertyMetadataOptions.AffectsRender));        public double InputAccel        {            get { return (double)this.GetValue(InputAccelProperty); }            set { SetValue(InputAccelProperty, value); }        }        public static readonly DependencyProperty SelectDirectionItemSourceProperty = DependencyProperty.Register("SelectDirectionItem", typeof(List<string>), typeof(LinMotOtherControl)            , new FrameworkPropertyMetadata(new List<string>() { "up", "down" }, FrameworkPropertyMetadataOptions.AffectsRender));        public List<string> SelectDirectionItemSource        {            get { return (List<string>)this.GetValue(SelectDirectionItemSourceProperty); }            set { SetValue(SelectDirectionItemSourceProperty, value); }        }        public static readonly DependencyProperty DirectionSelectedItemProperty = DependencyProperty.Register("DirectionSelectedItem", typeof(string), typeof(LinMotOtherControl)            , new FrameworkPropertyMetadata("", new PropertyChangedCallback(OnItemsSourceChanged)));        /// <summary>        /// DirectionSelectedItem        /// </summary>        public string DirectionSelectedItem        {            get {  return (string)this.GetValue(DirectionSelectedItemProperty); }            set {  this.SetValue(DirectionSelectedItemProperty, value);}        }        private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)        {            if (e.NewValue != null)            {                LinMotDevice data = (LinMotDevice)d.GetValue(DataContextProperty);                                string direction = (string)e.NewValue;                if ("up".Equals(direction))                {                    d.SetValue(InputSpeedProperty, data.LinMotDeviceData.UpMaxSpeed);                    d.SetValue(InputAccelProperty, (double)data.LinMotDeviceData.UpMaxAcceleration);                    d.SetValue(InputDecelProperty, (double)data.LinMotDeviceData.UpMaxDeceleration);                }                else if ("down".Equals(direction))                {                    d.SetValue(InputSpeedProperty, data.LinMotDeviceData.DownMaxSpeed);                    d.SetValue(InputAccelProperty, (double)data.LinMotDeviceData.DownMaxAcceleration);                    d.SetValue(InputDecelProperty, (double)data.LinMotDeviceData.DownMaxDeceleration);                }                else                {                    d.SetValue(InputSpeedProperty, 0.00);                    d.SetValue(InputAccelProperty, 0.00);                    d.SetValue(InputDecelProperty, 0.00);                }            }        }        public static readonly DependencyProperty IsEditEnableProperty = DependencyProperty.Register("IsEditEnable", typeof(bool), typeof(LinMotOtherControl));        public bool IsEditEnable        {            get { return (bool)this.GetValue(IsEditEnableProperty); }            set { SetValue(IsEditEnableProperty, value); }        }        /// <summary>        /// 方向改变后的回调函数        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private static void DirectionChangedCallBack(DependencyObject sender, DependencyPropertyChangedEventArgs e)        {            if(e.NewValue!=null)            {                string newValue = e.NewValue.ToString();                LinMotDevice data = (LinMotDevice)sender.GetValue(DataContextProperty);                LinMotDeviceData linMotDeviceData = data.LinMotDeviceData;                if (linMotDeviceData != null)                {                    if (newValue == "up")                    {                        sender.SetValue(SpeedProperty, linMotDeviceData.UpMaxSpeed.ToString());                        sender.SetValue(AccelProperty, linMotDeviceData.UpMaxAcceleration.ToString());                        sender.SetValue(DecelProperty, linMotDeviceData.UpMaxDeceleration.ToString());                    }                    else if(newValue=="down")                    {                        sender.SetValue(SpeedProperty, linMotDeviceData.DownMaxSpeed.ToString());                        sender.SetValue(AccelProperty, linMotDeviceData.DownMaxAcceleration.ToString());                        sender.SetValue(DecelProperty, linMotDeviceData.DownMaxDeceleration.ToString());                    }                    else                    {                        sender.SetValue(SpeedProperty, "");                        sender.SetValue(AccelProperty, "");                        sender.SetValue(DecelProperty, "");                    }                }            }        }        #endregion        /// <summary>        /// 构造函数        /// </summary>        public LinMotOtherControl()        {            InitializeComponent();        }        /// <summary>        /// 启动        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void Start_Click(object sender, RoutedEventArgs e)        {            InvokeClient.Instance.Service.DoOperation($"{ModuleName}.StartPosition",Cycle);        }        private void Stop_Click(object sender, RoutedEventArgs e)        {            InvokeClient.Instance.Service.DoOperation($"{ModuleName}.Stop");        }        private void Abort_Click(object sender, RoutedEventArgs e)        {            InvokeClient.Instance.Service.DoOperation($"{ModuleName}.Abort");        }        private void Reset_Click(object sender, RoutedEventArgs e)        {            InvokeClient.Instance.Service.DoOperation($"{ModuleName}.Reset");        }        private void Apply_Click(object sender, RoutedEventArgs e)        {            if (String.IsNullOrEmpty(DirectionSelectedItem))            {                MessageBox.Show("Select direction first", "Warning", MessageBoxButton.OK, MessageBoxImage.Error);            }            else if (DirectionSelectedItem == "up")            {                double speed = InputSpeed == 0.00 ? LinMotDeviceData.UpMaxSpeed : InputSpeed;                int acc = InputAccel == 0.00 ? LinMotDeviceData.UpMaxAcceleration : (int)InputAccel;                int dec = InputDecel == 0.00 ? LinMotDeviceData.UpMaxDeceleration : (int)InputDecel;                InvokeClient.Instance.Service.DoOperation($"{ModuleName}.UpdateSpeedData", ModuleName, DirectionSelectedItem, speed, acc, dec);            }            else            {                double speed = InputSpeed != 0.00 ? InputSpeed : LinMotDeviceData.DownMaxSpeed;                int acc = InputAccel != 0.00 ? (int)InputAccel : LinMotDeviceData.DownMaxAcceleration;                int dec = InputDecel != 0.00 ? (int)InputDecel : LinMotDeviceData.DownMaxDeceleration;                InvokeClient.Instance.Service.DoOperation($"{ModuleName}.UpdateSpeedData", ModuleName, DirectionSelectedItem, speed, acc, dec);            }        }        private void Status_Click(object sender, RoutedEventArgs e)        {            IsEditEnable = false;        }        private void Setting_Click(object sender, RoutedEventArgs e)        {            IsEditEnable = true;        }    }}
 |