redis
This commit is contained in:
@@ -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<string, string> dic = _redisCache.HGetAll(s).Result;
|
||||
Dictionary<string, string> dic = _redisData.HGetAll(s).Result;
|
||||
foreach (KeyValuePair<string, string> kv in dic)
|
||||
{
|
||||
if (!kv.Key.Contains(sign))
|
||||
@@ -40,7 +40,7 @@ namespace Tnb.ProductionMgr
|
||||
JObject? res = JsonConvert.DeserializeObject<JObject>(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<string, string> dic = _redisCache.HGetAll(s).Result;
|
||||
Dictionary<string, string> dic = _redisData.HGetAll(s).Result;
|
||||
foreach (KeyValuePair<string, string> 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<string, string> dic = _redisCache.HGetAll(s).Result;
|
||||
Dictionary<string, string> dic = _redisData.HGetAll(s).Result;
|
||||
foreach (KeyValuePair<string, string> 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)
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
/// <summary>
|
||||
/// 根据机号获取重量
|
||||
@@ -28,31 +28,13 @@ namespace Tnb.ProductionMgr
|
||||
[HttpPost]
|
||||
public async Task<dynamic> 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<JObject>(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;
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
}
|
||||
@@ -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<CacheOptions>();
|
||||
|
||||
services.AddSingleton( typeof(ISingleton), typeof(RedisData));
|
||||
// 微信
|
||||
services.AddSenparcGlobalServices(App.Configuration) // Senparc.CO2NET 全局注册
|
||||
.AddSenparcWeixinServices(App.Configuration); // Senparc.Weixin 注册(如果使用Senparc.Weixin SDK则添加)
|
||||
|
||||
247
common/Tnb.Common/Redis/RedisData.cs
Normal file
247
common/Tnb.Common/Redis/RedisData.cs
Normal file
@@ -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<RedisOptions>("Redis", true);
|
||||
_instance = new CSRedis.CSRedisClient(string.Format(_RedisOptions.RedisConnectionString, _RedisOptions.ip, _RedisOptions.port, _RedisOptions.password));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用于在 key 存在时删除 key.
|
||||
/// </summary>
|
||||
/// <param name="key">键.</param>
|
||||
public long Del(params string[] key)
|
||||
{
|
||||
return _instance.Del(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用于在 key 存在时删除 key.
|
||||
/// </summary>
|
||||
/// <param name="key">键.</param>
|
||||
public Task<long> DelAsync(params string[] key)
|
||||
{
|
||||
return _instance.DelAsync(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用于在 key 模板存在时删除.
|
||||
/// </summary>
|
||||
/// <param name="pattern">key模板.</param>
|
||||
public async Task<long> 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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检查给定 key 是否存在.
|
||||
/// </summary>
|
||||
/// <param name="key">键.</param>
|
||||
public bool Exists(string key)
|
||||
{
|
||||
return _instance.Exists(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检查给定 key 是否存在.
|
||||
/// </summary>
|
||||
/// <param name="key">键.</param>
|
||||
public Task<bool> ExistsAsync(string key)
|
||||
{
|
||||
return _instance.ExistsAsync(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定 key 的增量值.
|
||||
/// </summary>
|
||||
/// <param name="key">键.</param>
|
||||
/// <param name="incrBy">增量.</param>
|
||||
/// <returns></returns>
|
||||
public long Incrby(string key, long incrBy)
|
||||
{
|
||||
return _instance.IncrBy(key, incrBy);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定 key 的增量值.
|
||||
/// </summary>
|
||||
/// <param name="key">键.</param>
|
||||
/// <param name="incrBy">增量.</param>
|
||||
/// <returns></returns>
|
||||
public Task<long> IncrbyAsync(string key, long incrBy)
|
||||
{
|
||||
return _instance.IncrByAsync(key, incrBy);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定 key 的值.
|
||||
/// </summary>
|
||||
/// <param name="key">键.</param>
|
||||
public string Get(string key)
|
||||
{
|
||||
return _instance.Get(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定 key 的值.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">byte[] 或其他类型.</typeparam>
|
||||
/// <param name="key">键.</param>
|
||||
public T Get<T>(string key)
|
||||
{
|
||||
return _instance.Get<T>(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定 key 的值.
|
||||
/// </summary>
|
||||
/// <param name="key">键.</param>
|
||||
/// <returns></returns>
|
||||
public Task<string> GetAsync(string key)
|
||||
{
|
||||
return _instance.GetAsync(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定 key 的值.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">byte[] 或其他类型.</typeparam>
|
||||
/// <param name="key">键.</param>
|
||||
public Task<T> GetAsync<T>(string key)
|
||||
{
|
||||
return _instance.GetAsync<T>(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置指定 key 的值,所有写入参数object都支持string | byte[] | 数值 | 对象.
|
||||
/// </summary>
|
||||
/// <param name="key">键.</param>
|
||||
/// <param name="value">值.</param>
|
||||
public bool Set(string key, object value)
|
||||
{
|
||||
return _instance.Set(key, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置指定 key 的值,所有写入参数object都支持string | byte[] | 数值 | 对象.
|
||||
/// </summary>
|
||||
/// <param name="key">键.</param>
|
||||
/// <param name="value">值.</param>
|
||||
/// <param name="expire">有效期.</param>
|
||||
public bool Set(string key, object value, TimeSpan expire)
|
||||
{
|
||||
return _instance.Set(key, value, expire);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置指定 key 的值,所有写入参数object都支持string | byte[] | 数值 | 对象.
|
||||
/// </summary>
|
||||
/// <param name="key">键.</param>
|
||||
/// <param name="value">值.</param>
|
||||
public Task<bool> SetAsync(string key, object value)
|
||||
{
|
||||
return _instance.SetAsync(key, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 保存.
|
||||
/// </summary>
|
||||
/// <param name="key">键.</param>
|
||||
/// <param name="value">值.</param>
|
||||
/// <param name="expire">过期时间.</param>
|
||||
/// <returns></returns>
|
||||
public Task<bool> SetAsync(string key, object value, TimeSpan expire)
|
||||
{
|
||||
return _instance.SetAsync(key, value, expire);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 只有在 key 不存在时设置 key 的值.
|
||||
/// </summary>
|
||||
/// <param name="key">键.</param>
|
||||
/// <param name="value">值.</param>
|
||||
/// <param name="expire">有效期.</param>
|
||||
public bool SetNx(string key, object value, TimeSpan expire)
|
||||
{
|
||||
if (_instance.SetNx(key, value))
|
||||
{
|
||||
_instance.Set(key, value, expire);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 只有在 key 不存在时设置 key 的值.
|
||||
/// </summary>
|
||||
/// <param name="key">键.</param>
|
||||
/// <param name="value">值.</param>
|
||||
public bool SetNx(string key, object value)
|
||||
{
|
||||
return _instance.SetNx(key, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有key.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public List<string> GetAllKeys()
|
||||
{
|
||||
return _instance.Keys("*").ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取缓存过期时间.
|
||||
/// </summary>
|
||||
/// <param name="key">键值.</param>
|
||||
/// <returns></returns>
|
||||
public DateTime GetCacheOutTime(string key)
|
||||
{
|
||||
long second = _instance.PTtl(key);
|
||||
return DateTime.Now.AddMilliseconds(second);
|
||||
}
|
||||
public Task<string> GetHash(string key, string field)
|
||||
{
|
||||
return _instance.HGetAsync(key, field);
|
||||
}
|
||||
public Task<bool> HashExist(string key, string field)
|
||||
{
|
||||
return _instance.HExistsAsync(key, field);
|
||||
}
|
||||
public Task<Dictionary<string, string>> HGetAll(string key)
|
||||
{
|
||||
return _instance.HGetAllAsync(key);
|
||||
}
|
||||
public Task<bool> HSet(string key, string field, string value)
|
||||
{
|
||||
return _instance.HSetAsync(key, field, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
34
common/Tnb.Common/Redis/RedisOptions.cs
Normal file
34
common/Tnb.Common/Redis/RedisOptions.cs
Normal file
@@ -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
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Redis配置.
|
||||
/// </summary>
|
||||
public string RedisConnectionString { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 服务器地址.
|
||||
/// </summary>
|
||||
public string ip { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 端口.
|
||||
/// </summary>
|
||||
public int port { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 密码.
|
||||
/// </summary>
|
||||
public string password { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user