using LocalhostMES.Core; using LocalhostMES.DataBase; using LocalhostMES.Models; using LocalhostMES.ViewModels.Services; using Newtonsoft.Json; using Prism.Commands; using Prism.Mvvm; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Windows.Media; namespace LocalhostMES.ViewModels.Tabs { public class ApiTestViewModel : BindableBase, IDisposable { private readonly IMesWorkspace _workspace; public ApiTestViewModel(IMesWorkspace workspace) { _workspace = workspace; _workspace.Initialize(); if (_workspace is INotifyPropertyChanged npc) { npc.PropertyChanged += OnWorkspacePropertyChanged; } } public ObservableCollection Logs => _workspace.Logs; public string StatusTxt => _workspace.StatusLine; public Brush StatusForeground => _workspace.StatusForeground; private void OnWorkspacePropertyChanged(object sender, PropertyChangedEventArgs e) { if (e.PropertyName == nameof(IMesWorkspace.StatusLine)) { RaisePropertyChanged(nameof(StatusTxt)); } if (e.PropertyName == nameof(IMesWorkspace.StatusForeground)) { RaisePropertyChanged(nameof(StatusForeground)); } } private DelegateCommand _testSnPrintCommand; public DelegateCommand TestSnPrintCommand => _testSnPrintCommand ?? (_testSnPrintCommand = new DelegateCommand(TestSnPrint)); private DelegateCommand _testSnComponentCommand; public DelegateCommand TestSnComponentCommand => _testSnComponentCommand ?? (_testSnComponentCommand = new DelegateCommand(TestSnComponent)); private DelegateCommand _testProcessParamsCommand; public DelegateCommand TestProcessParamsCommand => _testProcessParamsCommand ?? (_testProcessParamsCommand = new DelegateCommand(TestProcessParams)); private DelegateCommand _clearLogCommand; public DelegateCommand ClearLogCommand => _clearLogCommand ?? (_clearLogCommand = new DelegateCommand(() => Logs.Clear())); private async void TestSnPrint() { try { if (_workspace.MesManagement.ApiClient == null) { _workspace.ShowStatus("请先启动服务并配置API客户端", true); return; } var first = _workspace.WorkOrders.FirstOrDefault(); if (first == null) { _workspace.ShowStatus("请先在「工单管理」中添加工单", true); return; } AddLog("=== 测试SN打印请求接口 ==="); var request = new SnPrintRequest { plant = "1211", workShop = "附件工厂", line = first.LineCode, station = "ST01", site = "ST01-01", equipment = "PRINTER001", orderNo = null, workOrderNo = first.WorkOrderNo, count = 2, printType = 1, messageTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") }; AddLog("请求参数:"); AddLog(JsonConvert.SerializeObject(request, Formatting.Indented)); var response = await _workspace.MesManagement.ApiClient.RequestSnPrintAsync(request); AddLog("响应结果:"); LogHelper.WriteLogInfo(JsonConvert.SerializeObject(response, Formatting.Indented)); _workspace.ShowStatus("SN打印请求测试完成", false); } catch (Exception ex) { AddLog($"错误: {ex.Message}"); _workspace.ShowStatus($"测试失败: {ex.Message}", true); } } private async void TestSnComponent() { try { if (_workspace.MesManagement.ApiClient == null) { _workspace.ShowStatus("请先启动服务并配置API客户端", true); return; } var first = _workspace.WorkOrders.FirstOrDefault(); if (first == null) { _workspace.ShowStatus("请先在「工单管理」中添加工单", true); return; } AddLog("=== 测试SN关键件绑定接口 ==="); var infos = DatabaseHelper.SelectSnInfo(first.WorkOrderNo, true, false); if (infos.Count != 0) { var request = new SnKeyComponentRequest { businessTpye = "SCAN_TASK", plant = "1", workShop = "ZPCJ", lineCode = "ZPX-01", stationCode = "ZPX-01-01", positionCode = "ZPX-01-01-01", barcodeBoundProcInfo = "1", equipment = "SCANNER001", scanTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), taskCode = "TASK-001", scanTpye = "1", sn = infos[0].Sn, gbCode = "GB123456", snStatus = "1", employee = "张三", part = new List { new PartInfo { partNum = "5120101-ZE02-001", partQty = 1, materialCode = "MAT-001", oldPartNum = "", materialType = "1", positionNo = "P001" } } }; AddLog("请求参数:"); AddLog(JsonConvert.SerializeObject(request, Formatting.Indented)); var response = await _workspace.MesManagement.ApiClient.SendSnComponentAsync(request); AddLog("响应结果:"); AddLog(JsonConvert.SerializeObject(response, Formatting.Indented)); } else { AddLog("无可用SN"); } _workspace.ShowStatus("SN关键件绑定测试完成", false); } catch (Exception ex) { AddLog($"错误: {ex.Message}"); _workspace.ShowStatus($"测试失败: {ex.Message}", true); } } private async void TestProcessParams() { try { if (_workspace.MesManagement.ApiClient == null) { _workspace.ShowStatus("请先启动服务并配置API客户端", true); return; } var first = _workspace.WorkOrders.FirstOrDefault(); if (first == null) { _workspace.ShowStatus("请先在「工单管理」中添加工单", true); return; } AddLog("=== 测试加工参数上报接口 ==="); var infos = DatabaseHelper.SelectSnInfo(first.WorkOrderNo, true, false); if (infos.Count != 0) { var request = new ProcessParameterRequest { businessTpye = "SCAN_TASK", plant = "1211", workShop = "ZPCJ", line = "ZPX-01", station = "ZPX-01-01", site = "ZPX-01-01-01", sn = infos[0].Sn, barcode = "PART-001", materialCode = "T03GCD", equipment = "EQUIP001", overallResult = "OK", tightenResultDetail = new List { new TightenResult { point_num = 1, pset = 1, torque = 45.08m, angle = 44.0m, tighten_status = "OK", tighten_dt = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") } }, stepResultDetail = new List { new StepResult { tagCode = "TEST_PRESSURE", tagValue = "3.0", tagRage = "2.7-3.3", tagResult = "OK", tagUnit = "kg", ngCode = new List() } }, reservedField1 = "", reservedField2 = "", reservedField3 = "", messageTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") }; AddLog("请求参数:"); AddLog(JsonConvert.SerializeObject(request, Formatting.Indented)); var response = await _workspace.MesManagement.ApiClient.SendProcessParametersAsync(request); AddLog("响应结果:"); AddLog(JsonConvert.SerializeObject(response, Formatting.Indented)); infos[0].IsUsed = true; DatabaseHelper.UpdateSnInfo(infos[0]); } else { AddLog("无可用SN"); var request = new ProcessParameterRequest { businessTpye = "SCAN_TASK", plant = "1211", workShop = "ZPCJ", line = "ZPX-01", station = "ZPX-01-01", site = "ZPX-01-01-01", sn = "LingPao", barcode = "PART-001", materialCode = "T03GCD", equipment = "EQUIP001", overallResult = "OK", tightenResultDetail = new List { new TightenResult { point_num = 1, pset = 1, torque = 45.08m, angle = 44.0m, tighten_status = "OK", tighten_dt = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") } }, stepResultDetail = new List { new StepResult { tagCode = "TEST_PRESSURE", tagValue = "3.0", tagRage = "2.7-3.3", tagResult = "OK", tagUnit = "kg", ngCode = new List() } }, reservedField1 = "", reservedField2 = "", reservedField3 = "", messageTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") }; AddLog("请求参数:"); AddLog(JsonConvert.SerializeObject(request, Formatting.Indented)); var response = await _workspace.MesManagement.ApiClient.SendProcessParametersAsync(request); AddLog("响应结果:"); AddLog(JsonConvert.SerializeObject(response, Formatting.Indented)); } _workspace.ShowStatus("加工参数上报测试完成", false); } catch (Exception ex) { AddLog($"错误: {ex.Message}"); _workspace.ShowStatus($"测试失败: {ex.Message}", true); } } private void AddLog(string message) { _workspace.Logs.Add($"[{DateTime.Now:HH:mm:ss}] {message}\n"); } public void Dispose() { if (_workspace is INotifyPropertyChanged npc) { npc.PropertyChanged -= OnWorkspacePropertyChanged; } } } }