|
@@ -14,6 +14,10 @@ import com.welampiot.dto.*;
|
|
|
import com.welampiot.handle.MqttHandler;
|
|
|
import com.welampiot.service.*;
|
|
|
import com.welampiot.vo.DevEnumVO;
|
|
|
+import io.ipdata.client.Ipdata;
|
|
|
+import io.ipdata.client.error.IpdataException;
|
|
|
+import io.ipdata.client.model.IpdataModel;
|
|
|
+import io.ipdata.client.service.IpdataService;
|
|
|
import org.apache.commons.httpclient.HttpClient;
|
|
|
import org.apache.commons.httpclient.methods.PostMethod;
|
|
|
import org.eclipse.paho.client.mqttv3.MqttClient;
|
|
@@ -28,18 +32,18 @@ import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.web.context.request.RequestContextHolder;
|
|
|
+import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import java.io.*;
|
|
|
import java.lang.reflect.Field;
|
|
|
-import java.net.HttpURLConnection;
|
|
|
-import java.net.InetAddress;
|
|
|
-import java.net.URL;
|
|
|
-import java.net.URLConnection;
|
|
|
+import java.net.*;
|
|
|
import java.text.DecimalFormat;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.*;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
import java.util.regex.Pattern;
|
|
|
import java.util.stream.Collectors;
|
|
|
import java.util.zip.CRC32;
|
|
@@ -2169,7 +2173,7 @@ System.out.println(res);
|
|
|
*/
|
|
|
public boolean openCloseLoop(String sn, String deviceId, Integer status) {
|
|
|
String sendTopic = "/gateway/commandIn/" + sn;
|
|
|
- String backTopic = "gateway/commandOut/" + sn;
|
|
|
+ String backTopic = "/gateway/commandOut/" + sn;
|
|
|
|
|
|
if (status == 0) { // 这里0是闭合
|
|
|
status = 1; // 发MQTT消息里面1就是开
|
|
@@ -2206,4 +2210,85 @@ System.out.println(res);
|
|
|
int err = (int) dataJson.get("err");
|
|
|
return err != 1;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取本机IP地址
|
|
|
+ * @return 本机IP地址
|
|
|
+ */
|
|
|
+ public static String getIpAddress() throws UnknownHostException {
|
|
|
+ InetAddress localHost = InetAddress.getLocalHost();
|
|
|
+ return localHost.getHostAddress();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取请求IP地址
|
|
|
+ */
|
|
|
+ public static String getPostIpAddress() {
|
|
|
+ ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
|
|
+ if (attributes == null) return null;
|
|
|
+
|
|
|
+ HttpServletRequest request = attributes.getRequest();
|
|
|
+ // 从请求头部获取客户端IP地址
|
|
|
+ String clientIp = request.getHeader("X-Forwarded-For");
|
|
|
+ if (clientIp == null || clientIp.isEmpty() || "unknown".equalsIgnoreCase(clientIp)) {
|
|
|
+ clientIp = request.getHeader("X-Real-IP");
|
|
|
+ }
|
|
|
+ if (clientIp == null || clientIp.isEmpty() || "unknown".equalsIgnoreCase(clientIp)) {
|
|
|
+ clientIp = request.getRemoteAddr();
|
|
|
+ }
|
|
|
+ return clientIp;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取IP数据
|
|
|
+ */
|
|
|
+ public static IpdataModel getIpDataModel(String ipAddress) {
|
|
|
+ try {
|
|
|
+ URL url = new URL("https://api.ipdata.co");
|
|
|
+ IpdataService ipdataService = Ipdata.builder().url( url)
|
|
|
+ .withCache()
|
|
|
+ .timeout(30, TimeUnit.MINUTES)
|
|
|
+ .maxSize(8 * 1024)
|
|
|
+ .registerCacheConfig()
|
|
|
+ .key("ca96d4ab11053ad63f4a7dbcf7cb8751394a0772abafdaa5d509ac55")
|
|
|
+ .get();
|
|
|
+ return ipdataService.ipdata(ipAddress);
|
|
|
+ } catch (MalformedURLException | IpdataException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取公网IP
|
|
|
+ * @return 公网IP
|
|
|
+ */
|
|
|
+ public static String getPublicIp() {
|
|
|
+ try {
|
|
|
+ URL url = new URL("https://api.ipify.org?format=json");
|
|
|
+ HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
|
|
+ conn.setRequestMethod("GET");
|
|
|
+
|
|
|
+ BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
|
|
|
+ String inputLine;
|
|
|
+ StringBuilder response = new StringBuilder();
|
|
|
+ while ((inputLine = in.readLine()) != null) {
|
|
|
+ response.append(inputLine);
|
|
|
+ }
|
|
|
+ in.close();
|
|
|
+
|
|
|
+ // 从API的响应中解析出IP地址
|
|
|
+ String publicIp = response.toString().replaceAll("[{}\"]","");
|
|
|
+ // 去除多余的字符
|
|
|
+ publicIp = publicIp.split(":")[1];
|
|
|
+ // 取出IP地址部分
|
|
|
+ return publicIp;
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ System.out.println(getPublicIp());
|
|
|
+ }
|
|
|
}
|