123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- package com.zy.omp.config.mqtt;
- import com.zy.omp.common.Constant;
- import com.zy.omp.pojo.dto.MqttMsgDto;
- import com.zy.omp.common.exception.ApiRuntimeException;
- import com.zy.omp.model.*;
- import com.zy.omp.service.*;
- import com.zy.omp.utils.*;
- import com.zy.omp.utils.CoordTransformUtil;
- import com.zy.omp.utils.GaoDeApiUtil;
- import com.zy.omp.websocket.WebSocketServer;
- import org.springframework.stereotype.Component;
- import javax.annotation.Resource;
- import java.math.BigDecimal;
- import java.time.LocalDateTime;
- @Component
- public class MqttCallbackHandler {
- @Resource
- private DeviceService deviceService;
- @Resource
- private DeviceLpService deviceLpService;
- @Resource
- private SetBaseService setBaseService;
- @Resource
- private SetService setService;
- @Resource
- private LocationService deviceLocationService;
- @Resource
- private WebSocketServer webSocketServer;
- @Resource
- private CallRecordsService callRecordsService;
- @Resource
- private MqttLogService mqttLogService;
- @Resource
- private RabbitMQApi rabbitMQApi;
-
- void handle(String topic, String payload) {
-
- mqttLogService.saveLog(topic, payload, 0);
-
- MqttMsgDto msgDto = MqttMsgDto.parse(payload);
-
- if (topic.startsWith("$regdtx2") || topic.startsWith("$dtxlp")) {
- switch (msgDto.getM()) {
-
- case Constant.M_CODE_REGISTER: {
- registerHandler_LP(msgDto);
- break;
- }
-
- case Constant.M_CODE_UPLOAD_LOCATION_INFO_LP: {
- uploadLocationInfoLPHandler(topic, msgDto);
- break;
- }
-
- case Constant.M_CODE_UPLOAD_INFO_LP: {
- uploadDeviceInfoLPHandler(topic, msgDto);
- break;
- }
- }
- }
-
- else {
- switch (msgDto.getM()) {
-
- case Constant.M_CODE_REGISTER: {
- registerHandler_OMP(msgDto);
- break;
- }
-
- case Constant.M_CODE_UPLOAD_LOCATION: {
- uploadLocationHandler(msgDto);
- break;
- }
-
- case Constant.M_CODE_UPLOAD_CALL_RECORD: {
- uploadCallRecordsHandler(msgDto);
- break;
- }
- }
-
- if (!topic.equals(Constant.TOPIC_REGISTER_SERVER)) {
- webSocketServer.massMessage(msgDto.getDeviceId(), payload);
- }
- }
- }
-
- private void registerHandler_OMP(MqttMsgDto obj) {
- String num = obj.getString("regnum");
-
- Device device = deviceService.getByNum(num);
-
- if (device == null) {
- device = new Device();
- device.setNum(num);
- device.setClientId(Constant.OLD_PHONE_DEVICE_ID_PREFIX + num);
- device.setPassword(MD5Util.MD5Encode(num));
- deviceService.save(device);
- SetBase setBase = new SetBase();
- setBase.setDeviceId(device.getClientId());
- setBaseService.save(setBase);
- }
-
- rabbitMQApi.registerMqtt(device.getClientId(), device.getPassword());
-
- setService.returnCode_OMP(device);
- }
-
- private void registerHandler_LP(MqttMsgDto obj) {
- String num = obj.getString("regnum");
-
- DeviceLp deviceLp = deviceLpService.getByNum(num);
-
- if (deviceLp == null) {
- deviceLp = new DeviceLp();
- deviceLp.setNum(num);
- deviceLp.setClientId(Constant.LP_DEVICE_ID_PREFIX + num);
- deviceLp.setPassword(MD5Util.encodeCut(num));
- deviceLp.setGroup(obj.getString("group"));
- deviceLpService.save(deviceLp);
- }
-
- rabbitMQApi.registerMqtt(deviceLp.getClientId(), deviceLp.getPassword());
-
- setService.returnCode_LP(deviceLp);
- }
-
- private void uploadLocationHandler(MqttMsgDto msg) {
- try {
- Location location = new Location();
- location.setDeviceId(msg.getDeviceId());
- location.setBatteryNum(msg.getInt("batterynum"));
- location.setSignalNum(msg.getInt("signalnum"));
- location.setMode(msg.getInt("mode"));
- location.setLon(msg.getString("lon"));
- location.setLat(msg.getString("lat"));
- location.setSpeed(msg.getString("speed"));
- location.setNum(msg.getInt("num"));
- location.setUploadTime(msg.getDate("createtime"));
-
- String[] gcj = CoordTransformUtil.wgs84toGcj02(location.getLon(), location.getLat());
- location.setLonGcj(gcj[0]);
- location.setLatGcj(gcj[1]);
-
- location.setSite(GaoDeApiUtil.regeo(location.getLonGcj(), location.getLatGcj()));
-
- deviceLocationService.saveLocation(location);
- } catch (Exception e) {
- e.printStackTrace();
- throw new ApiRuntimeException("处理设备上传位置定位信息出错");
- }
- }
-
- private void uploadLocationInfoLPHandler(String topic, MqttMsgDto msg) {
- try {
- String num = topic.split("IMEI")[1];
- DeviceLp deviceLp = new DeviceLp();
- deviceLp.setMode(msg.getInt("mode"));
- deviceLp.setLon(msg.getString("lng"));
- deviceLp.setLat(msg.getString("lat"));
-
- String[] gcj = CoordTransformUtil.wgs84toGcj02(deviceLp.getLon(), deviceLp.getLat());
- deviceLp.setLonGcj(gcj[0]);
- deviceLp.setLatGcj(gcj[1]);
-
- deviceLp.setSite(GaoDeApiUtil.regeo(deviceLp.getLonGcj(), deviceLp.getLatGcj()));
-
- deviceLpService.updateByNum(deviceLp, num);
- } catch (Exception e) {
- e.printStackTrace();
- throw new ApiRuntimeException("处理路牌设备上传定位信息出错");
- }
- }
-
- private void uploadDeviceInfoLPHandler(String topic, MqttMsgDto msg) {
- try {
- String num = topic.split("IMEI")[1];
- setService.getDeviceLocation_LP(num);
-
- DeviceLp dbDevice = deviceLpService.getByNum(num);
- Double dbWakeInt = dbDevice.getWakeInt() == null ? null : dbDevice.getWakeInt().setScale(2, BigDecimal.ROUND_HALF_DOWN).doubleValue();
- Double wakeInt = msg.getDouble("sleept") == null ? null : BigDecimal.valueOf(msg.getDouble("sleept")).setScale(2, BigDecimal.ROUND_HALF_DOWN).doubleValue();
- if (dbWakeInt == null || !dbWakeInt.equals(wakeInt) || !dbDevice.getThresh().equals(msg.getInt("thresh"))) {
- setService.updateDevice_LP(num, dbDevice.getWakeInt(), dbDevice.getThresh());
- }
- DeviceLp deviceLp = new DeviceLp();
- deviceLp.setDataType(msg.getString("datetype"));
- deviceLp.setBattery(msg.getString("batterynum"));
- deviceLp.setS4g(msg.getString("s4g"));
- deviceLp.setX(msg.getString("x"));
- deviceLp.setY(msg.getString("y"));
- deviceLp.setZ(msg.getString("z"));
- deviceLp.setAnglex(msg.getString("anglex"));
- deviceLp.setAngley(msg.getString("angley"));
- deviceLp.setAnglez(msg.getString("anglez"));
- deviceLp.setTemp(msg.getString("temp"));
- deviceLp.setUpdateTime(LocalDateTime.now());
-
- deviceLpService.updateByNum(deviceLp, num);
- } catch (Exception e) {
- e.printStackTrace();
- throw new ApiRuntimeException("处理路牌设备上传基本信息出错");
- }
- }
-
- private void uploadCallRecordsHandler(MqttMsgDto obj) {
- CallRecords callRecords = new CallRecords();
- callRecords.setDeviceId(obj.getDeviceId());
- callRecords.setCallType(obj.getInt("type"));
- callRecords.setKeyNum(obj.getString("key"));
- callRecords.setPhoneNum(obj.getString("phnoenum"));
- callRecords.setTalkTime(obj.getInt("talktime"));
- callRecords.setEndTime(obj.getString("endtime"));
- callRecordsService.save(callRecords);
- }
- }
|