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<InspectionShift> _alertInspectionShifts = new List<InspectionShift>();
|
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<InspectionConfig>(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<DefectPositionSet> CheckDefectNames { get; set; } = new List<DefectPositionSet>();
|
|
[Browsable(false)]
|
public DateTime? LastInspectTime { get; set; } = null;
|
|
|
[Browsable(false)]
|
public string Excelpath { get; set; } = "";
|
|
[ResCategory("点检时间")]
|
[ResDescription("设置每次点检的开始时间")]
|
[ResDisplayName("点检设置集合")]
|
[TypeConverter(typeof(ComplexObjectConvert))]
|
[Editor(typeof(ComplexCollectionEditor<InspectionShift>), typeof(UITypeEditor))]
|
public List<InspectionShift> InspectionShifts { get; set; } = new List<InspectionShift>();
|
|
[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
|
{
|
|
/// <summary>
|
/// 工站
|
/// </summary>
|
public string PositionNum { get; set; } = "";
|
|
/// <summary>
|
/// 产品序号
|
/// </summary>
|
public string ProduceIndex { get; set; } = "";
|
|
/// <summary>
|
/// 测量项code
|
/// </summary>
|
public string Code { get; set; } = "";
|
|
/// <summary>
|
/// 标准值
|
/// </summary>
|
public string StandardValue { get; set; } = "";
|
|
/// <summary>
|
/// 最大值
|
/// </summary>
|
public string MaxValue { get; set; } = "";
|
|
/// <summary>
|
/// 最小值
|
/// </summary>
|
public string MinValue { get; set; } = "";
|
|
/// <summary>
|
/// excel录入值
|
/// </summary>
|
public string InsertValue { get; set; } = "";
|
|
/// <summary>
|
/// 测量值
|
/// </summary>
|
public string TestValue { get; set; } = "";
|
|
/// <summary>
|
/// 相关性差值
|
/// </summary>
|
public string Relevance { get; set; } = "";
|
|
/// <summary>
|
/// 相关性百分比
|
/// </summary>
|
public string Relevance2 { get; set; } = "";
|
|
/// <summary>
|
/// 是否为尺寸测量,true尺寸,false,外观
|
/// </summary>
|
public bool Ismeasure { get; set; } = false;
|
|
/// <summary>
|
/// 点检时间
|
/// </summary>
|
[Newtonsoft.Json.JsonIgnore]
|
public DateTime? CheckTime { get; set; } = null;
|
|
|
/// <summary>
|
/// 点检结果
|
/// </summary>
|
[Newtonsoft.Json.JsonIgnore]
|
public bool result { get; set; } = false;
|
|
}
|
}
|