PgFlyAd.xaml.cs 11.6 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2
using CommunityToolkit.Mvvm.Input;
using FLY.Thick.Base.Common;
潘栩锋's avatar
潘栩锋 committed
3
using FLY.Thick.Base.IService;
潘栩锋's avatar
潘栩锋 committed
4
using System;
5
using System.Collections.Generic;
潘栩锋's avatar
潘栩锋 committed
6 7
using System.ComponentModel;
using System.Net;
8
using System.Windows;
潘栩锋's avatar
潘栩锋 committed
9 10
using System.Windows.Controls;
using Unity;
潘栩锋's avatar
潘栩锋 committed
11 12 13 14 15 16 17 18 19 20 21 22

namespace FLY.Thick.Base.UI
{
    /// <summary>
    /// Page_FlyAD.xaml 的交互逻辑
    /// </summary>
    public partial class PgFlyAd : Page
    {
        PgFlyAdVm viewModel;
        public PgFlyAd()
        {
            InitializeComponent();
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
            if (System.ComponentModel.LicenseManager.UsageMode != System.ComponentModel.LicenseUsageMode.Runtime)
                return;

            this.Loaded += UcGridGraph_Loaded;
            this.Unloaded += UcGridGraph_Unloaded;
        }

        private void UcGridGraph_Unloaded(object sender, RoutedEventArgs e)
        {
            viewModel.DisposeBinding();
        }

        private void UcGridGraph_Loaded(object sender, RoutedEventArgs e)
        {
            viewModel.SetBinding();
潘栩锋's avatar
潘栩锋 committed
38 39 40 41 42 43
        }

        [InjectionMethod]
        public void Init(
            IUnityContainer container,
            IFlyADService flyAdService,
44 45
            ITDGageService gageService,
            IInitParamService initParamService)
潘栩锋's avatar
潘栩锋 committed
46 47
        {
            viewModel = new PgFlyAdVm();
48
            viewModel.Init(flyAdService, gageService, initParamService);
潘栩锋's avatar
潘栩锋 committed
49 50 51 52 53 54 55 56 57 58 59
            this.DataContext = viewModel;
            container.BuildUp(mircoGage);
        }


    }
    public class PgFlyAdVm : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        #region 参数
        public IPEndPoint EP { get; set; }
60
        public UInt16 PosOfGrid { get; set; }
潘栩锋's avatar
潘栩锋 committed
61 62 63 64 65 66
        public MOTORTYPE MotorType { set; get; }
        public UInt16 Ratio01 { get; set; }
        public UInt16 Ratio02 { get; set; }
        public Int16 PosOffset { set; get; }//脉冲平移
        public UInt32 JogVelocity { set; get; }
        public int GridSmooth { get; set; }
67
        public bool IsCalSpeed { get; set; }
68
        public int ADLag { get; set; }
69 70 71
        public bool HasPosMaxMin { get; set; }
        public int PosMin { get; set; }
        public int PosMax { get; set; }
潘栩锋's avatar
潘栩锋 committed
72 73
        #endregion

74 75
        public int Version { get; set; }
        public int HardwareVersion { get; set; }
潘栩锋's avatar
潘栩锋 committed
76
        public int HardwareVersionMinor { get; set; }
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
        #region initparam
        public double Encoder1_mmpp { get; set; }
        public int PosLength { get; set; }
        /// <summary>
        /// 设置的速度,与 实际速度比例 Speed1 = VScan*Speed1Scale
        /// </summary>
        public double Speed1Scale { get; set; }
        #region 速度


        /// <summary>
        /// 调试时速度,向前走,向后走 Velocity Of JOG
        /// </summary>
        public UInt32 VJOG { get; set; }

        /// <summary>
        /// 开始速度 Start Velocity
        /// </summary>
        public UInt32 SVelocity { set; get; }
        /// <summary>
        /// 加速时间
        /// </summary>
        public UInt32 ATime { set; get; }
        /// <summary>
        /// 减速时间
        /// </summary>
        public UInt32 DTime { set; get; }
        /// <summary>
        /// 归0速度1
        /// </summary>
        public UInt32 HVelocity1 { set; get; }
        /// <summary>
        /// 归0速度2
        /// </summary>
        public UInt32 HVelocity2 { set; get; }

        #endregion
        #endregion
潘栩锋's avatar
潘栩锋 committed
115 116 117 118
        public int TargetPos { get; set; }

        #region Command
        public RelayCommand ApplyCmd { get; }
119
        public RelayCommand Apply2Cmd { get; }
潘栩锋's avatar
潘栩锋 committed
120 121 122 123 124 125 126 127 128
        public RelayCommand RunToCmd { get; }
        public RelayCommand BackwCmd { get; }
        public RelayCommand ForwCmd { get; }
        public RelayCommand OrgCmd { get; }
        public RelayCommand StopCmd { get; }
        #endregion

