NBValveControlService.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using LeaRun.Application.Code;
  2. using LeaRun.Application.Entity.NBManage;
  3. using LeaRun.Data;
  4. using LeaRun.Util;
  5. using Newtonsoft.Json.Linq;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Data;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace LeaRun.Application.Service.NBManage
  13. {
  14. public class NBValveControlService
  15. {
  16. SqlHelper sqlHelper = new SqlHelper("NBDB");
  17. public string SendCommand(string id, string commandValue, string addr, string IOT_Code,string cmdText)
  18. {
  19. try
  20. {
  21. //查询 此设备的开关阀状态(本地存储的状态)
  22. NBMeterEntity NBMeter = new NBUserManageService().GetMeterByMeterId(id);
  23. string strParams = "";
  24. //当前状态为开 发送关阀命令
  25. if ("99".Equals(cmdText))
  26. {
  27. strParams = "{\"deviceId\": \"" + IOT_Code + "\",\"command\": {\"method\": \"SET_VALVE_STATUS\",\"paras\": {\"addr\": \"" + addr + "\",\"open\":\""+0x99+ "\"},\"serviceId\": \"WaterMeterX\"},\"callbackUrl\":\"https://60.205.104.179:" + NBCommon.NB_ServerPort + "/NBManage/NBSubscribeListener/CallBackValveControl\"}";
  28. }
  29. else
  30. {
  31. strParams = "{\"deviceId\": \"" + IOT_Code + "\",\"command\": {\"method\": \"SET_VALVE_STATUS\",\"paras\": {\"addr\": \"" + addr + "\",\"open\":\""+0x55+ "\"},\"serviceId\": \"WaterMeterX\"},\"callbackUrl\":\"https://60.205.104.179:" + NBCommon.NB_ServerPort + "/NBManage/NBSubscribeListener/CallBackValveControl\"}";
  32. }
  33. //发送指令
  34. JObject jObj = NBCommon.SendCommand(strParams);
  35. if (jObj["error_code"] != null)
  36. {
  37. return "";
  38. }
  39. string commandId = jObj["commandId"].ToString();//命令ID
  40. string status = jObj["status"].ToString();//命令状态
  41. string userId = OperatorProvider.Provider.Current().UserId;//命令操作者
  42. //命令保存
  43. string insertCommandSql = "INSERT INTO RMRS_SendCmdRecord (MeterId, CmdType, CmdID, Operator,CmdResult,SendText) VALUES(" + id + "," + commandValue + ",'" + commandId + "','" + userId + "','" + status + "','" + strParams + "')";
  44. sqlHelper.ExecuteNoParams(insertCommandSql, CommandType.Text);
  45. string reJson = "{\"commandId\":\"" + commandId + "\",\"status\":\"" + status + "\"}";
  46. return reJson;
  47. }
  48. catch (Exception ex)
  49. {
  50. throw ex;
  51. }
  52. }
  53. public string SendCommandSY(string id, string commandValue, string addr, string IOT_Code, string cmdText)
  54. {
  55. try
  56. {
  57. //查询 此设备的开关阀状态(本地存储的状态)
  58. NBMeterEntity NBMeter = new NBUserManageService().GetMeterByMeterId(id);
  59. string strParams = "";
  60. //当前状态为开 发送关阀命令
  61. if ("99".Equals(cmdText))
  62. {
  63. strParams = "{\"deviceId\": \"" + IOT_Code + "\",\"command\": {\"method\": \"SWITCH_DEVICE_VALVE\",\"paras\": {\"meterEleno\": \"" + addr + "\",\"value\":\"0\"},\"serviceId\": \"WaterMeterBasic\"},\"callbackUrl\":\"https://60.205.104.179:" + NBCommon.NB_ServerPort + "/NBManage/NBSubscribeListener/CallBackValveControl\"}";
  64. }
  65. else
  66. {
  67. strParams = "{\"deviceId\": \"" + IOT_Code + "\",\"command\": {\"method\": \"SWITCH_DEVICE_VALVE\",\"paras\": {\"meterEleno\": \"" + addr + "\",\"value\":\"1\"},\"serviceId\": \"WaterMeterBasic\"},\"callbackUrl\":\"https://60.205.104.179:" + NBCommon.NB_ServerPort + "/NBManage/NBSubscribeListener/CallBackValveControl\"}";
  68. }
  69. //发送指令
  70. JObject jObj = NBCommon.SendCommand(strParams);
  71. if (jObj["error_code"] != null)
  72. {
  73. return "";
  74. }
  75. string commandId = jObj["commandId"].ToString();//命令ID
  76. string status = jObj["status"].ToString();//命令状态
  77. string userId = OperatorProvider.Provider.Current().UserId;//命令操作者
  78. //命令保存
  79. string insertCommandSql = "INSERT INTO RMRS_SendCmdRecord (MeterId, CmdType, CmdID, Operator,CmdResult,SendText) VALUES(" + id + "," + commandValue + ",'" + commandId + "','" + userId + "','" + status + "','" + strParams + "')";
  80. sqlHelper.ExecuteNoParams(insertCommandSql, CommandType.Text);
  81. string reJson = "{\"commandId\":\"" + commandId + "\",\"status\":\"" + status + "\"}";
  82. return reJson;
  83. }
  84. catch (Exception ex)
  85. {
  86. throw ex;
  87. }
  88. }
  89. }
  90. }