|
|
|
@ -1,8 +1,12 @@
|
|
|
|
|
using Admin.Core.Api.PLTBusiness.Entity;
|
|
|
|
|
using Admin.Core.Common;
|
|
|
|
|
using Admin.Core.IService;
|
|
|
|
|
using Admin.Core.PlcServer;
|
|
|
|
|
using Admin.Core.Service;
|
|
|
|
|
using log4net;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
using NPOI.HSSF.Record;
|
|
|
|
|
using RabbitMQ.Client;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
@ -19,22 +23,17 @@ namespace Admin.Core.Api.PLTBusiness
|
|
|
|
|
|
|
|
|
|
public class HttpService : BackgroundService
|
|
|
|
|
{
|
|
|
|
|
private readonly IServiceScopeFactory _scopeFactory;
|
|
|
|
|
private readonly log4net.ILog log = LogManager.GetLogger(typeof(HttpService));
|
|
|
|
|
|
|
|
|
|
private string _token;
|
|
|
|
|
private readonly IServiceScopeFactory _scopeFactory;
|
|
|
|
|
|
|
|
|
|
private int _tokenExpirationTime;
|
|
|
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
|
|
|
|
|
|
|
|
|
private Dictionary<string, string> _tokenDictionary = new Dictionary<string, string>()
|
|
|
|
|
{
|
|
|
|
|
{"authCode", "QT_XXX"},
|
|
|
|
|
{"appid", "q7cloud_XXX"},
|
|
|
|
|
{"secret", "f434eab8b9652524dc719e33f4ff255a45570XXX"}
|
|
|
|
|
};
|
|
|
|
|
private readonly string _host;
|
|
|
|
|
|
|
|
|
|
private string _host;
|
|
|
|
|
private string _token;
|
|
|
|
|
|
|
|
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
|
|
|
|
private int _tokenExpirationTime = 0;
|
|
|
|
|
|
|
|
|
|
public HttpService(IHttpClientFactory httpClientFactory, IServiceScopeFactory scopeFactory)
|
|
|
|
|
{
|
|
|
|
@ -44,25 +43,128 @@ namespace Admin.Core.Api.PLTBusiness
|
|
|
|
|
|
|
|
|
|
protected async override Task ExecuteAsync(CancellationToken stoppingToken)
|
|
|
|
|
{
|
|
|
|
|
int second = 1000 * 10;
|
|
|
|
|
using var scope = _scopeFactory.CreateScope();
|
|
|
|
|
var services = scope.ServiceProvider;
|
|
|
|
|
var xlBusiness = services.GetService<XlBusiness>();
|
|
|
|
|
|
|
|
|
|
while (!stoppingToken.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
//执行任务
|
|
|
|
|
Console.WriteLine($"{DateTime.Now}");
|
|
|
|
|
//log.Info($"{DateTime.Now}");
|
|
|
|
|
|
|
|
|
|
if (_tokenExpirationTime == 0)
|
|
|
|
|
{
|
|
|
|
|
if (await GetToken())
|
|
|
|
|
{
|
|
|
|
|
log.Info("Token Get! Value:" + _token);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
log.Error("Token Get Falied");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else if (_tokenExpirationTime <= 86400)
|
|
|
|
|
{
|
|
|
|
|
RefreshToken();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_tokenExpirationTime = _tokenExpirationTime - (second / 1000);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await xlBusiness.InsertNewMaterialData(null);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (_token.IsNotEmptyOrNull())
|
|
|
|
|
{
|
|
|
|
|
await xlBusiness.InsertNewMaterialData(await GetMaterialDataPageList());
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
log.Error("No Token!!!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
log.Error("Error: " + ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await Task.Delay(second);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await Task.Delay(1000*10);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region Mes信息拉取
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 分页拉取所有Mes物料信息
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
private async Task<List<MaterialRecordListItem>> GetMaterialDataPageList()
|
|
|
|
|
{
|
|
|
|
|
int pageSize = 200;
|
|
|
|
|
int pageIndex = 0;
|
|
|
|
|
int pageCount = 1;
|
|
|
|
|
string orgCode = "Mesnac";
|
|
|
|
|
List<MaterialRecordListItem> allMaterials = new List<MaterialRecordListItem>();
|
|
|
|
|
Dictionary<string, object> _pageInfo = new Dictionary<string, object>()
|
|
|
|
|
{
|
|
|
|
|
{"pageIndex", 200},
|
|
|
|
|
{"pageSize", 0},
|
|
|
|
|
{"orgCode", "Mesnac"},
|
|
|
|
|
};
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
string body = JsonSerializer.Serialize(_pageInfo);
|
|
|
|
|
string materialJson = await PostAsync(_host + "/qtn/api/q7/com/inventory/page",
|
|
|
|
|
body, "application/json", "application/json",
|
|
|
|
|
new Dictionary<string, string> { { "token", _token } }, 10);
|
|
|
|
|
ResponseRoot<MaterialData> responseRoot = JsonSerializer.Deserialize<ResponseRoot<MaterialData>>(materialJson);
|
|
|
|
|
if (responseRoot == null)
|
|
|
|
|
{
|
|
|
|
|
log.Error("物料信息拉取失败!");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
if (responseRoot.code != 200)
|
|
|
|
|
{
|
|
|
|
|
log.Error("ERROR 状态码:" + responseRoot.code);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
pageCount = responseRoot.Data.pageCount;
|
|
|
|
|
pageIndex = responseRoot.Data.pageIndex;
|
|
|
|
|
allMaterials.AddRange(responseRoot.Data.recordList);
|
|
|
|
|
pageIndex = pageIndex + 1;
|
|
|
|
|
}
|
|
|
|
|
while (pageIndex < pageSize);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
log.Error("Mes物料信息拉取失败 Error: " + ex);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return allMaterials;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async void GetToken()
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Token获取
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取Token
|
|
|
|
|
/// </summary>
|
|
|
|
|
private async Task<bool> GetToken()
|
|
|
|
|
{
|
|
|
|
|
Dictionary<string, string> _tokenDictionary = new Dictionary<string, string>()
|
|
|
|
|
{
|
|
|
|
|
{"authCode", "QT_XXX"},
|
|
|
|
|
{"appid", "q7cloud_XXX"},
|
|
|
|
|
{"secret", "f434eab8b9652524dc719e33f4ff255a45570XXX"}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
string body = JsonSerializer.Serialize(_tokenDictionary);
|
|
|
|
@ -70,22 +172,63 @@ namespace Admin.Core.Api.PLTBusiness
|
|
|
|
|
ResponseRoot<TokenData> responseRoot = JsonSerializer.Deserialize<ResponseRoot<TokenData>>(tokenJson);
|
|
|
|
|
if (responseRoot == null)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Token获取失败!");
|
|
|
|
|
return;
|
|
|
|
|
log.Error("Token获取失败!");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (responseRoot.code != 200)
|
|
|
|
|
{
|
|
|
|
|
log.Error("ERROR 状态码:" + responseRoot.code);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
_token = responseRoot.Data.token;
|
|
|
|
|
_tokenExpirationTime = responseRoot.Data.expiration;
|
|
|
|
|
log.Info("Token获取成功:" + _token + "超时时间:" + _tokenExpirationTime);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
log.Error("Token获取失败 ERROR: " + ex.ToString());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 刷新Token
|
|
|
|
|
/// </summary>
|
|
|
|
|
private async Task<bool> RefreshToken()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
string tokenJson = await PostAsync(_host + "/qtn/api/auth/token/refresh", ""
|
|
|
|
|
, "application/json", "application/x-www-form-urlencoded"
|
|
|
|
|
, new Dictionary<string, string>() { { "token", _token } }, 10);
|
|
|
|
|
ResponseRoot<TokenData> responseRoot = JsonSerializer.Deserialize<ResponseRoot<TokenData>>(tokenJson);
|
|
|
|
|
if (responseRoot == null)
|
|
|
|
|
{
|
|
|
|
|
log.Error("Token更新失败!");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if(responseRoot.code != 200)
|
|
|
|
|
if (responseRoot.code != 200)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("ERROR 状态码:" + responseRoot.code);
|
|
|
|
|
return;
|
|
|
|
|
log.Error("ERROR 状态码:" + responseRoot.code);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
_token = responseRoot.Data.token;
|
|
|
|
|
_tokenExpirationTime = responseRoot.Data.expiration;
|
|
|
|
|
log.Info("Token更新成功:" + _token + "超时时间:" + _tokenExpirationTime);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("ERROR: " + ex.ToString());
|
|
|
|
|
log.Error("Token更新失败 ERROR: " + ex.ToString());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region HTTP操作
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 发起POST异步请求
|
|
|
|
|
/// </summary>
|
|
|
|
@ -136,8 +279,8 @@ namespace Admin.Core.Api.PLTBusiness
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"ERROR: {ex.ToString()} Time: {DateTime.Now}");
|
|
|
|
|
return string.Empty;
|
|
|
|
|
log.Error($"Post请求失败 URL:{url} ERROR: {ex.ToString()} Time: {DateTime.Now}");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
@ -182,8 +325,8 @@ namespace Admin.Core.Api.PLTBusiness
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"ERROR: {ex.ToString()} Time: {DateTime.Now}");
|
|
|
|
|
return string.Empty;
|
|
|
|
|
log.Error($"Get请求失败 URL:{url} ERROR: {ex.ToString()} Time: {DateTime.Now}");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -217,14 +360,15 @@ namespace Admin.Core.Api.PLTBusiness
|
|
|
|
|
{
|
|
|
|
|
// 读取响应内容
|
|
|
|
|
var responseBody = await response.Content.ReadAsStringAsync();
|
|
|
|
|
Console.WriteLine($"Response: {responseBody}");
|
|
|
|
|
log.Info($"Response: {responseBody}");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"Error: {response.StatusCode}");
|
|
|
|
|
log.Error($"Error: {response.StatusCode}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region 私有函数
|
|
|
|
|
|
|
|
|
|