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 { /// /// 数据采集 /// [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 _repository; private readonly ElevatorControlConfiguration _eleCtlCfg = App.Configuration.Build(); public OverideVisualDevFunc OverideFuncs { get; } = new OverideVisualDevFunc(); public RedisDataService(RedisData redisData, ISqlSugarRepository repository) { _redisData = redisData; _repository = repository; OverideFuncs.CreateAsync = Create; } private async Task 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); } /// /// 根据机号获取重量 /// [HttpPost] public async Task GetWeight(string device, string jihao) { // EqpDaq eqpDaq1 = await _repository.AsSugarClient().Queryable(). // LeftJoin((x,y)=>x.equip_id==y.id). // Where((x,y) => y.code == device && x.label_name.Contains("允许称重")).FirstAsync(); // if (eqpDaq1 == null) // { // throw Oops.Bah("未在数据采集中设置允许称重标签点"); // } EqpDaq eqpDaq2 = await _repository.AsSugarClient().Queryable(). LeftJoin((x,y)=>x.equip_id==y.id). Where((x,y) => y.code == device && x.label_name.Contains("当前重量")).FirstAsync(); if (eqpDaq2 == null) { throw Oops.Bah("未在数据采集中设置当前重量标签点"); } string eqpcode = device; device = eqpDaq2.equip_code; // Dictionary 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); // Log.Information($"{device},{eqpDaq1.label_name}确认称重返回:{str}"); 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(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(). // LeftJoin((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 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++) // { // string responseresult = await HttpClientHelper.GetRequestAsync(_eleCtlCfg.WriteTagUrl, dicCommand2,false); // Log.Information($"{device},{eqpDaq3.label_name}称重完成返回结果:{responseresult}"); // } // } int equipStatus = await _redisData.TryGetValueByKeyField(device, "1 启动 2 停止 3手动 4 急停"); Log.Information($"换箱机状态:{equipStatus}"); if (equipStatus.ToString() != "1") { if (equipStatus.ToString().Trim() == "2") { throw Oops.Bah("换箱机状态为停止"); } if (equipStatus.ToString().Trim() == "3") { throw Oops.Bah("换箱机状态为手动"); } if (equipStatus.ToString().Trim() == "4") { throw Oops.Bah("换箱机状态为急停"); } } return result; } } }