注解读取改成代码读取
This commit is contained in:
parent
69205be463
commit
54afedabfc
@ -15,6 +15,8 @@ public class Actions {
|
||||
|
||||
private List<Action> actions;//动作解析
|
||||
|
||||
public Actions() {}
|
||||
|
||||
public Actions(Nlp nlp){
|
||||
actions = new ArrayList<>();
|
||||
nlp.getKeys().sort(Comparator.comparing(NlpKey::getType)); //解析,按照type从小到大排序
|
||||
|
||||
@ -34,11 +34,12 @@ import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
|
||||
public class LacNlpService implements INlp {
|
||||
private static WebClient webClient = WebClient.builder()
|
||||
.defaultHeader(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON_VALUE).build();
|
||||
@Value("${lac.url}")
|
||||
private String url;
|
||||
//免得注入没有相关配置报错
|
||||
// @Value("${lac.url}")
|
||||
// private String url;
|
||||
|
||||
private Mono<JSONObject> getNlpFromLac(LacRequest request){
|
||||
return webClient.post().uri(url + "/predict/lac").bodyValue(JSONObject.toJSON(request))
|
||||
return webClient.post().uri(SpringUtil.getProperty("lac.url") + "/predict/lac").bodyValue(JSONObject.toJSON(request))
|
||||
.retrieve()
|
||||
.bodyToMono(JSONObject.class).doOnNext(res -> {
|
||||
if (!Objects.equals(res.getInteger("status"), 0)) {
|
||||
|
||||
@ -13,10 +13,14 @@ import com.qiuguo.iot.data.entity.device.DeviceUserBindEntity;
|
||||
import com.qiuguo.iot.data.entity.system.SystemTalkAnswerConfigEntity;
|
||||
import com.qiuguo.iot.data.request.device.DeviceInfoRequest;
|
||||
import com.qiuguo.iot.data.request.device.DeviceUserBindRequest;
|
||||
import com.qiuguo.iot.data.request.user.UserHandlingDeviceRequest;
|
||||
import com.qiuguo.iot.data.service.device.DeviceInfoService;
|
||||
import com.qiuguo.iot.data.service.device.DeviceUserBindService;
|
||||
import com.qiuguo.iot.data.service.system.SystemTalkAnswerConfigService;
|
||||
import com.qiuguo.iot.data.service.user.UserHandlingDeviceService;
|
||||
import com.qiuguo.iot.third.nlp.action.Actions;
|
||||
import com.qiuguo.iot.third.service.NlpService;
|
||||
import com.qiuguo.iot.third.service.TuyaDeviceService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.slf4j.MDC;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
@ -55,6 +59,12 @@ public class BoxWebSocketHandler implements WebSocketHandler {
|
||||
@Resource
|
||||
private DeviceUserBindService deviceUserBindService;
|
||||
|
||||
@Resource
|
||||
private UserHandlingDeviceService userHandlingDeviceService;
|
||||
|
||||
@Resource
|
||||
private TuyaDeviceService TuyaDeviceService;
|
||||
|
||||
public static ConcurrentHashMap<String, BoxSession> group = new ConcurrentHashMap<>();
|
||||
|
||||
@Override
|
||||
@ -70,14 +80,11 @@ public class BoxWebSocketHandler implements WebSocketHandler {
|
||||
return session.close();
|
||||
}
|
||||
String signature = headers.get("signature").get(0);
|
||||
String token = null;
|
||||
List<String> tokens = headers.get("token");
|
||||
if(tokens != null && tokens.size() > 0){
|
||||
token = tokens.get(0);//用户token
|
||||
}
|
||||
Long userId = Long.parseLong(headers.get("userId").get(0));
|
||||
|
||||
|
||||
//校验
|
||||
checkToken(sn, linkTime, signature, token);
|
||||
checkToken(sn, linkTime, signature, userId);
|
||||
//
|
||||
|
||||
log.info("登录成功SN:{}", sn);
|
||||
@ -86,22 +93,34 @@ public class BoxWebSocketHandler implements WebSocketHandler {
|
||||
String text = webSocketMessage.getPayloadAsText();
|
||||
log.info("设备端收到消息:{}", text);
|
||||
BoxTalkMessage boxTalkMessage = JSONObject.parseObject(text, BoxTalkMessage.class);
|
||||
nlpService.getActionWithLacSingle(boxTalkMessage.getMessage()).map(actions -> {
|
||||
nlpService.getActionWithLacSingle(boxTalkMessage.getMessage()).defaultIfEmpty(new Actions()).map(actions -> {
|
||||
//处理
|
||||
if(actions.getActions().size() > 0){
|
||||
SystemTalkAnswerConfigEntity talkAnswerConfigEntity =
|
||||
systemTalkAnswerConfigService.getSystemTalkWithKey(actions.getActions().get(0).getAction());
|
||||
|
||||
log.info("匹配到自定义指令{}", talkAnswerConfigEntity);
|
||||
}else{
|
||||
if(actions.getActions() == null || actions.getActions().size() == 0){
|
||||
//调用千问回答
|
||||
log.info("未匹配到自定义命令,调用千问");
|
||||
}else{
|
||||
//目前只处理第一条动作
|
||||
SystemTalkAnswerConfigEntity talkAnswerConfigEntity =
|
||||
systemTalkAnswerConfigService.getSystemTalkWithKey(actions.getActions().get(0).getAction());
|
||||
DeviceUserBindRequest deviceUserBindRequest = new DeviceUserBindRequest();
|
||||
deviceUserBindRequest.setUserId(userId);
|
||||
deviceUserBindRequest.setPageSize(2);
|
||||
deviceUserBindRequest.setBindName(actions.getActions().get(0).getName().get(0));
|
||||
deviceUserBindService.selectDeviceUserBindsByRequest(deviceUserBindRequest).map(binds ->{
|
||||
if(binds.getTotal() > 1){
|
||||
//返回告诉有多个设备,请详细说明具体说明设备
|
||||
}else{
|
||||
UserHandlingDeviceRequest request = new UserHandlingDeviceRequest();
|
||||
|
||||
//userHandlingDeviceService.selectUserHandlingDeviceByRequest()
|
||||
}
|
||||
return Mono.empty();
|
||||
});
|
||||
|
||||
log.info("匹配到自定义指令{}", talkAnswerConfigEntity);
|
||||
}
|
||||
|
||||
return Mono.empty();
|
||||
}).thenEmpty(empty ->{
|
||||
//调用千问回答
|
||||
log.info("未匹配到自定义命令,调用千问");
|
||||
}).subscribe();
|
||||
|
||||
log.info("收到SN:{},消息{}", boxTalkMessage.getSn(), boxTalkMessage.getMessage());
|
||||
@ -126,7 +145,7 @@ public class BoxWebSocketHandler implements WebSocketHandler {
|
||||
}).then();
|
||||
}
|
||||
|
||||
private void checkToken(String sn, Long linkTime, String signature, String token){
|
||||
private void checkToken(String sn, Long linkTime, String signature, Long userId){
|
||||
ReactiveValueOperations<String, String> operations = reactiveStringRedisTemplate.opsForValue();
|
||||
operations.get(RedisConstans.DEVICE_INFO + sn).defaultIfEmpty("").flatMap(s -> {
|
||||
if(com.qiuguo.iot.base.utils.StringUtils.isNotBlank(s)){
|
||||
@ -160,35 +179,33 @@ public class BoxWebSocketHandler implements WebSocketHandler {
|
||||
}
|
||||
boxSession.getSession().close().subscribe();
|
||||
}else{
|
||||
bindBox(dv, token);
|
||||
bindBox(dv, userId);
|
||||
}
|
||||
return Mono.empty();
|
||||
}).subscribe();
|
||||
}
|
||||
|
||||
private void bindBox(DeviceInfoEntity dv, String token){
|
||||
if(StringUtils.isNotEmpty(token)){
|
||||
log.info("开始绑定设备userToken:{}, SN:{}", token, dv);
|
||||
Long userId = getUserIdWithToken(token);
|
||||
DeviceUserBindRequest request = new DeviceUserBindRequest();
|
||||
request.setUserId(userId);
|
||||
request.setDeviceId(dv.getId());
|
||||
deviceUserBindService.selectDeviceUserBindByRequest(request).thenEmpty(other ->{
|
||||
DeviceUserBindEntity entity = new DeviceUserBindEntity();
|
||||
entity.setUserId(userId);
|
||||
entity.setDeviceId(dv.getId());
|
||||
deviceUserBindService.insertDeviceUserBind(entity).map(l ->{
|
||||
log.info("绑定成功userToken:{}, SN:{} userId:{}", token, dv, userId);
|
||||
return Mono.empty();
|
||||
private void bindBox(DeviceInfoEntity dv, Long userId){
|
||||
|
||||
log.info("开始绑定设备userId:{}, SN:{}", userId, dv);
|
||||
DeviceUserBindRequest request = new DeviceUserBindRequest();
|
||||
request.setUserId(userId);
|
||||
request.setDeviceId(dv.getId());
|
||||
|
||||
deviceUserBindService.selectDeviceUserBindByRequest(request)
|
||||
.defaultIfEmpty(new DeviceUserBindEntity())
|
||||
.map(entity ->{
|
||||
if(entity.getId() == null){
|
||||
entity.setUserId(userId);
|
||||
entity.setDeviceId(dv.getId());
|
||||
return deviceUserBindService.insertDeviceUserBind(entity).map(l ->{
|
||||
log.info("绑定成功SN:{} userId:{}", dv, userId);
|
||||
return entity;
|
||||
}).subscribe();
|
||||
}
|
||||
return entity;
|
||||
}).subscribe();
|
||||
|
||||
}).subscribe();
|
||||
}
|
||||
}
|
||||
|
||||
private Long getUserIdWithToken(String token){
|
||||
//暂时勰思,灯token写过来在写入
|
||||
return 6025l;
|
||||
}
|
||||
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user