CtMicroGageVm.cs 6.87 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1
using CommunityToolkit.Mvvm.Input;
2
using FLY.OBJComponents.IService;
潘栩锋's avatar
潘栩锋 committed
3
using FLY.Thick.Base.Common;
4
using FLY.Thick.Base.IService;
潘栩锋's avatar
潘栩锋 committed
5 6 7 8
using System;
using System.ComponentModel;
using System.Linq;
using System.Windows.Threading;
潘栩锋's avatar
潘栩锋 committed
9
using Unity;
潘栩锋's avatar
潘栩锋 committed
10

潘栩锋's avatar
潘栩锋 committed
11
namespace FLY.Thick.Base.UI
潘栩锋's avatar
潘栩锋 committed
12
{
潘栩锋's avatar
潘栩锋 committed
13
    class CtMicroGageVm : INotifyPropertyChanged
潘栩锋's avatar
潘栩锋 committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27
    {
        #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; }

28
        public double PosMm { get; set; }
潘栩锋's avatar
潘栩锋 committed
29
        public double Velocity { get; set; }
30
        public double FilmVelocity { get; set; }
潘栩锋's avatar
潘栩锋 committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
        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
51 52 53
        DynArea dynArea;
        ITDGageService gageService;
        IInitParamService initParam;
54
        IWarningSystem2Service warningSystem;
潘栩锋's avatar
潘栩锋 committed
55 56 57 58 59 60 61
        DispatcherTimer timer_error;
        private int reason_list_index = -1;

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

潘栩锋's avatar
潘栩锋 committed
78 79 80 81 82
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="initParam"></param>
        /// <param name="gageService"></param>
83
        /// <param name="warningSystem"></param>
潘栩锋's avatar
潘栩锋 committed
84
        [InjectionMethod]
潘栩锋's avatar
潘栩锋 committed
85
        public void Init(
86 87
            IInitParamService initParam,
            ITDGageService gageService,
88
            IWarningSystem2Service warningSystem)
潘栩锋's avatar
潘栩锋 committed
89 90
        {

潘栩锋's avatar
潘栩锋 committed
91 92 93
            this.gageService = gageService;
            dynArea = gageService.DynArea;
            this.initParam = initParam;
94
            this.warningSystem = warningSystem;
潘栩锋's avatar
潘栩锋 committed
95

潘栩锋's avatar
潘栩锋 committed
96 97 98 99
            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));
100
            Misc.BindingOperations.SetBinding(dynArea, nameof(dynArea.PosMm), this, nameof(PosMm));
潘栩锋's avatar
潘栩锋 committed
101 102 103
            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
104
            {
潘栩锋's avatar
潘栩锋 committed
105
                FLY.Thick.Base.UI.Converter.ControllerStateConverter converter = new FLY.Thick.Base.UI.Converter.ControllerStateConverter();
潘栩锋's avatar
潘栩锋 committed
106
                ControllerState = (string)converter.Convert(dynArea.ControllerState, typeof(string), null, null);
潘栩锋's avatar
潘栩锋 committed
107 108
            });

潘栩锋's avatar
潘栩锋 committed
109 110 111
            Misc.BindingOperations.SetBinding(this.initParam, nameof(CtMicroGageVm.initParam.PosLength), this, nameof(PosLength));
            Misc.BindingOperations.SetBinding(dynArea, nameof(dynArea.OStatus), this, nameof(OStatus));
            Misc.BindingOperations.SetBinding(dynArea, nameof(dynArea.IStatus), this, nameof(IStatus));
潘栩锋's avatar
潘栩锋 committed
112 113 114 115 116 117 118 119


            //报警原因轮流显示
            timer_error = new DispatcherTimer();
            timer_error.Interval = TimeSpan.FromSeconds(3);
            timer_error.Tick += (s, e) =>
            {
                reason_list_index--;
潘栩锋's avatar
潘栩锋 committed
120 121
                if (reason_list_index < 0)
                {
122 123 124 125 126
                    if (warningSystem.ReasonList != null && warningSystem.ReasonList.Count() > 0)
                        reason_list_index = warningSystem.ReasonList.Count() - 1;
                    else
                        reason_list_index = -1;
                }
潘栩锋's avatar
潘栩锋 committed
127 128
                UpdateError();
            };
129
            warningSystem.PropertyChanged += WarningSystem_PropertyChanged;
潘栩锋's avatar
潘栩锋 committed
130

潘栩锋's avatar
潘栩锋 committed
131 132
            if (this.gageService is FObjBase.FObjServiceClient)
            {
潘栩锋's avatar
潘栩锋 committed
133 134
                var client = this.gageService as FObjBase.FObjServiceClient;
                client.PropertyChanged += (s, e) =>
潘栩锋's avatar
潘栩锋 committed
135
                {
潘栩锋's avatar
潘栩锋 committed
136 137
                    if (e.PropertyName == nameof(FObjBase.FObjServiceClient.IsConnected))
                    {
潘栩锋's avatar
潘栩锋 committed
138 139 140 141
                        UpdateError();
                    }
                };
            }
潘栩锋's avatar
潘栩锋 committed
142 143 144

            UpdateError();

潘栩锋's avatar
潘栩锋 committed
145

潘栩锋's avatar
潘栩锋 committed
146 147
        }

148
        private void WarningSystem_PropertyChanged(object sender, PropertyChangedEventArgs e)
潘栩锋's avatar
潘栩锋 committed
149
        {
150 151 152 153 154 155
            if (e.PropertyName == nameof(warningSystem.ReasonList))
            {
                if (warningSystem.ReasonList != null && warningSystem.ReasonList.Count() > 0)
                    reason_list_index = warningSystem.ReasonList.Count() - 1;
                else
                    reason_list_index = -1;
潘栩锋's avatar
潘栩锋 committed
156

157 158
                UpdateError();
            }
潘栩锋's avatar
潘栩锋 committed
159 160 161 162
        }

        void UpdateError()
        {
潘栩锋's avatar
潘栩锋 committed
163
            if (this.gageService is FObjBase.FObjServiceClient)
潘栩锋's avatar
潘栩锋 committed
164
            {
潘栩锋's avatar
潘栩锋 committed
165
                var client = this.gageService as FObjBase.FObjServiceClient;
潘栩锋's avatar
潘栩锋 committed
166 167
                if (!client.IsConnected)
                {
潘栩锋's avatar
潘栩锋 committed
168 169 170 171 172 173
                    ErrMsg = "服务器连接断开";
                    IsError = true;
                    reason_list_index = -1;
                    timer_error.Stop();
                    return;
                }
潘栩锋's avatar
潘栩锋 committed
174
            }
175 176

            if (warningSystem.ReasonList != null && warningSystem.ReasonList.Count() > 0)
潘栩锋's avatar
潘栩锋 committed
177
            {
178 179
                if (reason_list_index >= warningSystem.ReasonList.Count())
                    reason_list_index = warningSystem.ReasonList.Count() - 1;
潘栩锋's avatar
潘栩锋 committed
180 181 182 183
                else if (reason_list_index < 0)
                    reason_list_index = 0;


184
                ErrMsg = warningSystem.ReasonList[reason_list_index].Description;
潘栩锋's avatar
潘栩锋 committed
185 186 187 188 189 190 191 192 193 194 195 196 197
                IsError = true;
                timer_error.Start();
            }
            else
            {
                IsError = false;
                ErrMsg = "";
                reason_list_index = -1;
                timer_error.Stop();
            }
        }
    }
}