using Newtonsoft.Json;
|
using System;
|
using System.Net.Http;
|
using System.Text;
|
using WIDESEA_Common.Tools;
|
using WIDESEA_Core.Configuration;
|
using WIDESEA_Core.Extensions;
|
using WIDESEA_Entity.DomainModels;
|
|
namespace WIDESEA_WCS.SchedulerExecute
|
{
|
public class SendMESTask
|
{
|
public static async void SendMesTask(dt_agvtask agvtask, int type)
|
{
|
try
|
{
|
string lot = "";
|
if (!string.IsNullOrEmpty(agvtask.agv_materbarcode))
|
{
|
string[] lots = agvtask.agv_materbarcode.Split("*");
|
lot = lots[0];
|
}
|
|
dynamic body = new
|
{
|
CmdID = 10101,
|
TaskNo = agvtask.agv_tasknum,
|
Type = type,
|
EID = "",
|
UploadTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
|
Data = new
|
{
|
DevidGet = agvtask.agv_fromaddress,
|
DevidPut = agvtask.agv_toaddress,
|
LotNo = lot,
|
MaterialType= "1",
|
StartTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
|
EndTime = type > 0 ? DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") : ""
|
}
|
};
|
HttpClient client = new HttpClient();
|
var url = AppSetting.Configuration["MESURL"];
|
var content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json");
|
var response = await client.PostAsync(url, content).Result.Content.ReadAsStringAsync();
|
WriteLog.Info("MES接口数据").Write($"调用接口成功,发送数据:{JsonConvert.SerializeObject(body)}返回数据:{response} 时间:{DateTime.Now}", "MES接口数据");
|
//if (response.IsNullOrEmpty())
|
//{
|
// for (int i = 0; i < 2; i++)
|
// {
|
// response = await client.PostAsync(url, content).Result.Content.ReadAsStringAsync();
|
// if (!response.IsNullOrEmpty())
|
// {
|
// var resp = JsonConvert.DeserializeObject<Response>(response);
|
// if ( resp.Data.Status != 1)//resp.Code != 200 ||
|
// {
|
// for (int j = 0; j < 2; j++)
|
// {
|
// response = await client.PostAsync(url, content).Result.Content.ReadAsStringAsync();
|
// resp = JsonConvert.DeserializeObject<Response>(response);
|
|
// if (resp.Code == 200 || resp.Data.Status == 1) break;
|
// }
|
// }
|
// }
|
// }
|
//}
|
//else
|
//{
|
// var resp = JsonConvert.DeserializeObject<Response>(response);
|
// if (resp.Data.Status != 1)//resp.Code != 200 ||
|
// {
|
// for (int i = 0; i < 2; i++)
|
// {
|
// response = await client.PostAsync(url, content).Result.Content.ReadAsStringAsync();
|
// resp = JsonConvert.DeserializeObject<Response>(response);
|
|
// if (resp.Code == 200 && resp.Data.Status == 1) break;
|
// }
|
// }
|
//}
|
|
var result = JsonConvert.DeserializeObject<Data>(response);
|
WriteLog.Info("MES接口数据").Write($"调用接口1:{DateTime.Now}", "MES接口数据");
|
if (result.Status != 1)
|
{
|
WriteLog.Info("MES接口数据").Write($"调用接口失败:重新发送 时间:{DateTime.Now}", "MES接口数据");
|
for (int i = 0; i < 2; i++)
|
{
|
response = await client.PostAsync(url, content).Result.Content.ReadAsStringAsync();
|
result = JsonConvert.DeserializeObject<Data>(response);
|
|
if (result.Status == 1) break;
|
}
|
}
|
}
|
catch (Exception ex)
|
{
|
WriteLog.Info("MES接口数据").Write($"调用接口失败,返回数据:{ex.Message} 时间:{DateTime.Now}", "MES接口数据");
|
}
|
|
}
|
}
|
|
public class Data
|
{
|
public int Status { get; set; }
|
public string Msg { get; set; }
|
}
|
|
public class Response
|
{
|
public int Code { get; set; }
|
public string Msg { get; set; }
|
public Data Data { get; set; }
|
}
|
}
|