using JNPF.Common.Cache; using Microsoft.Extensions.Hosting; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Tnb.ProductionMgr.Entities.Dto; using Tnb.ProductionMgr.Entities.Enums; using Tnb.ProductionMgr.Interfaces; namespace Tnb.ProductionMgr { //redis定时获取数采数据 public class RedisBackGround : IHostedService, IDisposable { private Timer? packtimer; private Timer? limittimer; private readonly RedisCache _redisCache; private readonly IPrdInstockService _prdInstockService; public RedisBackGround(RedisCache redisCache, IPrdInstockService prdInstockService) { _redisCache = redisCache; _prdInstockService = prdInstockService; } //获取注塑装箱状态 private void GetZSPackStatus(object state) { // Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss}"); // Console.WriteLine($"获取注塑装箱状态"); string[] strs = new string[1] { "TY4C-ZHUSU1" }; string sign = "agvMode"; foreach (string s in strs) { var dic = _redisCache.HGetAll(s).Result; foreach (var kv in dic) { if (!kv.Key.Contains(sign)) continue; var res = JsonConvert.DeserializeObject(kv.Value); if (res != null && res["Value"] != null) { if (int.Parse(res["Value"]!.ToString()) != (int)Eagvmode.无请求 || int.Parse(res["Value"]!.ToString()) != (int)Eagvmode.收到请求) { InstockInput instockInput = new InstockInput(); instockInput.equip_code = res["TagName"]!.ToString(); _prdInstockService.InstockTypeOne(instockInput); } } } } } //获取挤出装箱状态 private void GetJCPackStatus(object state) { // Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss}"); // Console.WriteLine($"获取挤出装箱状态"); string[] strs = new string[1] { "TY4C-SHUSONG-JC" }; string sign = "AGVFullCall"; foreach (string s in strs) { var dic = _redisCache.HGetAll(s).Result; foreach (var kv in dic) { if (!kv.Key.Contains(sign)) continue; var res = JsonConvert.DeserializeObject(kv.Value); if (res != null && res["Value"] != null) { if (res.Value("Value")) { InstockInput instockInput = new InstockInput(); instockInput.equip_code = res["TagName"]!.ToString(); _prdInstockService.InstockTypeOne(instockInput); } } } } } //获取限位状态 private void GetLimitStatus(object state) { // Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss}"); // Console.WriteLine($"获取限位状态"); string[] strs = new string[1] { "TY4C-WAIBAO" }; string sign = "AGVCall"; foreach (string s in strs) { var dic = _redisCache.HGetAll(s).Result; foreach (var kv in dic) { if (!kv.Key.Contains(sign)) continue; var res = JsonConvert.DeserializeObject(kv.Value); if (res != null && res["Value"] != null) { if (res.Value("Value")) { // } } } } } public void Dispose() { packtimer?.Dispose(); limittimer?.Dispose(); } public Task StartAsync(CancellationToken cancellationToken) { // packtimer = new Timer(GetPackStatus, null, TimeSpan.Zero, TimeSpan.FromSeconds(2)); // limittimer = new Timer(GetLimitStatus, null, TimeSpan.Zero, TimeSpan.FromSeconds(2)); return Task.CompletedTask; } public Task StopAsync(CancellationToken cancellationToken) { return Task.CompletedTask; } } }