using Community; using DAL; using MahApps.Metro.Controls.Dialogs; using Model; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Xml; namespace ViewModel { public class StandVM : NotificationObject { /// /// 上报异常的委托 /// /// public delegate void delegeateExcep(string str); /// /// 上报异常的委托事件 /// public event delegeateExcep eventExcepReport; /// /// 数据引擎 /// MainWindowDAL dal = new MainWindowDAL(); #region 站别参数 private StandModel myStand; /// /// 站别参数 /// public StandModel MyStand { get { return myStand; } set { myStand = value; RaisePropertyChanged("MyStand"); } } #endregion #region 输入信号 private List inputList; /// /// 输入信号 /// public List InputList { get { return inputList; } set { inputList = value; RaisePropertyChanged("InputList"); } } #endregion #region 输出信号 private List outputList; /// /// 输出信号 /// public List OutputList { get { return outputList; } set { outputList = value; RaisePropertyChanged("OutputList"); } } #endregion #region 通讯连接对象 private CommunicationManagementClass myConnect; /// /// 通讯连接对象 /// public CommunicationManagementClass MyConnect { get { return myConnect; } set { myConnect = value; RaisePropertyChanged("MyConnect"); } } /// /// 测试软件返回的信息 /// CmdMessage TestSoftMessage { get; set; } /// /// 测试仪器回调函数 /// /// private void TestContact_ReceiveCallback(string str) { TestSoftMessage = XmlTocmdResp(str); TestLog(str);//记录日志信息 // TestSoftResp(TestSoftMessage); } #endregion #region 取料点 /// /// 取料点 /// public RobotPoint TakePoint { get; set; } #endregion #region 放料点 /// /// 放料点 /// public RobotPoint PutPoint { get; set; } #endregion #region 指令集合 private List cmdList; /// /// 指令集合 /// public List CmdList { get { return cmdList; } set { cmdList = value; RaisePropertyChanged("CmdList"); } } #endregion #region 选中的测试指令 private CommonClass selectCmd; /// /// 选中的测试指令 /// public CommonClass SelectCmd { get { return selectCmd; } set { selectCmd = value; RaisePropertyChanged("SelectCmd"); } } #endregion #region 各种方法 /// /// 手动发送测试指令 /// public DelegateCommand ShouDongSendCmdCommand { get; set; } /// /// 生产数据清零 /// public DelegateCommand CountClearedCommand { get; set; } #endregion /// /// 当前机种对象 /// public MachineModel CurrMachine { get; set; } private bool isEnable; /// /// 编辑权限 /// public bool IsEnable { get { return isEnable; } set { isEnable = value; RaisePropertyChanged("IsEnable"); } } #region 构造函数 /// /// 构造函数 /// public StandVM(StandModel sm) { this.MyStand = sm; this.InputList = dal.GetInputList(sm.StandType); this.OutputList = dal.GetOutputList(sm.StandType); BoxOpenState = new ShowInfo(); CmdList = new List() { new CommonClass() { CommonName="状态询问",CmdStr="A0 01 00 E0" }, new CommonClass() { CommonName="发送SN",CmdStr="SendSN"}, new CommonClass() { CommonName="PCB放置完成",CmdStr="A0 03 00 E0"}, new CommonClass() { CommonName="开始测试",CmdStr="A0 04 00 E0"}, }; ShouDongSendCmdCommand = new DelegateCommand() { ExecuteAction = new Action(ShouDongSendCmd) }; CountClearedCommand = new DelegateCommand() { ExecuteAction = new Action(CountCleared) }; } #endregion #region 生产数据清零 /// /// 生产数据清零 /// /// private void CountCleared(object obj) { if (MyStand != null) { MyStand.BadQuantity = 0; MyStand.Capacity = 0; MyStand.CTime = 0; MyStand.InputQuantity = 0; MyStand.OutputQuantity = 0; MyStand.RunTime = ""; MyStand.Yield = 1; MyStand.NgCount = 0; } } #endregion #region 手动发送测试指令 /// /// 手动发送测试指令 /// /// private void ShouDongSendCmd(object obj) { if (SelectCmd != null) { //MyConnect.HexSend = true; //if (SelectCmd.CmdStr == "SendSN") //{ // if (!string.IsNullOrEmpty(SnCode)) // { // string str = string.Format("A0 02 {0}E0", StringToHex16String(SnCode.Trim())); // MyConnect.Send(str); // } // else // { // MyMessageBox.Show("SN错误"); // } //} //else //{ // MyConnect.Send(SelectCmd.CmdStr); //} } } #endregion #region 测试盒开关 private OutputSignal boxSwitche; /// /// 测试盒开关 /// public OutputSignal BoxSwitche { get { return boxSwitche; } set { boxSwitche = value; RaisePropertyChanged("BoxSwitche"); } } #endregion /// /// 测试盒子打开信号 /// public InputSignal BoxOpen { get; set; } /// /// 测试盒关闭信号 /// public InputSignal BoxClose { get; set; } #region 开关箱子状态 private ShowInfo boxOpenState; /// /// 开关箱子状态 /// public ShowInfo BoxOpenState { get { return boxOpenState; } set { boxOpenState = value; RaisePropertyChanged("BoxOpenState"); } } #endregion /// /// MES连接对象 /// public ERP_ParameterClass MyMes { get; set; } private bool runSwitch; /// /// 运行开关 /// public bool RunSwitch { get { return runSwitch; } set { runSwitch = value; } } /// /// 运行 /// public void Run() { if (RunSwitch) { TestRun(TestFlow); } } /// /// 测试流程 /// public FlowModel TestFlow { get; set; } #region 当前测试产品的SN private string snCode; /// /// 当前测试产品的SN /// public string SnCode { get { return snCode; } set { snCode = value; RaisePropertyChanged("SnCode"); } } #endregion #region 测试运行 /// /// 要发送的指令 /// CmdModel SendCmd { get; set; } = new CmdModel(); /// /// 开始测试时间 /// DateTime StartTestTime { get; set; } /// /// 测试时长 /// int Testdur { get; set; } = 30; /// /// 连续测试失败次数 /// int testNGNumber { get; set; } /// /// 重测次数 /// int chongceNumber { get; set; } /// /// 测试运行 /// private void TestRun(FlowModel MyFlow) { switch (MyFlow.RunStep) { case GengralProcessEnum.Start: //查询状态 SendCmd = new CmdModel() { cmdId = "106", devId = "0", tip = "Query status", uid = "xxxxxxx" }; MyConnect.SendInfo("[[" + ConvertToXmL(SendCmd) + "]]"); TestLog("[[" + ConvertToXmL(SendCmd) + "]]"); MyFlow.RunStep++; MyFlow.RunTime = DateTime.Now; break; case GengralProcessEnum.Step1: //if (TestSoftRespObj.cmdId == "106" && TestSoftRespObj.status == "1") if (TestSoftMessage.cmdId == "106") { //二维码不为空 发送二维码 if (!string.IsNullOrEmpty(MyStand.Product.StatuInfo)) { //发送二维码 SendCmd = new CmdModel() { cmdId = "102", tip = "set barcode", devId = "0", uid = "xxxxxxx", QRCode = MyStand.Product.StatuInfo }; MyConnect.SendInfo("[[" + ConvertToXmL(SendCmd) + "]]"); MyFlow.RunStep++; MyFlow.RunTime = DateTime.Now; TestLog("[[" + ConvertToXmL(SendCmd) + "]]"); } } else if (MyFlow.RunTime.AddMilliseconds(1000) < DateTime.Now) //超过一秒没有得到回应 返回上一步继续发送 { MyFlow.RunStep--; } break; case GengralProcessEnum.Step2: //二维码响应正常 if (TestSoftMessage.cmdId == "102") { //开始测试 SendCmd = new CmdModel() { cmdId = "103", tip = "start test", devId = "0", uid = "xxxxxxx" }; MyConnect.SendInfo("[[" + ConvertToXmL(SendCmd) + "]]"); MyFlow.RunStep++; MyFlow.RunTime = DateTime.Now; TestLog("[[" + ConvertToXmL(SendCmd) + "]]"); //开始计时 StartTestTime = DateTime.Now; } break; case GengralProcessEnum.Step3: //开始命令响应正常 if (TestSoftMessage.cmdId == "103" && TestSoftMessage.result == "0") { //开始测试 MyFlow.RunStep++; MyFlow.RunTime = DateTime.Now; } else if (MyFlow.RunTime.AddMilliseconds(2000) < DateTime.Now) { //再发一次开始测试 SendCmd = new CmdModel() { cmdId = "103", tip = "start test", devId = "0", uid = "xxxxxxx" }; MyConnect.SendInfo("[[" + ConvertToXmL(SendCmd) + "]]"); MyFlow.RunTime = DateTime.Now; } break; case GengralProcessEnum.Step4: if (TestSoftMessage.notifyId == "201" && TestSoftMessage.testId == "801") { UnderLineFlow = new FlowModel("划线流程"); MyStand.StandState.StatuInfo = "烧录完成,开始划线"; MyFlow.RunStep++; MyFlow.RunTime = DateTime.Now; } break; case GengralProcessEnum.Step5: //划线运行 if (UnderLineRun(UnderLineFlow)) { MyFlow.RunStep++; MyFlow.RunTime = DateTime.Now; } break; case GengralProcessEnum.Step6: if (TestSoftMessage.notifyId == "201" && TestSoftMessage.testId == "800") { PressFlow = new FlowModel("按压流程"); MyStand.StandState.StatuInfo = "划线完成,按压开始"; MyFlow.RunStep++; MyFlow.RunTime = DateTime.Now; } break; case GengralProcessEnum.Step7: //按压运行 if (PressRun(PressFlow)) { MyFlow.RunStep++; MyFlow.RunTime = DateTime.Now; } break; case GengralProcessEnum.Step8: //等待结果 if (TestSoftMessage.notifyId == "204") { MyFlow.RunStep++; MyFlow.RunTime = DateTime.Now; } else if (StartTestTime.AddSeconds(Testdur) < DateTime.Now) { eventExcepReport(this.MyStand.StandName + "测试超时"); } break; case GengralProcessEnum.Step9: if (TestSoftMessage.totalResult == "0") { this.MyStand.InputQuantity++; this.MyStand.OutputQuantity++; this.MyStand.StandState.StatuInfo = "OK"; this.MyStand.StandState.StatusBackground = ColorStr.Green; testNGNumber = 0; chongceNumber = 0; } else { if (chongceNumber >= 1) { this.MyStand.InputQuantity++; this.MyStand.InputQuantity++; chongceNumber = 0; this.MyStand.StandState.StatuInfo = "NG"; this.MyStand.StandState.StatusBackground = ColorStr.Red; testNGNumber++; if (testNGNumber > 3) { eventExcepReport(this.MyStand.StandName + "连续3次NG"); testNGNumber = 0; } } else { //chongceNumber++; //BoxContact.SendInfo("2C"); //发送测试盒取出完成命令 (重测直接发送取出完成信息号) } } break; } } #endregion #region 划线运行 /// /// 划线流程 /// FlowModel UnderLineFlow { get; set; } /// /// 划线运行 /// private bool UnderLineRun(FlowModel MyFlow) { bool bl = false; switch (MyFlow.RunStep) { case GengralProcessEnum.Start: break; case GengralProcessEnum.Step1: break; } return bl; } #endregion #region 按压运行 /// /// 按压流程 /// FlowModel PressFlow { get; set; } /// /// 按压运行 /// /// private bool PressRun(FlowModel MyFlow) { bool bl = false; switch (MyFlow.RunStep) { case GengralProcessEnum.Start: break; case GengralProcessEnum.Step1: break; } return bl; } #endregion #region 记录上传MES日志信息到文件 /// /// 记录上传MES日志信息到文件 /// /// private void RecordMesLog(string showMag) { try { string path = @"D:\\Log\MesLog\\" + DateTime.Now.ToString("D") + "\\"; //文件夹路径 string filePath = path + DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".txt"; // //创建文件夹 if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } //打开或创建文件 FS = File.Open(filePath, FileMode.Append); //获得字节数组 data = System.Text.Encoding.Default.GetBytes(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff") + showMag + "\r\n"); //开始写入 FS.Write(data, 0, data.Length); //清空缓冲区、关闭流 FS.Flush(); FS.Close(); } catch (Exception) { } } #endregion #region 字符串转HEX /// /// 字符串转HEX /// /// /// public string StringToHex16String(string _str) { //将字符串转换成字节数组。 byte[] buffer = System.Text.Encoding.UTF8.GetBytes(_str); //定义一个string类型的变量,用于存储转换后的值。 string result = string.Empty; for (int i = 0; i < buffer.Length; i++) { //将每一个字节数组转换成16进制的字符串,以空格相隔开。 result += Convert.ToString(buffer[i], 16) + " "; } return result; } #endregion #region XML转实体对象 /// /// XML转实体对象 /// /// /// public CmdMessage XmlTocmdResp(string xml) { xml = xml.Replace("[[", ""); xml = xml.Replace("]]", ""); CmdMessage entity = new CmdMessage(); XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); XmlElement xe = doc.DocumentElement; foreach (PropertyInfo p in typeof(CmdMessage).GetProperties()) { p.SetValue(entity, xe.GetAttribute(p.Name), null);//获得s对象属性的值复制给d对象的属性 } string biaoji = ConvertToXmL(entity); return entity; } #endregion #region 实体对象转换成XML元素 /// /// 实体对象转换成XML元素 /// /// /// /// public string ConvertToXmL(T command) { XmlElement student = new XmlDocument().CreateElement(typeof(T).Name);//插入一个student节点 foreach (PropertyInfo p in typeof(T).GetProperties()) { try { student.SetAttribute(p.Name, p.GetValue(command, null).ToString());//设置id属性 } catch (Exception) { } } return student.OuterXml; } #endregion #region 记录日志信息到文件 /// /// 打开的文件流对象 /// FileStream FS { get; set; } byte[] data { get; set; } /// /// 记录日志信息到文件 /// /// private void TestLog(string showMag) { lock (this) { string path = @"D:\\TestLog"; //文件夹路径 string paths = @"D:\\TestLog\" + this.MyStand.StandName + DateTime.Now.ToString("D") + ".txt"; //文件路径 以年月日为名称 //创建文件夹 if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } //打开或创建文件 FS = File.Open(paths, FileMode.Append); //获得字节数组 data = System.Text.Encoding.Default.GetBytes(DateTime.Now.ToString("yyyyMMddHHmmss") + "→" + showMag + "\r\n"); //开始写入 FS.Write(data, 0, data.Length); //清空缓冲区、关闭流 FS.Flush(); FS.Close(); } } #endregion } /// /// 发送测试命令模型 /// public class CmdModel { /// /// 指令ID /// public string cmdId { get; set; } /// /// 注解 /// public string tip { get; set; } public string devId { get; set; } /// /// Uid /// public string uid { get; set; } /// /// 二维码 /// public string QRCode { get; set; } } /// /// 测试指令响应或通知信息类 /// public class CmdMessage { /// /// 指令ID /// public string cmdId { get; set; } /// /// 响应通知ID /// public string notifyId { get; set; } /// /// 注解 /// public string tip { get; set; } /// /// Uid /// public string uid { get; set; } public string devId { get; set; } /// /// 状态 status为1表示空闲 /// public string status { get; set; } /// /// 二维码 /// public string QRCode { get; set; } /// /// 结果 /// public string result { get; set; } /// /// 测试步骤ID /// public string testId { get; set; } /// /// 最终结结果 0:fals 1:true /// public string totalResult { get; set; } public CmdMessage() { cmdId = ""; notifyId = ""; tip = ""; uid = ""; devId = ""; status = ""; QRCode = ""; result = ""; testId = ""; totalResult = ""; } } }