123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- 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.*;
- /**
- * RabbitMQ HTTP API接口请求
- *
- * @author yang xiao kun
- * create on 2021/5/14
- */
- @Slf4j
- public class RabbitMQApi {
- private final static String authorization = "Basic bHEyMDE5OkxpUXVhblJhYmJpdA==";
- /**
- * 向 MQTT 服务器申请用户,供设备进行登录
- * <p>
- * 因为存在先后问题,需要先注册用户,在开通虚拟机权限,
- * 所以逻辑为
- * 发送注册用户请求
- * 尝试申请权限
- * 如果失败,则间隔一秒再次请求
- * 如果申请次数超过3次,则尝试重新注册
- * 重新注册后再次尝试申请
- * 如果申请五次都失败,则结束方法,记录日志
- *
- * @param username 设备登录ID
- * @param password 设备登录密码
- */
- public static void register(String username, String password) {
- addRabbitMqUser(username, password);
- int count = 0;
- while (!setPermission(username)) {
- try {
- Thread.sleep(1000);
- if (count == 2) {
- addRabbitMqUser(username, password);
- }
- if (count > 4) {
- break;
- }
- count++;
- } catch (InterruptedException e) {
- log.error("向 MQTT 服务器申请用户失败", e);
- break;
- }
- }
- }
- /**
- * 注册 RabbitMQ 用户
- *
- * @param username 用户名
- * @param password 用户密码
- */
- private static void addRabbitMqUser(String username, String password) {
- String url = "http://view.ailishi.org:15672/api/users/" + username;
- //请求头
- HttpHeaders headers = new HttpHeaders();
- headers.add("authorization", authorization);
- //请求参数
- Map<String, String> params = new HashMap<>();
- params.put("username", username);
- params.put("password", password);
- params.put("tags", "");
- HttpUtil.putForJSONEntity(url, headers, JSON.toJSONString(params));
- }
- /**
- * 申请虚拟机权限
- *
- * @param clientId 用户ID
- */
- private static boolean setPermission(String clientId) {
- String url = "http://view.ailishi.org:15672/api/permissions/%2F/" + clientId;
- //请求头
- HttpHeaders headers = new HttpHeaders();
- headers.add("authorization", authorization);
- //请求参数
- Map<String, String> params = new HashMap<>();
- params.put("username", clientId);
- params.put("vhost", "/");
- params.put("configure", ".*");
- params.put("write", ".*");
- params.put("read", ".*");
- try {
- HttpUtil.putForJSONEntity(url, headers, JSON.toJSONString(params));
- } catch (Exception e) {
- return false;
- }
- 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;
- }
- }
|