|
@@ -1,9 +1,13 @@
|
|
|
|
|
+using Prism.Events;
|
|
|
|
|
+using Prism.Ioc;
|
|
|
using System;
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
using System.Collections.Generic;
|
|
|
using System.Net.Http;
|
|
using System.Net.Http;
|
|
|
using System.Text;
|
|
using System.Text;
|
|
|
using System.Threading;
|
|
using System.Threading;
|
|
|
using System.Threading.Tasks;
|
|
using System.Threading.Tasks;
|
|
|
|
|
+using TeamAAS_VP.Enums;
|
|
|
|
|
+using TeamAAS_VP.Events;
|
|
|
using TeamAAS_VP.Interfaces;
|
|
using TeamAAS_VP.Interfaces;
|
|
|
using TeamAAS_VP.Models;
|
|
using TeamAAS_VP.Models;
|
|
|
|
|
|
|
@@ -23,6 +27,7 @@ namespace TeamAAS_VP.Services
|
|
|
/// </summary>
|
|
/// </summary>
|
|
|
public class MesService : IMesService
|
|
public class MesService : IMesService
|
|
|
{
|
|
{
|
|
|
|
|
+ IEventAggregator _eventAggregator;
|
|
|
/// <summary>
|
|
/// <summary>
|
|
|
/// 用于执行 HTTP 请求的客户端实例。建议在服务生命周期内重用,避免频繁创建导致套接字/端口耗尽。
|
|
/// 用于执行 HTTP 请求的客户端实例。建议在服务生命周期内重用,避免频繁创建导致套接字/端口耗尽。
|
|
|
/// </summary>
|
|
/// </summary>
|
|
@@ -43,11 +48,12 @@ namespace TeamAAS_VP.Services
|
|
|
/// </summary>
|
|
/// </summary>
|
|
|
/// <param name="configService">用于获取设备配置信息的实现,不能为空。</param>
|
|
/// <param name="configService">用于获取设备配置信息的实现,不能为空。</param>
|
|
|
/// <exception cref="ArgumentNullException">当 <paramref name="configService"/> 为 null 时抛出。</exception>
|
|
/// <exception cref="ArgumentNullException">当 <paramref name="configService"/> 为 null 时抛出。</exception>
|
|
|
- public MesService(IConfigService configService)
|
|
|
|
|
|
|
+ public MesService(IConfigService configService, IContainerProvider container, IEventAggregator ea)
|
|
|
{
|
|
{
|
|
|
_configService = configService ?? throw new ArgumentNullException(nameof(configService));
|
|
_configService = configService ?? throw new ArgumentNullException(nameof(configService));
|
|
|
_httpClient = new HttpClient();
|
|
_httpClient = new HttpClient();
|
|
|
_disposed = false;
|
|
_disposed = false;
|
|
|
|
|
+ _eventAggregator = ea;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@@ -70,7 +76,7 @@ namespace TeamAAS_VP.Services
|
|
|
/// - 捕获 <see cref="OperationCanceledException"/> 返回 "Canceled",捕获其它异常则返回异常消息。
|
|
/// - 捕获 <see cref="OperationCanceledException"/> 返回 "Canceled",捕获其它异常则返回异常消息。
|
|
|
/// - 若实例已被释放则抛出 <see cref="ObjectDisposedException"/>。
|
|
/// - 若实例已被释放则抛出 <see cref="ObjectDisposedException"/>。
|
|
|
/// </remarks>
|
|
/// </remarks>
|
|
|
- public async Task<(bool IsSuccess, string Response)> StationGetAsync(string sn, CancellationToken cancellationToken = default)
|
|
|
|
|
|
|
+ public async Task<(bool IsSuccess, string Response)> StationGetAsync(string sn, string comp, CancellationToken cancellationToken = default)
|
|
|
{
|
|
{
|
|
|
// 如果已经释放,则抛出异常,防止在已释放资源上继续操作。
|
|
// 如果已经释放,则抛出异常,防止在已释放资源上继续操作。
|
|
|
if (_disposed) throw new ObjectDisposedException(nameof(MesService));
|
|
if (_disposed) throw new ObjectDisposedException(nameof(MesService));
|
|
@@ -80,19 +86,40 @@ namespace TeamAAS_VP.Services
|
|
|
{
|
|
{
|
|
|
return (true, "MES功能未启用!");
|
|
return (true, "MES功能未启用!");
|
|
|
}
|
|
}
|
|
|
- if (string.IsNullOrWhiteSpace(device.MesStationUrl))
|
|
|
|
|
|
|
+ if (string.IsNullOrWhiteSpace(device.MesUrl))
|
|
|
return (false, "Missing MES station URL");
|
|
return (false, "Missing MES station URL");
|
|
|
|
|
+ if (device.F == "PTL")
|
|
|
|
|
+ {
|
|
|
|
|
+ if (string.IsNullOrEmpty(device.comp))
|
|
|
|
|
+ {
|
|
|
|
|
+ return (false, "PTL模式下comp不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- var url = string.Format(device.MesStationUrl, sn);
|
|
|
|
|
|
|
+ string url = "";
|
|
|
|
|
+ if (device.F == "PTL")
|
|
|
|
|
+ {
|
|
|
|
|
+ url = $"{device.MesUrl}?c=QUERY_RECORD&line={device.Line}&station={device.Station}&fixtureid={device.FixtureId}&sn={sn}&comp={comp}&p=unit_process_check,model";
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ url = $"{device.MesUrl}?c=QUERY_RECORD&line={device.Line}&station={device.Station}&fixtureid={device.FixtureId}&sn={sn}&p=unit_process_check,model";
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
+ LogHelper.WriteLogMes($"【询问URL】{url}");
|
|
|
|
|
+ SendTaskMessage($"【询问URL】{url}", MessageLevel.Info);
|
|
|
(bool IsSuccess, string Response) result = (false, string.Empty);
|
|
(bool IsSuccess, string Response) result = (false, string.Empty);
|
|
|
- for (int i = 0; i < 5; i++)
|
|
|
|
|
|
|
+
|
|
|
|
|
+ for (int i = 0; i < 3; i++)
|
|
|
{
|
|
{
|
|
|
try
|
|
try
|
|
|
{
|
|
{
|
|
|
- using (var rsp = await _httpClient.GetAsync(url, cancellationToken))
|
|
|
|
|
|
|
+ using (var rsp = await _httpClient.GetAsync(url))
|
|
|
{
|
|
{
|
|
|
var text = await rsp.Content.ReadAsStringAsync();
|
|
var text = await rsp.Content.ReadAsStringAsync();
|
|
|
|
|
+ LogHelper.WriteLogMes($"【HTTP接收】{text}");
|
|
|
|
|
+ SendTaskMessage($"【HTTP接收】{text}", MessageLevel.Info);
|
|
|
|
|
+
|
|
|
// "SFC_OK" 代表和 MES 连接成功
|
|
// "SFC_OK" 代表和 MES 连接成功
|
|
|
if (text.Contains("SFC_OK"))
|
|
if (text.Contains("SFC_OK"))
|
|
|
{
|
|
{
|
|
@@ -122,11 +149,11 @@ namespace TeamAAS_VP.Services
|
|
|
/// <param name="sn">序列号(SN)。</param>
|
|
/// <param name="sn">序列号(SN)。</param>
|
|
|
/// <param name="timeoutInSeconds">请求超时,单位为秒。</param>
|
|
/// <param name="timeoutInSeconds">请求超时,单位为秒。</param>
|
|
|
/// <returns>与 <see cref="StationGetAsync(string, CancellationToken)"/> 相同的返回语义。</returns>
|
|
/// <returns>与 <see cref="StationGetAsync(string, CancellationToken)"/> 相同的返回语义。</returns>
|
|
|
- public async Task<(bool IsSuccess, string Response)> StationGetAsync(string sn, double timeoutInSeconds)
|
|
|
|
|
|
|
+ public async Task<(bool IsSuccess, string Response)> StationGetAsync(string sn, string comp, double timeoutInSeconds)
|
|
|
{
|
|
{
|
|
|
using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(timeoutInSeconds)))
|
|
using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(timeoutInSeconds)))
|
|
|
{
|
|
{
|
|
|
- return await StationGetAsync(sn, cts.Token);
|
|
|
|
|
|
|
+ return await StationGetAsync(sn, comp, cts.Token);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -152,7 +179,7 @@ namespace TeamAAS_VP.Services
|
|
|
/// - 捕获 <see cref="OperationCanceledException"/> 返回 "Canceled",捕获其它异常则返回异常消息。
|
|
/// - 捕获 <see cref="OperationCanceledException"/> 返回 "Canceled",捕获其它异常则返回异常消息。
|
|
|
/// - 若实例已被释放则抛出 <see cref="ObjectDisposedException"/>。
|
|
/// - 若实例已被释放则抛出 <see cref="ObjectDisposedException"/>。
|
|
|
/// </remarks>
|
|
/// </remarks>
|
|
|
- public async Task<(bool IsSuccess, string Response)> SubmitGetAsync(string sn, bool isSuccess, DateTime start_time, DateTime stop_time, CancellationToken cancellationToken = default)
|
|
|
|
|
|
|
+ public async Task<(bool IsSuccess, string Response)> SubmitGetAsync(string sn, string comp, bool isSuccess, DateTime start_time, DateTime stop_time, CancellationToken cancellationToken = default)
|
|
|
{
|
|
{
|
|
|
// 如果已经释放,则抛出异常,防止在已释放资源上继续操作。
|
|
// 如果已经释放,则抛出异常,防止在已释放资源上继续操作。
|
|
|
if (_disposed) throw new ObjectDisposedException(nameof(MesService));
|
|
if (_disposed) throw new ObjectDisposedException(nameof(MesService));
|
|
@@ -162,19 +189,41 @@ namespace TeamAAS_VP.Services
|
|
|
{
|
|
{
|
|
|
return (true, "MES功能未启用!");
|
|
return (true, "MES功能未启用!");
|
|
|
}
|
|
}
|
|
|
- if (string.IsNullOrWhiteSpace(device.MesStationUrl))
|
|
|
|
|
|
|
+ if (string.IsNullOrWhiteSpace(device.MesUrl))
|
|
|
return (false, "Missing MES submit URL");
|
|
return (false, "Missing MES submit URL");
|
|
|
|
|
+ if (device.F == "PTL")
|
|
|
|
|
+ {
|
|
|
|
|
+ if (string.IsNullOrEmpty(device.comp))
|
|
|
|
|
+ {
|
|
|
|
|
+ return (false, "PTL模式下comp不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- var url = string.Format(device.MesStationUrl, sn, isSuccess ? "PASS" : "FAIL", start_time.ToString("yyyy-MM-dd HH:mm:ss"), stop_time.ToString("yyyy-MM-dd HH:mm:ss"));
|
|
|
|
|
-
|
|
|
|
|
|
|
+ string res = isSuccess ? "PASS" : "FAIL";
|
|
|
|
|
+ string url = "";
|
|
|
|
|
+ if (device.F == "PTL")
|
|
|
|
|
+ {
|
|
|
|
|
+ url = $"{device.MesUrl}?c=ADD_RECORD&line={device.Line}&station={device.Station}&fixtureid={device.FixtureId}&sn={sn}&f=PTL&comp={comp}" +
|
|
|
|
|
+ $"&result={res}&start_time={start_time.ToString("yyyy-MM-dd HH:mm:ss")}&stop_time={stop_time.ToString("yyyy-MM-dd HH:mm:ss")}";
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ url = $"{device.MesUrl}?c=ADD_RECORD&line={device.Line}&station={device.Station}&fixtureid={device.FixtureId}&sn={sn}&f=PQC" +
|
|
|
|
|
+ $"&result={res}&start_time={start_time.ToString("yyyy-MM-dd HH:mm:ss")}&stop_time={stop_time.ToString("yyyy-MM-dd HH:mm:ss")}";
|
|
|
|
|
+ }
|
|
|
|
|
+ LogHelper.WriteLogMes($"【上传URL】{url}");
|
|
|
|
|
+ SendTaskMessage($"【上传URL】{url}", MessageLevel.Info);
|
|
|
(bool IsSuccess, string Response) result = (false, string.Empty);
|
|
(bool IsSuccess, string Response) result = (false, string.Empty);
|
|
|
- for (int i = 0; i < 5; i++)
|
|
|
|
|
|
|
+
|
|
|
|
|
+ for (int i = 0; i < 3; i++)
|
|
|
{
|
|
{
|
|
|
try
|
|
try
|
|
|
{
|
|
{
|
|
|
using (var rsp = await _httpClient.GetAsync(url, cancellationToken))
|
|
using (var rsp = await _httpClient.GetAsync(url, cancellationToken))
|
|
|
{
|
|
{
|
|
|
var text = await rsp.Content.ReadAsStringAsync();
|
|
var text = await rsp.Content.ReadAsStringAsync();
|
|
|
|
|
+ LogHelper.WriteLogMes($"【HTTP接收】{text}");
|
|
|
|
|
+ SendTaskMessage($"【HTTP接收】{text}", MessageLevel.Info);
|
|
|
// "SFC_OK" 代表和 MES 连接成功
|
|
// "SFC_OK" 代表和 MES 连接成功
|
|
|
if (text.Contains("SFC_OK"))
|
|
if (text.Contains("SFC_OK"))
|
|
|
{
|
|
{
|
|
@@ -209,11 +258,11 @@ namespace TeamAAS_VP.Services
|
|
|
/// <param name="stop_time">结束时间。</param>
|
|
/// <param name="stop_time">结束时间。</param>
|
|
|
/// <param name="timeoutInSeconds">请求超时,单位为秒。</param>
|
|
/// <param name="timeoutInSeconds">请求超时,单位为秒。</param>
|
|
|
/// <returns>与 <see cref="SubmitGetAsync(string, bool, DateTime, DateTime, CancellationToken)"/> 相同的返回语义。</returns>
|
|
/// <returns>与 <see cref="SubmitGetAsync(string, bool, DateTime, DateTime, CancellationToken)"/> 相同的返回语义。</returns>
|
|
|
- public async Task<(bool IsSuccess, string Response)> SubmitGetAsync(string sn, bool isSuccess, DateTime start_time, DateTime stop_time, double timeoutInSeconds)
|
|
|
|
|
|
|
+ public async Task<(bool IsSuccess, string Response)> SubmitGetAsync(string sn, string comp, bool isSuccess, DateTime start_time, DateTime stop_time, double timeoutInSeconds)
|
|
|
{
|
|
{
|
|
|
using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(timeoutInSeconds)))
|
|
using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(timeoutInSeconds)))
|
|
|
{
|
|
{
|
|
|
- return await SubmitGetAsync(sn, isSuccess, start_time, stop_time, cts.Token);
|
|
|
|
|
|
|
+ return await SubmitGetAsync(sn, comp, isSuccess, start_time, stop_time, cts.Token);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -229,5 +278,12 @@ namespace TeamAAS_VP.Services
|
|
|
_disposed = true;
|
|
_disposed = true;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+ public void SendTaskMessage(string msg, MessageLevel level)
|
|
|
|
|
+ {
|
|
|
|
|
+ App.Current.Dispatcher.Invoke(() =>
|
|
|
|
|
+ {
|
|
|
|
|
+ _eventAggregator.GetEvent<TaskMessageNotification>().Publish(new Models.MessageStruct() { Message = msg, level = level });
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|