PgBlowingVm.cs 13.2 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1
using FLY.Thick.Blowing.Client;
2 3
using FLY.Thick.Blowing.IService;
using System;
潘栩锋's avatar
潘栩锋 committed
4
using System.Collections.Generic;
5 6 7
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
潘栩锋's avatar
潘栩锋 committed
8 9 10 11 12 13 14 15 16
using System.Net;
using System.Text;
using System.Threading.Tasks;
using FLY.Thick.Base.UI;
using GalaSoft.MvvmLight.Command;
using System.Threading;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Information;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Logical;
using Unity.Injection;
17 18 19 20 21 22 23 24 25

namespace FLY.Thick.Blowing360.UI
{
    class PgBlowingVm : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        #region 属性
        #region 参数

潘栩锋's avatar
潘栩锋 committed
26

27
        /// <summary>
潘栩锋's avatar
潘栩锋 committed
28 29 30
        /// 人字架 旋转1周 设置 时间。 
        /// 刚开机时,RenZiJiaPeriod = DefaultRPeriod;
        /// 异常时,RenZiJiaPeriod = DefaultRPeriod;
潘栩锋's avatar
潘栩锋 committed
31
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
32
        public TimeSpan DefaultRPeriod { get; set; }
潘栩锋's avatar
潘栩锋 committed
33

34

潘栩锋's avatar
潘栩锋 committed
35

36 37 38 39 40 41 42 43 44 45
        /// <summary>
        /// 人字架到测厚仪膜长 单位m
        /// 当旋转架为立式时, 膜长为人字架复位时,的最短膜距离
        /// </summary>
        public double FilmLength { get; set; }

        /// <summary>
        /// 辊周长,单位mm
        /// </summary>
        public double RollPerimeter { get; set; }
潘栩锋's avatar
潘栩锋 committed
46 47


潘栩锋's avatar
潘栩锋 committed
48 49 50 51 52 53 54 55 56 57 58
        /// <summary>
        /// 使用编码器检测旋转架旋转位置模式
        /// </summary>
        public bool IsRPosMode { get; set; }

        /// <summary>
        /// 离开限位 到 撞下一个限位 的 旋转架转动总脉冲 单位 脉冲;
        /// 通过看 转向信号列表,可以看到
        /// </summary>
        public int RPosOfR { get; set; }

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
        /// <summary>
        /// 自定义 转向信号 输入口序号 从0开始 默认9-1
        /// </summary>
        public int InNo_Limit0 { get; set; } = 9 - 1;

        /// <summary>
        /// 自定义 转向信号 输入口序号 从0开始 默认10-1
        /// </summary>
        public int InNo_Limit1 { get; set; } = 10 - 1;

        /// <summary>
        /// 自定义 辊速度 输入口序号 从0开始 默认11-1
        /// </summary>
        public int InNo_Roll { get; set; } = 11 - 1;

74 75 76 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 115 116 117
        #endregion
        #region 分区设定
        /// <summary>
        /// 加热通道数
        /// </summary>
        public int ChannelCnt { get; set; }

        /// <summary>
        /// 分区数/加热通道数
        /// </summary>
        public int BPC { get; set; }

        /// <summary>
        /// 人字架总分区数,从1开始数
        /// </summary>
        public int NBolts => ChannelCnt * BPC;

        /// <summary>
        /// 人字架复位时, 探头对应的分区位, 也就是 0° 对应的分区位
        /// </summary>
        public int OrgBoltNo { get; set; }
        /// <summary>
        /// 当人字架复位时,探头在右边
        /// </summary>
        public bool IsProbeRight { get; set; } = true;
        /// <summary>
        /// 分区1号 对应时钟的位置  1 ~ 12
        /// </summary>
        public int No1InClock { get; set; } = 3;
        /// <summary>
        /// 使用分区表
        /// </summary>
        public bool IsUsedMap { get; set; }

