ApiTestViewModel.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. using LocalhostMES.Core;
  2. using LocalhostMES.DataBase;
  3. using LocalhostMES.Models;
  4. using LocalhostMES.ViewModels.Services;
  5. using Newtonsoft.Json;
  6. using Prism.Commands;
  7. using Prism.Mvvm;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Collections.ObjectModel;
  11. using System.ComponentModel;
  12. using System.Linq;
  13. using System.Windows.Media;
  14. namespace LocalhostMES.ViewModels.Tabs
  15. {
  16. public class ApiTestViewModel : BindableBase, IDisposable
  17. {
  18. private readonly IMesWorkspace _workspace;
  19. public ApiTestViewModel(IMesWorkspace workspace)
  20. {
  21. _workspace = workspace;
  22. _workspace.Initialize();
  23. if (_workspace is INotifyPropertyChanged npc)
  24. {
  25. npc.PropertyChanged += OnWorkspacePropertyChanged;
  26. }
  27. }
  28. public ObservableCollection<string> Logs => _workspace.Logs;
  29. public string StatusTxt => _workspace.StatusLine;
  30. public Brush StatusForeground => _workspace.StatusForeground;
  31. private void OnWorkspacePropertyChanged(object sender, PropertyChangedEventArgs e)
  32. {
  33. if (e.PropertyName == nameof(IMesWorkspace.StatusLine))
  34. {
  35. RaisePropertyChanged(nameof(StatusTxt));
  36. }
  37. if (e.PropertyName == nameof(IMesWorkspace.StatusForeground))
  38. {
  39. RaisePropertyChanged(nameof(StatusForeground));
  40. }
  41. }
  42. private DelegateCommand _testSnPrintCommand;
  43. public DelegateCommand TestSnPrintCommand =>
  44. _testSnPrintCommand ?? (_testSnPrintCommand = new DelegateCommand(TestSnPrint));
  45. private DelegateCommand _testSnComponentCommand;
  46. public DelegateCommand TestSnComponentCommand =>
  47. _testSnComponentCommand ?? (_testSnComponentCommand = new DelegateCommand(TestSnComponent));
  48. private DelegateCommand _testProcessParamsCommand;
  49. public DelegateCommand TestProcessParamsCommand =>
  50. _testProcessParamsCommand ?? (_testProcessParamsCommand = new DelegateCommand(TestProcessParams));
  51. private DelegateCommand _clearLogCommand;
  52. public DelegateCommand ClearLogCommand =>
  53. _clearLogCommand ?? (_clearLogCommand = new DelegateCommand(() => Logs.Clear()));
  54. private async void TestSnPrint()
  55. {
  56. try
  57. {
  58. if (_workspace.MesManagement.ApiClient == null)
  59. {
  60. _workspace.ShowStatus("请先启动服务并配置API客户端", true);
  61. return;
  62. }
  63. var first = _workspace.WorkOrders.FirstOrDefault();
  64. if (first == null)
  65. {
  66. _workspace.ShowStatus("请先在「工单管理」中添加工单", true);
  67. return;
  68. }
  69. AddLog("=== 测试SN打印请求接口 ===");
  70. var request = new SnPrintRequest
  71. {
  72. plant = "1211",
  73. workShop = "附件工厂",
  74. line = first.LineCode,
  75. station = "ST01",
  76. site = "ST01-01",
  77. equipment = "PRINTER001",
  78. orderNo = null,
  79. workOrderNo = first.WorkOrderNo,
  80. count = 2,
  81. printType = 1,
  82. messageTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  83. };
  84. AddLog("请求参数:");
  85. AddLog(JsonConvert.SerializeObject(request, Formatting.Indented));
  86. var response = await _workspace.MesManagement.ApiClient.RequestSnPrintAsync(request);
  87. AddLog("响应结果:");
  88. LogHelper.WriteLogInfo(JsonConvert.SerializeObject(response, Formatting.Indented));
  89. _workspace.ShowStatus("SN打印请求测试完成", false);
  90. }
  91. catch (Exception ex)
  92. {
  93. AddLog($"错误: {ex.Message}");
  94. _workspace.ShowStatus($"测试失败: {ex.Message}", true);
  95. }
  96. }
  97. private async void TestSnComponent()
  98. {
  99. try
  100. {
  101. if (_workspace.MesManagement.ApiClient == null)
  102. {
  103. _workspace.ShowStatus("请先启动服务并配置API客户端", true);
  104. return;
  105. }
  106. var first = _workspace.WorkOrders.FirstOrDefault();
  107. if (first == null)
  108. {
  109. _workspace.ShowStatus("请先在「工单管理」中添加工单", true);
  110. return;
  111. }
  112. AddLog("=== 测试SN关键件绑定接口 ===");
  113. var infos = DatabaseHelper.SelectSnInfo(first.WorkOrderNo, true, false);
  114. if (infos.Count != 0)
  115. {
  116. var request = new SnKeyComponentRequest
  117. {
  118. businessTpye = "SCAN_TASK",
  119. plant = "1",
  120. workShop = "ZPCJ",
  121. lineCode = "ZPX-01",
  122. stationCode = "ZPX-01-01",
  123. positionCode = "ZPX-01-01-01",
  124. barcodeBoundProcInfo = "1",
  125. equipment = "SCANNER001",
  126. scanTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  127. taskCode = "TASK-001",
  128. scanTpye = "1",
  129. sn = infos[0].Sn,
  130. gbCode = "GB123456",
  131. snStatus = "1",
  132. employee = "张三",
  133. part = new List<PartInfo>
  134. {
  135. new PartInfo
  136. {
  137. partNum = "5120101-ZE02-001",
  138. partQty = 1,
  139. materialCode = "MAT-001",
  140. oldPartNum = "",
  141. materialType = "1",
  142. positionNo = "P001"
  143. }
  144. }
  145. };
  146. AddLog("请求参数:");
  147. AddLog(JsonConvert.SerializeObject(request, Formatting.Indented));
  148. var response = await _workspace.MesManagement.ApiClient.SendSnComponentAsync(request);
  149. AddLog("响应结果:");
  150. AddLog(JsonConvert.SerializeObject(response, Formatting.Indented));
  151. }
  152. else
  153. {
  154. AddLog("无可用SN");
  155. }
  156. _workspace.ShowStatus("SN关键件绑定测试完成", false);
  157. }
  158. catch (Exception ex)
  159. {
  160. AddLog($"错误: {ex.Message}");
  161. _workspace.ShowStatus($"测试失败: {ex.Message}", true);
  162. }
  163. }
  164. private async void TestProcessParams()
  165. {
  166. try
  167. {
  168. if (_workspace.MesManagement.ApiClient == null)
  169. {
  170. _workspace.ShowStatus("请先启动服务并配置API客户端", true);
  171. return;
  172. }
  173. var first = _workspace.WorkOrders.FirstOrDefault();
  174. if (first == null)
  175. {
  176. _workspace.ShowStatus("请先在「工单管理」中添加工单", true);
  177. return;
  178. }
  179. AddLog("=== 测试加工参数上报接口 ===");
  180. var infos = DatabaseHelper.SelectSnInfo(first.WorkOrderNo, true, false);
  181. if (infos.Count != 0)
  182. {
  183. var request = new ProcessParameterRequest
  184. {
  185. businessTpye = "SCAN_TASK",
  186. plant = "1211",
  187. workShop = "ZPCJ",
  188. line = "ZPX-01",
  189. station = "ZPX-01-01",
  190. site = "ZPX-01-01-01",
  191. sn = infos[0].Sn,
  192. barcode = "PART-001",
  193. materialCode = "T03GCD",
  194. equipment = "EQUIP001",
  195. overallResult = "OK",
  196. tightenResultDetail = new List<TightenResult>
  197. {
  198. new TightenResult
  199. {
  200. point_num = 1,
  201. pset = 1,
  202. torque = 45.08m,
  203. angle = 44.0m,
  204. tighten_status = "OK",
  205. tighten_dt = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  206. }
  207. },
  208. stepResultDetail = new List<StepResult>
  209. {
  210. new StepResult
  211. {
  212. tagCode = "TEST_PRESSURE",
  213. tagValue = "3.0",
  214. tagRage = "2.7-3.3",
  215. tagResult = "OK",
  216. tagUnit = "kg",
  217. ngCode = new List<NgCodeInfo>()
  218. }
  219. },
  220. reservedField1 = "",
  221. reservedField2 = "",
  222. reservedField3 = "",
  223. messageTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  224. };
  225. AddLog("请求参数:");
  226. AddLog(JsonConvert.SerializeObject(request, Formatting.Indented));
  227. var response = await _workspace.MesManagement.ApiClient.SendProcessParametersAsync(request);
  228. AddLog("响应结果:");
  229. AddLog(JsonConvert.SerializeObject(response, Formatting.Indented));
  230. infos[0].IsUsed = true;
  231. DatabaseHelper.UpdateSnInfo(infos[0]);
  232. }
  233. else
  234. {
  235. AddLog("无可用SN");
  236. var request = new ProcessParameterRequest
  237. {
  238. businessTpye = "SCAN_TASK",
  239. plant = "1211",
  240. workShop = "ZPCJ",
  241. line = "ZPX-01",
  242. station = "ZPX-01-01",
  243. site = "ZPX-01-01-01",
  244. sn = "LingPao",
  245. barcode = "PART-001",
  246. materialCode = "T03GCD",
  247. equipment = "EQUIP001",
  248. overallResult = "OK",
  249. tightenResultDetail = new List<TightenResult>
  250. {
  251. new TightenResult
  252. {
  253. point_num = 1,
  254. pset = 1,
  255. torque = 45.08m,
  256. angle = 44.0m,
  257. tighten_status = "OK",
  258. tighten_dt = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  259. }
  260. },
  261. stepResultDetail = new List<StepResult>
  262. {
  263. new StepResult
  264. {
  265. tagCode = "TEST_PRESSURE",
  266. tagValue = "3.0",
  267. tagRage = "2.7-3.3",
  268. tagResult = "OK",
  269. tagUnit = "kg",
  270. ngCode = new List<NgCodeInfo>()
  271. }
  272. },
  273. reservedField1 = "",
  274. reservedField2 = "",
  275. reservedField3 = "",
  276. messageTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  277. };
  278. AddLog("请求参数:");
  279. AddLog(JsonConvert.SerializeObject(request, Formatting.Indented));
  280. var response = await _workspace.MesManagement.ApiClient.SendProcessParametersAsync(request);
  281. AddLog("响应结果:");
  282. AddLog(JsonConvert.SerializeObject(response, Formatting.Indented));
  283. }
  284. _workspace.ShowStatus("加工参数上报测试完成", false);
  285. }
  286. catch (Exception ex)
  287. {
  288. AddLog($"错误: {ex.Message}");
  289. _workspace.ShowStatus($"测试失败: {ex.Message}", true);
  290. }
  291. }
  292. private void AddLog(string message)
  293. {
  294. _workspace.Logs.Add($"[{DateTime.Now:HH:mm:ss}] {message}\n");
  295. }
  296. public void Dispose()
  297. {
  298. if (_workspace is INotifyPropertyChanged npc)
  299. {
  300. npc.PropertyChanged -= OnWorkspacePropertyChanged;
  301. }
  302. }
  303. }
  304. }