|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Net.Http;
|
|
|
|
using System.Text;
|
|
|
|
using System.Text.Json;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Microsoft.CodeAnalysis;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
using Volo.Abp.Application.Services;
|
|
|
|
using Win_in.Sfs.Basedata.Application.Contracts;
|
|
|
|
using Win_in.Sfs.Shared.Domain;
|
|
|
|
using Win_in.Sfs.Shared.Domain.Shared;
|
|
|
|
using Win_in.Sfs.Wms.DataExchange.Domain;
|
|
|
|
using Win_in.Sfs.Wms.DataExchange.Domain.Shared;
|
|
|
|
using Win_in.Sfs.Wms.DataExchange.WMS.PCK;
|
|
|
|
using Win_in.Sfs.Wms.Store.Application.Contracts;
|
|
|
|
using System.Text.Json.Serialization;
|
|
|
|
using System.IdentityModel.Tokens.Jwt;
|
|
|
|
using Volo.Abp;
|
|
|
|
using System.Net.NetworkInformation;
|
|
|
|
using Win_in.Sfs.Shared.Domain.Shared.Enums.Store;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
|
|
namespace Win_in.Sfs.Wms.DataExchange.Fawtyg.InjectionMoldingTaskAgent.Incoming;
|
|
|
|
public class InjectionMoldingRequestReader : IReader
|
|
|
|
{
|
|
|
|
|
|
|
|
private readonly IInjectionIssueRequestAppService _injectionRequest;
|
|
|
|
private readonly IItemBasicAppService _itemService;
|
|
|
|
private readonly ILocationAppService _locService;
|
|
|
|
private readonly ILogger<InjectionMoldingRequestReader> _logger;
|
|
|
|
private readonly IOptions<InjectionMoldingTaskOptions> _options;
|
|
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
|
|
|
|
|
|
|
public InjectionMoldingRequestReader(
|
|
|
|
IInjectionIssueRequestAppService injectionRequest
|
|
|
|
|
|
|
|
, ILogger<InjectionMoldingRequestReader> logger
|
|
|
|
, IOptions<InjectionMoldingTaskOptions> options
|
|
|
|
, IHttpClientFactory httpClientFactory
|
|
|
|
, IItemBasicAppService itemService
|
|
|
|
, ILocationAppService locService
|
|
|
|
|
|
|
|
)
|
|
|
|
{
|
|
|
|
_injectionRequest = injectionRequest;
|
|
|
|
|
|
|
|
_logger = logger;
|
|
|
|
_options = options;
|
|
|
|
_httpClientFactory = httpClientFactory;
|
|
|
|
_itemService=itemService;
|
|
|
|
_locService = locService;
|
|
|
|
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
|
|
/// 读取注塑叫料任务
|
|
|
|
/// </summary>
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
|
|
public virtual async Task<List<IncomingFromExternal>> ReadAsync()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var sleepTime = (_options.Value.AutoRemote.TimeCycle*60/ _options.Value.AutoRemote.Interval)-10;
|
|
|
|
var invterval = _options.Value.AutoRemote.Interval;
|
|
|
|
|
|
|
|
var successTime = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i <invterval; i++)
|
|
|
|
{
|
|
|
|
Stopwatch sw = Stopwatch.StartNew();
|
|
|
|
|
|
|
|
Client client = new Client(_options.Value.AutoRemote.IpAddress, _httpClientFactory.CreateClient());//调用客户记录
|
|
|
|
|
|
|
|
var flag = await client.GetMacStaticAsync().ConfigureAwait(false);
|
|
|
|
if (flag != 1)
|
|
|
|
{
|
|
|
|
// 记录错误日志并返回空列表
|
|
|
|
_logger.LogInformation($"读取到摄像头信息为不可用{DateTime.Now},请检查摄像头");
|
|
|
|
return new List<IncomingFromExternal>();
|
|
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(_options.Value.AutoRemote.AreaIDs))
|
|
|
|
{
|
|
|
|
_logger.LogInformation($"没有设置指定区域AutoRemote->AreaIDs");
|
|
|
|
return new List<IncomingFromExternal>();
|
|
|
|
}
|
|
|
|
var ids = _options.Value.AutoRemote.AreaIDs.Split(",");
|
|
|
|
List<ResponCargoItem> camralist = new List<ResponCargoItem>();
|
|
|
|
foreach (var itm in ids)//遍历区域查找所有货物
|
|
|
|
{
|
|
|
|
var idsList = await client.GetCargoStaticAsync(itm).ConfigureAwait(false);
|
|
|
|
camralist.AddRange(idsList);
|
|
|
|
}
|
|
|
|
successTime++;
|
|
|
|
if (successTime == invterval)
|
|
|
|
{
|
|
|
|
|
|
|
|
List<UnCompletedRequestDto> inputdetail = new List<UnCompletedRequestDto>();
|
|
|
|
foreach (var item in camralist)
|
|
|
|
{
|
|
|
|
UnCompletedRequestDto jobrequestinput = new UnCompletedRequestDto();
|
|
|
|
jobrequestinput.ItemCode = item.PartCode;
|
|
|
|
jobrequestinput.LocCode = item.AreaID.ToString();
|
|
|
|
jobrequestinput.PositionCode = item.CargoID.ToString();
|
|
|
|
inputdetail.Add(jobrequestinput);
|
|
|
|
}
|
|
|
|
|
|
|
|
var uncompletelist = await _injectionRequest.GetUnCompletedRequestAsync(inputdetail).ConfigureAwait(false);//将所有库位零件信息查找未完成记录
|
|
|
|
|
|
|
|
var query = from itm in camralist
|
|
|
|
join itm1 in uncompletelist on new { locCode = itm.AreaID.ToString(), itmCode = itm.PartCode, PositionCode = itm.CargoID.ToString() }
|
|
|
|
equals new { locCode = itm1.LocCode, itmCode = itm1.ItemCode, PositionCode = itm1.PositionCode } into temp
|
|
|
|
from tm in temp.DefaultIfEmpty()
|
|
|
|
where tm == null
|
|
|
|
select itm;
|
|
|
|
var injectionList = query.ToList();//已经完成的记录
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var inject in injectionList)
|
|
|
|
{
|
|
|
|
List<InjectionRequest> cameraList = new List<InjectionRequest>();
|
|
|
|
|
|
|
|
InjectionIssueRequestEditInput input = new InjectionIssueRequestEditInput();
|
|
|
|
input.Worker = "Vision";
|
|
|
|
input.IssueRequestType = EnumIssueRequestType.Vision;
|
|
|
|
input.AutoSubmit = true;
|
|
|
|
input.ActiveDate = DateTime.Now;
|
|
|
|
input.UseOnTheWayLocation = false;
|
|
|
|
input.Remark = "视觉叫料";
|
|
|
|
List<InjectionIssueRequestDetailInput> injectionRequestDetails = new List<InjectionIssueRequestDetailInput>();
|
|
|
|
//foreach (var job in cameraList)
|
|
|
|
//{
|
|
|
|
var detailInput = new InjectionIssueRequestDetailInput()
|
|
|
|
{
|
|
|
|
ItemCode = inject.PartCode,
|
|
|
|
ToLocationCode = inject.AreaID.ToString(),
|
|
|
|
PositionCode = inject.CargoID,
|
|
|
|
Qty = 1,
|
|
|
|
RecommendType = EnumRecommendType.RAW,
|
|
|
|
IssuedQty = 0,
|
|
|
|
ReceivedQty = 0,
|
|
|
|
Status = EnumStatus.Open,
|
|
|
|
RequestStatus = EnumRequestStatus.New,
|
|
|
|
BoxQty = 1
|
|
|
|
};
|
|
|
|
// 添加注塑叫料明细任务数据
|
|
|
|
injectionRequestDetails.Add(detailInput);
|
|
|
|
//}
|
|
|
|
input.Details.AddRange(injectionRequestDetails);
|
|
|
|
// 通过 BindAsync 方法对零件仓库进行赋值
|
|
|
|
var errors = await BindAsync(input.Details).ConfigureAwait(false);
|
|
|
|
if (errors.Count > 0)
|
|
|
|
{
|
|
|
|
// 记录错误日志并返回空列表
|
|
|
|
foreach (var error in errors)
|
|
|
|
{
|
|
|
|
_logger.LogInformation(error);
|
|
|
|
}
|
|
|
|
return new List<IncomingFromExternal>();
|
|
|
|
}
|
|
|
|
// 创建新的注塑请求并将数据写入数据库
|
|
|
|
await _injectionRequest.CreateAsync(input).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Thread.Sleep(sleepTime * 1000);
|
|
|
|
}
|
|
|
|
sw.Stop();
|
|
|
|
_logger.LogInformation($"读取成功次数{successTime},耗时 ${sw.ElapsedMilliseconds / 1000 / 60}分钟");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Client client = new Client(_options.Value.AutoRemote.IpAddress, _httpClientFactory.CreateClient());//调用客户记录
|
|
|
|
|
|
|
|
//var flag= await client.GetMacStaticAsync().ConfigureAwait(false);
|
|
|
|
//if (flag!=1)
|
|
|
|
//{
|
|
|
|
// // 记录错误日志并返回空列表
|
|
|
|
// _logger.LogInformation($"读取到摄像头信息为不可用{DateTime.Now},请检查摄像头");
|
|
|
|
// return new List<IncomingFromExternal>();
|
|
|
|
//}
|
|
|
|
|
|
|
|
//if (string.IsNullOrEmpty(_options.Value.AutoRemote.AreaIDs))
|
|
|
|
//{
|
|
|
|
// _logger.LogInformation($"没有设置指定区域AutoRemote->AreaIDs");
|
|
|
|
// return new List<IncomingFromExternal>();
|
|
|
|
//}
|
|
|
|
|
|
|
|
//var ids=_options.Value.AutoRemote.AreaIDs.Split(",");
|
|
|
|
//List<ResponCargoItem> camralist = new List<ResponCargoItem>();
|
|
|
|
//foreach (var itm in ids)//遍历区域查找所有货物
|
|
|
|
//{
|
|
|
|
// var idsList= await client.GetCargoStaticAsync(itm).ConfigureAwait(false);
|
|
|
|
// camralist.AddRange(idsList);
|
|
|
|
//}
|
|
|
|
//camralist = camralist.Where(p=>p.Flag==0).ToList();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
// 捕获特定异常并记录日志
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
_logger.LogInformation(ex.Message);
|
|
|
|
}
|
|
|
|
// 返回空列表
|
|
|
|
return new List<IncomingFromExternal>();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 绑定零件库位信息,如果对错误返回错误新列表
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="p_list"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
private async Task<List<string>> BindAsync(List<InjectionIssueRequestDetailInput> p_list)
|
|
|
|
{
|
|
|
|
// 异步方法,将输入的请求绑定到对应的零件和库位信息,返回错误列表
|
|
|
|
List<string> errors = new List<string>();
|
|
|
|
foreach (var request in p_list)
|
|
|
|
{
|
|
|
|
// 获取对应零件信息
|
|
|
|
var itm = await _itemService.GetByCodeAsync(request.ItemCode).ConfigureAwait(false);
|
|
|
|
if (itm == null) { errors.Add($"编号:{request.ItemCode}零件表中没找到!"); }
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// 更新请求中的零件描述和名称
|
|
|
|
request.ItemDesc1 = itm.Desc1;
|
|
|
|
request.ItemDesc2 = itm.Desc2;
|
|
|
|
request.ItemName = itm.Name;
|
|
|
|
request.Uom = itm.BasicUom;
|
|
|
|
}
|
|
|
|
// 获取对应库位信息
|
|
|
|
var loc = await _locService.GetByCodeAsync(request.ToLocationCode).ConfigureAwait(false);
|
|
|
|
if (loc == null) { errors.Add($"编号:{request.ToLocationCode}库位表中没找到!"); }
|
|
|
|
else
|
|
|
|
{
|
|
|
|
|
|
|
|
// 更新请求中的库位相关信息
|
|
|
|
request.ToLocationCode = loc.Code;
|
|
|
|
request.ToLocationGroup = loc.LocationGroupCode;
|
|
|
|
request.ToLocationErpCode = loc.ErpLocationCode;
|
|
|
|
request.ToWarehouseCode = loc.WarehouseCode;
|
|
|
|
request.ToLocationArea = loc.AreaCode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 返回错误列表
|
|
|
|
return errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
// /// <summary>
|
|
|
|
// /// 读取摄像头API
|
|
|
|
// /// </summary>
|
|
|
|
// /// <param name="p_type"></param>
|
|
|
|
// /// <returns></returns>
|
|
|
|
// public async Task<string> ReaderCameraApi()
|
|
|
|
// {
|
|
|
|
|
|
|
|
// try
|
|
|
|
// {
|
|
|
|
|
|
|
|
// // 从配置中获取远程摄像头的地址、用户名、密码和令牌
|
|
|
|
// var address = _options.Value.AutoRemote.IpAddress;
|
|
|
|
// var username = _options.Value.AutoRemote.UserName;
|
|
|
|
// var password = _options.Value.AutoRemote.Password;
|
|
|
|
// var token = _options.Value.AutoRemote.Token;
|
|
|
|
|
|
|
|
// // 创建一个HttpClient实例
|
|
|
|
// var client = _httpClientFactory.CreateClient();
|
|
|
|
|
|
|
|
// // 将用户名和密码转换为Base64编码的凭据,并设置请求的身份验证信息
|
|
|
|
// var credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{username}:{password}"));
|
|
|
|
// client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", credentials);
|
|
|
|
|
|
|
|
|
|
|
|
// // 发送GET请求到远程摄像头地址并等待响应
|
|
|
|
// var response = await client.PostAsync(address, new StringContent(token, Encoding.UTF8, "application/json")).ConfigureAwait(false);
|
|
|
|
|
|
|
|
// // 如果请求成功,则返回响应内容,否则返回错误信息
|
|
|
|
// if (response.IsSuccessStatusCode)
|
|
|
|
// {
|
|
|
|
// return await response.Content.ReadAsStringAsync().ConfigureAwait(false);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// catch (HttpRequestException ex)
|
|
|
|
// {
|
|
|
|
// _logger.LogInformation("远程摄像头连接失败:" + ex.Message);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// return "Error occurred";
|
|
|
|
// }
|
|
|
|
|
|
|
|
// private List<InjectionRequest> Parse(string p_str)
|
|
|
|
// {
|
|
|
|
// List<InjectionRequest> requests = new List<InjectionRequest>();
|
|
|
|
|
|
|
|
// return System.Text.Json.JsonSerializer.Deserialize<List<InjectionRequest>>(p_str);
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class InjectionRequest
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// 零件M
|
|
|
|
/// </summary>
|
|
|
|
public string ItemCode { get; set; }
|
|
|
|
/// <summary>
|
|
|
|
/// 零件名称
|
|
|
|
/// </summary>
|
|
|
|
public string ItemName { get; set; }
|
|
|
|
/// <summary>
|
|
|
|
/// 发运库位
|
|
|
|
/// </summary>
|
|
|
|
public string ToLocCode { get; set; }
|
|
|
|
/// <summary>
|
|
|
|
/// 来源库位
|
|
|
|
/// </summary>
|
|
|
|
public string FromLocCode { get; set; }
|
|
|
|
/// <summary>
|
|
|
|
/// 数量
|
|
|
|
/// </summary>
|
|
|
|
public decimal Qty { get; set; }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|