using Prism.Events; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; using TeamAAS_VP.Core.PLCs; using TeamAAS_VP.Enums; using TeamAAS_VP.Events; using TeamAAS_VP.Interfaces; using TeamAAS_VP.Models; using TeamAAS_VP.Resources.Languages; using TeamAAS_VP.Services; using TeamAAS_VP.ViewModels.Home; using TouchSocket.Core; namespace TeamAAS_VP.Views { /// /// AddScrewView.xaml 的交互逻辑 /// public partial class AddScrewView : UserControl { IConfigService _configService; IEventAggregator _eventAggregator; IPlcService _plcService; public ICommand OkCommand { get; } //public ICommand CancelCommand { get; } public string ScannedBarcode { get; private set; } public AddScrewView(IConfigService configService, IEventAggregator eventAggregator, IPlcService plcService) { InitializeComponent(); this.Width = 800; this.Height = 500; //_configService = configService; //_eventAggregator = eventAggregator; //_plcService=plcService; //OkCommand = new RelayCommand(OkExecute); //CancelCommand = new RelayCommand(CancelExecute); this.Loaded += (s, e) => { BarcodeTextBox.Focus(); }; // 把Command赋值给DataContext,方便绑定 //DataContext = this; } ///// ///// PLC命令触发时 ///// ///// //private async void PlcCommandTrigger((string key, string nodeId, object value) tuple) //{ // try // { // if (tuple.key != "AutoSensor") // { // return; // } // var addressConfig = _configService.GetPlcAddresses(); // var plc = _plcService.GetPlcByNumber(addressConfig.PlcNo) as OpcUaClientPLC; // string a = ""; // var stationConfig = _configService.GetDeviceInfo(0); // ScannedBarcode = BarcodeTextBox.Text; // if (ScannedBarcode.Contains("|")) // { // string[] arr = ScannedBarcode.Split("|"); // for (int i = 0; i < 16; i++) // { // a += arr[i]; // } // } // else // { // a = ScannedBarcode; // } // if (string.IsNullOrWhiteSpace(ScannedBarcode)) // { // MessageBox.Show("请先扫描螺丝条码", "提示", MessageBoxButton.OK, MessageBoxImage.Warning); // BarcodeTextBox.Focus(); // 重新聚焦 // return; // } // if (a == stationConfig.DataInfo.Screwsize1 || a == stationConfig.DataInfo.Screwsize2) // { // MessageBox.Show("螺丝型号匹配成功", "提示", MessageBoxButton.OK, MessageBoxImage.Warning); // //SendTaskMessage($"螺丝型号匹配成功", MessageLevel.Info); // ExportDataToCsv(DateTime.Now.ToString("yyyy-MM-dd HH:mm"), a, true, @"D:\添加螺丝记录\螺丝上料Log.csv", appendMode: true); // await plc.WriteNodeAsync(string.Format(addressConfig.Out_MesStatus.Address, 1), (Int16)1); // } // else // { // MessageBox.Show("螺丝型号匹配失败", "提示", MessageBoxButton.OK, MessageBoxImage.Warning); // //SendTaskMessage($"螺丝型号匹配失败", MessageLevel.Error); // ExportDataToCsv(DateTime.Now.ToString("yyyy-MM-dd HH:mm"), a, false, @"D:\添加螺丝记录\螺丝上料Log.csv", appendMode: true); // await plc.WriteNodeAsync(string.Format(addressConfig.Out_MesStatus.Address, 1), (Int16)1); // } // DialogResult = true; // Close(); // } // catch (Exception ex) // { // } //} private async void OkExecute(object sender) { //try //{ // var addressConfig = _configService.GetPlcAddresses(); // var plc = _plcService.GetPlcByNumber(addressConfig.PlcNo) as OpcUaClientPLC; // string a = ""; // var stationConfig = _configService.GetDeviceInfo(0); // ScannedBarcode = BarcodeTextBox.Text; // if (ScannedBarcode.Contains("|")) // { // string[] arr = ScannedBarcode.Split("|"); // for (int i = 0; i < 16; i++) // { // a += arr[i]; // } // } // else // { // a = ScannedBarcode; // } // if (string.IsNullOrWhiteSpace(ScannedBarcode)) // { // MessageBox.Show("请先扫描螺丝条码", "提示", MessageBoxButton.OK, MessageBoxImage.Warning); // BarcodeTextBox.Focus(); // 重新聚焦 // return; // } // if (a == stationConfig.DataInfo.Screwsize1 || a == stationConfig.DataInfo.Screwsize2 || a == stationConfig.DataInfo.Screwsize3) // { // await plc.WriteNodeAsync(addressConfig.Out_PlcFail.Address, false); // MessageBox.Show("螺丝型号匹配成功", "提示", MessageBoxButton.OK, MessageBoxImage.Warning); // //SendTaskMessage($"螺丝型号匹配成功", MessageLevel.Info); // ExportDataToCsv(DateTime.Now.ToString("yyyy-MM-dd HH:mm"), a, stationConfig.DataInfo, true, @"D:\添加螺丝记录\螺丝上料Log.csv", appendMode: true); // } // else // { // await plc.WriteNodeAsync(addressConfig.Out_PlcFail.Address, true); // MessageBox.Show("螺丝型号匹配失败", "提示", MessageBoxButton.OK, MessageBoxImage.Warning); // //SendTaskMessage($"螺丝型号匹配失败", MessageLevel.Error); // ExportDataToCsv(DateTime.Now.ToString("yyyy-MM-dd HH:mm"), a, stationConfig.DataInfo, false, @"D:\添加螺丝记录\螺丝上料Log.csv", appendMode: true); // } //} //catch (Exception ex) //{ //} //PlcCommandTrigger(("AutoSensor", "", "")); //string a = ""; //var stationConfig = _configService.GetDeviceInfo(0); //ScannedBarcode = BarcodeTextBox.Text; //if (ScannedBarcode.Contains("|")) //{ // string[] arr = ScannedBarcode.Split("|"); // for (int i = 0; i < 16; i++) // { // a += arr[i]; // } //} //else //{ // a = ScannedBarcode; //} //if (string.IsNullOrWhiteSpace(ScannedBarcode)) //{ // MessageBox.Show("请先扫描螺丝条码", "提示", MessageBoxButton.OK, MessageBoxImage.Warning); // BarcodeTextBox.Focus(); // 重新聚焦 // return; //} //if (a == stationConfig.DataInfo.Screwsize1 || a == stationConfig.DataInfo.Screwsize2) //{ // MessageBox.Show("螺丝型号匹配成功", "提示", MessageBoxButton.OK, MessageBoxImage.Warning); // //SendTaskMessage($"螺丝型号匹配成功", MessageLevel.Info); // ExportDataToCsv(DateTime.Now.ToString("yyyy-MM-dd HH:mm"), a, true, @"D:\添加螺丝记录\螺丝上料Log.csv", appendMode: true); //} //else //{ // MessageBox.Show("螺丝型号匹配失败", "提示", MessageBoxButton.OK, MessageBoxImage.Warning); // //SendTaskMessage($"螺丝型号匹配失败", MessageLevel.Error); // ExportDataToCsv(DateTime.Now.ToString("yyyy-MM-dd HH:mm"), a, false, @"D:\添加螺丝记录\螺丝上料Log.csv", appendMode: true); //} //DialogResult = true; //Close(); } private void CancelExecute(object sender) { } //public void SendTaskMessage(string msg, MessageLevel level) //{ // App.Current.Dispatcher.Invoke(() =>aZ // { // _eventAggregator.GetEvent().Publish(new Models.MessageStruct() { Message = msg, level = level }); // }); //} public static void ExportDataToCsv(string dateTime, string model, CTQ_Data DataInfo, bool isMatch, string savePath, bool appendMode = false) { string directory = global::System.IO.Path.GetDirectoryName(savePath); // 如果目录路径不为空且不存在,则创建 if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory)) { Directory.CreateDirectory(directory); } // 构建CSV内容 StringBuilder csvContent = new StringBuilder(); // 处理表头:仅当文件不存在/非追加模式时,才写入表头 if (!appendMode || !File.Exists(savePath)) { csvContent.AppendLine("日期时间,目标螺丝型号1,螺丝型号2,螺丝型号3,添加螺丝型号,是否匹配"); } // 处理数据行 string matchStr = isMatch ? "是" : "否"; // 如果型号中包含逗号,需要用引号包裹 string safeModel = model; if (safeModel.Contains(",")) { safeModel = $"\"{safeModel}\""; } csvContent.AppendLine($"{dateTime},{DataInfo.Screwsize1},{DataInfo.Screwsize2},{DataInfo.Screwsize3},{model},{matchStr}"); // 写入文件:UTF-8带BOM编码,Excel打开无乱码 if (appendMode && File.Exists(savePath)) { // 追加模式:在文件末尾新增内容 File.AppendAllText(savePath, csvContent.ToString(), Encoding.UTF8); } else { // 覆盖模式:新建/替换文件 File.WriteAllText(savePath, csvContent.ToString(), Encoding.UTF8); } } } //public class RelayCommand : ICommand //{ // private readonly Action _execute; // private readonly Func _canExecute; // public event EventHandler CanExecuteChanged // { // add { CommandManager.RequerySuggested += value; } // remove { CommandManager.RequerySuggested -= value; } // } // public RelayCommand(Action execute, Func canExecute = null) // { // _execute = execute ?? throw new ArgumentNullException(nameof(execute)); // _canExecute = canExecute; // } // public bool CanExecute(object parameter) => _canExecute == null || _canExecute(parameter); // public void Execute(object parameter) => _execute(parameter); //} }