131 lines
5.8 KiB
C#
131 lines
5.8 KiB
C#
using System.Reflection;
|
|
using JNPF;
|
|
using JNPF.Common.Cache;
|
|
using JNPF.Common.Dtos.VisualDev;
|
|
using JNPF.Common.Security;
|
|
using JNPF.DependencyInjection;
|
|
using JNPF.DynamicApiController;
|
|
using JNPF.FriendlyException;
|
|
using JNPF.Logging;
|
|
using JNPF.VisualDev;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
using SqlSugar;
|
|
using Tnb.Common.Extension;
|
|
using Tnb.Common.Redis;
|
|
using Tnb.ProductionMgr.Entities;
|
|
using Tnb.ProductionMgr.Interfaces;
|
|
using Tnb.WarehouseMgr.Entities.Configs;
|
|
using Tnb.Common.Utils;
|
|
using Tnb.EquipMgr.Entities;
|
|
|
|
namespace Tnb.ProductionMgr
|
|
{
|
|
/// <summary>
|
|
/// 数据采集
|
|
/// </summary>
|
|
[ApiDescriptionSettings(Tag = ModuleConst.Tag, Area = ModuleConst.Area, Order = 700)]
|
|
[Route("api/[area]/[controller]/[action]")]
|
|
[OverideVisualDev(ModuleId)]
|
|
public class RedisDataService : IOverideVisualDevService, IRedisDataService, IDynamicApiController, ITransient
|
|
{
|
|
private const string ModuleId = "30062789830933";
|
|
private readonly RedisData _redisData;
|
|
private readonly ISqlSugarRepository<RedisReadConfig> _repository;
|
|
private readonly ElevatorControlConfiguration _eleCtlCfg = App.Configuration.Build<ElevatorControlConfiguration>();
|
|
public OverideVisualDevFunc OverideFuncs { get; } = new OverideVisualDevFunc();
|
|
public RedisDataService(RedisData redisData, ISqlSugarRepository<RedisReadConfig> repository)
|
|
{
|
|
_redisData = redisData;
|
|
_repository = repository;
|
|
OverideFuncs.CreateAsync = Create;
|
|
}
|
|
private async Task<dynamic> Create(VisualDevModelDataCrInput input)
|
|
{
|
|
RedisReadConfig redisReadConfig = new RedisReadConfig();
|
|
redisReadConfig.id = SnowflakeIdHelper.NextId();
|
|
redisReadConfig.dev_name = input.data[nameof(RedisReadConfig.dev_name)].ToString();
|
|
redisReadConfig.tag_name = input.data[nameof(RedisReadConfig.tag_name)].ToString();
|
|
redisReadConfig.data = input.data[nameof(RedisReadConfig.data)].ToString();
|
|
redisReadConfig.event_type = int.Parse(input.data[nameof(RedisReadConfig.event_type)].ToString()!);
|
|
redisReadConfig.data_type = int.Parse(input.data[nameof(RedisReadConfig.data_type)].ToString()!);
|
|
redisReadConfig.check_type = int.Parse(input.data[nameof(RedisReadConfig.check_type)].ToString()!);
|
|
redisReadConfig.enabled = int.Parse(input.data[nameof(RedisReadConfig.enabled)].ToString()!);
|
|
await _repository.AsSugarClient().Insertable(redisReadConfig).ExecuteCommandAsync();
|
|
return await Task.FromResult(true);
|
|
}
|
|
/// <summary>
|
|
/// 根据机号获取重量
|
|
/// </summary>
|
|
[HttpPost]
|
|
public async Task<dynamic> GetWeight(string device, string jihao)
|
|
{
|
|
EqpDaq eqpDaq1 = await _repository.AsSugarClient().Queryable<EqpDaq>().
|
|
LeftJoin<EqpEquipment>((x,y)=>x.equip_id==y.id).
|
|
Where((x,y) => y.code == device && x.label_name.Contains("允许称重")).FirstAsync();
|
|
if (eqpDaq1 == null)
|
|
{
|
|
throw Oops.Bah("未在数据采集中设置允许称重标签点");
|
|
}
|
|
|
|
string eqpcode = device;
|
|
device = eqpDaq1.equip_code;
|
|
Dictionary<string, string> dicCommand = new(StringComparer.OrdinalIgnoreCase)
|
|
{
|
|
["DevName"] = device,
|
|
["token"] = _eleCtlCfg.token,
|
|
["TagName"] = eqpDaq1.label_name,
|
|
["Value"] = "true",
|
|
};
|
|
Log.Information($"确认称重参数:{JsonConvert.SerializeObject(dicCommand)}");
|
|
var str = await HttpClientHelper.GetRequestAsync(_eleCtlCfg.WriteTagUrl, dicCommand);
|
|
|
|
EqpDaq eqpDaq2 = await _repository.AsSugarClient().Queryable<EqpDaq>().
|
|
LeftJoin<EqpEquipment>((x,y)=>x.equip_id==y.id).
|
|
Where((x,y) => y.code == eqpcode && x.label_name.Contains("当前重量")).FirstAsync();
|
|
if (eqpDaq2 == null)
|
|
{
|
|
throw Oops.Bah("未在数据采集中设置当前重量标签点");
|
|
}
|
|
|
|
bool flag = await _redisData.HashExist(device, eqpDaq2.label_name);
|
|
if (!flag)
|
|
{
|
|
throw Oops.Bah("没有找到" + device + "----" + eqpDaq2.label_name + "的数据");
|
|
}
|
|
string data = await _redisData.GetHash(device, eqpDaq2.label_name);
|
|
JObject? res = JsonConvert.DeserializeObject<JObject>(data);
|
|
|
|
|
|
decimal result = res != null && res["Value"] != null ? decimal.Parse(res["Value"]!.ToString()) : throw Oops.Bah("数据格式错误");
|
|
if (result > 0)
|
|
{
|
|
EqpDaq eqpDaq3 = await _repository.AsSugarClient().Queryable<EqpDaq>().
|
|
LeftJoin<EqpEquipment>((x,y)=>x.equip_id==y.id).
|
|
Where((x,y) => y.code == eqpcode && x.label_name.Contains("称重完成")).FirstAsync();
|
|
|
|
if (eqpDaq3 == null)
|
|
{
|
|
throw Oops.Bah("未在数据采集中设置称重完成标签点");
|
|
}
|
|
|
|
Dictionary<string, string> dicCommand2 = new(StringComparer.OrdinalIgnoreCase)
|
|
{
|
|
["DevName"] = device,
|
|
["token"] = _eleCtlCfg.token,
|
|
["TagName"] = eqpDaq3.label_name,
|
|
["Value"] = "true",
|
|
};
|
|
Log.Information($"称重完成参数:{JsonConvert.SerializeObject(dicCommand2)}");
|
|
for (int i = 0; i < 1; i++)
|
|
{
|
|
await HttpClientHelper.GetRequestAsync(_eleCtlCfg.WriteTagUrl, dicCommand2);
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
}
|
|
}
|