DeviceInfoController.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using FreeRedis;
  2. using MessagePack.Resolvers;
  3. using MessagePack;
  4. using Microsoft.AspNetCore.Mvc;
  5. using WMS.BZWeb.Extensions;
  6. using WCS.Protocol.HJ.DataStructure;
  7. namespace WMS.BZWeb.Areas.CPManager.Controllers
  8. {
  9. [Area("CPManager")]
  10. public class DeviceInfoController : MvcControllerBase
  11. {
  12. private readonly RedisClient _CPRedis;
  13. public DeviceInfoController(IServiceProvider serviceProvider )
  14. {
  15. MessagePackSerializer.DefaultOptions = StandardResolver.Options.WithCompression(MessagePackCompression.Lz4Block);
  16. var redisDict = serviceProvider.GetRequiredService<Dictionary<string, RedisClient>>();
  17. if (redisDict.Any())
  18. {
  19. _CPRedis = redisDict["CPRedis"];
  20. _CPRedis.Serialize = obj => MessagePackSerializer.Serialize(obj);// JsonConvert.SerializeObject(obj);
  21. _CPRedis.DeserializeRaw = (bytes, type) => MessagePackSerializer.Deserialize(type, bytes);// JsonConvert.DeserializeObject(json, type);
  22. }
  23. }
  24. public IActionResult StationDetail()
  25. {
  26. return View();
  27. }
  28. public ActionResult GetDeviceData(string keyValue)
  29. {
  30. if (string.IsNullOrEmpty(keyValue))
  31. {
  32. return Fail("设备编号不能为空!");
  33. }
  34. var deviceDataPack = _CPRedis.Get<DeviceDataPack>("DeviceDataPack");
  35. //DeviceDataPack dp = new DeviceDataPack();
  36. if (deviceDataPack != null && deviceDataPack.StationDatas != null && deviceDataPack.StationDatas.Datas != null
  37. && deviceDataPack.StationDatas.Datas.Count() > 0)
  38. {
  39. var data = deviceDataPack.StationDatas.Datas.FirstOrDefault<StationData>(o => o.Code == keyValue);
  40. if (data != null)
  41. {
  42. var result = data.GetAttributesDBDetail();
  43. return Success(result);
  44. }
  45. }
  46. if (deviceDataPack != null && deviceDataPack.SRMDatas != null && deviceDataPack.SRMDatas.Datas != null
  47. && deviceDataPack.SRMDatas.Datas.Count() > 0)
  48. {
  49. var data = deviceDataPack.SRMDatas.Datas.FirstOrDefault<SRMData>(o => o.Code == keyValue?.ToUpper());
  50. if (data != null)
  51. {
  52. var result = data.GetAttributesDBDetail();
  53. return Success(result);
  54. }
  55. }
  56. return Fail("没有数据!");
  57. }
  58. }
  59. }