You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
151 lines
4.8 KiB
C#
151 lines
4.8 KiB
C#
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.Extensions.Logging;
|
|
using Nancy;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
using Serilog;
|
|
using Serilog.Events;
|
|
using SlnMesnac.Config;
|
|
using SlnMesnac.Model.AirportApiEntity;
|
|
using SlnMesnac.Model.domain;
|
|
using SlnMesnac.Serilog;
|
|
using SQLitePCL;
|
|
using SqlSugar;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using TouchSocket.Core;
|
|
using TouchSocket.Rpc;
|
|
using TouchSocket.Sockets;
|
|
using TouchSocket.WebApi;
|
|
using JsonSerializer = System.Text.Json.JsonSerializer;
|
|
|
|
namespace SlnMesnac.TouchSocket
|
|
{
|
|
public class MeshttpClient
|
|
{
|
|
private readonly AppConfig _appConfig;
|
|
private readonly SerilogHelper _logger;
|
|
public static Action<string, string> RefreshStateEvent;
|
|
public static Action<string> _RefreshLogMessageAction;
|
|
public MeshttpClient(AppConfig appConfig, SerilogHelper logger)
|
|
{
|
|
_appConfig = appConfig;
|
|
_logger = logger;
|
|
}
|
|
|
|
public static WebApiClient MESHttpClient;
|
|
|
|
public async Task<WebApiClient> CreateWebApiClientAsync(string IpHost)
|
|
{
|
|
try
|
|
{
|
|
MESHttpClient = new WebApiClient();
|
|
await MESHttpClient.SetupAsync(new TouchSocketConfig()
|
|
.SetRemoteIPHost(IpHost)
|
|
.ConfigurePlugins(a =>
|
|
{
|
|
}));
|
|
_logger.Info("正在连接:" + IpHost);
|
|
await MESHttpClient.ConnectAsync();
|
|
Console.WriteLine("连接成功");
|
|
_logger.Info(IpHost + "连接成功");
|
|
return MESHttpClient;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.Info("MES API连接ERROR: " + ex.Message);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 获取到的JToken类型转换为实体类
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="value"></param>
|
|
/// <returns></returns>
|
|
public T JTokenToEntity<T>(JToken value) where T : class
|
|
{
|
|
if (value == null)
|
|
{
|
|
return null;
|
|
}
|
|
string json = value.ToString();
|
|
if (string.IsNullOrEmpty(json))
|
|
{
|
|
return null;
|
|
}
|
|
T ResponseEntity;
|
|
ResponseEntity = JsonSerializer.Deserialize<T>(json);
|
|
return ResponseEntity;
|
|
}
|
|
|
|
|
|
public async Task<ApiResponse<MesOrderInfo>> GetMesOrderInfo(string lineCode, string datetime)
|
|
{
|
|
try
|
|
{
|
|
var invokeOption_30s = new InvokeOption(30 * 1000)
|
|
{
|
|
FeedbackType = FeedbackType.WaitInvoke
|
|
};
|
|
if (MESHttpClient == null)
|
|
{
|
|
return null;
|
|
}
|
|
var request = new WebApiRequest();
|
|
request.Method = HttpMethodType.Get;
|
|
request.Querys = new KeyValuePair<string, string>[] { new KeyValuePair<string, string>("lineCode", lineCode), new KeyValuePair<string, string>("planDate", datetime) };
|
|
|
|
var responseValue = await MESHttpClient.InvokeTAsync<int>("/api/plan/get", invokeOption_30s, request);
|
|
|
|
//JToken responseValue = await MESHttpClient.InvokeTAsync<JToken>("Get:/api/plan", null, requestValue);
|
|
|
|
return JTokenToEntity<ApiResponse<MesOrderInfo>>(responseValue);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.Info("ERROR: " + ex.Message);
|
|
return new ApiResponse<MesOrderInfo>();
|
|
}
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 主动上报
|
|
/// </summary>
|
|
/// <param name="requestValue"></param>
|
|
/// <returns></returns>
|
|
public async Task<ApiResponse<MesParaData>> autoread(MesParaData requestValue)
|
|
{
|
|
try
|
|
{
|
|
if (MESHttpClient == null)
|
|
{
|
|
return null;
|
|
}
|
|
JToken responseValue = await MESHttpClient.InvokeTAsync<JToken>("POST:/autoread", null, requestValue);
|
|
|
|
_RefreshLogMessageAction?.Invoke("发送MES自动读取指令"+JsonSerializer.Serialize(requestValue));
|
|
_logger.Info("发送MES自动读取指令"+ JsonSerializer.Serialize(requestValue));
|
|
|
|
RefreshStateEvent?.Invoke(requestValue.EQUID,requestValue.EPCID);
|
|
return JTokenToEntity<ApiResponse<MesParaData>>(responseValue);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.Info("autoread ERROR: " + ex.Message);
|
|
return new ApiResponse<MesParaData>();
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|