Files
tnb.server/ProductionMgr/Tnb.ProductionMgr/RedisBackGround.cs
qianjiawei 0cca20b0ff ctu存放
2023-12-15 16:37:13 +08:00

283 lines
12 KiB
C#

using System;
using System.Dynamic;
using System.Security.Policy;
using Aop.Api.Domain;
using JNPF;
using JNPF.Common.Cache;
using JNPF.FriendlyException;
using Microsoft.Extensions.Hosting;
using NetTaste;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Qiniu.Util;
using SqlSugar;
using Tnb.Common.Redis;
using Tnb.Common.Utils;
using Tnb.ProductionMgr.Entities;
using Tnb.ProductionMgr.Entities.Dto;
using Tnb.ProductionMgr.Entities.Enums;
using Tnb.ProductionMgr.Interfaces;
using Tnb.WarehouseMgr.Entities;
using Tnb.WarehouseMgr.Entities.Consts;
namespace Tnb.ProductionMgr
{
//redis定时获取数采数据
public class RedisBackGround : IHostedService, IDisposable
{
private Timer? Readtimer;
private Timer? CheckGettimer;
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.InstockTubeOne(instockInput);
break;
case EventType.:
_prdInstockService.InstockOutPack(instockInput);
break;
default:
break;
}
}
//ctu取货
private void CheckGet(object state)
{
Dictionary<string, string[]> getdic = new Dictionary<string, string[]>();
getdic.Add("SSX-021-005", new string[] { "CS05", "FullBoxAllowGet", "Code" });
getdic.Add("SSX-021-006", new string[] { "CS06", "FullBoxAllowGet", "Code" });
foreach (var key in getdic.Keys)
{
try
{
var strs = getdic.Where(p => p.Key == key).First().Value;
bool flag = _redisData.HashExist(strs[0], strs[1]).Result;
string data = _redisData.GetHash(strs[0], strs[1]).Result;
JObject? res = JsonConvert.DeserializeObject<JObject>(data);
bool result = res != null && res["Value"] != null ? res.Value<bool>("Value") : false;
if (result)
{
string codedata = _redisData.GetHash(strs[0], strs[2]).Result;
JObject? coderes = JsonConvert.DeserializeObject<JObject>(codedata);
string coderesult = coderes != null && coderes["Value"] != null ? coderes.Value<string>("Value")! : "";
var DistaskH= _repository.AsSugarClient().Queryable<WmsDistaskH>().Where(p => p.carry_code == coderesult && p.status == WmsWareHouseConst.TASK_BILL_STATUS_RUNING_ID).First();
dynamic reqBody = new ExpandoObject();
reqBody.taskCode = DistaskH.bill_code;
reqBody.slotCode = key;
reqBody.containerCode = coderesult;
CancellationTokenSource Ctu = new();
//{"code":500,"msg":"成功!"}
dynamic respBody = HttpClientHelper.PostStreamAsync("http://192.168.11.104:1880/wcs/notify/cargo", reqBody, Ctu.Token).Result;
Ctu.Dispose();
}
}
catch (Exception)
{
}
}
}
//获取注塑装箱状态
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();
CheckGettimer?.Dispose();
}
public Task StartAsync(CancellationToken cancellationToken)
{
// Readtimer = new Timer(GetRedisData, null, TimeSpan.Zero, TimeSpan.FromSeconds(300));
CheckGettimer = new Timer(CheckGet, null, TimeSpan.Zero, TimeSpan.FromSeconds(300));
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
}
}