@Transactional(readOnly = false)
public IotControlCommandRecord ControlSendJidiPost(IotControlDeviceData iotControlDeviceData, String URL) {
StringBuilder result = new StringBuilder();
BufferedReader in = null;
IotControlCommandRecord iotControlCommandRecord = new IotControlCommandRecord();
try {
logger.info(URL);
iotControlCommandRecord.setMsg("通信中间件连接异常!");
URL url = new URL(URL);
// 打开url连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// 设置url请求方式 ‘get’ 或者 ‘post’
connection.setRequestMethod("POST");
// 发送
in = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
String line = "";
while ((line = in.readLine()) != null) {
result.append(line);
}
logger.info(result.toString());
String str = "false";
if (result.toString().contains(str)) {
return iotControlCommandRecord;
} else {
iotControlCommandRecord.setMsg("控制命令已发送");
iotControlCommandRecord.setC(iotControlDeviceData.getCtlsn());
iotControlCommandRecord.setG(iotControlDeviceData.getGatewaysn());
iotControlCommandRecord.setE(iotControlDeviceData.getDevno());
// iotControlCommandRecord.setT(Integer.valueOf(iotControlDeviceData.getT() +
// ""));
if (iotControlDeviceData.getState()) {
iotControlCommandRecord.setM("1");
} else {
iotControlCommandRecord.setM("0");
}
iotControlCommandRecordService.save(iotControlCommandRecord);
}
} catch (IOException e) {
logger.error("URL 异常:" + e.getMessage());
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
logger.error("关闭流失败" + e.getMessage());
}
}
return iotControlCommandRecord;
}
public String SetJiDiUrl(CtdDevice ctdDevice, String switchData, Integer slaveId) {
// "http://222.143.21.116:8085/CTDFishing/mqtt/sendDeviceControlCommand?deviceID=0000010&deviceKey=NJcasyml&msgType=4&msgData.addr="+
// id + "&msgData.dout="+addr,
String url = Global.getConfig("JiDiURL") + "sendDeviceControlCommand?deviceID=" + ctdDevice.getDeviceId()
+ "&deviceKey=" + ctdDevice.getDevicekey() + "&msgType=4&msgData.addr=" + slaveId + "&msgData.dout="
+ switchData;
return url;
}
这是一个发送远程控制命令控制设备的程序,先拼接出来一个字符串url,