using FLY.Thick.Blowing.Client;
using FLY.Thick.Blowing.IService;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
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;
namespace FLY.Thick.Blowing360.UI
{
class PgBlowingVm : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
#region 属性
#region 参数
///
/// 人字架 旋转1周 设置 时间。
/// 刚开机时,RenZiJiaPeriod = DefaultRPeriod;
/// 异常时,RenZiJiaPeriod = DefaultRPeriod;
///
public TimeSpan DefaultRPeriod { get; set; }
///
/// 人字架到测厚仪膜长 单位m
/// 当旋转架为立式时, 膜长为人字架复位时,的最短膜距离
///
public double FilmLength { get; set; }
///
/// 辊周长,单位mm
///
public double RollPerimeter { get; set; }
///
/// 使用编码器检测旋转架旋转位置模式
///
public bool IsRPosMode { get; set; }
///
/// 离开限位 到 撞下一个限位 的 旋转架转动总脉冲 单位 脉冲;
/// 通过看 转向信号列表,可以看到
///
public int RPosOfR { get; set; }
///
/// 自定义 输入口
///
public bool IsInNoCustomized { get; set; }
///
/// 自定义 转向信号 输入口序号 从0开始 默认9-1
///
public int InNo_Limit0 { get; set; } = 9 - 1;
///
/// 自定义 转向信号 输入口序号 从0开始 默认10-1
///
public int InNo_Limit1 { get; set; } = 10 - 1;
///
/// 自定义 辊速度 输入口序号 从0开始 默认11-1
///
public int InNo_Roll { get; set; } = 11 - 1;
#endregion
#region 分区设定
///
/// 加热通道数
///
public int ChannelCnt { get; set; }
///
/// 分区数/加热通道数
///
public int BPC { get; set; }
///
/// 人字架总分区数,从1开始数
///
public int NBolts => ChannelCnt * BPC;
///
/// 人字架复位时, 探头对应的分区位, 也就是 0° 对应的分区位
///
public int OrgBoltNo { get; set; }
///
/// 当人字架复位时,探头在右边
///
public bool IsProbeRight { get; set; } = true;
///
/// 分区1号 对应时钟的位置 1 ~ 12
///
public int No1InClock { get; set; } = 3;
///
/// 使用分区表
///
public bool IsUsedMap { get; set; }
///
/// 分区表,只能看,不能改!!!
///
public ObservableCollection BoltMap { get; } = new ObservableCollection();
#endregion
///
/// 数据加载中......
///
public bool IsBufListLoading { get; set; }
///
/// 换向信号列表
///
public ObservableCollection SignList { get; } = new ObservableCollection();
#endregion
#region Command
public RelayCommand ApplyCmd { get; }
public RelayCommand DownloadCmd { get; }
#endregion
public IBlowingFixService renZiJiaService { get; protected set; }
public IBlowingDetectService bDetect { get; protected set; }
public PgBlowingVm()
{
ApplyCmd = new RelayCommand(Apply);
DownloadCmd = new RelayCommand(Download);
}
public void Init(
IBlowingFixService blowingFixService,
IBlowingDetectService blowingDetectService)
{
renZiJiaService = blowingFixService;
bDetect = blowingDetectService;
Misc.BindingOperations.SetBinding(bDetect, nameof(bDetect.DefaultRPeriod), this, nameof(DefaultRPeriod));
Misc.BindingOperations.SetBinding(bDetect, nameof(bDetect.FilmLength), this, nameof(FilmLength));
Misc.BindingOperations.SetBinding(bDetect, nameof(bDetect.RollPerimeter), this, nameof(RollPerimeter));
Misc.BindingOperations.SetBinding(bDetect, nameof(bDetect.IsRPosMode), this, nameof(IsRPosMode));
Misc.BindingOperations.SetBinding(bDetect, nameof(bDetect.RPosOfR), this, nameof(RPosOfR));
Misc.BindingOperations.SetBinding(bDetect, nameof(bDetect.IsInNoCustomized), this, nameof(IsInNoCustomized));
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));
Misc.BindingOperations.SetBinding(renZiJiaService, nameof(renZiJiaService.IsProbeRight), this, nameof(IsProbeRight));
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));
Misc.BindingOperations.SetBinding(renZiJiaService, nameof(renZiJiaService.Map), ()=> {
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)
{
if ((e.PropertyName == nameof(No1InClock))|| (e.PropertyName == nameof(IsProbeRight)))
{
if (isSkipUpdate)
return;
isSkipUpdate = true;
updateOrgBoltNo();
isSkipUpdate = false;
}
else if (e.PropertyName == nameof(OrgBoltNo)) {
if (isSkipUpdate)
return;
isSkipUpdate = true;
updateNo1InClock();
isSkipUpdate = false;
}
}
bool isSkipUpdate = false;
void updateNo1InClock()
{
double orgBoltNo = OrgBoltNo;
if (orgBoltNo < 1 || orgBoltNo > NBolts)
orgBoltNo = 1;
int no1;
if (IsProbeRight)
{
no1 = (int)Math.Round(3 + 12 * (orgBoltNo - 1) / NBolts );
}
else
{
no1 = (int)Math.Round(9 + 12 * (orgBoltNo - 1) / NBolts);
}
if (no1 > 12)
no1 -= 12;
else if (no1 < 1)
no1 += 12;
No1InClock = no1;
}
void updateOrgBoltNo()
{
double no1 = No1InClock;
if (no1 < 1 || no1 > 12)
no1 = 3;
//平移,以3为0点
no1 -= 3;
if (no1 < 0)
no1 += 12;
int orgBoltNo;
if (IsProbeRight)
{
orgBoltNo = (int)Math.Round(NBolts * no1 / 12) + 1;
}
else
{
orgBoltNo = (int)Math.Round(NBolts * (no1+6) / 12) + 1;
}
if (orgBoltNo > NBolts)
orgBoltNo -= NBolts;
else if(orgBoltNo<1)
orgBoltNo += NBolts;
OrgBoltNo = orgBoltNo;
}
void Download()
{
IsBufListLoading = true;
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) =>
{
GetSignListReponse reponse = retData as GetSignListReponse;
IsBufListLoading = false;
//反转顺序
if (reponse.datas != null)
{
for (int i = 0; i < reponse.datas.Count(); i++)
{
int idx = reponse.datas.Count() - 1 - i;
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);
}
}
}, 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
//设置到服务
bDetect.DefaultRPeriod=this.DefaultRPeriod;
bDetect.FilmLength=this.FilmLength;
bDetect.RollPerimeter=this.RollPerimeter;
bDetect.IsRPosMode = this.IsRPosMode;
bDetect.RPosOfR = this.RPosOfR;
bDetect.IsInNoCustomized = this.IsInNoCustomized;
bDetect.InNo_Limit0 = this.InNo_Limit0;
bDetect.InNo_Limit1 = this.InNo_Limit1;
bDetect.InNo_Roll = this.InNo_Roll;
bDetect.Apply();
renZiJiaService.ChannelCnt=this.ChannelCnt;
renZiJiaService.BPC=this.BPC;
renZiJiaService.OrgBoltNo=this.OrgBoltNo;
renZiJiaService.IsUsedMap=this.IsUsedMap;
renZiJiaService.IsProbeRight = this.IsProbeRight;
renZiJiaService.Apply();
FLY.ControlLibrary.Window_Tip.Show("应用成功",
null,
TimeSpan.FromSeconds(2));
}
}
bool CheckValid(out string message)
{
if ((this.DefaultRPeriod.TotalMinutes < 3) || (this.DefaultRPeriod.TotalMinutes > 25))
{
message = "旋转时间";
return false;
}
if ((this.RPosOfR < 1000))
{
message = "旋转总脉冲";
return false;
}
if ((this.RollPerimeter < 100) || (this.RollPerimeter > 1000))
{
message = "辊周长";
return false;
}
if ((this.FilmLength < 0) || (this.FilmLength > 100))
{
message = "膜距离";
return false;
}
if (this.ChannelCnt < 20 || this.ChannelCnt > 160)
{
message = "加热通道数";
return false;
}
if (this.BPC < 1 || this.BPC > 10)
{
message = "分区数/加热通道数";
return false;
}
if (this.OrgBoltNo < 1 || this.OrgBoltNo > (this.ChannelCnt * this.BPC))
{
message = "复位区号";
return false;
}
message = "成功";
return true;
}
}
///
/// 转向信号 视图
///
public class SignDataView : SignData
{
///
/// 时间间隔
///
public TimeSpan Interval { get; set; }
///
/// 与上一个On脉冲间隔
///
public int IntervalRPos { get; set; }
}
}