From 1b0cf0db5c82ea8308d2f338973a91555787c34d Mon Sep 17 00:00:00 2001 From: jiangping <jp@doumee.com> Date: 星期二, 18 三月 2025 11:17:23 +0800 Subject: [PATCH] Merge remote-tracking branch 'origin/dev' into dev --- server/services/src/main/java/com/doumee/service/business/impl/DeviceSubscribeServiceImpl.java | 177 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 171 insertions(+), 6 deletions(-) diff --git a/server/services/src/main/java/com/doumee/service/business/impl/DeviceSubscribeServiceImpl.java b/server/services/src/main/java/com/doumee/service/business/impl/DeviceSubscribeServiceImpl.java index 6da2187..f4c8335 100644 --- a/server/services/src/main/java/com/doumee/service/business/impl/DeviceSubscribeServiceImpl.java +++ b/server/services/src/main/java/com/doumee/service/business/impl/DeviceSubscribeServiceImpl.java @@ -1,14 +1,25 @@ package com.doumee.service.business.impl; +import com.alibaba.fastjson.JSONObject; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.doumee.core.constants.Constants; +import com.doumee.core.exception.BusinessException; +import com.doumee.core.mqtt.config.MqttConfig; import com.doumee.core.mqtt.service.MqttToolService; -import com.doumee.dao.business.model.Locks; -import com.doumee.service.business.DeviceService; -import com.doumee.service.business.DeviceSubcribeService; +import com.doumee.dao.business.MemberRidesMapper; +import com.doumee.dao.business.MqttLogMapper; +import com.doumee.dao.business.SitesMapper; +import com.doumee.dao.business.model.*; +import com.doumee.service.business.*; +import io.swagger.models.auth.In; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; +import java.util.Date; /** * 涓庣‖浠跺鎺ユ湇鍔� @@ -16,10 +27,164 @@ * @date 2023/10/09 18:06 */ @Service +@Slf4j public class DeviceSubscribeServiceImpl implements DeviceSubcribeService { - + @Autowired + @Lazy + MemberRidesService memberRidesService; + @Lazy + @Autowired + SitesMapper sitesMapper; + @Autowired + private MqttLogMapper mqttLogMapper; + @Autowired + private MqttConfig mqttConfig; @Override - public void listener(String param,String topic) { - System.out.println("mqtt娑堟伅璁㈤槄==================="+param); + public void listener(String msgId,String param,String topic) { + log.info("mqtt娑堟伅璁㈤槄==================="+param); + String info = Constants.MqttTopic.sub_lockInfo.substring(Constants.MqttTopic.sub_lockInfo.lastIndexOf("/")+1) ; + String closeLock = Constants.MqttTopic.sub_closeLock.substring(Constants.MqttTopic.sub_closeLock.lastIndexOf("/")+1) ; + String health = Constants.MqttTopic.sub_health.substring(Constants.MqttTopic.sub_health.lastIndexOf("/")+1) ; + String brokers = "brokers"; + if(!StringUtils.contains(topic, brokers)){ + if(topic.indexOf(Constants.MqttTopic.topic_index)!=0 + ||topic.split("/").length < 4 + || (!StringUtils.contains(topic, info) + &&!StringUtils.contains(topic, health) + &&!StringUtils.contains(topic,closeLock))){ + log.error("mqtt娑堟伅璁㈤槄===========鏃犳晥鏁版嵁========"+param); + return; + } + } + MqttLog mqttLog = new MqttLog(); + mqttLog.setMsgId(msgId); + mqttLog.setTopic(topic); + int msgCount =mqttLogMapper.selectCount(new QueryWrapper<MqttLog>().lambda().eq(MqttLog::getMsg, param).eq(MqttLog::getType, Constants.ZERO)); + if(msgCount>0){ + log.error("mqtt娑堟伅璁㈤槄==============宸叉秷璐规暟鎹�====="+param); + return; + } + String logId =Constants.getUUID(); + String logInfo = ""; + int result =0; + try { + if(!topic.contains(brokers)){ + String[] ss = topic.split("/"); + String siteid = ss[2];//绔欑偣缂栫爜 + if(StringUtils.isBlank(siteid) ){ + //濡傛灉閿佸ご缂栫爜涓虹┖ + log.error("mqtt娑堟伅璁㈤槄==============鏃犳晥鏁版嵁====="+topic+param); + return; + } + if(StringUtils.contains(topic, info)){ + //濡傛灉閿佸ご淇℃伅涓婃姤 + Integer lockid = Constants.formatIntegerFromStr(ss[3]) ;//閿佸ご缂栫爜 + if( lockid == null){ + //濡傛灉閿佸ご缂栫爜涓虹┖ + log.error("mqtt娑堟伅璁㈤槄==============鏃犳晥鏁版嵁====="+topic+param); + return; + } + Locks locks = JSONObject.parseObject(param, Locks.class); + locks.setSiteId(siteid); + locks.setCode(lockid); + locks.setInfo(logId); + result = memberRidesService.mqttLockInfoEvent(locks); + logInfo = "mqtt娑堟伅璁㈤槄閿佸ご淇℃伅"; + log.info("mqtt娑堟伅璁㈤槄=========閿佷俊鎭�==========鎴愬姛"); + }else if(StringUtils.contains(topic, closeLock)){ + //濡傛灉杩樿溅涓婃姤 + Integer lockid = Constants.formatIntegerFromStr(ss[3]);//閿佸ご缂栫爜 + if( lockid == null){ + //濡傛灉閿佸ご缂栫爜涓虹┖ + log.error("mqtt娑堟伅璁㈤槄==============鏃犳晥鏁版嵁====="+topic+param); + return; + } + JSONObject pjson = JSONObject.parseObject(param); + MemberRides bikes = new MemberRides(); + bikes.setBikeCode(pjson.getString("bikeCode")); + bikes.setBackLockId( lockid); + bikes.setBackSiteId( siteid); + bikes.setBackCommondId(logId); + bikes.setStatus(pjson.getInteger("status")); + result = memberRidesService.mqttCloseBikeEvent(bikes); + logInfo = "mqtt娑堟伅璁㈤槄杩樿溅娑堟伅"; + log.info("mqtt娑堟伅璁㈤槄=========杩樿溅==========鎴愬姛"); + }else if(StringUtils.contains(topic, health)){ + //蹇冭烦娑堟伅= + Sites site = new Sites(); + site.setId(siteid); + site.setOnline(Constants.ZERO); + site.setLastLinkDate(new Date()); + sitesMapper.updateById(site);//鏇存柊绔欑偣鐘舵�� + logInfo = "mqtt娑堟伅璁㈤槄蹇冭烦娑堟伅"; + log.info("mqtt娑堟伅璁㈤槄=========蹇冭烦娑堟伅==========鎴愬姛"); + + } + }else { + //濡傛灉绔欑偣涓婁笅绾挎秷鎭� + JSONObject pjson = JSONObject.parseObject(param); + String clientId = String.valueOf(pjson.get("clientid")); + if(clientId.contains("doumee")){ + log.error("mqtt娑堟伅璁㈤槄==============鏃犳晥璁㈤槄鐘舵��====="+topic+param); + return; + } + Sites site = sitesMapper.selectById(clientId); + if(site == null){ + log.error("mqtt娑堟伅璁㈤槄==============鏃犳晥璁㈤槄鐘舵��====="+topic+param); + return; + } + if (topic.endsWith("disconnected")) { + if(Constants.formatIntegerNum(site.getOnline()) == Constants.ONE){ + log.error("mqtt娑堟伅璁㈤槄==============宸茶闃呯姸鎬佹帀绾�====="+param); + return; + } + site.setOnline(Constants.ONE); + } else { + if(Constants.formatIntegerNum(site.getOnline()) == Constants.ZERO){ + log.error("mqtt娑堟伅璁㈤槄==============宸茶闃呯姸鎬佷笂绾�====="+param); + return; + } + site.setOnline(Constants.ZERO); + } + site.setLastLinkDate(new Date()); + sitesMapper.updateById(site);//鏇存柊绔欑偣鐘舵�� + log.info("mqtt娑堟伅璁㈤槄=========绔欑偣涓婁笅绾�==========鎴愬姛"); + } + }catch (BusinessException e){ + e.printStackTrace(); + logInfo = "mqtt娑堟伅璁㈤槄閿欒==="+e.getMessage(); + result =1; + }catch (Exception e){ + e.printStackTrace(); + logInfo = "mqtt娑堟伅璁㈤槄寮傚父==="+e.getMessage(); + result =1; + } + createSubLog(topic,msgId,logId,result,param,logInfo); + } + private void createSubLog(String topic, String msgId,String logId, int result,String param,String info) { + MqttLog log = new MqttLog(); + log.setId(logId); + log.setCreateDate(new Date()); + log.setResult(result); + log.setTopic(topic); + log.setClientid(mqttConfig.getClientid()); + log.setHostInfo(mqttConfig.getHost()); + log.setInfo(JSONObject.toJSONString(mqttConfig)); + log.setType(Constants.ZERO); + log.setMsg(param); + log.setInfo(info); + log.setIsdeleted(Constants.ZERO); + log.setMsgId(msgId); + mqttLogMapper.insert(log); + } + private String getLockIdFromTopic(String topic) { + String[] ss = topic.split("/"); + if(ss.length>2){ + return ss[ss.length-2]; + } + return null; +// topic = topic.substring(0,topic.lastIndexOf("/")); +// String id = topic.substring( topic.lastIndexOf("/")+1); +// return id; } } -- Gitblit v1.9.3