diff --git a/ProductionMgr/Tnb.ProductionMgr/RedisBackGround.cs b/ProductionMgr/Tnb.ProductionMgr/RedisBackGround.cs index b6e7f3ad..12879acb 100644 --- a/ProductionMgr/Tnb.ProductionMgr/RedisBackGround.cs +++ b/ProductionMgr/Tnb.ProductionMgr/RedisBackGround.cs @@ -2,6 +2,7 @@ using Microsoft.Extensions.Hosting; using Newtonsoft.Json; using Newtonsoft.Json.Linq; +using Tnb.Common.Redis; using Tnb.ProductionMgr.Entities.Dto; using Tnb.ProductionMgr.Entities.Enums; using Tnb.ProductionMgr.Interfaces; @@ -11,25 +12,24 @@ namespace Tnb.ProductionMgr //redis定时获取数采数据 public class RedisBackGround : IHostedService, IDisposable { - private readonly Timer? packtimer; - private readonly Timer? limittimer; - private readonly RedisCache _redisCache; + private Timer? ZSpacktimer; + private Timer? JCpacktimer; + private Timer? limittimer; + private readonly RedisData _redisData; private readonly IPrdInstockService _prdInstockService; - public RedisBackGround(RedisCache redisCache, IPrdInstockService prdInstockService) + public RedisBackGround(RedisData redisData, IPrdInstockService prdInstockService) { - _redisCache = redisCache; + _redisData = redisData; _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) { - Dictionary dic = _redisCache.HGetAll(s).Result; + Dictionary dic = _redisData.HGetAll(s).Result; foreach (KeyValuePair kv in dic) { if (!kv.Key.Contains(sign)) @@ -40,7 +40,7 @@ namespace Tnb.ProductionMgr JObject? res = JsonConvert.DeserializeObject(kv.Value); if (res != null && res["Value"] != null) { - if (int.Parse(res["Value"]!.ToString()) is not ((int)Eagvmode.无请求) or not ((int)Eagvmode.收到请求)) + if (int.Parse(res["Value"]!.ToString()) is not ((int)Eagvmode.无请求) and not ((int)Eagvmode.收到请求)) { InstockInput instockInput = new() { @@ -55,13 +55,11 @@ namespace Tnb.ProductionMgr //获取挤出装箱状态 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) { - Dictionary dic = _redisCache.HGetAll(s).Result; + Dictionary dic = _redisData.HGetAll(s).Result; foreach (KeyValuePair kv in dic) { if (!kv.Key.Contains(sign)) @@ -87,13 +85,17 @@ namespace Tnb.ProductionMgr //获取限位状态 private void GetLimitStatus(object state) { - // Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss}"); - // Console.WriteLine($"获取限位状态"); + /* + Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss}"); + Console.WriteLine($"获取挤出装箱状态"); + string data = _redisData.GetHash("TY4C-SHUSONG-JC", "AGVFullCall").Result; + Console.WriteLine(data);*/ + /* string[] strs = new string[1] { "TY4C-WAIBAO" }; string sign = "AGVCall"; foreach (string s in strs) { - Dictionary dic = _redisCache.HGetAll(s).Result; + Dictionary dic = _redisData.HGetAll(s).Result; foreach (KeyValuePair kv in dic) { if (!kv.Key.Contains(sign)) @@ -110,17 +112,19 @@ namespace Tnb.ProductionMgr } } } - } + }*/ } public void Dispose() { - packtimer?.Dispose(); + ZSpacktimer?.Dispose(); + JCpacktimer?.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)); + // ZSpacktimer = new Timer(GetZSPackStatus, null, TimeSpan.Zero, TimeSpan.FromSeconds(2)); + // JCpacktimer = new Timer(GetJCPackStatus, null, TimeSpan.Zero, TimeSpan.FromSeconds(2)); + // limittimer = new Timer(GetLimitStatus, null, TimeSpan.Zero, TimeSpan.FromSeconds(2)); return Task.CompletedTask; } public Task StopAsync(CancellationToken cancellationToken) diff --git a/ProductionMgr/Tnb.ProductionMgr/RedisDataService.cs b/ProductionMgr/Tnb.ProductionMgr/RedisDataService.cs index f6ce935f..6007f34f 100644 --- a/ProductionMgr/Tnb.ProductionMgr/RedisDataService.cs +++ b/ProductionMgr/Tnb.ProductionMgr/RedisDataService.cs @@ -5,6 +5,7 @@ using JNPF.FriendlyException; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; using Newtonsoft.Json.Linq; +using Tnb.Common.Redis; using Tnb.ProductionMgr.Interfaces; namespace Tnb.ProductionMgr @@ -16,11 +17,10 @@ namespace Tnb.ProductionMgr [Route("api/[area]/[controller]/[action]")] public class RedisDataService : IRedisDataService, IDynamicApiController, ITransient { - private readonly RedisCache _redisCache; - public RedisDataService(RedisCache redisCache) + private readonly RedisData _redisData; + public RedisDataService(RedisData redisData) { - _redisCache = redisCache; - + _redisData = redisData; } /// /// 根据机号获取重量 @@ -28,31 +28,13 @@ namespace Tnb.ProductionMgr [HttpPost] public async Task GetWeight(string device, string jihao) { - bool flag = await _redisCache.HashExist(device, jihao); + bool flag = await _redisData.HashExist(device, jihao); if (!flag) { throw Oops.Bah("没有找到" + device + "----" + jihao + "的数据"); } - - string data = await _redisCache.GetHash(device, jihao); + string data = await _redisData.GetHash(device, jihao); JObject? res = JsonConvert.DeserializeObject(data); - /* - var dic =await _redisCache.HGetAll("TY4C-JICHU"); - string a = ""; - foreach (var kvp in dic) - { - a += kvp.Key + "&" + kvp.Value + "$"; - } - - - string aa = ""; - var ss= aa.Split('$', StringSplitOptions.RemoveEmptyEntries); - foreach (var s in ss) - { - var b = s.Split('&'); - await _redisCache.HSet("TY4C-JICHU", b[0], b[1]); - }*/ - decimal result = res != null && res["Value"] != null ? decimal.Parse(res["Value"]!.ToString()) : throw Oops.Bah("数据格式错误"); return result; } diff --git a/apihost/Tnb.API.Entry/Configurations/Cache.json b/apihost/Tnb.API.Entry/Configurations/Cache.json index 8f0ff1f3..69116fb8 100644 --- a/apihost/Tnb.API.Entry/Configurations/Cache.json +++ b/apihost/Tnb.API.Entry/Configurations/Cache.json @@ -5,5 +5,11 @@ "port": 6379, "password": "05jWEoJa8v", "RedisConnectionString": "{0}:{1},password={2}, poolsize=500,ssl=false,defaultDatabase=7" + }, + "Redis": { + "ip": "localhost", + "port": 6379, + "password": "05jWEoJa8v", + "RedisConnectionString": "{0}:{1},password={2}, poolsize=500,ssl=false,defaultDatabase=0" } } \ No newline at end of file diff --git a/apihost/Tnb.API.Entry/Startup.cs b/apihost/Tnb.API.Entry/Startup.cs index d7d716d8..3dbfefd2 100644 --- a/apihost/Tnb.API.Entry/Startup.cs +++ b/apihost/Tnb.API.Entry/Startup.cs @@ -2,6 +2,7 @@ using JNPF.Common.Cache; using JNPF.Common.Core.Handlers; using JNPF.Common.Security; +using JNPF.DependencyInjection; using JNPF.SpecificationDocument; using JNPF.TaskScheduler.Interfaces.TaskScheduler; using JNPF.VisualDev; @@ -11,6 +12,7 @@ using Senparc.CO2NET.RegisterServices; using Senparc.Weixin; using Senparc.Weixin.Entities; using Senparc.Weixin.RegisterServices; +using Tnb.Common.Redis; using Tnb.ProductionMgr; using Tnb.WarehouseMgr; @@ -46,7 +48,7 @@ public class Startup : AppStartup services.AddMemoryCache(); // 使用本地缓存必须添加 services.AddConfigurableOptions(); - + services.AddSingleton( typeof(ISingleton), typeof(RedisData)); // 微信 services.AddSenparcGlobalServices(App.Configuration) // Senparc.CO2NET 全局注册 .AddSenparcWeixinServices(App.Configuration); // Senparc.Weixin 注册(如果使用Senparc.Weixin SDK则添加) diff --git a/common/Tnb.Common/Redis/RedisData.cs b/common/Tnb.Common/Redis/RedisData.cs new file mode 100644 index 00000000..3c296f5c --- /dev/null +++ b/common/Tnb.Common/Redis/RedisData.cs @@ -0,0 +1,247 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using CSRedis; +using JNPF; +using JNPF.Common.Cache; +using JNPF.DependencyInjection; +using Microsoft.Extensions.Options; + +namespace Tnb.Common.Redis +{ + public class RedisData : ISingleton + { + private static CSRedisClient _instance; + public RedisData() + { + RedisOptions _RedisOptions = App.GetConfig("Redis", true); + _instance = new CSRedis.CSRedisClient(string.Format(_RedisOptions.RedisConnectionString, _RedisOptions.ip, _RedisOptions.port, _RedisOptions.password)); + } + + /// + /// 用于在 key 存在时删除 key. + /// + /// 键. + public long Del(params string[] key) + { + return _instance.Del(key); + } + + /// + /// 用于在 key 存在时删除 key. + /// + /// 键. + public Task DelAsync(params string[] key) + { + return _instance.DelAsync(key); + } + + /// + /// 用于在 key 模板存在时删除. + /// + /// key模板. + public async Task DelByPatternAsync(string pattern) + { + if (string.IsNullOrEmpty(pattern)) + return default; + + // pattern = Regex.Replace(pattern, @"\{.*\}", "*"); + string[]? keys = await _instance.KeysAsync(pattern); + if (keys?.Length > 0) + { + return await _instance.DelAsync(keys); + } + + return default; + } + + /// + /// 检查给定 key 是否存在. + /// + /// 键. + public bool Exists(string key) + { + return _instance.Exists(key); + } + + /// + /// 检查给定 key 是否存在. + /// + /// 键. + public Task ExistsAsync(string key) + { + return _instance.ExistsAsync(key); + } + + /// + /// 获取指定 key 的增量值. + /// + /// 键. + /// 增量. + /// + public long Incrby(string key, long incrBy) + { + return _instance.IncrBy(key, incrBy); + } + + /// + /// 获取指定 key 的增量值. + /// + /// 键. + /// 增量. + /// + public Task IncrbyAsync(string key, long incrBy) + { + return _instance.IncrByAsync(key, incrBy); + } + + /// + /// 获取指定 key 的值. + /// + /// 键. + public string Get(string key) + { + return _instance.Get(key); + } + + /// + /// 获取指定 key 的值. + /// + /// byte[] 或其他类型. + /// 键. + public T Get(string key) + { + return _instance.Get(key); + } + + /// + /// 获取指定 key 的值. + /// + /// 键. + /// + public Task GetAsync(string key) + { + return _instance.GetAsync(key); + } + + /// + /// 获取指定 key 的值. + /// + /// byte[] 或其他类型. + /// 键. + public Task GetAsync(string key) + { + return _instance.GetAsync(key); + } + + /// + /// 设置指定 key 的值,所有写入参数object都支持string | byte[] | 数值 | 对象. + /// + /// 键. + /// 值. + public bool Set(string key, object value) + { + return _instance.Set(key, value); + } + + /// + /// 设置指定 key 的值,所有写入参数object都支持string | byte[] | 数值 | 对象. + /// + /// 键. + /// 值. + /// 有效期. + public bool Set(string key, object value, TimeSpan expire) + { + return _instance.Set(key, value, expire); + } + + /// + /// 设置指定 key 的值,所有写入参数object都支持string | byte[] | 数值 | 对象. + /// + /// 键. + /// 值. + public Task SetAsync(string key, object value) + { + return _instance.SetAsync(key, value); + } + + /// + /// 保存. + /// + /// 键. + /// 值. + /// 过期时间. + /// + public Task SetAsync(string key, object value, TimeSpan expire) + { + return _instance.SetAsync(key, value, expire); + } + + /// + /// 只有在 key 不存在时设置 key 的值. + /// + /// 键. + /// 值. + /// 有效期. + public bool SetNx(string key, object value, TimeSpan expire) + { + if (_instance.SetNx(key, value)) + { + _instance.Set(key, value, expire); + return true; + } + else + { + return false; + } + } + + /// + /// 只有在 key 不存在时设置 key 的值. + /// + /// 键. + /// 值. + public bool SetNx(string key, object value) + { + return _instance.SetNx(key, value); + } + + /// + /// 获取所有key. + /// + /// + public List GetAllKeys() + { + return _instance.Keys("*").ToList(); + } + + /// + /// 获取缓存过期时间. + /// + /// 键值. + /// + public DateTime GetCacheOutTime(string key) + { + long second = _instance.PTtl(key); + return DateTime.Now.AddMilliseconds(second); + } + public Task GetHash(string key, string field) + { + return _instance.HGetAsync(key, field); + } + public Task HashExist(string key, string field) + { + return _instance.HExistsAsync(key, field); + } + public Task> HGetAll(string key) + { + return _instance.HGetAllAsync(key); + } + public Task HSet(string key, string field, string value) + { + return _instance.HSetAsync(key, field, value); + } + } +} \ No newline at end of file diff --git a/common/Tnb.Common/Redis/RedisOptions.cs b/common/Tnb.Common/Redis/RedisOptions.cs new file mode 100644 index 00000000..1e718b4a --- /dev/null +++ b/common/Tnb.Common/Redis/RedisOptions.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using JNPF.Common.Cache; +using JNPF.ConfigurableOptions; + +namespace Tnb.Common.Redis +{ + public class RedisOptions : IConfigurableOptions + { + + /// + /// Redis配置. + /// + public string RedisConnectionString { get; set; } + + /// + /// 服务器地址. + /// + public string ip { get; set; } + + /// + /// 端口. + /// + public int port { get; set; } + + /// + /// 密码. + /// + public string password { get; set; } + } +} \ No newline at end of file