        /// <summary>
        /// 分区表,只能看,不能改!!!
        /// </summary>
        public ObservableCollection<BoltMapCell> BoltMap { get; } = new ObservableCollection<BoltMapCell>();
        #endregion
        /// <summary>
        /// 数据加载中......
        /// </summary>
        public bool IsBufListLoading { get; set; }
        /// <summary>
潘栩锋's avatar
潘栩锋 committed
118
        /// 换向信号列表
119
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
120
        public ObservableCollection<SignDataView> SignList { get; } = new ObservableCollection<SignDataView>();
潘栩锋's avatar
潘栩锋 committed
121

122 123 124 125 126 127
        #endregion

        #region Command
        public RelayCommand ApplyCmd { get; }
        public RelayCommand DownloadCmd { get; }
        #endregion
潘栩锋's avatar
潘栩锋 committed
128 129
        public IBlowingFixService renZiJiaService { get; protected set; }
        public IBlowingDetectService bDetect { get; protected set; }
130 131 132 133 134 135 136 137 138 139 140

        public PgBlowingVm()
        {
            ApplyCmd = new RelayCommand(Apply);
            DownloadCmd = new RelayCommand(Download);
        }



        public void Init(
            IBlowingFixService blowingFixService,
潘栩锋's avatar
潘栩锋 committed
141
            IBlowingDetectService blowingDetectService)
142 143 144 145
        {
            renZiJiaService = blowingFixService;
            bDetect = blowingDetectService;

潘栩锋's avatar
潘栩锋 committed
146
            Misc.BindingOperations.SetBinding(bDetect, nameof(bDetect.DefaultRPeriod), this, nameof(DefaultRPeriod));
147 148
            Misc.BindingOperations.SetBinding(bDetect, nameof(bDetect.FilmLength), this, nameof(FilmLength));
            Misc.BindingOperations.SetBinding(bDetect, nameof(bDetect.RollPerimeter), this, nameof(RollPerimeter));
潘栩锋's avatar
潘栩锋 committed
149 150
            Misc.BindingOperations.SetBinding(bDetect, nameof(bDetect.IsRPosMode), this, nameof(IsRPosMode));
            Misc.BindingOperations.SetBinding(bDetect, nameof(bDetect.RPosOfR), this, nameof(RPosOfR));
151

152 153 154 155
            Misc.BindingOperations.SetBinding(bDetect, nameof(bDetect.InNo_Limit0), this, nameof(InNo_Limit0));
            Misc.BindingOperations.SetBinding(bDetect, nameof(bDetect.InNo_Limit1), this, nameof(InNo_Limit1));
            Misc.BindingOperations.SetBinding(bDetect, nameof(bDetect.InNo_Roll), this, nameof(InNo_Roll));

潘栩锋's avatar
潘栩锋 committed
156

157
            Misc.BindingOperations.SetBinding(renZiJiaService, nameof(renZiJiaService.IsProbeRight), this, nameof(IsProbeRight));
潘栩锋's avatar
潘栩锋 committed
158

159 160 161 162
            Misc.BindingOperations.SetBinding(renZiJiaService, nameof(renZiJiaService.ChannelCnt), this, nameof(ChannelCnt));
            Misc.BindingOperations.SetBinding(renZiJiaService, nameof(renZiJiaService.BPC), this, nameof(BPC));
            Misc.BindingOperations.SetBinding(renZiJiaService, nameof(renZiJiaService.OrgBoltNo), this, nameof(OrgBoltNo));
            Misc.BindingOperations.SetBinding(renZiJiaService, nameof(renZiJiaService.IsUsedMap), this, nameof(IsUsedMap));
潘栩锋's avatar
潘栩锋 committed
163
            Misc.BindingOperations.SetBinding(renZiJiaService, nameof(renZiJiaService.Map), ()=> {
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
                BoltMap.Clear();
                if (renZiJiaService.Map != null)
                {
                    foreach (BoltMapCell m in renZiJiaService.Map)
                    {
                        BoltMap.Add(m);
                    }
                }
            });

            updateNo1InClock();
            this.PropertyChanged += PgBlowingVm_PropertyChanged;
        }

