HttpUtil.java
6.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package com.example.mypulsar.utils;
import com.example.mypulsar.bean.Command;
import com.example.mypulsar.bean.TuYaAirCondition;
import com.example.mypulsar.bean.TuYaCommand;
import com.example.mypulsar.task.SchduledTasks;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;
/**
* http 工具类
*/
@Slf4j
public class HttpUtil {
public static String post(String requestUrl, String accessToken, String params)
throws Exception {
String contentType = "application/json";
return HttpUtil.post(requestUrl, accessToken, contentType, params);
}
public static String post(String requestUrl, String accessToken, String contentType, String params)
throws Exception {
String encoding = "UTF-8";
if (requestUrl.contains("nlp")) {
encoding = "GBK";
}
return HttpUtil.post(requestUrl, accessToken, contentType, params, encoding);
}
public static String post(String requestUrl, String accessToken, String contentType, String params, String encoding)
throws Exception {
String url = requestUrl + "?access_token=" + accessToken;
return HttpUtil.postGeneralUrl(url, contentType, params, encoding);
}
public static String postGeneralUrl(String generalUrl, String contentType, String params, String encoding)
throws Exception {
URL url = new URL(generalUrl);
// 打开和URL之间的连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
// 设置通用的请求属性
connection.setRequestProperty("Content-Type", contentType);
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setUseCaches(false);
connection.setDoOutput(true);
connection.setDoInput(true);
// 得到请求的输出流对象
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.write(params.getBytes(encoding));
out.flush();
out.close();
// 建立实际的连接
connection.connect();
// 获取所有响应头字段
Map<String, List<String>> headers = connection.getHeaderFields();
// 遍历所有的响应头字段
for (String key : headers.keySet()) {
System.err.println(key + "--->" + headers.get(key));
}
// 定义 BufferedReader输入流来读取URL的响应
BufferedReader in = null;
in = new BufferedReader(
new InputStreamReader(connection.getInputStream(), encoding));
String result = "";
String getLine;
while ((getLine = in.readLine()) != null) {
result += getLine;
}
in.close();
// System.err.println("result:" + result);
return result;
}
public static boolean controlDev(String deviceId, String code, String value) {
String url = "http://120.26.116.253:5555/TuYa/sendDeviceCommandsFun?deviceId=" + deviceId;
RestTemplate restTemplate = new RestTemplate();
TuYaCommand tuYaCommand = new TuYaCommand();
List<Command> commandList = new ArrayList<>();
Command command = new Command();
command.setCode(code);
command.setValue(value);
commandList.add(command);
tuYaCommand.setCommands(commandList);
String responseEntity = restTemplate.postForObject(url, tuYaCommand, String.class);
log.info("tuYaCommand:" + tuYaCommand.toString() + "----deviceId:" + deviceId);
log.info("responseEntity:" + responseEntity);
return responseEntity.equals("1");
}
public static boolean IsDeviceOnline(String deviceId) {
String url = "http://121.40.109.21:8089/file/getCard";
RestTemplate restTemplate = new RestTemplate();
Map<String, Object> map = new HashMap<>();
map.put("deviceId", deviceId);
String res = restTemplate.getForObject(url, String.class, map);
return res.equals("1");
}
/**
* 控制设备
*
* @param conValue 控制指令
* @param conDevId 被控制设备id
* @param conDevName 设备名称
* @return
*/
public static String addAirCode(String conValue, String conDevId, String conDevName) {
try {
String url = String.format("http://120.26.116.253:5555/TuYa/%s/addAirCode", conDevId);
RestTemplate restTemplate = new RestTemplate();
TuYaAirCondition tuYaAirCondition = new TuYaAirCondition();
String power = "";
try {
JSONObject jsonObject = new JSONObject(conValue);
power = jsonObject.getString("power");
tuYaAirCondition.setMode(jsonObject.getString("mode"));
tuYaAirCondition.setPower(power);
tuYaAirCondition.setRemote_id(jsonObject.getString("remote_id"));
tuYaAirCondition.setWind(jsonObject.getString("wind"));
tuYaAirCondition.setTemp(jsonObject.getString("temp"));
tuYaAirCondition.setRemote_index(jsonObject.getString("remote_index"));
} catch (JSONException e) {
log.info("控制失败:失败信息",e);
}
log.info("开始发送遥控命令至设备:请求地址: {},设备名称: {},设备ID: {},请求参数: {}",url,conDevName,conDevId,tuYaAirCondition.toString());
String response = restTemplate.postForObject(url, tuYaAirCondition, String.class);
log.info("返回结果:{}",response);
// FileUtils.getInstance().writeLogs(String.format("%s设备%s--控制%s", power.equals("1") ? "成功" : "失败", conDevName + "-" + conDevId, response), FileUtils.addAirCode);
return response;
}catch (Exception e){
log.info("请求红外控制指令失败:{}",e);
if(SchduledTasks.conDevMap.containsKey(conDevId)){
SchduledTasks.conDevMap.remove(conDevId);
}
}
return null;
}
}