|
@@ -1,21 +1,20 @@
|
|
package com.zy.bms.config.mqtt;
|
|
package com.zy.bms.config.mqtt;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
+import com.zy.bms.common.Constant;
|
|
|
|
+import com.zy.bms.common.exception.ApiRuntimeException;
|
|
|
|
+import com.zy.bms.model.*;
|
|
import com.zy.bms.redis.DeviceManager;
|
|
import com.zy.bms.redis.DeviceManager;
|
|
-import com.zy.bms.service.DeviceLocationService;
|
|
|
|
-import com.zy.bms.service.DeviceService;
|
|
|
|
-import com.zy.bms.service.SetToDeviceService;
|
|
|
|
|
|
+import com.zy.bms.service.*;
|
|
import com.zy.bms.utils.CodeGenerator;
|
|
import com.zy.bms.utils.CodeGenerator;
|
|
|
|
+import com.zy.bms.utils.CoordTransformUtil;
|
|
|
|
+import com.zy.bms.utils.DateTimeUtil;
|
|
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 com.zy.bms.model.CallRecords;
|
|
|
|
-import com.zy.bms.model.Device;
|
|
|
|
-import com.zy.bms.model.DeviceLocation;
|
|
|
|
-import com.zy.bms.model.MqttMsgDto;
|
|
|
|
-import com.zy.bms.service.CallRecordsService;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
import javax.annotation.Resource;
|
|
|
|
+import java.util.Map;
|
|
|
|
|
|
/**
|
|
/**
|
|
* MQTT 消息返回处理类
|
|
* MQTT 消息返回处理类
|
|
@@ -29,6 +28,8 @@ public class MqttCallbackHandler {
|
|
@Resource
|
|
@Resource
|
|
private DeviceService deviceService;
|
|
private DeviceService deviceService;
|
|
@Resource
|
|
@Resource
|
|
|
|
+ private SetBaseService setBaseService;
|
|
|
|
+ @Resource
|
|
private SetToDeviceService setToDeviceService;
|
|
private SetToDeviceService setToDeviceService;
|
|
@Resource
|
|
@Resource
|
|
private DeviceLocationService deviceLocationService;
|
|
private DeviceLocationService deviceLocationService;
|
|
@@ -48,8 +49,6 @@ public class MqttCallbackHandler {
|
|
* @param payload 消息内容
|
|
* @param payload 消息内容
|
|
*/
|
|
*/
|
|
void handle(String topic, String payload) {
|
|
void handle(String topic, String payload) {
|
|
- System.out.println(payload);
|
|
|
|
- System.out.println("------------------------------------------------------");
|
|
|
|
if (topic.contains("/")) {
|
|
if (topic.contains("/")) {
|
|
webSocketServer.massMessage(topic.split("/")[1], payload);
|
|
webSocketServer.massMessage(topic.split("/")[1], payload);
|
|
}
|
|
}
|
|
@@ -57,16 +56,13 @@ public class MqttCallbackHandler {
|
|
MqttMsgDto msgObject = JSON.parseObject(payload, MqttMsgDto.class);
|
|
MqttMsgDto msgObject = JSON.parseObject(payload, MqttMsgDto.class);
|
|
//根据报文标识代码 M 处理消息
|
|
//根据报文标识代码 M 处理消息
|
|
switch (msgObject.getM()) {
|
|
switch (msgObject.getM()) {
|
|
-// case "10"://设备端注册报文
|
|
|
|
-// registerDevice(msgObject);
|
|
|
|
-// break;
|
|
|
|
-// case "12"://设备注册完成
|
|
|
|
-// setToDeviceService.registerOK(msgObject.getData().get("deviceid").toString());
|
|
|
|
-// break;
|
|
|
|
- case "20":// 设备位置信息上报
|
|
|
|
|
|
+ case Constant.M_CODE_REGISTER://设备端注册报文
|
|
|
|
+ registerDevice(msgObject);
|
|
|
|
+ break;
|
|
|
|
+ case Constant.M_CODE_UPLOAD_LOCATION:// 设备位置信息上报
|
|
LocationHandler(msgObject);
|
|
LocationHandler(msgObject);
|
|
break;
|
|
break;
|
|
- case "40"://设备上传通话记录
|
|
|
|
|
|
+ case Constant.M_CODE_UPLOAD_CALL_RECORD://设备上传通话记录
|
|
CallRecordsHandler(msgObject);
|
|
CallRecordsHandler(msgObject);
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
@@ -79,10 +75,9 @@ public class MqttCallbackHandler {
|
|
String num = obj.getData().get("num").toString();
|
|
String num = obj.getData().get("num").toString();
|
|
//查询数据库中是否有此设备
|
|
//查询数据库中是否有此设备
|
|
Device device = deviceService.getByNum(num);
|
|
Device device = deviceService.getByNum(num);
|
|
|
|
+ //没有该设备,保存至数据库
|
|
if (device == null) {
|
|
if (device == null) {
|
|
- device = new Device(num, CodeGenerator.generateClientId(), CodeGenerator.generateShortUuid());
|
|
|
|
- //没有该设备,保存至数据库
|
|
|
|
- deviceService.save(device);
|
|
|
|
|
|
+ device = createDevice(num);
|
|
}
|
|
}
|
|
//发送消息
|
|
//发送消息
|
|
setToDeviceService.returnCodeToDevice(device.getNum(), device.getClientId(), device.getPassword());
|
|
setToDeviceService.returnCodeToDevice(device.getNum(), device.getClientId(), device.getPassword());
|
|
@@ -91,8 +86,8 @@ public class MqttCallbackHandler {
|
|
/**
|
|
/**
|
|
* 处理上传位置
|
|
* 处理上传位置
|
|
*/
|
|
*/
|
|
- private void LocationHandler(MqttMsgDto obj) {
|
|
|
|
- DeviceLocation location = new DeviceLocation(obj);
|
|
|
|
|
|
+ private void LocationHandler(MqttMsgDto msg) {
|
|
|
|
+ DeviceLocation location = MqttMsgToLocation(msg);
|
|
// 逆地理位置解析
|
|
// 逆地理位置解析
|
|
location.setSite(gaoDeApiService.regeo(location.getLonGcj(), location.getLatGcj()));
|
|
location.setSite(gaoDeApiService.regeo(location.getLonGcj(), location.getLatGcj()));
|
|
// 保存位置信息--历史记录
|
|
// 保存位置信息--历史记录
|
|
@@ -107,12 +102,57 @@ public class MqttCallbackHandler {
|
|
private void CallRecordsHandler(MqttMsgDto obj) {
|
|
private void CallRecordsHandler(MqttMsgDto obj) {
|
|
CallRecords callRecords = new CallRecords();
|
|
CallRecords callRecords = new CallRecords();
|
|
callRecords.setDeviceId(obj.getR().split("/")[1]);
|
|
callRecords.setDeviceId(obj.getR().split("/")[1]);
|
|
- callRecords.setType((int) obj.getData().get("type"));
|
|
|
|
- callRecords.setKey((String) obj.getData().get("key"));
|
|
|
|
|
|
+ callRecords.setCallType((int) obj.getData().get("type"));
|
|
|
|
+ callRecords.setKeyNum((String) obj.getData().get("key"));
|
|
callRecords.setPhoneNum((String) obj.getData().get("phnoenum"));
|
|
callRecords.setPhoneNum((String) obj.getData().get("phnoenum"));
|
|
callRecords.setTalkTime((int) obj.getData().get("talktime"));
|
|
callRecords.setTalkTime((int) obj.getData().get("talktime"));
|
|
callRecords.setEndTime((String) obj.getData().get("endtime"));
|
|
callRecords.setEndTime((String) obj.getData().get("endtime"));
|
|
callRecordsService.save(callRecords);
|
|
callRecordsService.save(callRecords);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 创建设备信息,保存设备基础信息以及设置信息
|
|
|
|
+ *
|
|
|
|
+ * @param num 设备随机码
|
|
|
|
+ */
|
|
|
|
+ private Device createDevice(String num) {
|
|
|
|
+ Device device = new Device();
|
|
|
|
+ device.setNum(num);
|
|
|
|
+ device.setClientId(CodeGenerator.generateClientId());
|
|
|
|
+ device.setPassword(CodeGenerator.generateShortUuid());
|
|
|
|
+ device.setGroupId(1);
|
|
|
|
+ deviceService.save(device);
|
|
|
|
+ setBaseService.initSetBase(device.getClientId());
|
|
|
|
+ return device;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 解析信息转为位置对象
|
|
|
|
+ */
|
|
|
|
+ private DeviceLocation MqttMsgToLocation(MqttMsgDto msg) {
|
|
|
|
+ try {
|
|
|
|
+ DeviceLocation location = new DeviceLocation();
|
|
|
|
+ Map<String, Object> dataMap = msg.getData();
|
|
|
|
+ location.setDeviceId(msg.getR().split("/")[1]);
|
|
|
|
+ location.setBatteryNum((int) dataMap.get("batterynum"));
|
|
|
|
+ location.setSignalNum((int) dataMap.get("signalnum"));
|
|
|
|
+ location.setMode((int) dataMap.get("mode"));
|
|
|
|
+ location.setLon((String) dataMap.get("lon"));
|
|
|
|
+ location.setLon((String) dataMap.get("lat"));
|
|
|
|
+ location.setSpeed((String) dataMap.get("speed"));
|
|
|
|
+ location.setNum((int) dataMap.get("num"));
|
|
|
|
+ location.setUploadTime(DateTimeUtil.strToTime((String) dataMap.get("createtime")));
|
|
|
|
+ Object cellInfo = dataMap.get("cellInfo");
|
|
|
|
+ if (cellInfo != null) {
|
|
|
|
+ location.setCellInfo(cellInfo.toString());
|
|
|
|
+ }
|
|
|
|
+ //转换后的坐标
|
|
|
|
+ String[] gcj = CoordTransformUtil.wgs84toGcj02(location.getLon(), location.getLat());
|
|
|
|
+ location.setLonGcj(gcj[0]);
|
|
|
|
+ location.setLatGcj(gcj[1]);
|
|
|
|
+ return location;
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ throw new ApiRuntimeException("处理设备上传位置定位信息出错");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|