AddScrewView.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. using Prism.Events;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.ObjectModel;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Shapes;
  17. using TeamAAS_VP.Core.PLCs;
  18. using TeamAAS_VP.Enums;
  19. using TeamAAS_VP.Events;
  20. using TeamAAS_VP.Interfaces;
  21. using TeamAAS_VP.Models;
  22. using TeamAAS_VP.Resources.Languages;
  23. using TeamAAS_VP.Services;
  24. using TeamAAS_VP.ViewModels.Home;
  25. using TouchSocket.Core;
  26. namespace TeamAAS_VP.Views
  27. {
  28. /// <summary>
  29. /// AddScrewView.xaml 的交互逻辑
  30. /// </summary>
  31. public partial class AddScrewView : UserControl
  32. {
  33. IConfigService _configService;
  34. IEventAggregator _eventAggregator;
  35. IPlcService _plcService;
  36. public ICommand OkCommand { get; }
  37. //public ICommand CancelCommand { get; }
  38. public string ScannedBarcode { get; private set; }
  39. public AddScrewView(IConfigService configService, IEventAggregator eventAggregator, IPlcService plcService)
  40. {
  41. InitializeComponent();
  42. this.Width = 800;
  43. this.Height = 500;
  44. //_configService = configService;
  45. //_eventAggregator = eventAggregator;
  46. //_plcService=plcService;
  47. //OkCommand = new RelayCommand(OkExecute);
  48. //CancelCommand = new RelayCommand(CancelExecute);
  49. this.Loaded += (s, e) => { BarcodeTextBox.Focus(); };
  50. // 把Command赋值给DataContext,方便绑定
  51. //DataContext = this;
  52. }
  53. ///// <summary>
  54. ///// PLC命令触发时
  55. ///// </summary>
  56. ///// <param name="tuple"></param>
  57. //private async void PlcCommandTrigger((string key, string nodeId, object value) tuple)
  58. //{
  59. // try
  60. // {
  61. // if (tuple.key != "AutoSensor")
  62. // {
  63. // return;
  64. // }
  65. // var addressConfig = _configService.GetPlcAddresses();
  66. // var plc = _plcService.GetPlcByNumber(addressConfig.PlcNo) as OpcUaClientPLC;
  67. // string a = "";
  68. // var stationConfig = _configService.GetDeviceInfo(0);
  69. // ScannedBarcode = BarcodeTextBox.Text;
  70. // if (ScannedBarcode.Contains("|"))
  71. // {
  72. // string[] arr = ScannedBarcode.Split("|");
  73. // for (int i = 0; i < 16; i++)
  74. // {
  75. // a += arr[i];
  76. // }
  77. // }
  78. // else
  79. // {
  80. // a = ScannedBarcode;
  81. // }
  82. // if (string.IsNullOrWhiteSpace(ScannedBarcode))
  83. // {
  84. // MessageBox.Show("请先扫描螺丝条码", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
  85. // BarcodeTextBox.Focus(); // 重新聚焦
  86. // return;
  87. // }
  88. // if (a == stationConfig.DataInfo.Screwsize1 || a == stationConfig.DataInfo.Screwsize2)
  89. // {
  90. // MessageBox.Show("螺丝型号匹配成功", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
  91. // //SendTaskMessage($"螺丝型号匹配成功", MessageLevel.Info);
  92. // ExportDataToCsv(DateTime.Now.ToString("yyyy-MM-dd HH:mm"), a, true, @"D:\添加螺丝记录\螺丝上料Log.csv", appendMode: true);
  93. // await plc.WriteNodeAsync(string.Format(addressConfig.Out_MesStatus.Address, 1), (Int16)1);
  94. // }
  95. // else
  96. // {
  97. // MessageBox.Show("螺丝型号匹配失败", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
  98. // //SendTaskMessage($"螺丝型号匹配失败", MessageLevel.Error);
  99. // ExportDataToCsv(DateTime.Now.ToString("yyyy-MM-dd HH:mm"), a, false, @"D:\添加螺丝记录\螺丝上料Log.csv", appendMode: true);
  100. // await plc.WriteNodeAsync(string.Format(addressConfig.Out_MesStatus.Address, 1), (Int16)1);
  101. // }
  102. // DialogResult = true;
  103. // Close();
  104. // }
  105. // catch (Exception ex)
  106. // {
  107. // }
  108. //}
  109. private async void OkExecute(object sender)
  110. {
  111. //try
  112. //{
  113. // var addressConfig = _configService.GetPlcAddresses();
  114. // var plc = _plcService.GetPlcByNumber(addressConfig.PlcNo) as OpcUaClientPLC;
  115. // string a = "";
  116. // var stationConfig = _configService.GetDeviceInfo(0);
  117. // ScannedBarcode = BarcodeTextBox.Text;
  118. // if (ScannedBarcode.Contains("|"))
  119. // {
  120. // string[] arr = ScannedBarcode.Split("|");
  121. // for (int i = 0; i < 16; i++)
  122. // {
  123. // a += arr[i];
  124. // }
  125. // }
  126. // else
  127. // {
  128. // a = ScannedBarcode;
  129. // }
  130. // if (string.IsNullOrWhiteSpace(ScannedBarcode))
  131. // {
  132. // MessageBox.Show("请先扫描螺丝条码", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
  133. // BarcodeTextBox.Focus(); // 重新聚焦
  134. // return;
  135. // }
  136. // if (a == stationConfig.DataInfo.Screwsize1 || a == stationConfig.DataInfo.Screwsize2 || a == stationConfig.DataInfo.Screwsize3)
  137. // {
  138. // await plc.WriteNodeAsync<bool>(addressConfig.Out_PlcFail.Address, false);
  139. // MessageBox.Show("螺丝型号匹配成功", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
  140. // //SendTaskMessage($"螺丝型号匹配成功", MessageLevel.Info);
  141. // ExportDataToCsv(DateTime.Now.ToString("yyyy-MM-dd HH:mm"), a, stationConfig.DataInfo, true, @"D:\添加螺丝记录\螺丝上料Log.csv", appendMode: true);
  142. // }
  143. // else
  144. // {
  145. // await plc.WriteNodeAsync<bool>(addressConfig.Out_PlcFail.Address, true);
  146. // MessageBox.Show("螺丝型号匹配失败", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
  147. // //SendTaskMessage($"螺丝型号匹配失败", MessageLevel.Error);
  148. // ExportDataToCsv(DateTime.Now.ToString("yyyy-MM-dd HH:mm"), a, stationConfig.DataInfo, false, @"D:\添加螺丝记录\螺丝上料Log.csv", appendMode: true);
  149. // }
  150. //}
  151. //catch (Exception ex)
  152. //{
  153. //}
  154. //PlcCommandTrigger(("AutoSensor", "", ""));
  155. //string a = "";
  156. //var stationConfig = _configService.GetDeviceInfo(0);
  157. //ScannedBarcode = BarcodeTextBox.Text;
  158. //if (ScannedBarcode.Contains("|"))
  159. //{
  160. // string[] arr = ScannedBarcode.Split("|");
  161. // for (int i = 0; i < 16; i++)
  162. // {
  163. // a += arr[i];
  164. // }
  165. //}
  166. //else
  167. //{
  168. // a = ScannedBarcode;
  169. //}
  170. //if (string.IsNullOrWhiteSpace(ScannedBarcode))
  171. //{
  172. // MessageBox.Show("请先扫描螺丝条码", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
  173. // BarcodeTextBox.Focus(); // 重新聚焦
  174. // return;
  175. //}
  176. //if (a == stationConfig.DataInfo.Screwsize1 || a == stationConfig.DataInfo.Screwsize2)
  177. //{
  178. // MessageBox.Show("螺丝型号匹配成功", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
  179. // //SendTaskMessage($"螺丝型号匹配成功", MessageLevel.Info);
  180. // ExportDataToCsv(DateTime.Now.ToString("yyyy-MM-dd HH:mm"), a, true, @"D:\添加螺丝记录\螺丝上料Log.csv", appendMode: true);
  181. //}
  182. //else
  183. //{
  184. // MessageBox.Show("螺丝型号匹配失败", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
  185. // //SendTaskMessage($"螺丝型号匹配失败", MessageLevel.Error);
  186. // ExportDataToCsv(DateTime.Now.ToString("yyyy-MM-dd HH:mm"), a, false, @"D:\添加螺丝记录\螺丝上料Log.csv", appendMode: true);
  187. //}
  188. //DialogResult = true;
  189. //Close();
  190. }
  191. private void CancelExecute(object sender)
  192. {
  193. }
  194. //public void SendTaskMessage(string msg, MessageLevel level)
  195. //{
  196. // App.Current.Dispatcher.Invoke(() =>aZ
  197. // {
  198. // _eventAggregator.GetEvent<TaskMessageNotification>().Publish(new Models.MessageStruct() { Message = msg, level = level });
  199. // });
  200. //}
  201. public static void ExportDataToCsv(string dateTime, string model, CTQ_Data DataInfo, bool isMatch, string savePath, bool appendMode = false)
  202. {
  203. string directory = global::System.IO.Path.GetDirectoryName(savePath);
  204. // 如果目录路径不为空且不存在,则创建
  205. if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
  206. {
  207. Directory.CreateDirectory(directory);
  208. }
  209. // 构建CSV内容
  210. StringBuilder csvContent = new StringBuilder();
  211. // 处理表头:仅当文件不存在/非追加模式时,才写入表头
  212. if (!appendMode || !File.Exists(savePath))
  213. {
  214. csvContent.AppendLine("日期时间,目标螺丝型号1,螺丝型号2,供料器1颜色,供料器2颜色,螺丝型号3,添加螺丝型号,是否匹配");
  215. }
  216. // 处理数据行
  217. string matchStr = isMatch ? "是" : "否";
  218. // 如果型号中包含逗号,需要用引号包裹
  219. string safeModel = model;
  220. if (safeModel.Contains(","))
  221. {
  222. safeModel = $"\"{safeModel}\"";
  223. }
  224. csvContent.AppendLine($"{dateTime},{DataInfo.Feeder1ScrewSN},{DataInfo.Feeder2ScrewSN},{DataInfo.Feeder1ScrewColor},{DataInfo.Feeder2ScrewColor},{model},{matchStr}");
  225. // 写入文件:UTF-8带BOM编码,Excel打开无乱码
  226. if (appendMode && File.Exists(savePath))
  227. {
  228. // 追加模式:在文件末尾新增内容
  229. File.AppendAllText(savePath, csvContent.ToString(), Encoding.UTF8);
  230. }
  231. else
  232. {
  233. // 覆盖模式:新建/替换文件
  234. File.WriteAllText(savePath, csvContent.ToString(), Encoding.UTF8);
  235. }
  236. }
  237. }
  238. //public class RelayCommand : ICommand
  239. //{
  240. // private readonly Action<object> _execute;
  241. // private readonly Func<object, bool> _canExecute;
  242. // public event EventHandler CanExecuteChanged
  243. // {
  244. // add { CommandManager.RequerySuggested += value; }
  245. // remove { CommandManager.RequerySuggested -= value; }
  246. // }
  247. // public RelayCommand(Action<object> execute, Func<object, bool> canExecute = null)
  248. // {
  249. // _execute = execute ?? throw new ArgumentNullException(nameof(execute));
  250. // _canExecute = canExecute;
  251. // }
  252. // public bool CanExecute(object parameter) => _canExecute == null || _canExecute(parameter);
  253. // public void Execute(object parameter) => _execute(parameter);
  254. //}
  255. }