Files
tnb.server/ProductionMgr/Tnb.ProductionMgr/RedisBackGround.cs
2023-11-22 16:02:09 +08:00

232 lines
9.4 KiB
C#

using Aop.Api.Domain;
using JNPF.Common.Cache;
using Microsoft.Extensions.Hosting;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SqlSugar;
using Tnb.Common.Redis;
using Tnb.ProductionMgr.Entities;
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? Readtimer;
private readonly RedisData _redisData;
private readonly IPrdInstockService _prdInstockService;
private readonly ISqlSugarRepository<RedisReadConfig> _repository;
public RedisBackGround(RedisData redisData, IPrdInstockService prdInstockService, ISqlSugarRepository<RedisReadConfig> repository)
{
_redisData = redisData;
_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)
{
try
{
string[] strs = new string[1] { "TY4C-ZHUSU1" };
string sign = "agvMode";
foreach (string s in strs)
{
Dictionary<string, string> dic = _redisData.HGetAll(s).Result;
foreach (KeyValuePair<string, string> kv in dic)
{
if (!kv.Key.Contains(sign))
{
continue;
}
JObject? res = JsonConvert.DeserializeObject<JObject>(kv.Value);
if (res != null && res["Value"] != null)
{
if (int.Parse(res["Value"]!.ToString()) is not ((int)Eagvmode.) and not ((int)Eagvmode.))
{
InstockInput instockInput = new()
{
equip_code = res["DevName"]!.ToString(),
label_code = res["TagName"]!.ToString()
};
_prdInstockService.InstockTypeOne(instockInput);
}
}
}
}
}
catch (Exception)
{
}
}
//获取挤出装箱状态
private void GetJCPackStatus(object state)
{
try
{
string[] strs = new string[1] { "TY4C-SHUSONG-JC" };
string sign = "AGVFullCall";
foreach (string s in strs)
{
Dictionary<string, string> dic = _redisData.HGetAll(s).Result;
foreach (KeyValuePair<string, string> kv in dic)
{
if (!kv.Key.Contains(sign))
{
continue;
}
JObject? res = JsonConvert.DeserializeObject<JObject>(kv.Value);
if (res != null && res["Value"] != null)
{
if (res.Value<bool>("Value"))
{
InstockInput instockInput = new()
{
equip_code = res["DevName"]!.ToString(),
label_code = res["TagName"]!.ToString()
};
_prdInstockService.InstockTypeOne(instockInput);
}
}
}
}
}
catch (Exception)
{
}
}
//获取限位状态
private void GetLimitStatus(object state)
{
/*
Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss}");
Console.WriteLine($"获取挤出装箱状态");
string data = _redisData.GetHash("TY4C-SHUSONG-JC", "AGVFullCall").Result;
Console.WriteLine(data);*/
try
{
string[] strs = new string[1] { "TY4C-WAIBAO" };
string sign = "AGVCall";
foreach (string s in strs)
{
Dictionary<string, string> dic = _redisData.HGetAll(s).Result;
foreach (KeyValuePair<string, string> kv in dic)
{
if (!kv.Key.Contains(sign))
{
continue;
}
JObject? res = JsonConvert.DeserializeObject<JObject>(kv.Value);
if (res != null && res["Value"] != null)
{
if (res.Value<bool>("Value"))
{
InstockInput instockInput = new()
{
equip_code = res["DevName"]!.ToString(),
label_code = res["TagName"]!.ToString()
};
_prdInstockService.InstockOutPack(instockInput);
}
}
}
}
}
catch (Exception)
{
}
}
public void Dispose()
{
Readtimer?.Dispose();
}
public Task StartAsync(CancellationToken cancellationToken)
{
Readtimer = new Timer(GetRedisData, null, TimeSpan.Zero, TimeSpan.FromSeconds(60));
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
}
}