using LeaRun.Application.Code; using LeaRun.Application.Entity.NBManage; using LeaRun.Data; using LeaRun.Util; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LeaRun.Application.Service.NBManage { public class NBValveControlService { SqlHelper sqlHelper = new SqlHelper("NBDB"); public string SendCommand(string id, string commandValue, string addr, string IOT_Code,string cmdText) { try { //查询 此设备的开关阀状态(本地存储的状态) NBMeterEntity NBMeter = new NBUserManageService().GetMeterByMeterId(id); string strParams = ""; //当前状态为开 发送关阀命令 if ("99".Equals(cmdText)) { 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\"}"; } else { 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\"}"; } //发送指令 JObject jObj = NBCommon.SendCommand(strParams); if (jObj["error_code"] != null) { return ""; } string commandId = jObj["commandId"].ToString();//命令ID string status = jObj["status"].ToString();//命令状态 string userId = OperatorProvider.Provider.Current().UserId;//命令操作者 //命令保存 string insertCommandSql = "INSERT INTO RMRS_SendCmdRecord (MeterId, CmdType, CmdID, Operator,CmdResult,SendText) VALUES(" + id + "," + commandValue + ",'" + commandId + "','" + userId + "','" + status + "','" + strParams + "')"; sqlHelper.ExecuteNoParams(insertCommandSql, CommandType.Text); string reJson = "{\"commandId\":\"" + commandId + "\",\"status\":\"" + status + "\"}"; return reJson; } catch (Exception ex) { throw ex; } } public string SendCommandSY(string id, string commandValue, string addr, string IOT_Code, string cmdText) { try { //查询 此设备的开关阀状态(本地存储的状态) NBMeterEntity NBMeter = new NBUserManageService().GetMeterByMeterId(id); string strParams = ""; //当前状态为开 发送关阀命令 if ("99".Equals(cmdText)) { 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\"}"; } else { 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\"}"; } //发送指令 JObject jObj = NBCommon.SendCommand(strParams); if (jObj["error_code"] != null) { return ""; } string commandId = jObj["commandId"].ToString();//命令ID string status = jObj["status"].ToString();//命令状态 string userId = OperatorProvider.Provider.Current().UserId;//命令操作者 //命令保存 string insertCommandSql = "INSERT INTO RMRS_SendCmdRecord (MeterId, CmdType, CmdID, Operator,CmdResult,SendText) VALUES(" + id + "," + commandValue + ",'" + commandId + "','" + userId + "','" + status + "','" + strParams + "')"; sqlHelper.ExecuteNoParams(insertCommandSql, CommandType.Text); string reJson = "{\"commandId\":\"" + commandId + "\",\"status\":\"" + status + "\"}"; return reJson; } catch (Exception ex) { throw ex; } } } }