using System; using System.Dynamic; using System.Security.Policy; using Aop.Api.Domain; using JNPF; using JNPF.Common.Cache; using JNPF.Common.Dtos.VisualDev; using JNPF.FriendlyException; using Microsoft.Extensions.Hosting; using NetTaste; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Qiniu.Util; using SqlSugar; using Tnb.Common.Redis; using Tnb.Common.Utils; using Tnb.ProductionMgr.Entities; using Tnb.ProductionMgr.Entities.Dto; using Tnb.ProductionMgr.Entities.Enums; using Tnb.ProductionMgr.Interfaces; using Tnb.WarehouseMgr.Entities; using Tnb.WarehouseMgr.Entities.Consts; using Tnb.WarehouseMgr.Interfaces; namespace Tnb.ProductionMgr { //redis定时获取数采数据 public class RedisBackGround : IHostedService, IDisposable { private Timer? Readtimer; private Timer? CheckGettimer; private Timer? Scantimer; private readonly RedisData _redisData; private readonly IPrdInstockService _prdInstockService; private readonly ISqlSugarRepository _repository; private readonly IWmsPDAScanInStockService _wmsPDAScanInStock; public RedisBackGround(RedisData redisData, IPrdInstockService prdInstockService, ISqlSugarRepository repository, IWmsPDAScanInStockService wmsPDAScanInStock) { _redisData = redisData; _prdInstockService = prdInstockService; _repository = repository; _wmsPDAScanInStock = wmsPDAScanInStock; } //获取redis数据 private void GetRedisData(object state) { var _redisReadConfigs = _repository.AsQueryable().Where(p => p.enabled == 1).ToList(); foreach (var config in _redisReadConfigs) { try { var json = _redisData.GetHash(config.dev_name!, config.tag_name!).Result; JObject? res = JsonConvert.DeserializeObject(json); if (config.data_type == (int)DataType.INT) { if (config.check_type == (int)CheckType.相等) { if (res.Value("Value") == int.Parse(config.data!)) { InstockInput instockInput = new() { equip_code = res["DevName"]!.ToString(), label_code = res["TagName"]!.ToString() }; TriggerEvent((EventType)config.event_type, instockInput); } } else if (config.check_type == (int)CheckType.包含) { int[] ints = Array.ConvertAll(config.data!.Replace("[", "").Replace("]", "").Split(",", StringSplitOptions.RemoveEmptyEntries), int.Parse); if (ints.Contains(res.Value("Value"))) { InstockInput instockInput = new() { equip_code = res["DevName"]!.ToString(), label_code = res["TagName"]!.ToString() }; TriggerEvent((EventType)config.event_type, instockInput); } } } else if (config.data_type == (int)DataType.BOOL) { if (config.check_type == (int)CheckType.相等) { if (res.Value("Value") == bool.Parse(config.data!)) { InstockInput instockInput = new() { equip_code = res["DevName"]!.ToString(), label_code = res["TagName"]!.ToString() }; TriggerEvent((EventType)config.event_type, instockInput); } } } } catch (Exception) { } } } private void TriggerEvent(EventType eventType, InstockInput instockInput) { switch (eventType) { case EventType.注塑空满箱请求: _prdInstockService.InstockTypeOne(instockInput); break; case EventType.挤出空满箱请求: _prdInstockService.InstockTubeOne(instockInput); break; case EventType.限位请求: _prdInstockService.InstockOutPack(instockInput); break; default: break; } } //ctu取货 private void CheckGet(object state) { Dictionary getdic = new Dictionary(); getdic.Add("SSX-021-005", new string[] { "CS05", "FullBoxAllowGet", "Code" }); getdic.Add("SSX-021-006", new string[] { "CS06", "FullBoxAllowGet", "Code" }); foreach (var key in getdic.Keys) { try { var strs = getdic.Where(p => p.Key == key).First().Value; bool flag = _redisData.HashExist(strs[0], strs[1]).Result; string data = _redisData.GetHash(strs[0], strs[1]).Result; JObject? res = JsonConvert.DeserializeObject(data); bool result = res != null && res["Value"] != null ? res.Value("Value") : false; if (result) { string codedata = _redisData.GetHash(strs[0], strs[2]).Result; JObject? coderes = JsonConvert.DeserializeObject(codedata); string coderesult = coderes != null && coderes["Value"] != null ? coderes.Value("Value")! : ""; var DistaskH = _repository.AsSugarClient().Queryable().Where(p => p.carry_code == coderesult && p.status == WmsWareHouseConst.TASK_BILL_STATUS_RUNING_ID).First(); dynamic reqBody = new ExpandoObject(); reqBody.taskCode = DistaskH.bill_code; reqBody.slotCode = key; reqBody.containerCode = coderesult; CancellationTokenSource Ctu = new(); //{"code":500,"msg":"成功!"} dynamic respBody = HttpClientHelper.PostStreamAsync("http://192.168.11.104:1880/wcs/notify/cargo", reqBody, Ctu.Token).Result; Ctu.Dispose(); } } catch (Exception) { } } } //扫码入库 private void ScanInStock(object state) { var carry_code = "carry_code"; WmsCarryH? carry = _repository.AsSugarClient().Queryable().Single(it => it.carry_code == carry_code); if (carry != null) { var WmsCarryCode = _repository.AsSugarClient().Queryable().Single(it => it.carry_id == carry.id); VisualDevModelDataCrInput input = new VisualDevModelDataCrInput(); input.data = new Dictionary(); input.data.Add("barcode", carry_code); input.data.Add("codeqty", WmsCarryCode.codeqty);//条码数量 input.data.Add("material_code", WmsCarryCode.material_code); input.data.Add("extras", WmsCarryCode.location_code);//location_code input.data.Add("warehouse_id", WmsCarryCode.warehouse_id!); input.data.Add("bill_code", "");//采购收货单号 input.data.Add("code_batch", WmsCarryCode.code_batch!);//批次 input.data.Add("material_specification", WmsCarryCode.material_specification!);//规格型号 input.data.Add("container_no", WmsCarryCode.container_no!);//箱号 input.data.Add("material_id", WmsCarryCode.material_id); input.data.Add("id", null); _wmsPDAScanInStock.ScanInStock(input); } } public void Dispose() { Readtimer?.Dispose(); CheckGettimer?.Dispose(); } public Task StartAsync(CancellationToken cancellationToken) { // Readtimer = new Timer(GetRedisData, null, TimeSpan.Zero, TimeSpan.FromSeconds(300)); // CheckGettimer = new Timer(CheckGet, null, TimeSpan.Zero, TimeSpan.FromSeconds(300)); // Scantimer = new Timer(ScanInStock, null, TimeSpan.Zero, TimeSpan.FromSeconds(300)); return Task.CompletedTask; } public Task StopAsync(CancellationToken cancellationToken) { return Task.CompletedTask; } } }