| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- 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
- {
- /// <summary>
- /// AddScrewView.xaml 的交互逻辑
- /// </summary>
- 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;
- }
- ///// <summary>
- ///// PLC命令触发时
- ///// </summary>
- ///// <param name="tuple"></param>
- //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<bool>(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<bool>(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<TaskMessageNotification>().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<object> _execute;
- // private readonly Func<object, bool> _canExecute;
- // public event EventHandler CanExecuteChanged
- // {
- // add { CommandManager.RequerySuggested += value; }
- // remove { CommandManager.RequerySuggested -= value; }
- // }
- // public RelayCommand(Action<object> execute, Func<object, bool> 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);
- //}
- }
|