CtMicroGageVm.cs 6.9 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3
using FLY.OBJComponents.Client;
using FLY.Thick.Base.Client;
using FLY.Thick.Base.Common;
4
using FLY.Thick.Base.IService;
潘栩锋's avatar
潘栩锋 committed
5 6 7 8 9 10 11 12 13
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
潘栩锋's avatar
潘栩锋 committed
14
using Unity;
潘栩锋's avatar
潘栩锋 committed
15

潘栩锋's avatar
潘栩锋 committed
16
namespace FLY.Thick.Base.UI
潘栩锋's avatar
潘栩锋 committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
{
    class CtMicroGageVm:ICtMicroGageVm
    {
        #region 延时推送 MARKNO
        const int MARKNO_UPDATEERROR = 1;
        #endregion

        public event PropertyChangedEventHandler PropertyChanged;


        public double Thick { get; set; }
        public int AD { get; set; }
        public int ADMax { get; set; }
        public int Position { get; set; }
        public int PosLength { get; set; }

        public double Posmm { get; set; }
        public double Velocity { get; set; }
35
        public double FilmVelocity { get; set; }
潘栩锋's avatar
潘栩锋 committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
        public string ControllerState { get; set; }

        public UInt16 OStatus { get; set; }
        public UInt16 IStatus { get; set; }

        public bool IsError { get; set; }
        /// <summary>
        /// 异常消息
        /// </summary>
        public string ErrMsg { get; set; }

        public RelayCommand StopCmd { get; private set; }

        public RelayCommand ForwCmd { get; private set; }

        public RelayCommand BackwCmd { get; private set; }

        public RelayCommand OrgCmd { get; private set; }


潘栩锋's avatar
潘栩锋 committed
56 57 58
        DynArea dynArea;
        ITDGageService gageService;
        IInitParamService initParam;
潘栩锋's avatar
潘栩锋 committed
59 60 61 62 63 64 65 66
        BufferWindow<FLY.OBJComponents.Common.FlyData_WarningHistory> mWindow;
        DispatcherTimer timer_error;
        private int reason_list_index = -1;

        public CtMicroGageVm()
        {
            StopCmd = new RelayCommand(() =>
            {
潘栩锋's avatar
潘栩锋 committed
67
                gageService.StartP2(STARTP2_MODE.STOP);
潘栩锋's avatar
潘栩锋 committed
68 69 70
            });
            OrgCmd = new RelayCommand(() =>
            {
潘栩锋's avatar
潘栩锋 committed
71
                gageService.StartP2(STARTP2_MODE.ORG);
潘栩锋's avatar
潘栩锋 committed
72 73 74
            });
            ForwCmd = new RelayCommand(() =>
            {
潘栩锋's avatar
潘栩锋 committed
75
                gageService.StartP2(STARTP2_MODE.FORW);
潘栩锋's avatar
潘栩锋 committed
76 77 78
            });
            BackwCmd = new RelayCommand(() =>
            {
潘栩锋's avatar
潘栩锋 committed
79
                gageService.StartP2(STARTP2_MODE.BACKW);
潘栩锋's avatar
潘栩锋 committed
80 81
            });
        }
82

潘栩锋's avatar
潘栩锋 committed
83 84 85 86 87 88 89
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="initParam"></param>
        /// <param name="gageService"></param>
        /// <param name="warningReasonWindow"></param>
        [InjectionMethod]
潘栩锋's avatar
潘栩锋 committed
90
        public void Init(
91 92 93
            IInitParamService initParam,
            ITDGageService gageService,
            WarningReasonWindow warningReasonWindow)
潘栩锋's avatar
潘栩锋 committed
94 95
        {

潘栩锋's avatar
潘栩锋 committed
96 97 98
            this.gageService = gageService;
            dynArea = gageService.DynArea;
            this.initParam = initParam;
潘栩锋's avatar
潘栩锋 committed
99 100
            mWindow = warningReasonWindow;

潘栩锋's avatar
潘栩锋 committed
101 102 103 104 105 106 107
            Misc.BindingOperations.SetBinding(dynArea, nameof(dynArea.Thk), this, nameof(Thick));
            Misc.BindingOperations.SetBinding(dynArea, nameof(dynArea.AD), this, nameof(AD));
            Misc.BindingOperations.SetBinding(dynArea, nameof(dynArea.ADMax), this, nameof(ADMax));
            Misc.BindingOperations.SetBinding(dynArea, nameof(dynArea.Position), this, nameof(Position));
            Misc.BindingOperations.SetBinding(dynArea, nameof(dynArea.Velocity), this, nameof(Velocity));
            Misc.BindingOperations.SetBinding(dynArea, nameof(dynArea.FilmVelocity), this, nameof(FilmVelocity));
            Misc.BindingOperations.SetBinding(dynArea, nameof(dynArea.ControllerState), () =>
潘栩锋's avatar
潘栩锋 committed
108
            {
潘栩锋's avatar
潘栩锋 committed
109
                FLY.Thick.Base.UI.Converter.ControllerStateConverter converter = new FLY.Thick.Base.UI.Converter.ControllerStateConverter();
潘栩锋's avatar
潘栩锋 committed
110
                ControllerState = (string)converter.Convert(dynArea.ControllerState, typeof(string), null, null);
潘栩锋's avatar
潘栩锋 committed
111 112
            });

潘栩锋's avatar
潘栩锋 committed
113
            Misc.BindingOperations.SetBinding(this.initParam, nameof(CtMicroGageVm.initParam.PosLength), this, nameof(PosLength));
潘栩锋's avatar
潘栩锋 committed
114 115 116

            Action action = () =>
            {
潘栩锋's avatar
潘栩锋 committed
117
                Posmm = dynArea.Position * this.initParam.Encoder1_mmpp;
潘栩锋's avatar
潘栩锋 committed
118
            };
潘栩锋's avatar
潘栩锋 committed
119 120
            Misc.BindingOperations.SetBinding(dynArea, nameof(dynArea.Position), action);
            Misc.BindingOperations.SetBinding(this.initParam, nameof(CtMicroGageVm.initParam.Encoder1_mmpp), action);
潘栩锋's avatar
潘栩锋 committed
121

潘栩锋's avatar
潘栩锋 committed
122 123
            Misc.BindingOperations.SetBinding(dynArea, nameof(dynArea.OStatus), this, nameof(OStatus));
            Misc.BindingOperations.SetBinding(dynArea, nameof(dynArea.IStatus), this, nameof(IStatus));
潘栩锋's avatar
潘栩锋 committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137


            //报警原因轮流显示
            timer_error = new DispatcherTimer();
            timer_error.Interval = TimeSpan.FromSeconds(3);
            timer_error.Tick += (s, e) =>
            {
                reason_list_index--;
                if (reason_list_index < 0)
                    reason_list_index = mWindow.Record.Count();
                UpdateError();
            };
            mWindow.Record.CollectionChanged += Record_CollectionChanged;

潘栩锋's avatar
潘栩锋 committed
138 139 140
            if (this.gageService is FObjBase.FObjServiceClient) {
                var client = this.gageService as FObjBase.FObjServiceClient;
                client.PropertyChanged += (s, e) =>
潘栩锋's avatar
潘栩锋 committed
141
                {
潘栩锋's avatar
潘栩锋 committed
142 143 144 145 146
                    if (e.PropertyName == nameof(FObjBase.FObjServiceClient.IsConnected)) {
                        UpdateError();
                    }
                };
            }
潘栩锋's avatar
潘栩锋 committed
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165

            UpdateError();

            
        }

        private void Record_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            FObjBase.PollModule.Current.Poll_JustOnce(
                new FObjBase.PollModule.PollHandler(delegate ()
                {
                    reason_list_index = mWindow.Record.Count() - 1;

                    UpdateError();
                }), this, MARKNO_UPDATEERROR);
        }

        void UpdateError()
        {
潘栩锋's avatar
潘栩锋 committed
166
            if (this.gageService is FObjBase.FObjServiceClient)
潘栩锋's avatar
潘栩锋 committed
167
            {
潘栩锋's avatar
潘栩锋 committed
168 169 170 171 172 173 174 175
                var client = this.gageService as FObjBase.FObjServiceClient;
                if (!client.IsConnected) {
                    ErrMsg = "服务器连接断开";
                    IsError = true;
                    reason_list_index = -1;
                    timer_error.Stop();
                    return;
                }
潘栩锋's avatar
潘栩锋 committed
176
            }
潘栩锋's avatar
潘栩锋 committed
177 178
            
            if (mWindow.Record.Count > 0)
潘栩锋's avatar
潘栩锋 committed
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
            {
                if (reason_list_index >= mWindow.Record.Count)
                    reason_list_index = mWindow.Record.Count - 1;
                else if (reason_list_index < 0)
                    reason_list_index = 0;


                ErrMsg = mWindow.Record[reason_list_index].Description;
                IsError = true;
                timer_error.Start();
            }
            else
            {
                IsError = false;
                ErrMsg = "";
                reason_list_index = -1;
                timer_error.Stop();
            }
        }
    }
}