| | |
| | | using System.Drawing.Imaging; |
| | | using System.IO; |
| | | using System.Linq; |
| | | using System.Text; |
| | | using Windows.ApplicationModel.Appointments; |
| | | using ZXing; |
| | | using static Bro.Common.Helper.EnumHelper; |
| | | using static Bro.Process.ProcessControl; |
| | | using static Org.BouncyCastle.Math.EC.ECCurve; |
| | | |
| | | namespace Bro.M141.Process |
| | |
| | | public static object _productListLock = new object(); |
| | | public List<ProductModel> ProductList = new List<ProductModel>(); |
| | | MachineLearningBase ML = null; |
| | | TcpListenerWrap TcpListener = null; |
| | | Spec _errorSpec = null; |
| | | |
| | | TcpListenerWrap _realTimeServer = null; |
| | | |
| | | volatile int _productIndex = 0; |
| | | |
| | | int _backgroundImageWidth = 2448; |
| | | //volatile int _productIndex = 0; |
| | | |
| | | M141Process_Mysql mysqlhelper = new M141Process_Mysql(); |
| | | |
| | | |
| | | public override void InitialProcessMethods() |
| | | { |
| | | base.InitialProcessMethods(); |
| | | |
| | | |
| | | if (ThHeartPlc == null) |
| | | { |
| | | ThHeartPlc = new Thread(Heartplc); |
| | | ThHeartPlc.IsBackground = true; |
| | | ThHeartPlc.Start(); |
| | | } |
| | | } |
| | | |
| | | public override void ProcessRunStateChanged() |
| | |
| | | } |
| | | |
| | | public PLCBase Plc1; |
| | | Thread ThHeartPlc; |
| | | bool devicestate = false; |
| | | PLCBase Plc2; |
| | | public Thread ThHeartPlc; |
| | | |
| | | |
| | | |
| | | public override void Open() |
| | | { |
| | | base.Open(); |
| | | |
| | | devicestate = true; |
| | | ML = DeviceCollection.FirstOrDefault(u => u is MachineLearningBase) as MachineLearningBase; |
| | | if (ML == null) |
| | | { |
| | |
| | | _positionCheckTimeDict.Clear(); |
| | | _positionSpecHeads.Clear(); |
| | | |
| | | mysqlhelper.IniDBIP(M141Config.IPforall); |
| | | |
| | | if (ThHeartPlc == null) |
| | | { |
| | | ThHeartPlc = new Thread(Heartplc); |
| | | ThHeartPlc.IsBackground = true; |
| | | ThHeartPlc.Start(); |
| | | } |
| | | } |
| | | |
| | | |
| | | public override void Close() |
| | | { |
| | | devicestate = false; |
| | | base.Close(); |
| | | try |
| | | { |
| | | if (ThHeartPlc != null) |
| | | { |
| | | ThHeartPlc.Abort(); |
| | | } |
| | | |
| | | } |
| | | catch |
| | | { |
| | | |
| | | } |
| | | ThHeartPlc = null; |
| | | } |
| | | |
| | | public void Heartplc() |
| | | { |
| | | while (true) |
| | | Thread.Sleep(1000); |
| | | |
| | | //Open(); |
| | | |
| | | string _statisticFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Statistic.json"); |
| | | if (File.Exists(_statisticFilePath)) |
| | | { |
| | | try |
| | | { |
| | | if (Plc1 != null) |
| | | string dataStr = ""; |
| | | using (StreamReader reader = new StreamReader(_statisticFilePath, System.Text.Encoding.UTF8)) |
| | | { |
| | | Plc1.WriteSingleAddress(1505, 0, out _); |
| | | dataStr = reader.ReadToEnd(); |
| | | } |
| | | |
| | | lock (StatisticRecordsFull) |
| | | { |
| | | var temRecords = JsonConvert.DeserializeObject<StatisticRecords_Full>(dataStr); |
| | | if (StatisticRecordsFull != null && temRecords != null) |
| | | { |
| | | StatisticRecordsFull.CurRecord = temRecords.CurRecord; |
| | | StatisticRecordsFull.HistoryRecord = temRecords.HistoryRecord; |
| | | } |
| | | } |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | LogAsync(DateTime.Now, EnumHelper.LogLevel.Error, $"产品统计信息反序列化异常"); |
| | | StatisticRecordsFull = new StatisticRecords_Full(); |
| | | } |
| | | } |
| | | |
| | | int numsum = 0; |
| | | //List<int> Statelist = new List<int>(); |
| | | |
| | | |
| | | Dictionary<string, List<int>> StateDIC = new Dictionary<string, List<int>>(); |
| | | int numplc = 0; |
| | | while (true) |
| | | { |
| | | numplc++; |
| | | numsum++; |
| | | |
| | | |
| | | if (!devicestate) |
| | | { |
| | | Thread.Sleep(1000); |
| | | continue; |
| | | } |
| | | try |
| | | { |
| | | if (M141Config.WorkShiftList != null && M141Config.WorkShiftList.Count > 0 && numsum >= 0) |
| | | { |
| | | for (int i = 0; i < M141Config.WorkShiftList.Count; i++) |
| | | { |
| | | if (M141Config.WorkShiftList[i].IsClearProductSummary) |
| | | { |
| | | double timeshap = (DateTime.Now - M141Config.WorkShiftList[i].ShiftTime_Start).TotalMinutes % 1440; |
| | | |
| | | if (timeshap > 0 && timeshap < 1) |
| | | { |
| | | StatisticRecordsFull.CurRecord.ProductSummary.RecordsList.Clear(); |
| | | StatisticRecordsFull.CurRecord.DefectSummary.RecordsList.Clear(); |
| | | numsum = -62; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | lock (StatisticRecordsFull) |
| | | { |
| | | if (numsum > 20) |
| | | { |
| | | numsum = 0; |
| | | using (FileStream fileStream = new FileStream(_statisticFilePath, FileMode.OpenOrCreate, FileAccess.Write)) |
| | | { |
| | | fileStream.Seek(0L, SeekOrigin.Begin); |
| | | string s = JsonConvert.SerializeObject(StatisticRecordsFull); |
| | | byte[] bytes = Encoding.UTF8.GetBytes(s); |
| | | fileStream.Write(bytes, 0, bytes.Length); |
| | | fileStream.SetLength(bytes.Length); |
| | | fileStream.Flush(); |
| | | fileStream.Close(); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | |
| | | } |
| | | |
| | | |
| | | try |
| | | { |
| | | if (numplc > 3) |
| | | { |
| | | numplc = 0; |
| | | if (Plc1 != null) |
| | | { |
| | | Plc1.WriteSingleAddress(M141Config.heartadd, 0, out _); |
| | | } |
| | | } |
| | | } |
| | | catch |
| | |
| | | |
| | | } |
| | | |
| | | Thread.Sleep(3000); |
| | | |
| | | try |
| | | { |
| | | string csvhead = "时间"; |
| | | string csvdata = DateTime.Now.ToString("yyyyMMddHHmmss") + "T"; |
| | | |
| | | if (Plc1 != null) |
| | | { |
| | | foreach (var item1 in M141Config.PLCAlarm) |
| | | { |
| | | try |
| | | { |
| | | if (!item1.isused) |
| | | { |
| | | continue; |
| | | } |
| | | |
| | | var alrams = item1.AlarmDetails; |
| | | var plcdev = DeviceCollection.FirstOrDefault(u => u.Id == item1.plcname) as PLCBase; |
| | | |
| | | if (!StateDIC.ContainsKey(plcdev.Name)) |
| | | { |
| | | StateDIC[plcdev.Name] = new List<int>(); |
| | | } |
| | | |
| | | foreach (var item in alrams.GroupBy(u => u.address)) |
| | | { |
| | | int add = item.Key; |
| | | var readres = plcdev.Read(add, 1, out _)[0]; |
| | | |
| | | var Allbin = Convert.ToString(readres, 2).PadLeft(16, '0').Select(c => c - '0').ToArray(); |
| | | |
| | | Allbin = Allbin.Reverse().ToArray(); |
| | | |
| | | foreach (var item2 in item) |
| | | { |
| | | item2.value = Allbin[item2.address2]; |
| | | } |
| | | } |
| | | List<int> list = new List<int>(); |
| | | foreach (var item in alrams) |
| | | { |
| | | csvhead += $",{item.alarmname}"; |
| | | csvdata += $",{(item.value == 1 ? "1" : "")}"; |
| | | list.Add(item.value); |
| | | } |
| | | |
| | | if (!StateDIC[plcdev.Name].SequenceEqual(list)) |
| | | { |
| | | StateDIC[plcdev.Name].Clear(); |
| | | StateDIC[plcdev.Name].AddRange(list); |
| | | |
| | | CSVRecordAsync($"PLCstate_{plcdev.Name}.csv", csvdata, csvhead); |
| | | |
| | | var showdata = alrams.Where(u => u.value == 1).Select(u => u.alarmname).ToList(); |
| | | |
| | | if (showdata == null) |
| | | { |
| | | showdata = new List<string>(); |
| | | } |
| | | |
| | | if (showdata.Count > 0) |
| | | { |
| | | LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"{plcdev.Name}报警 个数:{showdata.Count} {string.Join(',', showdata)}"); |
| | | } |
| | | else |
| | | { |
| | | LogAsync(DateTime.Now, EnumHelper.LogLevel.Detail, $"{plcdev.Name}报警 个数:0"); |
| | | } |
| | | } |
| | | } |
| | | catch |
| | | { |
| | | |
| | | } |
| | | } |
| | | } |
| | | |
| | | } |
| | | catch |
| | | { |
| | | |
| | | } |
| | | |
| | | Thread.Sleep(1000); |
| | | } |
| | | } |
| | | |
| | | |
| | | /// <summary> |
| | | /// 网络预热 |
| | |
| | | /// <exception cref="NotImplementedException"></exception> |
| | | private void NetWarmUp() |
| | | { |
| | | |
| | | string warmUpImageFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "WarmUp"); |
| | | if (!Directory.Exists(warmUpImageFolder)) |
| | | { |
| | |
| | | p.PID = p.PID + "_" + sequence.Split('_')[sequence.Split('_').Length - 1]; |
| | | |
| | | p.Initial(M141Config.StationCode, M141Config.WorkPositionCollection.Where(u => u.IsEnabled).Select(u => u.PositionName).ToList()); |
| | | //LogAsync(DateTime.Now, EnumHelper.LogLevel.Warning, $"未能从数据库获取产品对象,使用临时新建产品对象"); |
| | | LogAsync(DateTime.Now, EnumHelper.LogLevel.Warning, $"未能从数据库获取产品对象,使用临时新建产品对象 {sequence}"); |
| | | } |
| | | |
| | | NewProductIntoList(p, false); |
| | | } |
| | | |
| | |
| | | var errorSpec = _errorSpec.Copy(); |
| | | errorSpec.Code = "检测TBD"; |
| | | errorSpec.ActualValue = -999; |
| | | DetectResult ngResult = new DetectResult() { Specs = new List<ISpec>() { errorSpec }, PID = u.Key }; |
| | | DetectResult ngResult = new DetectResult() { Specs = new List<ISpec>() { errorSpec }, PID = u.Key, Id = Guid.NewGuid().ToString() }; |
| | | ngResults.Add(ngResult); |
| | | } |
| | | } |
| | | }); |
| | | |
| | | detectResults.AddRange(ngResults); |
| | | resultList.AddRange(detectResults); |
| | | |
| | | |
| | | } |
| | | |
| | | |
| | | RunCustomizedMethod(products, triggerText, triggerSource, imgSet, measureBind.CustomizedMonitorId, resultList); |
| | | RunCustomizedMethod(products, triggerText, triggerSource, imgSet, measureBind.CustomizedCombineMethodId, resultList); |
| | | |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | |
| | | { |
| | | products.ForEach(p => |
| | | { |
| | | var pResults = resultList.Where(u => u.PID == p.PID).ToList(); |
| | | var pResults = resultList.ToList(); |
| | | p.AddNewDetectResults(M141Config.StationCode, measureBind.WorkPosition, pResults); |
| | | LogAsync(DateTime.Now, EnumHelper.LogLevel.Action, $"产品{p.PID}_{p.SEQUENCE}添加工位{measureBind.WorkPosition}检测结果,数量{pResults.Count}"); |
| | | }); |
| | |
| | | try |
| | | { |
| | | ConcurrentDictionary<MeasureBind, IImageSet> imgSetDicts = new ConcurrentDictionary<MeasureBind, IImageSet>(); |
| | | var positionSet = M141Config.WorkPositionCollection.Where(u => u.IsEnabled).FirstOrDefault(u => u.TriggerValue == triggerDatas[0]); |
| | | |
| | | measureBinds.AsParallel().ForAll(b => |
| | | { |
| | |
| | | try |
| | | { |
| | | imgSetDicts[b] = CollectHImage(camera, b.SnapshotOpConfig); |
| | | if (positionSet.ispiccover) |
| | | { |
| | | Plc1.WriteSingleAddress(positionSet.plcover, 1, out _); |
| | | } |
| | | } |
| | | catch (Exception ea) |
| | | { |
| | |
| | | p.SEQUENCE = $"{tempSequence}_{i}"; |
| | | p.PID = $"{pidstr}T_{i}"; |
| | | |
| | | if (M141Config.Isreadbasketcode) |
| | | { |
| | | p.BasketCode = M141Config.basketcode; |
| | | } |
| | | else |
| | | { |
| | | p.BasketCode = mysqlhelper.Getbasketcode(p.SEQUENCE, out string sntem); |
| | | p.SN = sntem; |
| | | p.PID = $"{sntem}_{i}"; |
| | | if ("NoRead".Equals(p.BasketCode)) |
| | | { |
| | | LogAsync(DateTime.Now, EnumHelper.LogLevel.Error, $"{p.PID}_{p.SEQUENCE}获取框具码失败 赋值Noread"); |
| | | } |
| | | } |
| | | |
| | | if (positionSet.IsLastPosition) |
| | | { |
| | | p.Initial(M141Config.StationCode, new List<string>() { positionSet.PositionName }); |
| | |
| | | }); |
| | | }); |
| | | |
| | | Thread.Sleep(50); |
| | | |
| | | int de = 0; |
| | | |
| | | measureBinds.Where(b => (b.CheckIndex == de || b.CheckIndex == 1) && b.ImageIndex == 0).AsParallel().ForAll(b => |
| | | { |
| | | List<int> temint = new List<int>(); |
| | |
| | | |
| | | //初始化产品的检测次数 |
| | | var checkIndexList = M141Config.MeasureBindCollection.Where(u => u.WorkPosition == b.WorkPosition && u.ProductIndices.Contains(i)).Select(u => u.CheckIndex).OrderBy(u => u).ToList(); |
| | | p.InitialPositionCheckList(b.WorkPosition, checkIndexList); |
| | | p.InitialPositionCheckList(b.WorkPosition, checkIndexList, M141Config.StationCode); |
| | | LogAsync(DateTime.Now, EnumHelper.LogLevel.Assist, $"产品{p.PID}_{p.SEQUENCE}已清理{b.WorkPosition}检测数据。当前已完成工位{string.Join(",", p.Details.Select(u => u.PositionName))}"); |
| | | |
| | | }); |
| | | |
| | | //初始化工位的检测次数 |
| | |
| | | plcresult.Add(isOK); |
| | | List<string> specHeads = new List<string>(); |
| | | string head = p.GetCSVHead(ref specHeads, positionName); |
| | | if (!_positionSpecHeads.ContainsKey(positionName)) |
| | | { |
| | | _positionSpecHeads[positionName] = specHeads; |
| | | } |
| | | |
| | | string data = p.GetCSVData(_positionSpecHeads[positionName], positionName); |
| | | //_positionSpecHeads[positionName] = specHeads; |
| | | |
| | | string data = p.GetCSVData(specHeads, positionName); |
| | | CSVRecordAsync($"{positionName}_Record_{DateTime.Now.ToString("yyyyMMdd")}.csv", data, head); |
| | | |
| | | //UpdatePositionResultToDB(detail); |
| | | var seqData = p.SEQUENCE.Split(new char[] { '_' }, StringSplitOptions.RemoveEmptyEntries).ToList(); |
| | | |
| | | |
| | | //var seqData = p.SEQUENCE.Split(new char[] { '_' }, StringSplitOptions.RemoveEmptyEntries).ToList(); |
| | | ReplyPlcData(positionSet, plcresult); |
| | | mysqlhelper.UpdateProduct(p); |
| | | if (positionSet.IsLastPosition) |
| | | { |
| | | UpdateProductResultAsync(p); |
| | | mysqlhelper.NewForAll(p, M141Config.StationCode); |
| | | if (M141Config.IsfinDevice) |
| | | { |
| | | SummaryAllprodata(p); |
| | | } |
| | | } |
| | | }); |
| | | ReplyPlcData(positionSet, plcresult); |
| | | //ReplyPlcData(positionSet, plcresult); |
| | | if (positionSet.IsLastPosition) |
| | | { |
| | | if (_ct != null) |
| | |
| | | } |
| | | |
| | | |
| | | public void SummaryAllprodata(ProductModel p) |
| | | { |
| | | _taskFactory.StartNew(() => |
| | | { |
| | | try |
| | | { |
| | | ProductModel newp = new ProductModel(); |
| | | newp.SEQUENCE = p.SEQUENCE; |
| | | newp.PID = p.PID; |
| | | newp.BasketCode = p.BasketCode; |
| | | newp.Result = p.Result; |
| | | newp.SN = p.SN; |
| | | |
| | | Thread.Sleep(500); |
| | | var plist = mysqlhelper.GetProductList(p.SEQUENCE); |
| | | |
| | | |
| | | if (plist != null) |
| | | { |
| | | foreach (var item in plist) |
| | | { |
| | | newp.Details.AddRange(item.Details); |
| | | } |
| | | } |
| | | LogAsync(DateTime.Now, EnumHelper.LogLevel.Error, $"AllDeviceProductRecord从数据库获取到数据{p.SEQUENCE} plist数量{plist.Count} Details数量{newp.Details.Count}"); |
| | | |
| | | //newp.Details.AddRange(p.Details); |
| | | |
| | | List<string> specHeadListforall = new List<string>(); |
| | | List<string> positionListforall = new List<string>(); |
| | | string csvHeadforall = newp.GetCSVHead(ref specHeadListforall, ref positionListforall); |
| | | CSVRecordAsync($"AllDeviceProductRecord.csv", newp.GetCSVData(specHeadListforall, positionListforall), csvHeadforall); |
| | | |
| | | } |
| | | catch (Exception exx) |
| | | { |
| | | LogAsync(DateTime.Now, EnumHelper.LogLevel.Error, "AllDeviceProductRecord数据汇总异常 " + exx.ToString()); |
| | | } |
| | | }); |
| | | |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | #endregion |
| | | } |
| | | |