| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- using Cognex.VisionPro.MultiLineFinder.Implementation.Internal;
- using Opc.Ua;
- using Prism.Events;
- using Prism.Mvvm;
- using Prism.Regions;
- using Prism.Services.Dialogs;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Input;
- using TeamAAS_VP.Core.PLCs;
- using TeamAAS_VP.Interfaces;
- using TeamAAS_VP.Models;
- using TeamAAS_VP.Views;
- namespace TeamAAS_VP.ViewModels
- {
- public class AddScrewViewModel : BindableBase,IDialogAware
- {
- IConfigService _configService;
- IEventAggregator _eventAggregator;
- IPlcService _plcService;
- private string _UserID;
- /// <summary>
- /// 员工工号
- /// </summary>
- public string UserID
- {
- get { return _UserID; }
- set { SetProperty(ref _UserID, value); }
- }
- private string _ScrewsizeCode;
- /// <summary>
- /// 螺丝条码
- /// </summary>
- public string ScrewsizeCode
- {
- get { return _ScrewsizeCode; }
- set { SetProperty(ref _ScrewsizeCode, value); }
- }
- private DelegateCommand _OkCommand;
- public DelegateCommand OkCommand =>
- _OkCommand ?? (_OkCommand = new DelegateCommand<string>(OkExecute));
- private DelegateCommand _CancelCommand;
- public event Action<IDialogResult> RequestClose;
- public DelegateCommand CancelCommand =>
- _CancelCommand ?? (_CancelCommand = new DelegateCommand<string>(CancelExecute));
- public string Title => "";
- public AddScrewViewModel(IConfigService configService, IEventAggregator eventAggregator, IPlcService plcService)
- {
- _configService = configService;
- _eventAggregator = eventAggregator;
- _plcService = plcService;
- }
- private async void OkExecute(string str)
- {
- try
- {
- var addressConfig = _configService.GetPlcAddresses();
- var plc = _plcService.GetPlcByNumber(addressConfig.PlcNo) as OpcUaClientPLC;
- string a = "";
- var stationConfig = _configService.GetDeviceInfo(0);
- if (ScrewsizeCode.Contains("|"))
- {
- string[] arr = ScrewsizeCode.Split('|');
- //for (int i = 0; i < 16; i++)
- //{
- // a += arr[i];
- //}
- a = arr[0];
- }
- else
- {
- a = ScrewsizeCode;
- }
- UserID = UserID.Replace("\r\n", "").Trim();
- if (string.IsNullOrWhiteSpace(a) && string.IsNullOrEmpty(UserID))
- {
- 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, UserID, @"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, UserID, @"D:\添加螺丝记录\螺丝上料Log.csv", appendMode: true);
- }
- //DialogResult = true;
- //Close();
- }
- catch (Exception ex)
- {
- MessageBox.Show("螺丝螺丝条码异常", "提示", MessageBoxButton.OK, MessageBoxImage.Error);
- return;
- }
- }
- private void CancelExecute(string str)
- {
- //DialogResult = false;
- //Close();
- }
- public static void ExportDataToCsv(string dateTime, string model, CTQ_Data DataInfo, bool isMatch,string UserId, 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,添加螺丝型号,添加螺丝员工,是否匹配");
- csvContent.AppendLine("DateTime,Screw1,Screw2,Screw3,AddScrew,UserID,Results");
- }
- // 处理数据行
- string matchStr = isMatch ? "OK" : "NG";
- // 如果型号中包含逗号,需要用引号包裹
- string safeModel = model;
- if (safeModel.Contains(","))
- {
- safeModel = $"\"{safeModel}\"";
- }
- csvContent.AppendLine($"{dateTime},{DataInfo.Screwsize1},{DataInfo.Screwsize2},{DataInfo.Screwsize3},{model},{UserId},{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 bool CanCloseDialog()
- {
- return true;
- }
- public void OnDialogClosed()
- {
- }
- public void OnDialogOpened(IDialogParameters parameters)
- {
- }
- }
- }
|