        IFlyADService flyAdService;
        ITDGageService gageService;
129
        IInitParamService initParamService;
潘栩锋's avatar
潘栩锋 committed
130

131 132 133 134 135 136
        Dictionary<object, List<Misc.BindingOperations.PropertyChangedEventContexts>> bindingConexts = new Dictionary<object, List<Misc.BindingOperations.PropertyChangedEventContexts>>();

        /// <summary>
        /// 数据已经绑定了
        /// </summary>
        bool isBinding;
潘栩锋's avatar
潘栩锋 committed
137
        public PgFlyAdVm()
潘栩锋's avatar
潘栩锋 committed
138 139
        {
            ApplyCmd = new RelayCommand(Apply);
140
            Apply2Cmd = new RelayCommand(Apply2);
潘栩锋's avatar
潘栩锋 committed
141 142 143 144 145 146 147 148 149
            RunToCmd = new RelayCommand(RunTo);
            BackwCmd = new RelayCommand(Backw);
            ForwCmd = new RelayCommand(Forw);
            OrgCmd = new RelayCommand(Org);
            StopCmd = new RelayCommand(Stop);
        }

        public void Init(
            IFlyADService flyAdService,
150 151
            ITDGageService gageService,
            IInitParamService initParamService)
潘栩锋's avatar
潘栩锋 committed
152 153 154
        {
            this.flyAdService = flyAdService;
            this.gageService = gageService;
155
            this.initParamService = initParamService;
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
            SetBinding();
        }

        /// <summary>
        /// 参数绑定
        /// </summary>
        public void SetBinding()
        {
            if (isBinding)
                return;
            isBinding = true;

            //下面全部event保存在bindingConexts
            Misc.BindingOperations.StartMarkdownEvents(bindingConexts);
            Misc.BindingOperations.SetBinding(this.flyAdService, nameof(this.flyAdService.Addr), this, () =>
171 172
            {
                EP = Misc.StringConverter.ToIPEndPoint(this.flyAdService.Addr);
潘栩锋's avatar
潘栩锋 committed
173
            });
174 175 176 177 178 179 180 181
            Misc.BindingOperations.SetBinding(this.flyAdService, nameof(this.flyAdService.PosOfGrid), this, nameof(PosOfGrid));
            Misc.BindingOperations.SetBinding(this.flyAdService, nameof(this.flyAdService.MotorType), this, nameof(MotorType));
            Misc.BindingOperations.SetBinding(this.flyAdService, nameof(this.flyAdService.Ratio01), this, nameof(Ratio01));
            Misc.BindingOperations.SetBinding(this.flyAdService, nameof(this.flyAdService.Ratio02), this, nameof(Ratio02));
            Misc.BindingOperations.SetBinding(this.flyAdService, nameof(this.flyAdService.PosOffset), this, nameof(PosOffset));
            Misc.BindingOperations.SetBinding(this.flyAdService, nameof(this.flyAdService.JogVelocity), this, nameof(JogVelocity));
            Misc.BindingOperations.SetBinding(this.flyAdService, nameof(this.flyAdService.GridSmooth), this, nameof(GridSmooth));
            Misc.BindingOperations.SetBinding(this.flyAdService, nameof(this.flyAdService.ADLag), this, nameof(ADLag));
潘栩锋's avatar
潘栩锋 committed
182

183 184 185 186
            Misc.BindingOperations.SetBinding(this.flyAdService, nameof(this.flyAdService.HasPosMaxMin), this, nameof(HasPosMaxMin));
            Misc.BindingOperations.SetBinding(this.flyAdService, nameof(this.flyAdService.PosMin), this, nameof(PosMin));
            Misc.BindingOperations.SetBinding(this.flyAdService, nameof(this.flyAdService.PosMax), this, nameof(PosMax));

187 188
            Misc.BindingOperations.SetBinding(this.flyAdService, nameof(this.flyAdService.Verson), this, nameof(Version));
            Misc.BindingOperations.SetBinding(this.flyAdService, nameof(this.flyAdService.HardwareVersion), this, nameof(HardwareVersion));
潘栩锋's avatar
潘栩锋 committed
189
            Misc.BindingOperations.SetBinding(this.flyAdService, nameof(this.flyAdService.HardwareVersionMinor), this, nameof(HardwareVersionMinor));
190 191

            Misc.BindingOperations.SetBinding(this.initParamService, nameof(this.initParamService.Encoder1_mmpp), this, nameof(Encoder1_mmpp));
192
            Misc.BindingOperations.SetBinding(this.initParamService, nameof(this.initParamService.Speed1Scale), this, nameof(Speed1Scale));
193
            Misc.BindingOperations.SetBinding(this.initParamService, nameof(this.initParamService.PosLength), this, nameof(PosLength));
194 195 196 197 198 199
            Misc.BindingOperations.SetBinding(this.initParamService, nameof(this.initParamService.VJOG), this, nameof(VJOG));
            Misc.BindingOperations.SetBinding(this.initParamService, nameof(this.initParamService.SVelocity), this, nameof(SVelocity));
            Misc.BindingOperations.SetBinding(this.initParamService, nameof(this.initParamService.ATime), this, nameof(ATime));
            Misc.BindingOperations.SetBinding(this.initParamService, nameof(this.initParamService.DTime), this, nameof(DTime));
            Misc.BindingOperations.SetBinding(this.initParamService, nameof(this.initParamService.HVelocity1), this, nameof(HVelocity1));
            Misc.BindingOperations.SetBinding(this.initParamService, nameof(this.initParamService.HVelocity2), this, nameof(HVelocity2));
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
            //备份event完毕, 必须关闭记录
            Misc.BindingOperations.StopMarkdownEvents();

        }
        /// <summary>
        /// 解除绑定
        /// </summary>
        public void DisposeBinding()
        {
            if (!isBinding)//已经解除绑定了
                return;
            isBinding = false;

            //释放全部 event
            Misc.BindingOperations.DisposeEventTargetObject(bindingConexts);


潘栩锋's avatar
潘栩锋 committed
217 218 219
        }
        private void Apply()
        {
潘栩锋's avatar
潘栩锋 committed
220
            if (this.EP == null)
221
            {
222 223 224
                string tit = (string)Application.Current.TryFindResource("str.PgFlyAd.ApplyFailed");
                string msg = (string)Application.Current.TryFindResource("str.PgFlyAd.IPisNull");
                FLY.ControlLibrary.Window_WarningTip.Show(tit, msg,TimeSpan.FromSeconds(2));
225
            }
潘栩锋's avatar
潘栩锋 committed
226 227 228
            if (!WdPassword.Authorize("FlyAd"))
                return;

229
            flyAdService.Addr = this.EP.ToString();
潘栩锋's avatar
潘栩锋 committed
230 231 232 233 234 235
            flyAdService.PosOfGrid = this.PosOfGrid;
            flyAdService.MotorType = this.MotorType;
            flyAdService.Ratio01 = this.Ratio01;
            flyAdService.Ratio02 = this.Ratio02;
            flyAdService.PosOffset = this.PosOffset;
            flyAdService.JogVelocity = this.JogVelocity;
潘栩锋's avatar
潘栩锋 committed
236
            flyAdService.GridSmooth = this.GridSmooth;
237 238
            flyAdService.ADLag = this.ADLag;

239 240 241 242
            flyAdService.HasPosMaxMin = this.HasPosMaxMin;
            flyAdService.PosMin = this.PosMin;
            flyAdService.PosMax = this.PosMax;

243
            flyAdService.HardwareVersion = HardwareVersion;
潘栩锋's avatar
潘栩锋 committed
244
            flyAdService.HardwareVersionMinor = HardwareVersionMinor;
245
            flyAdService.Apply();
246 247 248 249
            {
                string tit = (string)Application.Current.TryFindResource("str.PgFlyAd.ApplySuccessfully");
                FLY.ControlLibrary.Window_Tip.Show(tit, null, TimeSpan.FromSeconds(2));
            }
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
        }

