| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340 |
- 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<string> 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<PartInfo>
- {
- 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<TightenResult>
- {
- 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<StepResult>
- {
- new StepResult
- {
- tagCode = "TEST_PRESSURE",
- tagValue = "3.0",
- tagRage = "2.7-3.3",
- tagResult = "OK",
- tagUnit = "kg",
- ngCode = new List<NgCodeInfo>()
- }
- },
- 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<TightenResult>
- {
- 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<StepResult>
- {
- new StepResult
- {
- tagCode = "TEST_PRESSURE",
- tagValue = "3.0",
- tagRage = "2.7-3.3",
- tagResult = "OK",
- tagUnit = "kg",
- ngCode = new List<NgCodeInfo>()
- }
- },
- 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;
- }
- }
- }
- }
|