| | |
| | | package com.doumee.service.business.impl; |
| | | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | 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.MqttLogMapper; |
| | | import com.doumee.dao.business.model.Bikes; |
| | | import com.doumee.dao.business.model.Locks; |
| | | import com.doumee.service.business.DeviceService; |
| | | import com.doumee.service.business.DeviceSubcribeService; |
| | | import com.doumee.dao.business.model.MemberRides; |
| | | import com.doumee.dao.business.model.MqttLog; |
| | | import com.doumee.service.business.*; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.annotation.PostConstruct; |
| | | import java.util.Date; |
| | | |
| | | /** |
| | | * 与硬件对接服务 |
| | |
| | | * @date 2023/10/09 18:06 |
| | | */ |
| | | @Service |
| | | @Slf4j |
| | | public class DeviceSubscribeServiceImpl implements DeviceSubcribeService { |
| | | |
| | | @Autowired |
| | | MemberRidesService memberRidesService; |
| | | @Autowired |
| | | private MqttLogMapper mqttLogMapper; |
| | | @Autowired |
| | | private MqttConfig mqttConfig; |
| | | @Override |
| | | public void listener(String param,String topic) { |
| | | System.out.println("mqtt消息订阅==================="+param); |
| | | log.info("mqtt消息订阅==================="+param); |
| | | String info = Constants.MqttTopic.lockInfo.substring(Constants.MqttTopic.lockInfo.lastIndexOf("/")+1) ; |
| | | String closeLock = Constants.MqttTopic.lockInfo.substring(Constants.MqttTopic.closeLock.lastIndexOf("/")+1) ; |
| | | if(topic.indexOf(Constants.MqttTopic.topic_index)!=0 |
| | | || (!StringUtils.contains(topic, info) |
| | | &&!StringUtils.contains(topic,closeLock))){ |
| | | log.error("mqtt消息订阅===========无效数据========"+param); |
| | | return; |
| | | } |
| | | String lockid = getLockIdFromTopic(topic); |
| | | if(StringUtils.isBlank(lockid)){ |
| | | //如果锁头编码为空 |
| | | log.error("mqtt消息订阅==============无效数据====="+param); |
| | | return; |
| | | } |
| | | String logId =Constants.getUUID(); |
| | | String logInfo = ""; |
| | | int result =0; |
| | | try { |
| | | |
| | | if(StringUtils.contains(topic, info)){ |
| | | //如果锁头信息上报 |
| | | Locks locks = JSONObject.parseObject(param, Locks.class); |
| | | locks.setId(lockid); |
| | | locks.setInfo(logId); |
| | | result = memberRidesService.mqttLockInfoEvent(locks); |
| | | logInfo = "上报锁头信息"; |
| | | log.info("mqtt消息订阅=========锁信息==========成功"); |
| | | } |
| | | if(StringUtils.contains(topic, closeLock)){ |
| | | //如果还车上报 |
| | | JSONObject pjson = JSONObject.parseObject(param); |
| | | String bikeCode = pjson.getString("bikeCode"); |
| | | MemberRides bikes = new MemberRides(); |
| | | bikes.setBikeCode(bikeCode); |
| | | bikes.setBackLockId(lockid); |
| | | bikes.setBackCommondId(logId); |
| | | result = memberRidesService.mqttCloseBikeEvent(bikes); |
| | | logInfo = "上报还车消息"; |
| | | log.info("mqtt消息订阅=========还车==========成功"); |
| | | } |
| | | }catch (BusinessException e){ |
| | | result =1; |
| | | }catch (Exception e){ |
| | | result =1; |
| | | } |
| | | createSubLog(topic,logId,result,param,logInfo); |
| | | } |
| | | private void createSubLog(String topic, 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); |
| | | mqttLogMapper.insert(log); |
| | | } |
| | | private String getLockIdFromTopic(String topic) { |
| | | topic = topic.substring(0,topic.lastIndexOf("/")); |
| | | String id = topic.substring( topic.lastIndexOf("/")+1); |
| | | return id; |
| | | } |
| | | } |