redis获取数采数据
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Tnb.ProductionMgr.Interfaces
|
||||
{
|
||||
public interface IRedisDataService
|
||||
{
|
||||
/// <summary>
|
||||
/// 根据机号获取重量
|
||||
/// </summary>
|
||||
Task<dynamic> GetWeight(string key, string field);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
47
ProductionMgr/Tnb.ProductionMgr/RedisDataService.cs
Normal file
47
ProductionMgr/Tnb.ProductionMgr/RedisDataService.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using System.Collections;
|
||||
using JNPF.Common.Cache;
|
||||
using JNPF.Common.Manager;
|
||||
using JNPF.DependencyInjection;
|
||||
using JNPF.DynamicApiController;
|
||||
using JNPF.FriendlyException;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Tnb.ProductionMgr.Interfaces;
|
||||
|
||||
namespace Tnb.ProductionMgr
|
||||
{
|
||||
/// <summary>
|
||||
/// 数据采集
|
||||
/// </summary>
|
||||
[ApiDescriptionSettings(Tag = ModuleConst.Tag, Area = ModuleConst.Area, Order = 700)]
|
||||
[Route("api/[area]/[controller]/[action]")]
|
||||
public class RedisDataService: IRedisDataService, IDynamicApiController, ITransient
|
||||
{
|
||||
private readonly RedisCache _redisCache;
|
||||
public RedisDataService(RedisCache redisCache)
|
||||
{
|
||||
_redisCache = redisCache;
|
||||
}
|
||||
/// <summary>
|
||||
/// 根据机号获取重量
|
||||
/// </summary>
|
||||
[HttpPost]
|
||||
public async Task<dynamic> GetWeight(string device, string jihao)
|
||||
{
|
||||
decimal result = 0;
|
||||
if (!_redisCache.HashExist(device, jihao))
|
||||
throw Oops.Bah("没有找到" + device + "----" + jihao + "的数据");
|
||||
var data = _redisCache.GetHash(device, jihao);
|
||||
|
||||
var res = JsonConvert.DeserializeObject<JObject>(data);
|
||||
if (res != null && res["Value"] != null)
|
||||
{
|
||||
result=decimal.Parse( res["Value"]!.ToString());
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -224,4 +224,12 @@ public class RedisCache : ICache, ISingleton
|
||||
long second = RedisHelper.PTtl(key);
|
||||
return DateTime.Now.AddMilliseconds(second);
|
||||
}
|
||||
public string GetHash(string key, string field)
|
||||
{
|
||||
return RedisHelper.HGet(key, field);
|
||||
}
|
||||
public bool HashExist(string key, string field)
|
||||
{
|
||||
return RedisHelper.HExists(key, field);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user