ValueController.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using Microsoft.AspNetCore.Mvc;
  2. using Microsoft.Extensions.Caching.Memory;
  3. using Microsoft.Extensions.Logging;
  4. using System.Threading.Tasks;
  5. using System;
  6. //using Ropin.Environmentally.MqttService;
  7. //using static Ropin.Environmentally.MqttService.Controllers.ValueController;
  8. using System.Collections.Generic;
  9. namespace Ropin.Environmentally.LoRaService.Controllers
  10. {
  11. [ApiController]
  12. [Route("api/[controller]")]
  13. public class ValueController : ControllerBase
  14. {
  15. private readonly ILogger<ValueController> _logger;
  16. public IMemoryCache _memoryCache;
  17. public ValueController(ILogger<ValueController> logger, IMemoryCache memoryCache)
  18. {
  19. _logger = logger;
  20. _memoryCache = memoryCache;
  21. }
  22. public class TdevDevicePointSearchModel
  23. {
  24. public string[] groupnames { get; set; }
  25. public string[] names { get; set; }
  26. public int timeOut { get; set; } = 600;
  27. }
  28. //public class DeviceCommandModel
  29. //{
  30. // public string Topic { get; set; }
  31. // public string Command { get; set; }
  32. // public int timeOut { get; set; } = 600;
  33. //}
  34. public class DevPointValue
  35. {
  36. public string id { get; set; }
  37. public string timestamp { get; set; }
  38. public string dataType { get; set; }
  39. public string name { get; set; }
  40. public string value { get; set; }
  41. public string status { get; set; }
  42. public string boxId { get; set; }
  43. public string connState { get; set; }
  44. public string connStateTimestamp { get; set; }
  45. }
  46. [HttpGet("Get")]
  47. public string Get(string boxNo)
  48. {
  49. return _memoryCache.Get(boxNo)?.ToString();
  50. }
  51. [HttpPost("GetDevicePointByAsync")]
  52. public async Task<IList<DevPointValue>> GetDevicePointByAsync([FromQuery] string boxNo, [FromBody] TdevDevicePointSearchModel searchModel)
  53. {
  54. //if (searchModel == null)
  55. //{
  56. // return new ApiResult(ReturnCode.ArgsError);
  57. //}
  58. //searchModel.IsPagination = false;
  59. IList<DevPointValue> devPoints = new List<DevPointValue>();
  60. try
  61. {
  62. for (int i = 0; i < searchModel.names.Length; i++)
  63. {
  64. string name = searchModel.names[i];
  65. string groupname = searchModel.groupnames[i];
  66. string englishname = TCPServer._mqttDevicePointNameMappingDic[boxNo + groupname + name];
  67. LoRaDevicePoint mqttDevicePoint;
  68. TCPServer._mqttDevicePointDic.TryGetValue(englishname, out mqttDevicePoint);
  69. if (mqttDevicePoint != null)
  70. devPoints.Add(new DevPointValue
  71. {
  72. id = mqttDevicePoint.Id,
  73. timestamp = mqttDevicePoint.UpLoadTime.ToString("yyyy-MM-dd hh:mm:ss"),
  74. dataType = mqttDevicePoint.DataType,
  75. name = mqttDevicePoint.ChineseName,
  76. value = mqttDevicePoint.Value,
  77. status = mqttDevicePoint.BoxStatus,
  78. boxId = mqttDevicePoint.BoxId,
  79. connState = "1",
  80. connStateTimestamp = mqttDevicePoint.UpLoadTime.ToString("yyyy-MM-dd hh:mm:ss"),
  81. });
  82. }
  83. return devPoints;
  84. }
  85. catch (Exception ex)
  86. {
  87. //return new ApiResult(ReturnCode.GeneralError, ex.Message);
  88. return null;
  89. }
  90. }
  91. }
  92. }