        private void Apply2()
        {

            if (!WdPassword.Authorize("InitParam"))
                return;

            initParamService.Encoder1_mmpp = Encoder1_mmpp;
            initParamService.PosLength = PosLength;
            initParamService.VJOG = this.VJOG;
            initParamService.SVelocity = this.SVelocity;
            initParamService.ATime = this.ATime;
            initParamService.DTime = this.DTime;
            initParamService.HVelocity1 = this.HVelocity1;
            initParamService.HVelocity2 = this.HVelocity2;
266
            initParamService.Apply();
267 268 269

            string tit = (string)Application.Current.TryFindResource("str.PgFlyAd.ApplySuccessfully");
            FLY.ControlLibrary.Window_Tip.Show(tit,null,TimeSpan.FromSeconds(2));
潘栩锋's avatar
潘栩锋 committed
270

潘栩锋's avatar
潘栩锋 committed
271
        }
272

潘栩锋's avatar
潘栩锋 committed
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
        private void RunTo()
        {
            gageService.StartP2(STARTP2_MODE.RUNTO, TargetPos);
        }
        private void Stop()
        {
            gageService.StartP2(STARTP2_MODE.STOP);
        }
        private void Org()
        {
            gageService.StartP2(STARTP2_MODE.ORG);
        }
        private void Forw()
        {
            gageService.StartP2(STARTP2_MODE.FORW_UNTIL_STOP);
        }
        private void Backw()
        {
            gageService.StartP2(STARTP2_MODE.BACKW_UNTIL_STOP);
        }
    }
}