|
@@ -1,6 +1,5 @@
|
|
package com.zy.bms.config.mqtt;
|
|
package com.zy.bms.config.mqtt;
|
|
|
|
|
|
-import com.alibaba.fastjson.JSON;
|
|
|
|
import com.zy.bms.common.Constant;
|
|
import com.zy.bms.common.Constant;
|
|
import com.zy.bms.common.dto.MqttMsgDto;
|
|
import com.zy.bms.common.dto.MqttMsgDto;
|
|
import com.zy.bms.common.exception.ApiRuntimeException;
|
|
import com.zy.bms.common.exception.ApiRuntimeException;
|
|
@@ -10,12 +9,9 @@ import com.zy.bms.utils.*;
|
|
import com.zy.bms.utils.CoordTransformUtil;
|
|
import com.zy.bms.utils.CoordTransformUtil;
|
|
import com.zy.bms.utils.GaoDeApiUtil;
|
|
import com.zy.bms.utils.GaoDeApiUtil;
|
|
import com.zy.bms.websocket.WebSocketServer;
|
|
import com.zy.bms.websocket.WebSocketServer;
|
|
-import org.springframework.http.HttpHeaders;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
import javax.annotation.Resource;
|
|
-import java.util.HashMap;
|
|
|
|
-import java.util.Map;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
* MQTT 消息返回处理类
|
|
* MQTT 消息返回处理类
|
|
@@ -42,6 +38,8 @@ public class MqttCallbackHandler {
|
|
private CallRecordsService callRecordsService;
|
|
private CallRecordsService callRecordsService;
|
|
@Resource
|
|
@Resource
|
|
private MqttLogService mqttLogService;
|
|
private MqttLogService mqttLogService;
|
|
|
|
+ @Resource
|
|
|
|
+ private RabbitMQApi rabbitMQApi;
|
|
|
|
|
|
/**
|
|
/**
|
|
* 处理消息
|
|
* 处理消息
|
|
@@ -85,7 +83,7 @@ public class MqttCallbackHandler {
|
|
device = createDevice(num);
|
|
device = createDevice(num);
|
|
}
|
|
}
|
|
//mqtt服务器注册用户
|
|
//mqtt服务器注册用户
|
|
- registerMqtt(device.getClientId(), device.getPassword());
|
|
|
|
|
|
+ rabbitMQApi.registerMqtt(device.getClientId(), device.getPassword());
|
|
//发送消息
|
|
//发送消息
|
|
setToDeviceService.returnCodeToDevice(device);
|
|
setToDeviceService.returnCodeToDevice(device);
|
|
}
|
|
}
|
|
@@ -125,7 +123,7 @@ public class MqttCallbackHandler {
|
|
private Device createDevice(String num) {
|
|
private Device createDevice(String num) {
|
|
Device device = new Device();
|
|
Device device = new Device();
|
|
device.setNum(num);
|
|
device.setNum(num);
|
|
- String clientId = MD5Util.MD5Encode(num);
|
|
|
|
|
|
+ String clientId = Constant.OLD_PHONE_DEVICE_ID_PREFIX + MD5Util.MD5Encode(num);
|
|
device.setClientId(clientId);
|
|
device.setClientId(clientId);
|
|
device.setPassword(MD5Util.MD5Encode(clientId));
|
|
device.setPassword(MD5Util.MD5Encode(clientId));
|
|
device.setGroupId(1);
|
|
device.setGroupId(1);
|
|
@@ -165,84 +163,4 @@ public class MqttCallbackHandler {
|
|
throw new ApiRuntimeException("处理设备上传位置定位信息出错");
|
|
throw new ApiRuntimeException("处理设备上传位置定位信息出错");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 向 MQTT 服务器申请用户,供设备进行登录
|
|
|
|
- * <p>
|
|
|
|
- * 因为存在先后问题,需要先注册用户,在开通虚拟机权限,
|
|
|
|
- * 所以逻辑为
|
|
|
|
- * 发送注册用户请求
|
|
|
|
- * 尝试申请权限
|
|
|
|
- * 如果失败,则间隔一秒再次请求
|
|
|
|
- * 如果申请次数超过3次,则尝试重新注册
|
|
|
|
- * 重新注册后再次尝试申请
|
|
|
|
- * 如果申请五次都失败,则结束方法,记录日志
|
|
|
|
- *
|
|
|
|
- * @param clientId 设备登录ID
|
|
|
|
- * @param password 设备登录密码
|
|
|
|
- */
|
|
|
|
- private void registerMqtt(String clientId, String password) {
|
|
|
|
- addRabbitMqUser(clientId, password);
|
|
|
|
- int count = 0;
|
|
|
|
- while (!setPermission(clientId)) {
|
|
|
|
- try {
|
|
|
|
- Thread.sleep(1000);
|
|
|
|
- if (count == 2) {
|
|
|
|
- addRabbitMqUser(clientId, password);
|
|
|
|
- }
|
|
|
|
- if (count > 4) {
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
- count++;
|
|
|
|
- } catch (InterruptedException e) {
|
|
|
|
- e.printStackTrace();
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 注册 RabbitMQ 用户
|
|
|
|
- *
|
|
|
|
- * @param clientId 用户名
|
|
|
|
- * @param password 用户密码
|
|
|
|
- */
|
|
|
|
- private void addRabbitMqUser(String clientId, String password) {
|
|
|
|
- String url = "http://view.ailishi.org:15672/api/users/" + clientId;
|
|
|
|
- //请求头
|
|
|
|
- HttpHeaders headers = new HttpHeaders();
|
|
|
|
- headers.add("authorization", "Basic bHEyMDE5OkxpUXVhblJhYmJpdA==");
|
|
|
|
- //请求参数
|
|
|
|
- Map<String, String> params = new HashMap<>();
|
|
|
|
- params.put("username", clientId);
|
|
|
|
- params.put("password", password);
|
|
|
|
- params.put("tags", "");
|
|
|
|
- HttpUtil.putForJSONEntity(url, headers, JSON.toJSONString(params));
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 申请虚拟机权限
|
|
|
|
- *
|
|
|
|
- * @param clientId 用户ID
|
|
|
|
- */
|
|
|
|
- private boolean setPermission(String clientId) {
|
|
|
|
- String url = "http://view.ailishi.org:15672/api/permissions/%2F/" + clientId;
|
|
|
|
- //请求头
|
|
|
|
- HttpHeaders headers = new HttpHeaders();
|
|
|
|
- headers.add("authorization", "Basic bHEyMDE5OkxpUXVhblJhYmJpdA==");
|
|
|
|
- //请求参数
|
|
|
|
- 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;
|
|
|
|
- }
|
|
|
|
}
|
|
}
|