        private void PgBlowingVm_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
潘栩锋's avatar
潘栩锋 committed
180
            if ((e.PropertyName == nameof(No1InClock))|| (e.PropertyName == nameof(IsProbeRight)))
181 182 183 184 185 186 187
            {
                if (isSkipUpdate)
                    return;
                isSkipUpdate = true;
                updateOrgBoltNo();
                isSkipUpdate = false;
            }
潘栩锋's avatar
潘栩锋 committed
188
            else if (e.PropertyName == nameof(OrgBoltNo)) {
189 190 191 192 193 194 195 196 197

                if (isSkipUpdate)
                    return;
                isSkipUpdate = true;
                updateNo1InClock();
                isSkipUpdate = false;
            }
        }
        bool isSkipUpdate = false;
潘栩锋's avatar
潘栩锋 committed
198
        void updateNo1InClock() 
199 200 201 202 203 204 205 206 207
        {
            double orgBoltNo = OrgBoltNo;
            if (orgBoltNo < 1 || orgBoltNo > NBolts)
                orgBoltNo = 1;

            int no1;

            if (IsProbeRight)
            {
潘栩锋's avatar
潘栩锋 committed
208
                no1 = (int)Math.Round(3 + 12 * (orgBoltNo - 1) / NBolts );
209 210 211 212 213 214 215 216 217 218 219 220
            }
            else
            {
                no1 = (int)Math.Round(9 + 12 * (orgBoltNo - 1) / NBolts);
            }
            if (no1 > 12)
                no1 -= 12;
            else if (no1 < 1)
                no1 += 12;

            No1InClock = no1;
        }
潘栩锋's avatar
潘栩锋 committed
221
        void updateOrgBoltNo() 
222 223
        {
            double no1 = No1InClock;
潘栩锋's avatar
潘栩锋 committed
224
            if (no1 < 1 || no1 > 12) 
225
                no1 = 3;
潘栩锋's avatar
潘栩锋 committed
226
            
227 228 229 230 231 232 233 234 235 236
            //平移,以3为0点
            no1 -= 3;
            if (no1 < 0)
                no1 += 12;

            int orgBoltNo;
            if (IsProbeRight)
            {
                orgBoltNo = (int)Math.Round(NBolts * no1 / 12) + 1;
            }
潘栩锋's avatar
潘栩锋 committed
237
            else 
238
            {
潘栩锋's avatar
潘栩锋 committed
239
                orgBoltNo = (int)Math.Round(NBolts * (no1+6) / 12) + 1;
240 241 242
            }
            if (orgBoltNo > NBolts)
                orgBoltNo -= NBolts;
潘栩锋's avatar
潘栩锋 committed
243
            else if(orgBoltNo<1)
244 245 246 247 248 249 250 251
                orgBoltNo += NBolts;

            OrgBoltNo = orgBoltNo;
        }

        void Download()
        {
            IsBufListLoading = true;
潘栩锋's avatar
潘栩锋 committed
252 253 254 255 256 257 258 259 260 261 262
            DateTime beginTime = DateTime.Now - TimeSpan.FromMinutes(30);
            if (SignList.Count() > 0) {
                var dt = SignList.First().Time;
                if (dt > beginTime)
                    beginTime = dt;
                else {
                    SignList.Clear();
                }
            }
            bDetect.GetSignList(beginTime,
                (asyncContext, retData) =>
263
                {
潘栩锋's avatar
潘栩锋 committed
264
                    GetSignListReponse reponse = retData as GetSignListReponse;
潘栩锋's avatar
潘栩锋 committed
265
                    
266
                    IsBufListLoading = false;
潘栩锋's avatar
潘栩锋 committed
267 268 269 270 271

                    //反转顺序
                    if (reponse.datas != null)
                    {
                        for (int i = 0; i < reponse.datas.Count(); i++)
272
                        {
潘栩锋's avatar
潘栩锋 committed
273
                            int idx = reponse.datas.Count() - 1 - i;
潘栩锋's avatar
潘栩锋 committed
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
                            int idx2 = idx - 1;
                            var sd = reponse.datas[idx];
                            SignDataView sdv = new SignDataView()
                            {
                                Time = sd.Time,
                                No = sd.No,
                                On = sd.On,
                                GlobalRPos = sd.GlobalRPos
                            };
                            
                            if (idx2 >= 0) {
                                var sd2 = reponse.datas[idx2];
                                sdv.Interval = sd.Time - sd2.Time;
                                sdv.IntervalRPos = sd.GlobalRPos - sd2.GlobalRPos;
                            }

                            SignList.Insert(i, sdv);
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
                        }
                    }

                }, null);
        }

        void Apply()
        {
            if (!WdPassword.Authorize("Blowing"))
                return;

            string message;
            bool isvalid = CheckValid(out message);
            if (!isvalid)
            {
                //异常出错
                FLY.ControlLibrary.Window_WarningTip.Show(
                    "参数出错",
                    message + "异常");
            }
            else
            {
                //TODO
                //设置到服务
潘栩锋's avatar
潘栩锋 committed
315 316 317 318 319 320
                bDetect.DefaultRPeriod=this.DefaultRPeriod;
                bDetect.FilmLength=this.FilmLength;
                bDetect.RollPerimeter=this.RollPerimeter;
                bDetect.IsRPosMode = this.IsRPosMode;
                bDetect.RPosOfR = this.RPosOfR;

321 322 323 324
                bDetect.InNo_Limit0 = this.InNo_Limit0;
                bDetect.InNo_Limit1 = this.InNo_Limit1;
                bDetect.InNo_Roll = this.InNo_Roll;

325 326
                bDetect.Apply();

潘栩锋's avatar
潘栩锋 committed
327 328 329 330
                renZiJiaService.ChannelCnt=this.ChannelCnt;
                renZiJiaService.BPC=this.BPC;
                renZiJiaService.OrgBoltNo=this.OrgBoltNo;
                renZiJiaService.IsUsedMap=this.IsUsedMap;
331 332 333 334 335 336 337
                renZiJiaService.IsProbeRight = this.IsProbeRight;
                renZiJiaService.Apply();

                FLY.ControlLibrary.Window_Tip.Show("应用成功",
                    null,
                    TimeSpan.FromSeconds(2));
            }
潘栩锋's avatar
潘栩锋 committed
338
            
339 340 341
        }

        bool CheckValid(out string message)
