using Community;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Services.Description;
using System.Windows;
namespace Model
{
///
/// 机种类
///
public class MachineModel : NotificationObject
{
///
/// ID
///
public int ID { get; set; }
private string machineName;
///
/// 机种名称
///
public string MachineName
{
get { return machineName; }
set
{
machineName = value;
this.RaisePropertyChanged("MachineName");
}
}
private ObservableCollection measureRangeValue;
///
/// 测量范围值
///
public ObservableCollection MeasureRangeValue
{
get { return measureRangeValue; }
set
{
measureRangeValue = value != null ? value : new ObservableCollection();
this.RaisePropertyChanged("MeasureRangeValue");
}
}
private DateTime addTime;
///
/// 添加时间
///
public DateTime AddTime
{
get { return addTime; }
set
{
addTime = value;
this.RaisePropertyChanged("AddTime");
}
}
private bool enableS;
///
/// 是否启用
///
public bool EnableS
{
get { return enableS; }
set
{
enableS = value;
this.RaisePropertyChanged("EnableS");
}
}
private long cumulativeQuantity;
///
/// 累计生产数量
///
public long CumulativeQuantity
{
get { return cumulativeQuantity; }
set
{
cumulativeQuantity = value;
try
{
if (value > 0 && CumulativeProduce > 0)
{
CumulativeRejectRatio = Math.Round(((decimal)CumulativeProduce / value), 4);
}
}
catch
{
}
this.RaisePropertyChanged("CumulativeQuantity");
}
}
private int cumulativeProduce;
///
/// 累计产出
///
public int CumulativeProduce
{
get { return cumulativeProduce; }
set
{
cumulativeProduce = value;
try
{
if (value > 0 && CumulativeQuantity > 0)
{
CumulativeRejectRatio = Math.Round(((decimal)value / CumulativeQuantity), 4);
}
}
catch (Exception)
{
}
this.RaisePropertyChanged("CumulativeProduce");
}
}
private int cumulativeBads;
///
/// 累计不良
///
public int CumulativeBads
{
get { return cumulativeBads; }
set
{
cumulativeBads = value;
this.RaisePropertyChanged("CumulativeBads");
}
}
private decimal cumulativeRejectRatio;
///
/// 累计良率
///
public decimal CumulativeRejectRatio
{
get { return cumulativeRejectRatio; }
set
{
cumulativeRejectRatio = value;
this.RaisePropertyChanged("CumulativeRejectRatio");
}
}
private int quantitys;
///
/// 生产数量(投入数量)
///
public int Quantitys
{
get { return quantitys; }
set
{
if (value > 0 && quantitys > 0 && value > quantitys)
{
if (CumulativeQuantity > 0)
{
CumulativeQuantity += (value - quantitys);
}
else
{
CumulativeQuantity = value;
}
}
quantitys = value;
try
{
if (value > 0 && Produce > 0)
{
RejectRatio = Math.Round(((decimal)Produce / value), 4);
}
}
catch
{
}
this.RaisePropertyChanged("Quantitys");
}
}
private int produce;
///
/// 产出数量
///
public int Produce
{
get { return produce; }
set
{
if (value > 0 && produce > 0 && value > produce)
{
if (CumulativeProduce > 0)
{
CumulativeProduce += (value - produce);
}
else
{
CumulativeProduce = value;
}
}
if (CumulativeProduce <= 0)
{
CumulativeProduce = value;
}
produce = value;
try
{
if (value > 0 && Quantitys > 0)
{
RejectRatio = Math.Round(((decimal)value / Quantitys), 4);
}
}
catch
{
}
this.RaisePropertyChanged("Produce");
}
}
private int bads;
///
/// 不良数量
///
public int Bads
{
get { return bads; }
set
{
if (value > 0 && bads > 0 && value > bads)
{
CumulativeBads += (value - bads);
}
if (CumulativeBads <= 0)
{
CumulativeBads = value;
}
bads = value;
this.RaisePropertyChanged("Bads");
}
}
private decimal rejectRatio;
///
/// 良率
///
public decimal RejectRatio
{
get { return rejectRatio; }
set
{
rejectRatio = value;
this.RaisePropertyChanged("RejectRatio");
}
}
private double capacity;
///
/// 产能
///
public double Capacity
{
get { return capacity; }
set
{
capacity = value;
this.RaisePropertyChanged("Capacity");
}
}
private bool runningMode;
///
/// 空跑模式
///
public bool RunningMode
{
get { return runningMode; }
set
{
runningMode = value;
this.RaisePropertyChanged("RunningMode");
}
}
private MyParmeterModel myPar;
///
/// 其他参数
///
public MyParmeterModel MyPar
{
get { return myPar; }
set
{
myPar = value == null ? new MyParmeterModel() : value;
this.RaisePropertyChanged("MyPar");
}
}
private ObservableCollection commandGroupList;
///
/// 指令组集合
///
public ObservableCollection CommandGroupList
{
get { return commandGroupList; }
set
{
commandGroupList = value != null ? value : new ObservableCollection() {
new CommandGroup(){ Number=1, Enable=true,NameStr="新来的"}
};
//RaisePropertyChanged("CommandGroupList");
}
}
#region 机器人点位参数集合
private List robotPointList;
///
/// 机器人点位参数集合
///
public List RobotPointList
{
get { return robotPointList; }
set
{
robotPointList = value;
this.RaisePropertyChanged("RobotPointList");
}
}
#endregion
public MachineModel()
{
RejectRatio = 1;
MyPar = new MyParmeterModel();
CommandGroupList = new ObservableCollection() { new CommandGroup() { Number = 1, Enable = true, NameStr = "新来的" } };
}
}
///
/// 结果对象
///
public class RangeValueModel : NotificationObject
{
private int rangeType;
///
/// 解析方式 0:不解析 1:数值 2:ASCII
///
public int RangeType
{
get { return rangeType; }
set
{
rangeType = value;
RaisePropertyChanged("RangeType");
}
}
private double maxValue;
///
/// 最大值
///
public double MaxValue
{
get { return maxValue; }
set
{
maxValue = value;
RaisePropertyChanged("MaxValue");
}
}
private double minValue;
///
/// 最小值
///
public double MinValue
{
get { return minValue; }
set
{
minValue = value;
RaisePropertyChanged("MinValue");
}
}
private string baseValue;
///
/// 标准件值
///
public string BaseValue
{
get { return baseValue; }
set
{
baseValue = value;
RaisePropertyChanged("BaseValue");
}
}
private string actualValue;
///
/// 实际值
///
public string ActualValue
{
get { return actualValue; }
set
{
actualValue = value;
RaisePropertyChanged("ActualValue");
}
}
private double fixValue;
///
/// 对标差
///
public double FixValue
{
get { return fixValue; }
set
{
fixValue = value;
RaisePropertyChanged("FixValue");
}
}
private double scalingFactor;
///
/// 结果换算系数
///
public double ScalingFactor
{
get { return scalingFactor; }
set { scalingFactor = value; RaisePropertyChanged("ScalingFactor"); }
}
private int result;
///
/// 结果 0 未测 1 OK 2 NG
///
public int Result
{
get { return result; }
set
{
result = value;
switch (value)
{
case 1:
ResultStr = "OK";
break;
case 2:
ResultStr = "NG";
break;
default:
ResultStr = "";
break;
}
RaisePropertyChanged("Result");
}
}
private string resultStr;
///
/// 结果字符串
///
public string ResultStr
{
get { return resultStr; }
set
{
resultStr = value;
RaisePropertyChanged("ResultStr");
}
}
private bool notControlled;
///
/// 如果不受控 有值就OK
///
public bool NotControlled
{
get { return notControlled; }
set { notControlled = value; RaisePropertyChanged("NotControlled"); }
}
///
/// 计算结果
///
public int GetResult()
{
if (NotControlled)//不受控 直接Ok
{
if (!string.IsNullOrEmpty(ActualValue))
{
Result = 1;
}else
{
Result = 2;
}
}
else if (RangeType == 1)
{
if (!string.IsNullOrEmpty(ActualValue))
{
double res = Convert.ToDouble(ActualValue);
Result = res >= MinValue && res <= MaxValue ? 1 : 2;
}else
{
Result = 2;
}
}
else
{
if (!string.IsNullOrEmpty(BaseValue)&&!string.IsNullOrEmpty(ActualValue))
{
Result = ActualValue.Contains(BaseValue) ? 1 : 2;
}else
{
Result = 2;
}
}
return Result;
}
public RangeValueModel()
{
ScalingFactor = 1;
}
}
///
/// 其他参数模型
///
public class MyParmeterModel : NotificationObject
{
private int testTime;
///
/// 测试时间
///
public int TestTime
{
get { return testTime; }
set { testTime = value; RaisePropertyChanged("TestTime"); }
}
private double testHeatMin;
///
/// 测试温度Min
///
public double TestHeatMin
{
get { return testHeatMin; }
set { testHeatMin = value; RaisePropertyChanged("TestHeatMin"); }
}
private double testHeatMax;
///
/// 测试温度Max
///
public double TestHeatMax
{
get { return testHeatMax; }
set { testHeatMax = value; RaisePropertyChanged("TestHeatMax"); }
}
private int testModel;
///
/// 测试模式 0:Can 1:Lin
///
public int TestModel
{
get { return testModel; }
set { testModel = value; RaisePropertyChanged("TestModel"); }
}
///
/// 产品状态集合
///
public List StatusList { get; set; }
}
///
/// 指令组对象模型
///
public class CommandGroup : NotificationObject
{
private int number;
///
/// 序号
///
public int Number
{
get { return number; }
set { number = value; RaisePropertyChanged("Number"); }
}
private string nameStr;
///
/// 名称
///
public string NameStr
{
get { return nameStr; }
set { nameStr = value; RaisePropertyChanged("NameStr"); }
}
private bool enable;
///
/// 启用
///
public bool Enable
{
get { return enable; }
set { enable = value; RaisePropertyChanged("Enable"); }
}
private string notes;
///
/// 备注
///
public string Notes
{
get { return notes; }
set { notes = value; RaisePropertyChanged("Notes"); }
}
private ObservableCollection commandList;
///
/// 指令集合
///
public ObservableCollection CommandList
{
get { return commandList; }
set
{
commandList = value == null ? new ObservableCollection() : value;
RaisePropertyChanged("CommandList");
}
}
///
/// 运行是否完成
///
public bool RunOK { get; set; }
///
/// 结果
///
public bool ResOK { get; set; }
public CommandGroup()
{
CommandList = new ObservableCollection() { new CommandModel() };
}
}
///
/// 指令采集模型
///
public class CommandModel : NotificationObject
{
private string id;
///
/// 指令ID (hex)
///
public string ID
{
get { return id; }
set { id = value; RaisePropertyChanged("ID"); }
}
private string txID;
///
/// 请求ID
///
public string TxID
{
get { return txID; }
set { txID = value; RaisePropertyChanged("TxID"); }
}
private string rxID;
///
/// 响应ID
///
public string RxID
{
get { return rxID; }
set { rxID = value; RaisePropertyChanged("RxID"); }
}
private bool enable;
///
/// 启用
///
public bool Enable
{
get { return enable; }
set { enable = value; RaisePropertyChanged("Enable"); }
}
private string nameStr;
///
/// 指令名称
///
public string NameStr
{
get { return nameStr; }
set
{
nameStr = value;
RaisePropertyChanged("NameStr");
}
}
private int mytype;
///
/// 类型 1:工厂模式 2:头部+响应 3:读取电流
///
public int MyType
{
get { return mytype; }
set
{
mytype = value;
RaisePropertyChanged("MyType");
}
}
private int timeInterval;
///
/// 时间间隔(毫秒)
///
public int TimeInterval
{
get { return timeInterval; }
set
{
timeInterval = value;
RaisePropertyChanged("TimeInterval");
}
}
private string dataHex;
///
/// 指令数据 (hex)
///
public string DataHex
{
get { return dataHex; }
set
{
dataHex = value;
RaisePropertyChanged("DataHex");
}
}
private int dataLength;
///
/// 数据长度
///
public int DataLength
{
get { return dataLength; }
set
{
dataLength = value;
RaisePropertyChanged("DataLength");
}
}
private int parsingModel;
///
/// 解析方式 0:不解析 1:数值 2:ASCII 3:带通道号的数字解析 4:不解析,隐藏显示
///
public int ParsingModel
{
get { return parsingModel; }
set
{
parsingModel = value;
RaisePropertyChanged("ParsingModel");
}
}
///
/// 运行完成
///
public bool RunOK { get; set; }
///
/// 结果
///
public bool ResOK { get; set; }
private int sendCount;
///
/// 发送次数
///
public int SendCount
{
get { return sendCount; }
set
{
sendCount = value;
RaisePropertyChanged("SendCount");
}
}
private string returnValue;
///
/// 返回值
///
public string ReturnValue
{
get { return returnValue; }
set
{
returnValue = value;
RaisePropertyChanged("ReturnValue");
}
}
private RangeValueModel myValue;
///
/// 执行结果 用于记录测试值
///
public RangeValueModel MyValue
{
get { return myValue; }
set
{
myValue = value ?? new RangeValueModel();
RaisePropertyChanged("MyValue");
}
}
///
/// 结果解析
///
public void ParsingResult()
{
try
{
List strArr = ReturnValue.Trim().Split('-').ToList();
DataLength = DataLength >= 64 ? 8 : DataLength;//如果需要的字符长度大于等64 则直接取8位 追溯码 特殊情况
strArr = strArr.GetRange(5, DataLength);
if (ParsingModel == 0)
{
MyValue.ActualValue = string.Join("-", strArr);
}else
if (ParsingModel == 1)
{
// Hex转byte数组
byte[] dataBytes = strArr.Select(h => Convert.ToByte(h, 16)).ToArray();
// 1. 大端 32位无符号整数
// uint bigEndUint = BitConverter.ToUInt32(dataBytes.Reverse().ToArray(), 0);
// 2. 小端 32位无符号整数
uint res = BitConverter.ToUInt32(dataBytes, 0);
double jg = Math.Round((res * MyValue.ScalingFactor), 6);//转换系数 为了到达客户需求单位
MyValue.ActualValue = jg.ToString();
}
else if (ParsingModel == 2)
{
// Hex转byte数组
byte[] dataBytes = strArr.Select(h => Convert.ToByte(h, 16)).ToArray();
//按照指定编码将字节数组变为字符串
MyValue.ActualValue = System.Text.Encoding.ASCII.GetString(dataBytes);
}
else if (ParsingModel == 3)
{
strArr = strArr.GetRange(1, strArr.Count - 1);
if (strArr.Count > 2)
{
// Hex转byte数组
byte[] dataBytes = strArr.Select(h => Convert.ToByte(h, 16)).ToArray();
// 1. 大端 32位无符号整数
//uint res = BitConverter.ToUInt32(dataBytes.Reverse().ToArray(), 0);
// 2. 小端 32位无符号整数
uint res = BitConverter.ToUInt32(dataBytes, 0);
double jg = Math.Round((res * MyValue.ScalingFactor), 6);//转换系数 为了到达客户需求单位
MyValue.ActualValue = jg.ToString();
}
else
{
// Hex转byte数组
byte[] dataBytes = strArr.Select(h => Convert.ToByte(h, 16)).ToArray();
// 大端ushort
//ushort res = BitConverter.ToUInt16(dataBytes.Reverse().ToArray(), 0);
// 小端ushort
ushort res = BitConverter.ToUInt16(dataBytes, 0);
double jg = Math.Round((res * MyValue.ScalingFactor), 6);//转换系数 为了到达客户需求单位
MyValue.ActualValue = jg.ToString();
}
}
//结算结果
MyValue.RangeType = ParsingModel;
MyValue.GetResult();
}
catch (Exception)
{
}
}
public CommandModel()
{
NameStr = "新来的";
MyValue = new RangeValueModel();
}
}
///
/// 指令返回值模型
///
public class CommandReceive : NotificationObject
{
private string dir;
///
/// TX代表PC-->MCU,RX代表MCU-->PC
///
public string Dir
{
get { return dir; }
set
{
dir = value;
RaisePropertyChanged("ID");
}
}
private string id;
///
/// 放回ID
///
public string ID
{
get { return id; }
set
{
id = value;
RaisePropertyChanged("ID");
}
}
private int datalength;
///
/// 数据长度
///
public int Datalength
{
get { return datalength; }
set { datalength = value; RaisePropertyChanged("Datalength"); }
}
private int hexDataLength;
///
/// 返回值Hex长度
///
public int HexDataLength
{
get { return hexDataLength; }
set { hexDataLength = value; }
}
private string dataHex;
///
/// 返回数据的Hex格式
///
public string DataHex
{
get { return dataHex; }
set
{
dataHex = value;
ReceiptCompleted = GetLenght(value);
RaisePropertyChanged("DataHex");
}
}
///
/// 数据接受完成
///
public bool ReceiptCompleted { get; set; }
///
/// 获取数据长度
///
///
///
public bool GetLenght(string hex)
{
bool bl = false;
try
{
if (!string.IsNullOrEmpty(hex))
{
List strArr = hex.Split('-').ToList();
if (strArr.Count >= 8)
{
HexDataLength = strArr.Count - 6;//减6的目的是 去出头字符以及校验码
strArr = strArr.GetRange(2, 2);
// Hex转byte数组
byte[] dataBytes = strArr.Select(h => Convert.ToByte(h, 16)).ToArray();
// 1. 大端 32位无符号整数
//Datalength = BitConverter.ToUInt16(dataBytes.Reverse().ToArray(), 0);
// 2. 小端 32位无符号整数
Datalength = BitConverter.ToUInt16(dataBytes, 0);
if (HexDataLength >= Datalength)
{
bl = true;
}
}
}
}
catch (Exception)
{
}
return bl;
}
}
}