You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

111 lines
4.2 KiB

  1. using DataVAPI.Model;
  2. using DataVAPI.Tool.IOT;
  3. using DataVAPI.Tool.控制台显示;
  4. using DataVAPI.UpAndDown;
  5. using Microsoft.AspNetCore.Mvc;
  6. using System.Collections.Generic;
  7. using System.Net.Http;
  8. using System.Text;
  9. namespace DataVAPI.Controllers
  10. {
  11. /// <summary>
  12. /// 老IOT设备查询API接口
  13. /// </summary>
  14. public class IOTDeviceController : BaseController
  15. {
  16. /// <summary>
  17. /// 根据设备名称查询设备状态
  18. /// </summary>
  19. [HttpGet]
  20. public JsonMsg<ReceiveModel> GetDeviceProperty(string name)
  21. {
  22. string st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  23. try
  24. {
  25. if (string.IsNullOrEmpty(name))
  26. {
  27. return JsonMsg<ReceiveModel>.Error(null, st, "设备名称不能为空"); ;
  28. }
  29. else
  30. {
  31. //DataVModel dataV = SqlContext<DataVModel>.Base.GetInfo(name);
  32. //return JsonMsg<ReceiveModel>.OK(Tools.JsonToObjectTools<ReceiveModel>(dataV?.json),st);
  33. return JsonMsg<ReceiveModel>.Error(null, st, "设备名称不能为空"); ;
  34. }
  35. }
  36. catch (System.Exception ex)
  37. {
  38. return JsonMsg<ReceiveModel>.Error(null, ex.Message);
  39. }
  40. }
  41. /// <summary>
  42. /// 命令设置设备属性或命令控制
  43. /// </summary>
  44. [HttpPost]
  45. public JsonMsg<CommandModel> SetDeviceProperty([FromBody] CommandModel usersModel)
  46. {
  47. string st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  48. try
  49. {
  50. HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(Tools.JsonConvertTools<CommandModel>(usersModel), Encoding.GetEncoding("UTF-8"), "application/json") };
  51. if (usersModel == null || string.IsNullOrEmpty(usersModel.deviceName))
  52. {
  53. return JsonMsg<CommandModel>.Error(null, st, "设备名称不能为空"); ;
  54. }
  55. else
  56. {
  57. string PubTopic = "/broadcast/" + "grgpECHSL7q" + "/" + usersModel.deviceName + "_SetDevice";
  58. ProcessServer.Instance.Publish(PubTopic, usersModel);
  59. return JsonMsg<CommandModel>.OK(usersModel, st);
  60. }
  61. }
  62. catch (System.Exception ex)
  63. {
  64. return JsonMsg<CommandModel>.Error(null, st, ex.Message); ;
  65. }
  66. }
  67. /// <summary>
  68. /// 命令设置设备属性或命令控制
  69. /// </summary>
  70. /// <param name="deviceName">设备名称</param>
  71. /// <param name="CommandName">命令名称:0 控制类 1 设置属性 2 通知信息类</param>
  72. /// <param name="CommandKey">命令变量:执行变量 key为属性或时间 </param>
  73. /// <param name="CommandValue">命令变量:value为值或者消息</param>
  74. /// <returns></returns>
  75. [HttpGet]
  76. public JsonMsg<CommandModel> SetDeviceProperty(string deviceName, int CommandName, string CommandKey, string CommandValue)
  77. {
  78. string st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  79. try
  80. {
  81. if (string.IsNullOrEmpty(deviceName))
  82. {
  83. return JsonMsg<CommandModel>.Error(null, st, "设备名称不能为空"); ;
  84. }
  85. else
  86. {
  87. string PubTopic = "/broadcast/" + "grgpECHSL7q" + "/" + deviceName + "_SetDevice";
  88. Dictionary<string, string> keyValuePair = new Dictionary<string, string>();
  89. keyValuePair.Add(CommandKey, CommandValue);
  90. CommandModel command = new CommandModel() { CommandName = CommandName, deviceName = deviceName, CommandValue = keyValuePair };
  91. ProcessServer.Instance.Publish(PubTopic, command);
  92. return JsonMsg<CommandModel>.OK(command, st);
  93. }
  94. }
  95. catch (System.Exception ex)
  96. {
  97. return JsonMsg<CommandModel>.Error(null, st, ex.Message); ;
  98. }
  99. }
  100. }
  101. }