From c6ccf6e6b444b81d0034504aaa65c4bf548c249c Mon Sep 17 00:00:00 2001 From: wulin Date: Tue, 24 Oct 2023 21:08:58 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- iot-common/iot-data/pom.xml | 4 + .../system/SystemTalkAnswerConfigEntity.java | 2 +- .../device/DeviceAlarmClockRecordService.java | 2 +- .../service/device/DeviceInfoService.java | 18 +++- .../iot/third/enums/ActionPartSpeechEnum.java | 4 +- .../qiuguo/iot/third/nlp/action/Actions.java | 3 + .../controller/device/DeviceController.java | 6 +- .../api/command/IotActionCommand.java | 1 + .../api/command/QianWenActionCommand.java | 2 +- .../api/command/QiuGuoActionCommand.java | 1 + .../api/command/WeatherActionCommand.java | 1 + .../api/controller/WebsocketController.java | 93 ++++++------------- .../api/handler/BoxWebSocketHandler.java | 91 +++++------------- .../api/handler/CustomerWebSocketHandler.java | 28 +++--- .../api/service/BaseWebSocketService.java | 49 ++++++++++ .../src/main/resources/bootstrap-dev.yml | 2 +- .../api/mysql/MysqlUtilTable2Contoller.java | 7 +- .../api/mysql/MysqlUtilTable2Service.java | 13 +++ iot-modules/pom.xml | 24 ----- 19 files changed, 164 insertions(+), 187 deletions(-) diff --git a/iot-common/iot-data/pom.xml b/iot-common/iot-data/pom.xml index acddab6..3514316 100644 --- a/iot-common/iot-data/pom.xml +++ b/iot-common/iot-data/pom.xml @@ -54,6 +54,10 @@ org.springframework.amqp spring-rabbit + + org.springframework.data + spring-data-redis + ${project.artifactId} diff --git a/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/entity/system/SystemTalkAnswerConfigEntity.java b/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/entity/system/SystemTalkAnswerConfigEntity.java index cc9ff33..760b35c 100644 --- a/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/entity/system/SystemTalkAnswerConfigEntity.java +++ b/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/entity/system/SystemTalkAnswerConfigEntity.java @@ -77,7 +77,7 @@ public class SystemTalkAnswerConfigEntity extends GenericEntity { @Column(name = "play_type", nullable = false) private Integer playType; - @Comment("处理Bean名字") + @Comment("处理Bean名字,空使用qianWenActionCommand(动词后的量词、关键字等除外200,400)") @Column(name = "bean_name", nullable = false) private String beanName; diff --git a/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/service/device/DeviceAlarmClockRecordService.java b/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/service/device/DeviceAlarmClockRecordService.java index 4da54be..6382517 100644 --- a/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/service/device/DeviceAlarmClockRecordService.java +++ b/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/service/device/DeviceAlarmClockRecordService.java @@ -2,9 +2,9 @@ package com.qiuguo.iot.data.service.device; -import com.qiuguo.iot.base.utils.StringUtils; import com.qiuguo.iot.data.entity.device.DeviceAlarmClockRecordEntity; import com.qiuguo.iot.data.request.device.DeviceAlarmClockRecordRequest; +import com.qiuguo.iot.base.utils.StringUtils; import lombok.extern.slf4j.Slf4j; import org.hswebframework.ezorm.core.param.Sort; import org.hswebframework.ezorm.rdb.mapping.ReactiveQuery; diff --git a/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/service/device/DeviceInfoService.java b/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/service/device/DeviceInfoService.java index 00a0dc3..1f9f10c 100644 --- a/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/service/device/DeviceInfoService.java +++ b/iot-common/iot-data/src/main/java/com/qiuguo/iot/data/service/device/DeviceInfoService.java @@ -16,9 +16,11 @@ import org.hswebframework.ezorm.rdb.operator.dml.query.SortOrder; import org.hswebframework.web.api.crud.entity.PagerResult; import org.hswebframework.web.api.crud.entity.QueryParamEntity; import org.hswebframework.web.crud.service.GenericReactiveCrudService; +import org.springframework.data.redis.core.ReactiveStringRedisTemplate; import org.springframework.stereotype.Service; import reactor.core.publisher.Mono; +import javax.annotation.Resource; import java.time.Duration; import java.util.Arrays; import java.util.Date; @@ -36,7 +38,8 @@ import java.util.Date; @Slf4j public class DeviceInfoService extends GenericReactiveCrudService { - + @Resource + ReactiveStringRedisTemplate reactiveStringRedisTemplate; public Mono selectDeviceInfoByRequest(DeviceInfoRequest request){ ReactiveQuery reactiveQuery = createQuery(); reactiveQuery = reactiveQuery.and("is_delete", 0); @@ -390,4 +393,17 @@ public class DeviceInfoService extends GenericReactiveCrudService getDeviceInfo(String sn){ + DeviceInfoRequest request = new DeviceInfoRequest(); + request.setSn(sn); + return selectDeviceInfoByRequest(request).defaultIfEmpty(new DeviceInfoEntity()).map(dv -> { + if(dv.getId() != null){ + String redis = JSONObject.toJSONString(dv); + reactiveStringRedisTemplate.opsForValue().set(RedisConstans.DEVICE_INFO + dv.getSn(), redis, Duration.ofHours(1)).subscribe();//直接提交订阅 + } + + return dv; + }); + } } diff --git a/iot-common/iot-third/src/main/java/com/qiuguo/iot/third/enums/ActionPartSpeechEnum.java b/iot-common/iot-third/src/main/java/com/qiuguo/iot/third/enums/ActionPartSpeechEnum.java index 2529bdd..2a0d8d3 100644 --- a/iot-common/iot-third/src/main/java/com/qiuguo/iot/third/enums/ActionPartSpeechEnum.java +++ b/iot-common/iot-third/src/main/java/com/qiuguo/iot/third/enums/ActionPartSpeechEnum.java @@ -694,7 +694,9 @@ public enum ActionPartSpeechEnum implements IChinesePartSpeech{ actions.setName(""); action.setLbs(new ArrayList<>()); action.setAsk(actions.getRecordText()); - } + }/*else{ + action.setSystemTalkAnswerConfigEntity(actions.getLastSystemTalkAnswerConfigEntity()); + }*/ int index = Integer.parseInt(key.replace(">", "")); action.setDeviceUserBindEntity(includs.get(index)); actions.setName(""); diff --git a/iot-common/iot-third/src/main/java/com/qiuguo/iot/third/nlp/action/Actions.java b/iot-common/iot-third/src/main/java/com/qiuguo/iot/third/nlp/action/Actions.java index e6f9ef9..1e53b1f 100644 --- a/iot-common/iot-third/src/main/java/com/qiuguo/iot/third/nlp/action/Actions.java +++ b/iot-common/iot-third/src/main/java/com/qiuguo/iot/third/nlp/action/Actions.java @@ -52,12 +52,15 @@ public class Actions { public void setLastSystemTalkAnswerConfigEntity(SystemTalkAnswerConfigEntity systemTalkAnswerConfigEntity){ if(systemTalkAnswerConfigEntity != null){ + lastSystemTalkAnswerConfigEntity = systemTalkAnswerConfigEntity; for(Action action : actions){ if(action.getSystemTalkAnswerConfigEntity() == null && StringUtils.isNotEmpty(action.getName())){ action.setSystemTalkAnswerConfigEntity(systemTalkAnswerConfigEntity); + action.setAction(systemTalkAnswerConfigEntity.getAskKey()); } } + } } } diff --git a/iot-modules/iot-box-user-api/src/main/java/com/qiuguo/iot/user/api/controller/device/DeviceController.java b/iot-modules/iot-box-user-api/src/main/java/com/qiuguo/iot/user/api/controller/device/DeviceController.java index db255fe..eff9bbf 100644 --- a/iot-modules/iot-box-user-api/src/main/java/com/qiuguo/iot/user/api/controller/device/DeviceController.java +++ b/iot-modules/iot-box-user-api/src/main/java/com/qiuguo/iot/user/api/controller/device/DeviceController.java @@ -24,15 +24,15 @@ import com.qiuguo.iot.data.service.device.DeviceUserTalkRecordService; import com.qiuguo.iot.third.service.TuyaDeviceConnector; import com.qiuguo.iot.user.api.resp.device.DeviceInitResp; import com.qiuguo.iot.user.api.rest.device.SetDeviceBindInfoRest; -import lombok.extern.slf4j.Slf4j; -import org.hswebframework.web.api.crud.entity.PagerResult; -import org.hswebframework.web.exception.BusinessException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.data.redis.core.ReactiveStringRedisTemplate; import org.springframework.data.redis.core.ReactiveValueOperations; import org.springframework.web.bind.annotation.*; import reactor.core.publisher.Mono; +import lombok.extern.slf4j.Slf4j; +import org.hswebframework.web.api.crud.entity.PagerResult; +import org.hswebframework.web.exception.BusinessException; import javax.annotation.Resource; import java.time.LocalDateTime; diff --git a/iot-modules/iot-box-websocket-api/src/main/java/com/qiuguo/iot/box/websocket/api/command/IotActionCommand.java b/iot-modules/iot-box-websocket-api/src/main/java/com/qiuguo/iot/box/websocket/api/command/IotActionCommand.java index 3401edf..eb51c68 100644 --- a/iot-modules/iot-box-websocket-api/src/main/java/com/qiuguo/iot/box/websocket/api/command/IotActionCommand.java +++ b/iot-modules/iot-box-websocket-api/src/main/java/com/qiuguo/iot/box/websocket/api/command/IotActionCommand.java @@ -35,6 +35,7 @@ public class IotActionCommand extends ActionCommand implements IActionCommand{ public Mono process(Action action, BaseSession baseSession) { + log.info("IOT物联网 Action:{}", action.getAsk()); if(StringUtils.isNotEmpty(action.getName())){ if(action.getDeviceUserBindEntity() == null){ log.info("匹配时未找到对应的设备,模糊匹配{}", action.getName()); diff --git a/iot-modules/iot-box-websocket-api/src/main/java/com/qiuguo/iot/box/websocket/api/command/QianWenActionCommand.java b/iot-modules/iot-box-websocket-api/src/main/java/com/qiuguo/iot/box/websocket/api/command/QianWenActionCommand.java index 668bec4..3d595dd 100644 --- a/iot-modules/iot-box-websocket-api/src/main/java/com/qiuguo/iot/box/websocket/api/command/QianWenActionCommand.java +++ b/iot-modules/iot-box-websocket-api/src/main/java/com/qiuguo/iot/box/websocket/api/command/QianWenActionCommand.java @@ -10,7 +10,7 @@ import reactor.core.publisher.Mono; @Slf4j public class QianWenActionCommand extends ActionCommand implements IActionCommand{ public Mono process(Action action, BaseSession baseSession) { - + log.info("千问 Action:{}", action.getAsk()); return toQianWen(action, baseSession, AskTypeEnum.TTS.getCode()).flatMap(vo ->{ //千问只调一次 diff --git a/iot-modules/iot-box-websocket-api/src/main/java/com/qiuguo/iot/box/websocket/api/command/QiuGuoActionCommand.java b/iot-modules/iot-box-websocket-api/src/main/java/com/qiuguo/iot/box/websocket/api/command/QiuGuoActionCommand.java index 571fbce..0e22dc4 100644 --- a/iot-modules/iot-box-websocket-api/src/main/java/com/qiuguo/iot/box/websocket/api/command/QiuGuoActionCommand.java +++ b/iot-modules/iot-box-websocket-api/src/main/java/com/qiuguo/iot/box/websocket/api/command/QiuGuoActionCommand.java @@ -10,6 +10,7 @@ import reactor.core.publisher.Mono; @Slf4j public class QiuGuoActionCommand extends ActionCommand implements IActionCommand{ public Mono process(Action action, BaseSession baseSession) { + log.info("自定义关键词Time Action:{}", action.getAsk()); return baseWebSocketService.sendMessage(action, baseSession, action.getSystemTalkAnswerConfigEntity().getAnswerValue(), diff --git a/iot-modules/iot-box-websocket-api/src/main/java/com/qiuguo/iot/box/websocket/api/command/WeatherActionCommand.java b/iot-modules/iot-box-websocket-api/src/main/java/com/qiuguo/iot/box/websocket/api/command/WeatherActionCommand.java index 33c2c94..0714ab9 100644 --- a/iot-modules/iot-box-websocket-api/src/main/java/com/qiuguo/iot/box/websocket/api/command/WeatherActionCommand.java +++ b/iot-modules/iot-box-websocket-api/src/main/java/com/qiuguo/iot/box/websocket/api/command/WeatherActionCommand.java @@ -22,6 +22,7 @@ public class WeatherActionCommand extends ActionCommand implements IActionComma @Resource protected WeatherService weatherService; public Mono process(Action action, BaseSession baseSession) { + log.info("天气 Action:{}", action.getAsk()); ThirdWeatherInfoRequest req = new ThirdWeatherInfoRequest(); //String city = ""; if(action.getLbs() != null && action.getLbs().size() > 0){ diff --git a/iot-modules/iot-box-websocket-api/src/main/java/com/qiuguo/iot/box/websocket/api/controller/WebsocketController.java b/iot-modules/iot-box-websocket-api/src/main/java/com/qiuguo/iot/box/websocket/api/controller/WebsocketController.java index 734c7d1..88fe3ac 100644 --- a/iot-modules/iot-box-websocket-api/src/main/java/com/qiuguo/iot/box/websocket/api/controller/WebsocketController.java +++ b/iot-modules/iot-box-websocket-api/src/main/java/com/qiuguo/iot/box/websocket/api/controller/WebsocketController.java @@ -9,6 +9,7 @@ import com.qiuguo.iot.base.enums.AskTypeEnum; import com.qiuguo.iot.base.utils.Md5Utils; import com.qiuguo.iot.base.utils.StringUtils; import com.qiuguo.iot.box.websocket.api.domain.box.BoxSession; +import com.qiuguo.iot.box.websocket.api.service.BaseWebSocketService; import com.qiuguo.iot.box.websocket.api.service.WebsocketService; import com.qiuguo.iot.data.entity.device.DeviceInfoEntity; import com.qiuguo.iot.data.request.device.DeviceInfoRequest; @@ -41,15 +42,15 @@ public class WebsocketController { @Autowired LacNlpService lacNlpService; + @Autowired + BaseWebSocketService baseWebSocketService; + @Autowired SystemTalkAnswerConfigService systemTalkAnswerConfigService; @Autowired private ReactiveStringRedisTemplate reactiveStringRedisTemplate; - @Resource - private DeviceInfoService deviceInfoService; - @Value("${device.checkTimeout}") private Boolean checkTimeout; @@ -82,18 +83,7 @@ public class WebsocketController { return lacNlpService.geSingletNlp(value); } - private Mono getDeviceInfo(String sn){ - DeviceInfoRequest request = new DeviceInfoRequest(); - request.setSn(sn); - return deviceInfoService.selectDeviceInfoByRequest(request).defaultIfEmpty(new DeviceInfoEntity()).map(dv -> { - if(dv.getId() != null){ - String redis = JSONObject.toJSONString(dv); - reactiveStringRedisTemplate.opsForValue().set(RedisConstans.DEVICE_INFO + dv.getSn(), redis, Duration.ofHours(1)).subscribe();//直接提交订阅 - } - return dv; - }); - } @GetMapping("/tts/token") public Mono getTtsToken(ServerHttpRequest serverHttpRequest) { @@ -107,64 +97,35 @@ public class WebsocketController { log.info("设备{},请求数据已超时", sn); return Mono.just("请求超时"); } - - return reactiveStringRedisTemplate.opsForValue() - .get(RedisConstans.DEVICE_INFO + sn).defaultIfEmpty("") - .flatMap(s -> { - if(com.qiuguo.iot.base.utils.StringUtils.isNotBlank(s)){ - try{ - DeviceInfoEntity dv = JSONObject.parseObject(s, DeviceInfoEntity.class); - if(dv.getId() == null){ - log.info("redis设备缓存异常,清楚"); - return getDeviceInfo(sn); + return baseWebSocketService.checkBoxSignature(sn, signature, linkTime).flatMap(b -> { + if(b.getId() != null){ + return reactiveStringRedisTemplate.opsForValue() + .get(RedisConstans.ALI_TTS_TOKEN).defaultIfEmpty("") + .flatMap(toen -> { + if(StringUtils.isNotBlank(toen)){ + return Mono.just(toen); } - return Mono.just(dv); - }catch (Exception e){ - log.info("转换异常,清除redis。下次连接成功{}", e); + try { + AccessToken accessToken = new AccessToken(aliAccessKeyId, aliAccessSecret); - } + accessToken.apply(); - } - return getDeviceInfo(sn); - - }).flatMap(dv ->{ - - String signalMd5 = Md5Utils.getBoxSignal(sn, dv.getWifiMac(), dv.getBtMac(), dv.getKey(), linkTime); - if(!signalMd5.equals(signature)){ - log.info("设备{},验签失败。正常签:{}", sn, signalMd5); - - return Mono.just("验签失败"); - }else{ - log.info("设备{},验签成功", sn); - return reactiveStringRedisTemplate.opsForValue() - .get(RedisConstans.ALI_TTS_TOKEN).defaultIfEmpty("") - .flatMap(toen -> { - if(StringUtils.isNotBlank(toen)){ - return Mono.just(toen); - } - try { - AccessToken accessToken = new AccessToken(aliAccessKeyId, aliAccessSecret); - - - accessToken.apply(); - - String token = accessToken.getToken(); - long expireTime = accessToken.getExpireTime() - 600000L;//提前10分钟更新 - //return Mono.just(token); - return reactiveStringRedisTemplate.opsForValue().set(RedisConstans.ALI_TTS_TOKEN, - token, Duration.ofMillis(expireTime)).flatMap(t -> { - return Mono.just(token); - }); - - }catch (Exception e){ - log.info("获取阿里Token异常{}", e); - } - return Mono.just("获取token异常"); + String token = accessToken.getToken(); + long expireTime = accessToken.getExpireTime() - 600000L;//提前10分钟更新 + return reactiveStringRedisTemplate.opsForValue().set(RedisConstans.ALI_TTS_TOKEN, + token, Duration.ofMillis(expireTime)).flatMap(t -> { + return Mono.just(token); }); - } - }); + }catch (Exception e){ + log.info("获取阿里Token异常{}", e); + } + return Mono.just("获取token异常"); + }); + } + return Mono.just("验签错误"); + }); } } diff --git a/iot-modules/iot-box-websocket-api/src/main/java/com/qiuguo/iot/box/websocket/api/handler/BoxWebSocketHandler.java b/iot-modules/iot-box-websocket-api/src/main/java/com/qiuguo/iot/box/websocket/api/handler/BoxWebSocketHandler.java index 09dc2dd..529f3ee 100644 --- a/iot-modules/iot-box-websocket-api/src/main/java/com/qiuguo/iot/box/websocket/api/handler/BoxWebSocketHandler.java +++ b/iot-modules/iot-box-websocket-api/src/main/java/com/qiuguo/iot/box/websocket/api/handler/BoxWebSocketHandler.java @@ -7,6 +7,7 @@ import com.qiuguo.iot.base.constans.RedisConstans; import com.qiuguo.iot.base.enums.*; import com.qiuguo.iot.base.model.UserDeviceInfoModel; import com.qiuguo.iot.base.utils.Md5Utils; +import com.qiuguo.iot.base.utils.StringUtils; import com.qiuguo.iot.box.websocket.api.command.ActionCommand; import com.qiuguo.iot.box.websocket.api.domain.BaseSession; import com.qiuguo.iot.box.websocket.api.domain.box.BoxSession; @@ -107,7 +108,7 @@ public class BoxWebSocketHandler implements WebSocketHandler { //校验 - checkToken(boxSession, sn, linkTime, signature, userId, isBind).contextWrite(context -> { + checkToken(boxSession, linkTime, signature, isBind).contextWrite(context -> { Context contextTmp = context.put(LogMdcConfiguration.PRINT_LOG_ID, boxSession.getLogId()); return contextTmp; @@ -140,18 +141,14 @@ public class BoxWebSocketHandler implements WebSocketHandler { String text = webSocketMessage.getPayloadAsText(); log.info("设备端收到消息:{}", text); BoxTalkMessage boxTalkMessage = JSONObject.parseObject(text, BoxTalkMessage.class); - BoxSession boxSession1 = baseWebSocketService.getBoxSessionWithSn(boxTalkMessage.getSn()); - if(boxSession != boxSession1){ - log.info("消息发送异常,或者未验签就收到信息不是同一个链接。可能传错SN"); - return baseWebSocketService.closeSendMsg(boxSession, "请等待验签结束或者SN可能错误", AskTypeEnum.TTS.getCode()).flatMap(b -> { - return Mono.empty(); - }); - } + boxTalkMessage.setSn(boxSession.getSn()); log.info("收到SN:{},消息:{}", boxTalkMessage.getSn(), boxTalkMessage.getMessage()); - return nlpService.getActionWithLacSingle(boxSession.getUserId(), boxTalkMessage.getMessage()).defaultIfEmpty(new Actions()).flatMap(actions -> { + return nlpService.getActionWithLacSingle( + boxSession.getUserId(), + boxTalkMessage.getMessage() + ).defaultIfEmpty(new Actions()).flatMap(actions -> { boxSession.setRequestId(boxSession.getRequestId() + 1); return ActionCommand.processAction(actions, boxSession); - //return Mono.empty(); }); } @@ -177,70 +174,26 @@ public class BoxWebSocketHandler implements WebSocketHandler { }); } - log.info("被踢下线断开连接:{}", boxSession.getSn()); + log.info("下线断开连接:{}", boxSession.getSn()); return Mono.empty(); } - private Mono errorLogin(BaseSession boxSession, String sn){ - //清除异常redis - return reactiveStringRedisTemplate.opsForValue().delete(RedisConstans.DEVICE_INFO + sn).flatMap(i -> { - return baseWebSocketService.closeSendMsg(boxSession, "异常,请重新登录", AskTypeEnum.TTS.getCode()); - }); - - } - - private Mono checkToken(BoxSession boxSession, String sn, Long linkTime, String signature, Long userId, Integer isBind){ - return reactiveStringRedisTemplate.opsForValue().get(RedisConstans.DEVICE_INFO + sn).defaultIfEmpty("").flatMap(s -> { - if(com.qiuguo.iot.base.utils.StringUtils.isNotBlank(s)){ - try{ - DeviceInfoEntity dv = JSONObject.parseObject(s, DeviceInfoEntity.class); - if(dv.getId() == null){ - log.info("redis设备缓存异常,清楚"); - return errorLogin(boxSession, sn).flatMap(v -> { - return Mono.empty(); - }); + private Mono checkToken(BoxSession boxSession, Long linkTime, String signature, Integer isBind){ + return baseWebSocketService.checkBoxSignature(boxSession.getSn(), signature, linkTime).flatMap(dv -> { + if(dv.getId() != null){ + boxSession.setDeviceId(dv.getId()); + BoxSession oldBoxSession = baseWebSocketService.putBoxSession(boxSession.getSn(), boxSession); + return bindBox(boxSession, dv, boxSession.getUserId(), isBind).flatMap(db ->{ + if(oldBoxSession != null){ + return baseWebSocketService.closeSendMsg(oldBoxSession, "您在其他地方登录", AskTypeEnum.TTS.getCode()); } - return Mono.just(dv); - }catch (Exception e){ - log.info("转换异常,清除redis。下次连接成功{}", e); - - return errorLogin(boxSession, sn).flatMap(v -> { - return Mono.empty(); - }); - } + return Mono.empty(); + }); } - DeviceInfoRequest request = new DeviceInfoRequest(); - request.setSn(sn); - return deviceInfoService.selectDeviceInfoByRequest(request).defaultIfEmpty(new DeviceInfoEntity()).map(dv -> { - if(dv.getId() != null){ - String redis = JSONObject.toJSONString(dv); - reactiveStringRedisTemplate.opsForValue().set(RedisConstans.DEVICE_INFO + dv.getSn(), redis, Duration.ofHours(1)).subscribe();//直接提交订阅 - } - - return dv; - }); - }).flatMap(dv ->{ - String signalMd5 = Md5Utils.getBoxSignal(sn, dv.getWifiMac(), dv.getBtMac(), dv.getKey(), linkTime); - if(!signalMd5.equals(signature)){ - log.info("设备{},验签失败。正常签:{}", sn, signalMd5); - if(boxSession != null){ - return baseWebSocketService.closeSendMsg(boxSession, "验签失败", AskTypeEnum.TTS.getCode()); - } - return Mono.just(dv); - }else{ - log.info("设备{},验签成功", sn); - return Mono.just(dv); + if(boxSession != null){ + return baseWebSocketService.closeSendMsg(boxSession, "验签失败", AskTypeEnum.TTS.getCode()); } - }).flatMap(d -> { - DeviceInfoEntity dv = (DeviceInfoEntity)d; - boxSession.setDeviceId(dv.getId()); - BoxSession oldBoxSession = baseWebSocketService.putBoxSession(sn, boxSession); - return bindBox(boxSession, dv, userId, isBind).flatMap(db ->{ - if(oldBoxSession != null){ - return baseWebSocketService.closeSendMsg(oldBoxSession, "您在其他地方登录", AskTypeEnum.TTS.getCode()); - } - return Mono.empty(); - }); + return Mono.empty(); }); } @@ -273,8 +226,6 @@ public class BoxWebSocketHandler implements WebSocketHandler { }else{ boxSession.setTtsId(entity.getTts()); - - if(entity.getIsBind().equals(YesNo.YES.getCode())){ //通知用户端设备绑定成功 sendNoticeToUser(userId, "设备联网成功,设备序列号:" + dv.getSn(), AskTypeEnum.BOX_ON_LINE.getCode()); diff --git a/iot-modules/iot-box-websocket-api/src/main/java/com/qiuguo/iot/box/websocket/api/handler/CustomerWebSocketHandler.java b/iot-modules/iot-box-websocket-api/src/main/java/com/qiuguo/iot/box/websocket/api/handler/CustomerWebSocketHandler.java index bece4f7..72770df 100644 --- a/iot-modules/iot-box-websocket-api/src/main/java/com/qiuguo/iot/box/websocket/api/handler/CustomerWebSocketHandler.java +++ b/iot-modules/iot-box-websocket-api/src/main/java/com/qiuguo/iot/box/websocket/api/handler/CustomerWebSocketHandler.java @@ -90,7 +90,7 @@ public class CustomerWebSocketHandler implements WebSocketHandler { }).then(); - checkToken(userSession, type, token, userId).contextWrite(context -> { + checkToken(userSession, type, token).contextWrite(context -> { Context contextTmp = context.put(LogMdcConfiguration.PRINT_LOG_ID, userSession.getLogId()); return contextTmp; @@ -115,16 +115,12 @@ public class CustomerWebSocketHandler implements WebSocketHandler { String text = webSocketMessage.getPayloadAsText(); log.info("收到用户消息:{}", text); UserTalkMessage userTalkMessage = JSONObject.parseObject(text, UserTalkMessage.class); - UserSession userSession1 = baseWebSocketService.getUserSessionWithUserId(userTalkMessage.getUserId()); - if(!userSession.equals(userSession1)){ - log.info("消息发送异常,或者未验签就收到信息不是同一个链接。可能传错用户ID"); - return baseWebSocketService.closeSendMsg(userSession, "请等待验签结束或者用户ID可能错误", AskTypeEnum.TTS.getCode()).flatMap(b -> { - return Mono.empty(); - }); - } + userTalkMessage.setUserId(userSession.getUserId()); log.info("收到用户userId:{},消息:{}", userTalkMessage.getUserId(), userTalkMessage.getMessage()); - return nlpService.getActionWithLacSingle(userSession.getUserId(), userTalkMessage.getMessage()) - .defaultIfEmpty(new Actions()).flatMap(actions -> { + return nlpService.getActionWithLacSingle( + userSession.getUserId(), + userTalkMessage.getMessage() + ).defaultIfEmpty(new Actions()).flatMap(actions -> { //处理 userSession.setRequestId(userSession.getRequestId() + 1); return ActionCommand.processAction(actions, userSession); @@ -141,7 +137,7 @@ public class CustomerWebSocketHandler implements WebSocketHandler { return Mono.empty(); } - private Mono checkToken(UserSession userSession, String type, String token, Long userId){ + private Mono checkToken(UserSession userSession, String type, String token){ Map reqHead = new HashMap<>(); reqHead.put(HttpHeaderConstans.API_TYPE, type); reqHead.put(HttpHeaderConstans.API_TOKEN, token); @@ -149,12 +145,12 @@ public class CustomerWebSocketHandler implements WebSocketHandler { log.info("验签获取的数据{}", jsonObject); if(jsonObject.getInteger("code").equals(ResponeEnum.SUCESS.getCode())){ Long userId1 = jsonObject.getJSONObject("data").getLong("id"); - if(userId1.equals(userId)){ - log.info("验签成功{}", userId); + if(userId1.equals(userSession.getUserId())){ + log.info("验签成功{}", userSession.getUserId()); DeviceUserBindRequest request = new DeviceUserBindRequest(); - request.setUserId(userId); + request.setUserId(userSession.getUserId()); request.setDeviceType(DeviceTypeEnum.GUO_BOX.getCode()); request.setIsMain(YesNo.YES.getCode()); @@ -170,7 +166,7 @@ public class CustomerWebSocketHandler implements WebSocketHandler { }else{ baseWebSocketService.normalSendMsg(userSession, "您暂未绑定果宝儿Box,快去绑定吧", AskTypeEnum.TTS.getCode()); } - UserSession oldUserSession = baseWebSocketService.putUserSession(userId, userSession); + UserSession oldUserSession = baseWebSocketService.putUserSession(userSession.getUserId(), userSession); if(oldUserSession != null){ return baseWebSocketService.closeSendMsg(oldUserSession, "您在其他地方登录", AskTypeEnum.TTS.getCode()); @@ -179,7 +175,7 @@ public class CustomerWebSocketHandler implements WebSocketHandler { }); } } - log.info("验签失败{}", userId); + log.info("验签失败{}", userSession.getUserId()); return baseWebSocketService.closeSendMsg(userSession, "非法登录", AskTypeEnum.TTS.getCode()).flatMap(b -> { return Mono.empty(); }); diff --git a/iot-modules/iot-box-websocket-api/src/main/java/com/qiuguo/iot/box/websocket/api/service/BaseWebSocketService.java b/iot-modules/iot-box-websocket-api/src/main/java/com/qiuguo/iot/box/websocket/api/service/BaseWebSocketService.java index 80bb995..9d24972 100644 --- a/iot-modules/iot-box-websocket-api/src/main/java/com/qiuguo/iot/box/websocket/api/service/BaseWebSocketService.java +++ b/iot-modules/iot-box-websocket-api/src/main/java/com/qiuguo/iot/box/websocket/api/service/BaseWebSocketService.java @@ -1,25 +1,34 @@ package com.qiuguo.iot.box.websocket.api.service; import com.alibaba.fastjson.JSONObject; +import com.alibaba.nls.client.AccessToken; +import com.qiuguo.iot.base.constans.RedisConstans; import com.qiuguo.iot.base.enums.AskTypeEnum; import com.qiuguo.iot.base.enums.YesNo; +import com.qiuguo.iot.base.utils.Md5Utils; import com.qiuguo.iot.base.utils.StringUtils; import com.qiuguo.iot.box.websocket.api.domain.BaseMessageResp; import com.qiuguo.iot.box.websocket.api.domain.BaseSession; import com.qiuguo.iot.box.websocket.api.domain.box.BoxSession; import com.qiuguo.iot.box.websocket.api.domain.box.resp.BoxMessageResp; import com.qiuguo.iot.box.websocket.api.domain.user.UserSession; +import com.qiuguo.iot.data.entity.device.DeviceInfoEntity; import com.qiuguo.iot.data.entity.device.DeviceUserTalkRecordEntity; +import com.qiuguo.iot.data.request.device.DeviceInfoRequest; +import com.qiuguo.iot.data.service.device.DeviceInfoService; import com.qiuguo.iot.data.service.device.DeviceUserTalkRecordService; import com.qiuguo.iot.third.nlp.action.Action; import com.qiuguo.iot.third.service.AudioService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeanUtils; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; +import org.springframework.data.redis.core.ReactiveStringRedisTemplate; import org.springframework.stereotype.Service; import reactor.core.publisher.Mono; import javax.annotation.Resource; +import java.time.Duration; import java.util.concurrent.ConcurrentHashMap; @Service @@ -37,12 +46,20 @@ public class BaseWebSocketService { @Resource AudioService audioService; + @Resource + DeviceInfoService deviceInfoService; + @Resource protected DeviceUserTalkRecordService deviceUserTalkRecordService; + @Autowired + private ReactiveStringRedisTemplate reactiveStringRedisTemplate; + protected static int ONE_MAX_TEXT = 30; + + public BoxSession getBoxSessionWithSn(String sn) { if(boxGroup.containsKey(sn)){ return boxGroup.get(sn); @@ -305,4 +322,36 @@ public class BaseWebSocketService { return s; });//.subscribeOn(Schedulers.single()).subscribe(); } + + public Mono checkBoxSignature(String sn, String signature, Long linkTime){ + return reactiveStringRedisTemplate.opsForValue() + .get(RedisConstans.DEVICE_INFO + sn).defaultIfEmpty("") + .flatMap(s -> { + if(StringUtils.isNotBlank(s)){ + try{ + DeviceInfoEntity dv = JSONObject.parseObject(s, DeviceInfoEntity.class); + if(dv.getId() != null){ + return Mono.just(dv); + } + log.info("redis设备缓存异常,清楚"); + }catch (Exception e){ + log.info("转换异常,清除redis。下次连接成功{}", e); + } + } + return deviceInfoService.getDeviceInfo(sn); + }).map(dv ->{ + if(dv.getId() != null){ + String signalMd5 = Md5Utils.getBoxSignal(sn, dv.getWifiMac(), dv.getBtMac(), dv.getKey(), linkTime); + if(!signalMd5.equals(signature)){ + log.info("设备{},验签失败。正常签:{}", sn, signalMd5); + return new DeviceInfoEntity(); + }else{ + log.info("设备{},验签成功", sn); + } + }else{ + log.info("设备不存在{}", sn); + } + return dv; + }); + } } diff --git a/iot-modules/iot-box-websocket-api/src/main/resources/bootstrap-dev.yml b/iot-modules/iot-box-websocket-api/src/main/resources/bootstrap-dev.yml index fe6656c..f3ee731 100644 --- a/iot-modules/iot-box-websocket-api/src/main/resources/bootstrap-dev.yml +++ b/iot-modules/iot-box-websocket-api/src/main/resources/bootstrap-dev.yml @@ -43,7 +43,7 @@ qiuguo: checktoken: url: https://qiuguo-app.pre.qiuguojihua.com/pre-api/user/user/getUser tts: - suanfa: true + suanfa: false url: http://192.168.8.211:18000/run/predict #算法语音合成 downurl: http://192.168.8.211:8880 #算法语音合成后资源下载路径前缀 lac: diff --git a/iot-modules/iot-customer-http-api/src/test/java/com/qiuguo/iot/customer/http/api/mysql/MysqlUtilTable2Contoller.java b/iot-modules/iot-customer-http-api/src/test/java/com/qiuguo/iot/customer/http/api/mysql/MysqlUtilTable2Contoller.java index 13d62bc..ed5efef 100644 --- a/iot-modules/iot-customer-http-api/src/test/java/com/qiuguo/iot/customer/http/api/mysql/MysqlUtilTable2Contoller.java +++ b/iot-modules/iot-customer-http-api/src/test/java/com/qiuguo/iot/customer/http/api/mysql/MysqlUtilTable2Contoller.java @@ -125,8 +125,11 @@ public class MysqlUtilTable2Contoller { List list = tableBean.getFieldList(); try { String content = "package com.admin.service.impl;\n\n\n\n" + - "import org.apache.commons.lang3.StringUtils;\n" + - "import java.util.Date;\n"; + "import java.util.Date;\n" + + "import reactor.core.publisher.Mono;\n" + + "import lombok.extern.slf4j.Slf4j;\n" + + "import org.hswebframework.web.api.crud.entity.PagerResult;\n" + + "import org.hswebframework.web.exception.BusinessException;\n"; content += "/**\n"; diff --git a/iot-modules/iot-customer-http-api/src/test/java/com/qiuguo/iot/customer/http/api/mysql/MysqlUtilTable2Service.java b/iot-modules/iot-customer-http-api/src/test/java/com/qiuguo/iot/customer/http/api/mysql/MysqlUtilTable2Service.java index b7160c3..9f40a8a 100644 --- a/iot-modules/iot-customer-http-api/src/test/java/com/qiuguo/iot/customer/http/api/mysql/MysqlUtilTable2Service.java +++ b/iot-modules/iot-customer-http-api/src/test/java/com/qiuguo/iot/customer/http/api/mysql/MysqlUtilTable2Service.java @@ -31,6 +31,19 @@ public class MysqlUtilTable2Service { List list = tableBean.getFieldList(); try { String content = "package com.admin.service.impl;\n\n\n\n" + + "import java.util.Date;\n" + "import com.qiuguo.iot.base.utils.StringUtils;\n" + + "import lombok.extern.slf4j.Slf4j;\n" + + "import org.hswebframework.ezorm.core.param.Sort;\n" + + "import org.hswebframework.ezorm.rdb.mapping.ReactiveQuery;\n" + + "import org.hswebframework.ezorm.rdb.mapping.ReactiveUpdate;\n" + + "import org.hswebframework.ezorm.rdb.operator.dml.query.SortOrder;\n" + + "import org.hswebframework.web.api.crud.entity.PagerResult;\n" + + "import org.hswebframework.web.api.crud.entity.QueryParamEntity;\n" + + "import org.hswebframework.web.crud.service.GenericReactiveCrudService;\n" + + "import org.springframework.stereotype.Service;\n" + + "import reactor.core.publisher.Mono;\n" + + "\n" + + "import java.util.Arrays;\n" + "import java.util.Date;\n"; diff --git a/iot-modules/pom.xml b/iot-modules/pom.xml index 8ad13aa..6ff50c5 100644 --- a/iot-modules/pom.xml +++ b/iot-modules/pom.xml @@ -39,12 +39,6 @@ org.springframework.cloud spring-cloud-starter-bootstrap - - - ch.qos.logback - logback-classic - - ${spring.cloud.version} @@ -56,12 +50,6 @@ org.springframework.boot spring-boot-starter-data-redis-reactive - - - ch.qos.logback - logback-classic - - ${redis.boot.reactor.version} @@ -69,24 +57,12 @@ org.springframework.boot spring-boot-starter-actuator - - - ch.qos.logback - logback-classic - - org.springframework.boot spring-boot-starter-data-r2dbc - - - ch.qos.logback - logback-classic - - ${spring.boot.version} From 00ce4c37314d43bd215b88f2b135ee7e8340df89 Mon Sep 17 00:00:00 2001 From: wulin Date: Wed, 25 Oct 2023 10:05:07 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=8E=BB=E6=8E=89=E8=AF=AD=E5=8F=A5?= =?UTF-8?q?=E7=9A=84=E5=A4=B4=E5=B0=BE=E6=A0=87=E7=82=B9=E7=AC=A6=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../websocket/api/service/BaseWebSocketService.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/iot-modules/iot-box-websocket-api/src/main/java/com/qiuguo/iot/box/websocket/api/service/BaseWebSocketService.java b/iot-modules/iot-box-websocket-api/src/main/java/com/qiuguo/iot/box/websocket/api/service/BaseWebSocketService.java index 9d24972..2eade2c 100644 --- a/iot-modules/iot-box-websocket-api/src/main/java/com/qiuguo/iot/box/websocket/api/service/BaseWebSocketService.java +++ b/iot-modules/iot-box-websocket-api/src/main/java/com/qiuguo/iot/box/websocket/api/service/BaseWebSocketService.java @@ -217,6 +217,14 @@ public class BaseWebSocketService { //标点符号起始会导致合成的声音第一句话有杂音 text = text.substring(1); } + if(text.endsWith(",") || + text.endsWith("。") || + text.endsWith(":") || + text.endsWith(";") || + text.endsWith("《") || + text.endsWith(" ")){ + text = text.substring(0, text.length() - 1); + } if(text.length() > ONE_MAX_TEXT){ StringBuilder builder = new StringBuilder(); sendAudioMessage(baseSession, @@ -229,6 +237,7 @@ public class BaseWebSocketService { }else{ BoxMessageResp boxMessageResp = new BoxMessageResp(); BeanUtils.copyProperties(baseMessageResp, boxMessageResp); + boxMessageResp.setText(text); sendAudioMessage(baseSession, boxMessageResp).subscribe(); } return; @@ -354,4 +363,5 @@ public class BaseWebSocketService { return dv; }); } + }