¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.doumee.api.common; |
| | | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.doumee.api.BaseController; |
| | | import com.doumee.biz.system.SystemDictDataBiz; |
| | | import com.doumee.core.annotation.trace.Trace; |
| | | import com.doumee.core.constants.Constants; |
| | | import com.doumee.core.utils.DateUtil; |
| | | import com.doumee.core.utils.aliyun.ALiYunUtil; |
| | | import com.doumee.service.business.DeviceService; |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiImplicitParam; |
| | | import io.swagger.annotations.ApiImplicitParams; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.web.bind.annotation.PostMapping; |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | | import org.springframework.web.bind.annotation.RequestParam; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | import org.springframework.web.multipart.MultipartHttpServletRequest; |
| | | import org.springframework.web.multipart.commons.CommonsMultipartResolver; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.io.PrintWriter; |
| | | import java.util.HashMap; |
| | | import java.util.Iterator; |
| | | import java.util.Map; |
| | | import java.util.UUID; |
| | | |
| | | /** |
| | | * @author Eva.Caesar Liu |
| | | * @date 2023/02/14 11:14 |
| | | */ |
| | | @Api(tags = "æµè¯æ¥å£å·¥å
·") |
| | | @Trace(exclude = true) |
| | | @RestController |
| | | @RequestMapping("/test") |
| | | @Slf4j |
| | | public class TestController extends BaseController { |
| | | |
| | | |
| | | @Autowired |
| | | private DeviceService deviceService; |
| | | |
| | | |
| | | @ApiOperation(value = "æµè¯mqttå叿¶æ¯", notes = "ä¸ä¼ ", httpMethod = "POST", position = 6) |
| | | @ApiImplicitParams({ |
| | | @ApiImplicitParam(name = "topic", value = "主é¢", required = true, paramType = "query", dataType = "String", dataTypeClass = String.class), |
| | | @ApiImplicitParam(name = "json", value = "å
容", required = true, paramType = "query", dataType = "String", dataTypeClass = String.class), |
| | | }) |
| | | @PostMapping(value = "/testPush" ) |
| | | public void testPush(@RequestParam String topic,@RequestParam String json, HttpServletRequest request, HttpServletResponse response) throws Exception { |
| | | deviceService.testPush(topic,json); |
| | | } |
| | | |
| | | } |
| | |
| | | Map<String, String> map = new LinkedHashMap<>(); |
| | | // è·¯å¾æ¦æªé
ç½® |
| | | // è·¯å¾æ¦æªé
ç½® |
| | | // map.put("/system/dictData/companyUserRules","anon"); |
| | | // map.put("/system/login", "anon"); |
| | | // map.put("/system/logout", "anon"); |
| | | // map.put("/common/captcha", "anon"); |
| | | // map.put("/business/areas/*", "anon"); |
| | | // map.put("/public/uploadPicture","anon"); |
| | | // map.put("/public/uploadLocal","anon"); |
| | | |
| | | map.put("/system/dictData/companyUserRules","anon"); |
| | | map.put("/system/login", "anon"); |
| | |
| | | map.put("/system/loginH5", "anon"); |
| | | map.put("/common/captcha", "anon"); |
| | | map.put("/business/areas/*", "anon"); |
| | | map.put("/test/testPush","anon"); |
| | | map.put("/public/uploadPicture","anon"); |
| | | map.put("/public/uploadLocal","anon"); |
| | | |
| | | |
| | | // map.put("/business/company/register", "anon"); |
| | | // map.put("/business/labels/page","anon"); |
| | | // map.put("/business/*/list","anon"); |
| | | // map.put("/business/goods/goodsPage","anon"); |
| | | // map.put("/business/goods/h5Image","anon"); |
| | | // map.put("/business/goods/export","anon"); |
| | | // map.put("/business/goods/listForH5","anon"); |
| | | |
| | | |
| | | // - æ¾è¡swagger |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.doumee.service; |
| | | |
| | | import com.doumee.dao.business.model.Locks; |
| | | import com.doumee.dao.business.model.MqttLog; |
| | | |
| | | /** |
| | | * ä¸ç¡¬ä»¶å¯¹æ¥æå¡ |
| | | * @author æ±è¹è¹ |
| | | * @date 2023/10/09 18:06 |
| | | */ |
| | | public interface MqttSubService { |
| | | |
| | | |
| | | void startSubcribe(); |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.doumee.service.impl; |
| | | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.doumee.core.constants.Constants; |
| | | 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.Locks; |
| | | import com.doumee.dao.business.model.MqttLog; |
| | | import com.doumee.service.MqttSubService; |
| | | import com.doumee.service.business.DeviceService; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.annotation.PostConstruct; |
| | | import java.util.Date; |
| | | |
| | | /** |
| | | * ä¸ç¡¬ä»¶å¯¹æ¥æå¡ |
| | | * @author æ±è¹è¹ |
| | | * @date 2023/10/09 18:06 |
| | | */ |
| | | @Service |
| | | public class MqttSubServiceImpl implements MqttSubService { |
| | | @Autowired |
| | | private MqttToolService mqttToolService; |
| | | @Autowired |
| | | private MqttLogMapper mqttLogMapper; |
| | | @Autowired |
| | | private MqttConfig mqttConfig; |
| | | @Override |
| | | @PostConstruct |
| | | public void startSubcribe() { |
| | | mqttToolService.subscribe(new String[]{ Constants.MqttTopic.lockInfo, Constants.MqttTopic.closeLock}); |
| | | } |
| | | } |
| | |
| | | |
| | | mqtt: |
| | | clientid: doumeetestplat |
| | | subclientid: doumeetestplatSub |
| | | |
| | |
| | | |
| | | public class MqttClientInit { |
| | | static MqttClient client; |
| | | static MqttClient subClient; |
| | | |
| | | public static synchronized MqttClient getInstance(MqttConfig config,MqttPushCallback callBack){ |
| | | public static synchronized MqttClient getInstance(MqttConfig config ){ |
| | | if(client !=null){ |
| | | return client; |
| | | } |
| | | try { |
| | | // host为主æºåï¼clientidå³è¿æ¥MQTTç客æ·ç«¯IDï¼ä¸è¬ä»¥å¯ä¸æ è¯ç¬¦è¡¨ç¤ºï¼MemoryPersistence设置clientidçä¿åå½¢å¼ï¼é»è®¤ä¸ºä»¥å
åä¿å |
| | | client = new org.eclipse.paho.client.mqttv3.MqttClient(config.getHost(), config.getClientid()+ Constants.getUUID(),new MemoryPersistence()); |
| | | // String clientId =config.getClientid()+ Constants.getUUID(); |
| | | String clientId =config.getClientid(); |
| | | client = new org.eclipse.paho.client.mqttv3.MqttClient(config.getHost(), clientId,new MemoryPersistence()); |
| | | // MQTTçè¿æ¥è®¾ç½® |
| | | MqttConnectOptions options = new MqttConnectOptions(); |
| | | // 设置æ¯å¦æ¸
空session,è¿éå¦æè®¾ç½®ä¸ºfalse表示æå¡å¨ä¼ä¿ç客æ·ç«¯çè¿æ¥è®°å½ï¼è®¾ç½®ä¸ºtrueè¡¨ç¤ºæ¯æ¬¡è¿æ¥å°æå¡å¨é½ä»¥æ°çèº«ä»½è¿æ¥ |
| | | options.setCleanSession(false); |
| | | // è®¾ç½®è¿æ¥çç¨æ·å |
| | | options.setUserName(config.getUsername()); |
| | | // è®¾ç½®è¿æ¥çå¯ç |
| | | options.setPassword(config.getPassword().toCharArray()); |
| | | // 设置è¶
æ¶æ¶é´ åä½ä¸ºç§ |
| | | options.setConnectionTimeout(10); |
| | | // 设置ä¼è¯å¿è·³æ¶é´ åä½ä¸ºç§ æå¡å¨ä¼æ¯é1.5*20ç§çæ¶é´å客æ·ç«¯åéä¸ªæ¶æ¯å¤æå®¢æ·ç«¯æ¯å¦å¨çº¿ï¼ä½è¿ä¸ªæ¹æ³å¹¶æ²¡æéè¿çæºå¶ |
| | | options.setKeepAliveInterval(20); |
| | | //设置æå¼åéæ°è¿æ¥ |
| | | options.setAutomaticReconnect(true); |
| | | MqttTopic topic = client.getTopic(clientId+"_close"); |
| | | //éå± |
| | | options.setWill(topic, "close".getBytes(), 1, true); |
| | | client.connect(options); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return client; |
| | | } |
| | | public static synchronized MqttClient getSubInstance(MqttConfig config,MqttPushCallback callBack){ |
| | | if(subClient !=null){ |
| | | return subClient; |
| | | } |
| | | try { |
| | | // host为主æºåï¼clientidå³è¿æ¥MQTTç客æ·ç«¯IDï¼ä¸è¬ä»¥å¯ä¸æ è¯ç¬¦è¡¨ç¤ºï¼MemoryPersistence设置clientidçä¿åå½¢å¼ï¼é»è®¤ä¸ºä»¥å
åä¿å |
| | | // String clientId =config.getClientid()+ Constants.getUUID(); |
| | | String clientId =config.getSubclientid(); |
| | | subClient = new org.eclipse.paho.client.mqttv3.MqttClient(config.getHost(), clientId,new MemoryPersistence()); |
| | | // MQTTçè¿æ¥è®¾ç½® |
| | | MqttConnectOptions options = new MqttConnectOptions(); |
| | | // 设置æ¯å¦æ¸
空session,è¿éå¦æè®¾ç½®ä¸ºfalse表示æå¡å¨ä¼ä¿ç客æ·ç«¯çè¿æ¥è®°å½ï¼è®¾ç½®ä¸ºtrueè¡¨ç¤ºæ¯æ¬¡è¿æ¥å°æå¡å¨é½ä»¥æ°çèº«ä»½è¿æ¥ |
| | |
| | | // 设置ä¼è¯å¿è·³æ¶é´ åä½ä¸ºç§ æå¡å¨ä¼æ¯é1.5*20ç§çæ¶é´å客æ·ç«¯åéä¸ªæ¶æ¯å¤æå®¢æ·ç«¯æ¯å¦å¨çº¿ï¼ä½è¿ä¸ªæ¹æ³å¹¶æ²¡æéè¿çæºå¶ |
| | | options.setKeepAliveInterval(20); |
| | | // 设置åè° |
| | | client.setCallback(callBack); |
| | | subClient.setCallback(callBack); |
| | | //设置æå¼åéæ°è¿æ¥ |
| | | options.setAutomaticReconnect(true); |
| | | MqttTopic topic = client.getTopic(config.getClientid()+"_close"); |
| | | MqttTopic topic = subClient.getTopic(clientId+"_close"); |
| | | //éå± |
| | | options.setWill(topic, "close".getBytes(), 1, true); |
| | | client.connect(options); |
| | | subClient.connect(options); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return client; |
| | | return subClient; |
| | | } |
| | | |
| | | |
| | | |
| | | public static synchronized void refreshClient() throws MqttException { |
| | | if(client !=null){ |
| | | boolean result = client.isConnected(); |
| | |
| | | */ |
| | | private String clientid; |
| | | /** |
| | | * 客æ·ç«¯ç¼ç ç¨æ·è®¢é
|
| | | */ |
| | | private String subclientid; |
| | | /** |
| | | * ææè´¦å· |
| | | */ |
| | | private String username ; |
| | |
| | | @Override |
| | | public void messageArrived(String topic, MqttMessage message) throws Exception { |
| | | // subscribeåå¾å°çæ¶æ¯ä¼æ§è¡å°è¿éé¢ |
| | | try { |
| | | System.out.println("æ¥æ¶æ¶æ¯ä¸»é¢ : " + topic); |
| | | System.out.println("æ¥æ¶æ¶æ¯Qos : " + message.getQos()); |
| | | System.out.println("æ¥æ¶æ¶æ¯å
容 : " + new String(message.getPayload())); |
| | | deviceSubcribeService.listener(new String(message.getPayload()),topic); |
| | | deviceSubcribeService.listener(message.getId()+"",new String(message.getPayload()),topic); |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | |
| | | for (int i = 0; i < Qos.length; i++) { |
| | | Qos[i] = 1; |
| | | } |
| | | MqttClientInit.getInstance(config,callBack).subscribe(topics, Qos); |
| | | MqttClientInit.getSubInstance(config,callBack).subscribe(topics, Qos); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | |
| | | mess.setRetained(true); |
| | | mess.setPayload(message.getBytes()); |
| | | try { |
| | | MqttClientInit.getInstance(config,callBack).publish(topic, mess); |
| | | MqttClientInit.getInstance(config).publish(topic, mess); |
| | | return 1; |
| | | } catch (Exception e) { |
| | | //LOGGER.error(e.getLocalizedMessage()); |
| | |
| | | */ |
| | | @Data |
| | | @ApiModel("ç³»ç»è¡ä¸ºæä½è®°å½è¡¨") |
| | | @TableName("`mqtt_log`") |
| | | @TableName("\"mqtt_log\"") |
| | | public class MqttLog { |
| | | |
| | | @ApiModelProperty(value = "ç¼ç ") |
| | |
| | | @ApiModelProperty(value = "æ¶æ¯å
容") |
| | | @ExcelColumn(name="æ¶æ¯å
容") |
| | | private String msg; |
| | | @ApiModelProperty(value = "mqttæ¶æ¯ç±»å®¹") |
| | | @ExcelColumn(name="mqttæ¶æ¯ç±»å®¹") |
| | | private String msgId; |
| | | |
| | | @ApiModelProperty(value = "ç»æ 0æå 1失败", example = "1") |
| | | @ExcelColumn(name="ç»æ 0æå 1失败") |
| | |
| | | */ |
| | | MqttLog openLock(Locks locks); |
| | | |
| | | void startSubcribe(); |
| | | |
| | | MqttLog getLockInfo(Locks locks); |
| | | |
| | | void testPush(String topic, String json); |
| | | } |
| | |
| | | * 设å¤ä¸æ¥æ°æ®ï¼ä¸æ¥ï¼ |
| | | * @param param 䏿¥åæ° |
| | | */ |
| | | void listener(String param ,String topic); |
| | | void listener(String msgId,String param ,String topic); |
| | | |
| | | } |
| | |
| | | import com.doumee.dao.business.model.MqttLog; |
| | | import com.doumee.service.business.DeviceService; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.scheduling.annotation.Async; |
| | | import org.springframework.stereotype.Service; |
| | | import javax.annotation.PostConstruct; |
| | | import java.util.Date; |
| | |
| | | private MqttLogMapper mqttLogMapper; |
| | | @Autowired |
| | | private MqttConfig mqttConfig; |
| | | @Override |
| | | @PostConstruct |
| | | public void startSubcribe() { |
| | | mqttToolService.subscribe(new String[]{ Constants.MqttTopic.openLock, Constants.MqttTopic.closeLock}); |
| | | } |
| | | |
| | | /** |
| | | * åèµ·å¼éæä»¤ |
| | | * @param locks |
| | |
| | | MqttLog mqttLog = createPushLog(topic,result,"宿¶æ¥è¯¢éä¿¡æ¯_"+locks.getId()); |
| | | return mqttLog; |
| | | } |
| | | @Override |
| | | @Async |
| | | public void testPush(String topic, String json){ |
| | | int result = mqttToolService.pubMessage(json,topic); |
| | | } |
| | | |
| | | private MqttLog createPushLog(String topic, int result,String info) { |
| | | MqttLog log = new MqttLog(); |
| | |
| | | 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; |
| | |
| | | @Autowired |
| | | private MqttConfig mqttConfig; |
| | | @Override |
| | | public void listener(String param,String topic) { |
| | | public void listener(String msgId,String param,String topic) { |
| | | 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) ; |
| | | String closeLock = Constants.MqttTopic.closeLock.substring(Constants.MqttTopic.closeLock.lastIndexOf("/")+1) ; |
| | | if(topic.indexOf(Constants.MqttTopic.topic_index)!=0 |
| | | || (!StringUtils.contains(topic, info) |
| | | &&!StringUtils.contains(topic,closeLock))){ |
| | |
| | | if(StringUtils.isBlank(lockid)){ |
| | | //妿é头ç¼ç 为空 |
| | | log.error("mqttæ¶æ¯è®¢é
==============æ ææ°æ®====="+param); |
| | | return; |
| | | } |
| | | MqttLog mqttLog = new MqttLog(); |
| | | mqttLog.setMsgId(msgId); |
| | | if(mqttLogMapper.selectCount(new QueryWrapper<MqttLog>().lambda().eq(MqttLog::getMsgId, msgId)) >0){ |
| | | log.error("mqttæ¶æ¯è®¢é
==============å·²æ¶è´¹æ°æ®====="+param); |
| | | return; |
| | | } |
| | | String logId =Constants.getUUID(); |
| | |
| | | MemberRides bikes = new MemberRides(); |
| | | bikes.setBikeCode(pjson.getString("bikeCode")); |
| | | bikes.setBackLockId( pjson.getString("lockId")); |
| | | bikes.setBackLockId( pjson.getString("siteId")); |
| | | bikes.setBackSiteId( pjson.getString("siteId")); |
| | | bikes.setBackCommondId(logId); |
| | | result = memberRidesService.mqttCloseBikeEvent(bikes); |
| | | logInfo = "mqttæ¶æ¯è®¢é
è¿è½¦æ¶æ¯"; |
| | |
| | | logInfo = "mqttæ¶æ¯è®¢é
å¼å¸¸==="; |
| | | result =1; |
| | | } |
| | | createSubLog(topic,logId,result,param,logInfo); |
| | | createSubLog(topic,msgId,logId,result,param,logInfo); |
| | | } |
| | | private void createSubLog(String topic, String logId, int result,String param,String info) { |
| | | 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.setType(Constants.ZERO); |
| | | log.setMsg(param); |
| | | log.setInfo(info); |
| | | log.setMsgId(msgId); |
| | | mqttLogMapper.insert(log); |
| | | } |
| | | private String getLockIdFromTopic(String topic) { |
| | |
| | | |
| | | //TODO-----JP------------æ ¹æ®è½¦è¾codeåæè½¦è¾ç±»åï¼å¾
ç¡®è®¤æ¹æ¡------------------- |
| | | private String getBileTypeByCode(String bikeCode) { |
| | | return null; |
| | | int temp=1+(int)(Math.random()*(8)); |
| | | return temp+""; |
| | | } |
| | | |
| | | private Locks dealLockAndSite(Locks locks) { |
| | |
| | | @Override |
| | | public int mqttCloseBikeEvent(MemberRides bikes){ |
| | | //å
è´¹éªè¡æ¶é¿æ¥è¯¢ï¼æ°æ®åå
¸é
ç½® |
| | | if(StringUtils.isBlank(bikes.getBikeCode()) ||StringUtils.isBlank(bikes.getRentLockId())){ |
| | | if(StringUtils.isBlank(bikes.getBikeCode()) ||StringUtils.isBlank(bikes.getBackLockId())||StringUtils.isBlank(bikes.getBackSiteId())){ |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"è¿è½¦ä¸æ¥åæ°é误ï¼"); |
| | | } |
| | | QueryWrapper<MemberRides> wrapper = new QueryWrapper<>(); |
| | |
| | | "status": 1, // ç¶æï¼0éå, 1æå¼ï¼2è¿è¡ä¸, 3å¼å¸¸ |
| | | "bikeCode": "1234567890" // èªè¡è½¦icå¡å·ï¼æ 车为空 |
| | | } |
| | | |
| | | ## {"bikeCode":"1234567890","id":"kjflksjlkfsjdlk","siteId": 456,"status":0,"code":"789"} |
| | | ``` |
| | | |
| | | ## pub: device/lock/{id}/getInfo |
| | |
| | | "bikeCode": "1234567890", |
| | | "lockId": 123, |
| | | "siteId": 234, |
| | | "time": "2023-10-13 10:12:90" // ç«ç¹ä¸ä½æºæ¶å°è¿è½¦æä»¤çæ¶é´ï¼ä»
ååèï¼è¯·ä»¥æå¡å¨æ¶é´ä¸ºå |
| | | "time": "2023-10-13 10:12:90" |
| | | } |
| | | ## {"bikeCode":"1234567890","lockId":789,"siteId": 456,"time":"2023-10-13 10:12:90"} |
| | | ``` |