using Bro.Common.Helper; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Collections.Specialized; using System.ComponentModel; using System.Drawing.Design; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Bro.Common.Model { public class OperationCTCollection : IComplexDisplay { [Category("CT配置")] [Description("CT动作名称")] public string Name { get; set; } [Category("CT配置")] [Description("PLC触发索引,从0开始,例如30,表示触发地址是45030")] public int TriggerIndex { get; set; } [Category("CT配置")] [Description("操作CT配置集合")] [TypeConverter(typeof(CollectionCountConvert))] [Editor(typeof(ComplexCollectionEditor), typeof(UITypeEditor))] public ObservableCollection CTList { get; set; } = new ObservableCollection(); [Category("CT时间")] [Description("操作CT合计")] [JsonIgnore] [ReadOnly(true)] public int CTSum { get; set; } = 0; [Category("CSV输出")] [Description("是否做CSV文件输出,true:将CT统计的CSV文件输出到全局配置CSV的文件夹目录下;false:不做CSV文件输出")] public bool IsCSVOutput { get; set; } = false; public string GetDisplayText() { return TriggerIndex + "--" + Name; } public OperationCTCollection() { CTList.CollectionChanged += CTList_CollectionChanged; } private void CTList_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { foreach (var ct in CTList) { ct.PropertyChanged += CTChanged; } CTChanged(null, null); } private void CTChanged(object sender, PropertyChangedEventArgs args) { CTSum = CTList.Sum(p => p.OpCT); } #region CSV输出 static object csvLock = new object(); public void CSVOutput(string foldPath) { if (!IsCSVOutput) { return; } DirectoryInfo dir = new DirectoryInfo(foldPath); if (!dir.Exists) { dir.Create(); } string filePath = Path.Combine(foldPath, Name + ".csv"); bool fileExist = File.Exists(filePath); lock (csvLock) { using (StreamWriter writer = new StreamWriter(filePath)) { if (!fileExist) { writer.WriteLine(CSVHeadOutput()); writer.WriteLine(CSVDataOutput()); } else { writer.WriteLine(CSVDataOutput()); } writer.Flush(); writer.Close(); } } } private string CSVHeadOutput() { return String.Join(",", CTList.Select(c => c.OpCode)); } private string CSVDataOutput() { return String.Join(",", CTList.Select(c => c.OpCT)); } #endregion } /// /// 工位动作CT /// public class OperationCT : IComplexDisplay, INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; [Category("动作CT配置")] [Description("PLC触发CT的索引值")] public int OpTrigger { get; set; } [Category("动作CT配置")] [Description("动作代号")] public string OpCode { get; set; } [Category("动作CT配置")] [Description("动作描述")] public string OpDesc { get; set; } private DateTime? startTime = null; [Category("CT时间")] [Description("动作开始时间")] [ReadOnly(true)] [TypeConverter(typeof(ShortTimeConverter))] [JsonIgnore] public DateTime? StartTime { get => startTime; set { startTime = value; CalculateCT(); } } private DateTime? endTime = null; [Category("CT时间")] [Description("动作结束时间")] [ReadOnly(true)] [TypeConverter(typeof(ShortTimeConverter))] [JsonIgnore] public DateTime? EndTime { get => endTime; set { endTime = value; CalculateCT(); } } private int opCT = 0; [Category("CT统计")] [Description("动作CT时间,单位:ms")] [ReadOnly(true)] [JsonIgnore] public int OpCT { get { return opCT; } set { if (value > 0) { opCT = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("OpCT")); } } } private void CalculateCT() { if (StartTime == null || EndTime == null) { OpCT = 0; } else { OpCT = (int)(EndTime.Value - StartTime.Value).TotalMilliseconds; } } public string GetDisplayText() { if (OpCT == 0) { return OpTrigger + "--" + OpCode + "--" + OpDesc; } else { return OpTrigger + "--" + OpCode + ":" + OpCT; } } } }