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.
 
 

115 lines
4.5 KiB

  1. using DataVAPI.Model;
  2. using DataVAPI.Tool.IOT;
  3. using DataVAPI.Tool.控制台显示;
  4. using Microsoft.AspNetCore.Mvc;
  5. using System.Collections.Generic;
  6. using System.Net.Http;
  7. using System.Text;
  8. namespace DataVAPI.Controllers
  9. {
  10. /// <summary>
  11. /// 老IOT设备查询API接口
  12. /// </summary>
  13. public class IOTDeviceController : BaseController
  14. {
  15. /// <summary>
  16. /// 根据设备名称查询设备状态
  17. /// </summary>
  18. [HttpGet]
  19. public JsonMsg<ReceiveModel> GetDeviceProperty(string name)
  20. {
  21. string st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  22. try
  23. {
  24. if (string.IsNullOrEmpty(name))
  25. {
  26. return JsonMsg<ReceiveModel>.Error(null, st, "设备名称不能为空"); ;
  27. }
  28. else
  29. {
  30. //DataVModel dataV = SqlContext<DataVModel>.Base.GetInfo(name);
  31. //return JsonMsg<ReceiveModel>.OK(Tools.JsonToObjectTools<ReceiveModel>(dataV?.json),st);
  32. return JsonMsg<ReceiveModel>.Error(null, st, "设备名称不能为空"); ;
  33. }
  34. }
  35. catch (System.Exception ex)
  36. {
  37. return JsonMsg<ReceiveModel>.Error(null, ex.Message);
  38. }
  39. }
  40. /// <summary>
  41. /// 命令设置设备属性或命令控制
  42. /// </summary>
  43. [HttpPost]
  44. public JsonMsg<CommandModel> SetDeviceProperty([FromBody] CommandModel usersModel)
  45. {
  46. string st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  47. try
  48. {
  49. HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(Tools.JsonConvertTools<CommandModel>(usersModel), Encoding.GetEncoding("UTF-8"), "application/json") };
  50. if (usersModel == null || string.IsNullOrEmpty(usersModel.deviceName))
  51. {
  52. return JsonMsg<CommandModel>.Error(null, st, "设备名称不能为空"); ;
  53. }
  54. else
  55. {
  56. string PubTopic = "/broadcast/" + "grgpECHSL7q" + "/" + usersModel.deviceName + "_SetDevice";
  57. ConsoleHelper.WriteWarningLine("发送数据 " + PubTopic + Tools.JsonConvertTools<CommandModel>(usersModel));
  58. IOTDevServer.GetInstance().IOT_Publish(PubTopic, Tools.JsonConvertTools<CommandModel>(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. ConsoleHelper.WriteWarningLine("发送数据 " + PubTopic + Tools.JsonConvertTools<CommandModel>(command));
  92. IOTDevServer.GetInstance().IOT_Publish(PubTopic, Tools.JsonConvertTools<CommandModel>(command));
  93. return JsonMsg<CommandModel>.OK(command, st);
  94. }
  95. }
  96. catch (System.Exception ex)
  97. {
  98. return JsonMsg<CommandModel>.Error(null, st, ex.Message); ;
  99. }
  100. }
  101. }
  102. }