潘栩锋's avatar
潘栩锋 committed
342 343 344 345
        {


            if ((this.DefaultRPeriod.TotalMinutes < 3) || (this.DefaultRPeriod.TotalMinutes > 25))
潘栩锋's avatar
潘栩锋 committed
346
            {
潘栩锋's avatar
潘栩锋 committed
347
                message = "旋转时间";
348 349
                return false;
            }
潘栩锋's avatar
潘栩锋 committed
350

潘栩锋's avatar
潘栩锋 committed
351 352 353 354 355 356
            if ((this.RPosOfR < 1000))
            {
                message = "旋转总脉冲";
                return false;
            }

357 358 359 360 361 362 363 364 365 366 367
            if ((this.RollPerimeter < 100) || (this.RollPerimeter > 1000))
            {
                message = "辊周长";
                return false;
            }
            if ((this.FilmLength < 0) || (this.FilmLength > 100))
            {
                message = "膜距离";
                return false;
            }

潘栩锋's avatar
潘栩锋 committed
368
            if (this.ChannelCnt < 20 || this.ChannelCnt > 160)
369 370 371 372
            {
                message = "加热通道数";
                return false;
            }
潘栩锋's avatar
潘栩锋 committed
373
            if (this.BPC < 1 || this.BPC > 10)
374 375 376 377 378 379 380 381 382 383 384 385 386
            {
                message = "分区数/加热通道数";
                return false;
            }
            if (this.OrgBoltNo < 1 || this.OrgBoltNo > (this.ChannelCnt * this.BPC))
            {
                message = "复位区号";
                return false;
            }
            message = "成功";
            return true;
        }
    }
潘栩锋's avatar
潘栩锋 committed
387

潘栩锋's avatar
潘栩锋 committed
388 389 390 391 392 393 394 395 396 397 398 399 400 401
    /// <summary>
    /// 转向信号 视图
    /// </summary>
    public class SignDataView : SignData
    {
        /// <summary>
        /// 时间间隔
        /// </summary>
        public TimeSpan Interval { get; set; }
        /// <summary>
        /// 与上一个On脉冲间隔
        /// </summary>
        public int IntervalRPos { get; set; }
    }
402 403 404


}