You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

370 lines
14 KiB

1 year ago
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;
1 year ago
using Win_in.Sfs.Shared.Domain.Shared.Enums.Store;
12 months ago
using System.Threading;
using System.Diagnostics;
1 year ago
namespace Win_in.Sfs.Wms.DataExchange.Fawtyg.InjectionMoldingTaskAgent.Incoming;
1 year ago
public class InjectionMoldingRequestReader : IReader
{
private readonly IInjectionIssueRequestAppService _injectionRequest;
1 year ago
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
1 year ago
1 year ago
, ILogger<InjectionMoldingRequestReader> logger
, IOptions<InjectionMoldingTaskOptions> options
1 year ago
, IHttpClientFactory httpClientFactory
, IItemBasicAppService itemService
, ILocationAppService locService
1 year ago
)
{
_injectionRequest = injectionRequest;
1 year ago
1 year ago
_logger = logger;
_options = options;
_httpClientFactory = httpClientFactory;
_itemService=itemService;
_locService = locService;
}
1 year ago
/// <summary>
/// 读取注塑叫料任务
/// </summary>
/// <returns></returns>
1 year ago
1 year ago
public virtual async Task<List<IncomingFromExternal>> ReadAsync()
1 year ago
{
1 year ago
try
{
11 months ago
#if DEBUG
_options.Value.AutoRemote.TimeCycle = 1;
_options.Value.AutoRemote.Interval = 2;
#endif
// var sleepTime = (_options.Value.AutoRemote.TimeCycle*60/ _options.Value.AutoRemote.Interval)-10;
12 months ago
var invterval = _options.Value.AutoRemote.Interval;
var successTime = 0;
11 months ago
var guid = DateTime.Now.ToString("yyyyMMddHHmmssfff");
12 months ago
for (int i = 0; i <invterval; i++)
{
Stopwatch sw = Stopwatch.StartNew();
Client client = new Client(_options.Value.AutoRemote.IpAddress, _httpClientFactory.CreateClient());//调用客户记录
12 months ago
var flag = await client.GetMacStaticAsync().ConfigureAwait(false);
if (flag != 1)
1 year ago
{
1 year ago
// 记录错误日志并返回空列表
_logger.LogInformation($"读取到摄像头信息为不可用{DateTime.Now},请检查摄像头");
12 months ago
return new List<IncomingFromExternal>();
}
if (string.IsNullOrEmpty(_options.Value.AutoRemote.AreaIDs))
{
_logger.LogInformation($"没有设置指定区域AutoRemote->AreaIDs");
1 year ago
return new List<IncomingFromExternal>();
}
12 months ago
var ids = _options.Value.AutoRemote.AreaIDs.Split(",");
List<ResponCargoItem> camralist = new List<ResponCargoItem>();
foreach (var itm in ids)//遍历区域查找所有货物
1 year ago
{
12 months ago
var idsList = await client.GetCargoStaticAsync(itm).ConfigureAwait(false);
camralist.AddRange(idsList);
1 year ago
}
12 months ago
successTime++;
11 months ago
sw.Stop();
_logger.LogInformation($"读取标识{guid}读取成功次数{successTime},耗时 {sw.ElapsedMilliseconds}毫秒");
Thread.Sleep(1000);
12 months ago
if (successTime == invterval)
{
11 months ago
sw = Stopwatch.StartNew();
11 months ago
_logger.LogInformation($"开始标识{guid}同步WMS开始");
12 months ago
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);
}
12 months ago
var uncompletelist = await _injectionRequest.GetUnCompletedRequestAsync(inputdetail).ConfigureAwait(false);//将所有库位零件信息查找未完成记录
12 months ago
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();//已经完成的记录
12 months ago
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(),
12 months ago
PositionCode = inject.CargoID,
Qty = 1,
RecommendType = EnumRecommendType.RAW,
IssuedQty = 0,
ReceivedQty = 0,
Status = EnumStatus.Open,
RequestStatus = EnumRequestStatus.New,
12 months ago
BoxQty = 1
};
// 添加注塑叫料明细任务数据
injectionRequestDetails.Add(detailInput);
12 months ago
//}
input.Details.AddRange(injectionRequestDetails);
// 通过 BindAsync 方法对零件仓库进行赋值
var errors = await BindAsync(input.Details).ConfigureAwait(false);
if (errors.Count > 0)
{
12 months ago
// 记录错误日志并返回空列表
foreach (var error in errors)
{
_logger.LogInformation(error);
}
return new List<IncomingFromExternal>();
}
12 months ago
// 创建新的注塑请求并将数据写入数据库
await _injectionRequest.CreateAsync(input).ConfigureAwait(false);
11 months ago
1 year ago
}
11 months ago
_logger.LogInformation($"标识{guid}同步WMS结束,耗时{sw.ElapsedMilliseconds }毫秒");
1 year ago
}
12 months ago
else
{
11 months ago
Thread.Sleep(1000);
12 months ago
}
11 months ago
12 months ago
}
12 months ago
//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>();
//}
12 months ago
//if (string.IsNullOrEmpty(_options.Value.AutoRemote.AreaIDs))
//{
12 months ago
// _logger.LogInformation($"没有设置指定区域AutoRemote->AreaIDs");
// return new List<IncomingFromExternal>();
//}
12 months ago
//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();
//}
1 year ago
}
1 year ago
// 捕获特定异常并记录日志
1 year ago
catch (Exception ex)
{
1 year ago
_logger.LogInformation(ex.Message);
}
1 year ago
// 返回空列表
1 year ago
return new List<IncomingFromExternal>();
}
1 year ago
/// <summary>
/// 绑定零件库位信息,如果对错误返回错误新列表
/// </summary>
/// <param name="p_list"></param>
/// <returns></returns>
private async Task<List<string>> BindAsync(List<InjectionIssueRequestDetailInput> p_list)
1 year ago
{
1 year ago
// 异步方法,将输入的请求绑定到对应的零件和库位信息,返回错误列表
1 year ago
List<string> errors = new List<string>();
foreach (var request in p_list)
{
1 year ago
// 获取对应零件信息
var itm = await _itemService.GetByCodeAsync(request.ItemCode).ConfigureAwait(false);
if (itm == null) { errors.Add($"编号:{request.ItemCode}零件表中没找到!"); }
else
1 year ago
{
1 year ago
// 更新请求中的零件描述和名称
1 year ago
request.ItemDesc1 = itm.Desc1;
request.ItemDesc2 = itm.Desc2;
request.ItemName = itm.Name;
1 year ago
request.Uom = itm.BasicUom;
1 year ago
}
1 year ago
// 获取对应库位信息
1 year ago
var loc = await _locService.GetByCodeAsync(request.ToLocationCode).ConfigureAwait(false);
if (loc == null) { errors.Add($"编号:{request.ToLocationCode}库位表中没找到!"); }
else
1 year ago
{
1 year ago
// 更新请求中的库位相关信息
1 year ago
request.ToLocationCode = loc.Code;
request.ToLocationGroup = loc.LocationGroupCode;
request.ToLocationErpCode = loc.ErpLocationCode;
1 year ago
request.ToWarehouseCode = loc.WarehouseCode;
1 year ago
request.ToLocationArea = loc.AreaCode;
1 year ago
}
}
1 year ago
// 返回错误列表
1 year ago
return errors;
}
// /// <summary>
// /// 读取摄像头API
// /// </summary>
// /// <param name="p_type"></param>
// /// <returns></returns>
// public async Task<string> ReaderCameraApi()
// {
1 year ago
// 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);
1 year ago
// // 发送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);
1 year ago
// }
1 year ago
// return "Error occurred";
// }
1 year ago
// private List<InjectionRequest> Parse(string p_str)
// {
// List<InjectionRequest> requests = new List<InjectionRequest>();
// return System.Text.Json.JsonSerializer.Deserialize<List<InjectionRequest>>(p_str);
// }
1 year ago
1 year ago
public class InjectionRequest
1 year ago
{
/// <summary>
/// 零件M
1 year ago
/// </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; }
}
1 year ago
}