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 _logger; private readonly IOptions _options; private readonly IHttpClientFactory _httpClientFactory; public InjectionMoldingRequestReader( IInjectionIssueRequestAppService injectionRequest , ILogger logger , IOptions options , IHttpClientFactory httpClientFactory , IItemBasicAppService itemService , ILocationAppService locService ) { _injectionRequest = injectionRequest; _logger = logger; _options = options; _httpClientFactory = httpClientFactory; _itemService=itemService; _locService = locService; } /// /// 读取注塑叫料任务 /// /// public virtual async Task> ReadAsync() { try { #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; var invterval = _options.Value.AutoRemote.Interval; var successTime = 0; var guid = DateTime.Now.ToString("yyyyMMddHHmmssfff"); for (int i = 0; i (); } if (string.IsNullOrEmpty(_options.Value.AutoRemote.AreaIDs)) { _logger.LogInformation($"没有设置指定区域AutoRemote->AreaIDs"); return new List(); } var ids = _options.Value.AutoRemote.AreaIDs.Split(","); List camralist = new List(); foreach (var itm in ids)//遍历区域查找所有货物 { var idsList = await client.GetCargoStaticAsync(itm).ConfigureAwait(false); camralist.AddRange(idsList); } successTime++; sw.Stop(); _logger.LogInformation($"读取标识{guid}读取成功次数{successTime},耗时 {sw.ElapsedMilliseconds}毫秒"); Thread.Sleep(1000); if (successTime == invterval) { sw = Stopwatch.StartNew(); _logger.LogInformation($"开始标识{guid}同步WMS开始"); List inputdetail = new List(); 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 cameraList = new List(); InjectionIssueRequestEditInput input = new InjectionIssueRequestEditInput(); input.Worker = "Vision"; input.IssueRequestType = EnumIssueRequestType.Vision; input.AutoSubmit = true; input.ActiveDate = DateTime.Now; input.UseOnTheWayLocation = false; input.Remark = "视觉叫料"; List injectionRequestDetails = new List(); //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(); } // 创建新的注塑请求并将数据写入数据库 await _injectionRequest.CreateAsync(input).ConfigureAwait(false); } _logger.LogInformation($"标识{guid}同步WMS结束,耗时{sw.ElapsedMilliseconds }毫秒"); } else { Thread.Sleep(1000); } } //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(); //} //if (string.IsNullOrEmpty(_options.Value.AutoRemote.AreaIDs)) //{ // _logger.LogInformation($"没有设置指定区域AutoRemote->AreaIDs"); // return new List(); //} //var ids=_options.Value.AutoRemote.AreaIDs.Split(","); //List camralist = new List(); //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(); } /// /// 绑定零件库位信息,如果对错误返回错误新列表 /// /// /// private async Task> BindAsync(List p_list) { // 异步方法,将输入的请求绑定到对应的零件和库位信息,返回错误列表 List errors = new List(); 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; } // /// // /// 读取摄像头API // /// // /// // /// // public async Task 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 Parse(string p_str) // { // List requests = new List(); // return System.Text.Json.JsonSerializer.Deserialize>(p_str); // } public class InjectionRequest { /// /// 零件M /// public string ItemCode { get; set; } /// /// 零件名称 /// public string ItemName { get; set; } /// /// 发运库位 /// public string ToLocCode { get; set; } /// /// 来源库位 /// public string FromLocCode { get; set; } /// /// 数量 /// public decimal Qty { get; set; } } }