redis读取可配置
This commit is contained in:
@@ -0,0 +1,49 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using JNPF.Common.Contracts;
|
||||||
|
using JNPF.Common.Security;
|
||||||
|
using SqlSugar;
|
||||||
|
|
||||||
|
namespace Tnb.ProductionMgr.Entities
|
||||||
|
{
|
||||||
|
[SugarTable("base_redis_read_config")]
|
||||||
|
public class RedisReadConfig : BaseEntity<string>
|
||||||
|
{
|
||||||
|
public RedisReadConfig()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// 设备名
|
||||||
|
/// </summary>
|
||||||
|
public string? dev_name { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 标签名
|
||||||
|
/// </summary>
|
||||||
|
public string? tag_name { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 事件类型1注塑装箱2挤出装箱3限位
|
||||||
|
/// </summary>
|
||||||
|
public int event_type { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 数据类型1bool2int
|
||||||
|
/// </summary>
|
||||||
|
public int data_type { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 判断类型1相等2包含
|
||||||
|
/// </summary>
|
||||||
|
|
||||||
|
public int check_type { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 数据
|
||||||
|
/// </summary>
|
||||||
|
public string? data { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 启用
|
||||||
|
/// </summary>
|
||||||
|
public int enabled { get; set; }
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,4 +8,20 @@
|
|||||||
空满框请求 = 3,
|
空满框请求 = 3,
|
||||||
收到请求 = 4
|
收到请求 = 4
|
||||||
}
|
}
|
||||||
|
public enum EventType
|
||||||
|
{
|
||||||
|
注塑空满箱请求=1,
|
||||||
|
挤出空满箱请求=2,
|
||||||
|
限位请求=3
|
||||||
|
}
|
||||||
|
public enum DataType
|
||||||
|
{
|
||||||
|
BOOL = 1,
|
||||||
|
INT = 2
|
||||||
|
}
|
||||||
|
public enum CheckType
|
||||||
|
{
|
||||||
|
相等 = 1,
|
||||||
|
包含 = 2
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
using JNPF.Common.Cache;
|
using Aop.Api.Domain;
|
||||||
|
using JNPF.Common.Cache;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
|
using SqlSugar;
|
||||||
using Tnb.Common.Redis;
|
using Tnb.Common.Redis;
|
||||||
|
using Tnb.ProductionMgr.Entities;
|
||||||
using Tnb.ProductionMgr.Entities.Dto;
|
using Tnb.ProductionMgr.Entities.Dto;
|
||||||
using Tnb.ProductionMgr.Entities.Enums;
|
using Tnb.ProductionMgr.Entities.Enums;
|
||||||
using Tnb.ProductionMgr.Interfaces;
|
using Tnb.ProductionMgr.Interfaces;
|
||||||
@@ -12,17 +15,93 @@ namespace Tnb.ProductionMgr
|
|||||||
//redis定时获取数采数据
|
//redis定时获取数采数据
|
||||||
public class RedisBackGround : IHostedService, IDisposable
|
public class RedisBackGround : IHostedService, IDisposable
|
||||||
{
|
{
|
||||||
private Timer? ZSpacktimer;
|
private Timer? Readtimer;
|
||||||
private Timer? JCpacktimer;
|
|
||||||
private Timer? limittimer;
|
|
||||||
private readonly RedisData _redisData;
|
private readonly RedisData _redisData;
|
||||||
private readonly IPrdInstockService _prdInstockService;
|
private readonly IPrdInstockService _prdInstockService;
|
||||||
public RedisBackGround(RedisData redisData, IPrdInstockService prdInstockService)
|
private readonly ISqlSugarRepository<RedisReadConfig> _repository;
|
||||||
|
public RedisBackGround(RedisData redisData, IPrdInstockService prdInstockService, ISqlSugarRepository<RedisReadConfig> repository)
|
||||||
{
|
{
|
||||||
_redisData = redisData;
|
_redisData = redisData;
|
||||||
_prdInstockService = prdInstockService;
|
_prdInstockService = prdInstockService;
|
||||||
|
_repository = repository;
|
||||||
}
|
}
|
||||||
//获取注塑装箱状态
|
//获取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<JObject>(json);
|
||||||
|
if (config.data_type == (int)DataType.INT)
|
||||||
|
{
|
||||||
|
if (config.check_type == (int)CheckType.相等)
|
||||||
|
{
|
||||||
|
if (res.Value<int>("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<int>("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<bool>("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.InstockTypeOne(instockInput);
|
||||||
|
break;
|
||||||
|
case EventType.限位请求:
|
||||||
|
_prdInstockService.InstockOutPack(instockInput);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//获取注塑装箱状态
|
||||||
private void GetZSPackStatus(object state)
|
private void GetZSPackStatus(object state)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@@ -137,15 +216,11 @@ namespace Tnb.ProductionMgr
|
|||||||
}
|
}
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
ZSpacktimer?.Dispose();
|
Readtimer?.Dispose();
|
||||||
JCpacktimer?.Dispose();
|
|
||||||
limittimer?.Dispose();
|
|
||||||
}
|
}
|
||||||
public Task StartAsync(CancellationToken cancellationToken)
|
public Task StartAsync(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
ZSpacktimer = new Timer(GetZSPackStatus, null, TimeSpan.Zero, TimeSpan.FromSeconds(5));
|
Readtimer = new Timer(GetRedisData, null, TimeSpan.Zero, TimeSpan.FromSeconds(5));
|
||||||
JCpacktimer = new Timer(GetJCPackStatus, null, TimeSpan.Zero, TimeSpan.FromSeconds(5));
|
|
||||||
limittimer = new Timer(GetLimitStatus, null, TimeSpan.Zero, TimeSpan.FromSeconds(5));
|
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
public Task StopAsync(CancellationToken cancellationToken)
|
public Task StopAsync(CancellationToken cancellationToken)
|
||||||
|
|||||||
Reference in New Issue
Block a user