using Bro.Common.Helper; using Bro.Common.Interface; using Bro.Common.Model; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.ComponentModel; using System.Configuration; using System.Drawing.Design; using System.Linq; using System.Text; using System.Text.Json.Serialization; using System.Threading.Tasks; namespace Bro.M141.Process { public partial class M141Process { public InspectionConfig InspectionConfig { get; set; } = new InspectionConfig(); private bool isInspectionDoing = false; volatile bool _isSpotCheckValid = true; public bool FistStart = true; const string SPOTALARMMSG = $"点检超时,请执行点检"; List _alertInspectionShifts = new List(); public bool IsInspectionDoing { get => isInspectionDoing; set { if (isInspectionDoing != value) { isInspectionDoing = value; if (!isInspectionDoing) { _alertInspectionShifts.Clear(); } } } } public bool SaveInspectionConfig(out string error) { error = ""; try { using (StreamWriter sw = new StreamWriter(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Inspection.json"), false, System.Text.Encoding.UTF8)) { sw.Write(JsonConvert.SerializeObject(InspectionConfig)); } } catch (Exception ex) { error = ex.GetExceptionMessage(); return false; } return true; } volatile int _accumulativeSpotCheckTime = 0; private bool CheckInspectionInPeriodic(int heartBeatInterval) { //lock (_spotCheckLock) { if (FistStart) { string StartPoint = ConfigurationManager.AppSettings["StartPoint"]; if (string.IsNullOrEmpty(StartPoint) || StartPoint.ToUpper().Equals("TRUE")) { } else { FistStart = false; } } if (_accumulativeSpotCheckTime == 0 || Interlocked.Add(ref _accumulativeSpotCheckTime, heartBeatInterval) >= 10000) { Interlocked.Exchange(ref _accumulativeSpotCheckTime, 1); CheckInspectionInPeriodicAsync(); } } return _isSpotCheckValid; } public async void CheckInspectionInPeriodicAsync() { await Task.Run(() => { DateTime dt = DateTime.Now; bool tempJudge = true; RaisedAlarm(SPOTALARMMSG, false); InspectionConfig.InspectionShifts.ForEach(s => { if (CheckInTimePeriod(s, dt)) { if (!CheckInTimePeriod(s, InspectionConfig.LastInspectTime)) { tempJudge = false; AlertInspection(s); } else if (FistStart && tempJudge) { tempJudge = false; AlertInspection(s); } } }); _isSpotCheckValid = tempJudge; }); } static object _alertInspectionShiftLock = new object(); private void AlertInspection(InspectionShift s) { lock (_alertInspectionShiftLock) { if (!_alertInspectionShifts.Any(u => u.Id == s.Id)) { _alertInspectionShifts.Add(s); Task.Run(() => { if (!IsInspectionDoing) { LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"{s.ShiftName}{SPOTALARMMSG}"); RaisedAlarm(SPOTALARMMSG, true, WarningLevel.Hint); MessageBox.Show($"{s.ShiftName}{SPOTALARMMSG}"); _alertInspectionShifts.RemoveAll(u => u.Id == s.Id); } }); } } } private static bool CheckInTimePeriod(InspectionShift u, DateTime? dt) { if (dt == null) return false; var startTime = dt.Value.Date.Add(u.ShiftTime_Start.TimeOfDay); var endTime = startTime.AddHours(u.ValidHours); var isInTimes = dt >= startTime && dt < endTime; if (!isInTimes) { startTime = startTime.AddDays(-1); endTime = startTime.AddHours(u.ValidHours); isInTimes = dt >= startTime && dt < endTime; } return isInTimes; } public void InitialInspectionConfig() { string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Inspection.json"); if (File.Exists(filePath)) { try { using (StreamReader reader = new StreamReader(filePath, System.Text.Encoding.UTF8)) { var dataStr = reader.ReadToEnd(); InspectionConfig = JsonConvert.DeserializeObject(dataStr); if (InspectionConfig == null) { InspectionConfig = new InspectionConfig(); } } } catch (Exception ex) { InspectionConfig = new InspectionConfig(); LogAsync(DateTime.Now, EnumHelper.LogLevel.Error, $"点检配置信息读取失败,初始化点检配置"); } } } } public class InspectionConfig { [Browsable(false)] public List CheckDefectNames { get; set; } = new List(); [Browsable(false)] public DateTime? LastInspectTime { get; set; } = null; [Browsable(false)] public string Excelpath { get; set; } = ""; [ResCategory("点检时间")] [ResDescription("设置每次点检的开始时间")] [ResDisplayName("点检设置集合")] [TypeConverter(typeof(ComplexObjectConvert))] [Editor(typeof(ComplexCollectionEditor), typeof(UITypeEditor))] public List InspectionShifts { get; set; } = new List(); [Category("尺寸点检")] [Description("尺寸点检误差百分比")] [DisplayName("尺寸点检误差百分比")] public int Check { get; set; } } public class InspectionShift : IComplexDisplay { [Browsable(false)] public string Id { get; set; } = Guid.NewGuid().ToString(); [ResCategory("点检设置")] [ResDescription("点检名称设置")] [ResDisplayName("\t\t点检名称")] public string ShiftName { get; set; } [ResCategory("点检设置")] [ResDescription("点检开始时间设置")] [ResDisplayName("\t点检开始时间")] [TypeConverter(typeof(ShortTimeHHmmConverter))] [Editor(typeof(TimePickerEditor), typeof(UITypeEditor))] public DateTime ShiftTime_Start { get; set; } [ResCategory("点检设置")] [ResDescription("点检有效覆盖时间,单位hr")] [ResDisplayName("点检有效周期")] public int ValidHours { get; set; } = 12; public string GetDisplayText() { return $"{(string.IsNullOrWhiteSpace(ShiftName) ? "" : $"{ShiftName}:")}{ShiftTime_Start.ToString("HH:mm")}"; } } public class DefectPositionSet { /// /// 工站 /// public string PositionNum { get; set; } = ""; /// /// 产品序号 /// public string ProduceIndex { get; set; } = ""; /// /// 测量项code /// public string Code { get; set; } = ""; /// /// 标准值 /// public string StandardValue { get; set; } = ""; /// /// 最大值 /// public string MaxValue { get; set; } = ""; /// /// 最小值 /// public string MinValue { get; set; } = ""; /// /// excel录入值 /// public string InsertValue { get; set; } = ""; /// /// 测量值 /// public string TestValue { get; set; } = ""; /// /// 相关性差值 /// public string Relevance { get; set; } = ""; /// /// 相关性百分比 /// public string Relevance2 { get; set; } = ""; /// /// 是否为尺寸测量,true尺寸,false,外观 /// public bool Ismeasure { get; set; } = false; /// /// 点检时间 /// [Newtonsoft.Json.JsonIgnore] public DateTime? CheckTime { get; set; } = null; /// /// 点检结果 /// [Newtonsoft.Json.JsonIgnore] public bool result { get; set; } = false; } }