|
@@ -1,11 +1,12 @@
|
|
|
package com.zy.bms.utils;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.Map;
|
|
|
+import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* RabbitMQ HTTP API接口请求
|
|
@@ -15,6 +16,8 @@ import java.util.Map;
|
|
|
*/
|
|
|
@Slf4j
|
|
|
public class RabbitMQApi {
|
|
|
+ private final static String authorization = "Basic bHEyMDE5OkxpUXVhblJhYmJpdA==";
|
|
|
+
|
|
|
/**
|
|
|
* 向 MQTT 服务器申请用户,供设备进行登录
|
|
|
* <p>
|
|
@@ -60,7 +63,7 @@ public class RabbitMQApi {
|
|
|
String url = "http://view.ailishi.org:15672/api/users/" + username;
|
|
|
//请求头
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
- headers.add("authorization", "Basic bHEyMDE5OkxpUXVhblJhYmJpdA==");
|
|
|
+ headers.add("authorization", authorization);
|
|
|
//请求参数
|
|
|
Map<String, String> params = new HashMap<>();
|
|
|
params.put("username", username);
|
|
@@ -78,7 +81,7 @@ public class RabbitMQApi {
|
|
|
String url = "http://view.ailishi.org:15672/api/permissions/%2F/" + clientId;
|
|
|
//请求头
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
- headers.add("authorization", "Basic bHEyMDE5OkxpUXVhblJhYmJpdA==");
|
|
|
+ headers.add("authorization", authorization);
|
|
|
//请求参数
|
|
|
Map<String, String> params = new HashMap<>();
|
|
|
params.put("username", clientId);
|
|
@@ -93,4 +96,53 @@ public class RabbitMQApi {
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询MQTT服务器链接状态
|
|
|
+ */
|
|
|
+ public static List<String> connections() {
|
|
|
+ String url = "http://view.ailishi.org:15672/api/connections";
|
|
|
+ List<String> result = new LinkedList<>();
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.add("authorization", authorization);
|
|
|
+ try {
|
|
|
+ String response = HttpUtil.getForEntity(url, headers);
|
|
|
+ //返回结果不为空
|
|
|
+ if (!response.equals("")) {
|
|
|
+ JSONArray jsonArray = JSON.parseArray(response);
|
|
|
+ for (int i = 0; i < jsonArray.size(); i++) {
|
|
|
+ JSONObject jsonObject = jsonArray.getJSONObject(i);
|
|
|
+ result.add(jsonObject.getString("user"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("查询MQTT服务器Connections错误", e);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询MQTT服务器注册的用户
|
|
|
+ */
|
|
|
+ public static List<String> users() {
|
|
|
+ String url = "http://view.ailishi.org:15672/api/users";
|
|
|
+ List<String> result = new LinkedList<>();
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.add("authorization", authorization);
|
|
|
+ try {
|
|
|
+ String response = HttpUtil.getForEntity(url, headers);
|
|
|
+ //返回结果不为空
|
|
|
+ if (!response.equals("")) {
|
|
|
+ JSONArray jsonArray = JSON.parseArray(response);
|
|
|
+ for (int i = 0; i < jsonArray.size(); i++) {
|
|
|
+ JSONObject jsonObject = jsonArray.getJSONObject(i);
|
|
|
+ result.add(jsonObject.getString("name"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("查询MQTT服务器users错误", e);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
}
|