增加天气香型

This commit is contained in:
wulin 2023-10-16 10:49:03 +08:00
parent 9a3e7691c8
commit fd4b22607e
9 changed files with 138 additions and 39 deletions

View File

@ -0,0 +1,12 @@
package com.qiuguo.iot.third.nlp.lac;
import lombok.Data;
import java.util.List;
@Data
public class HubLacRequest {
List<String> text;//请求内容
Integer batch_size = 1;
Boolean use_gpu = false;
}

View File

@ -5,7 +5,6 @@ import lombok.Data;
import java.util.List;
@Data
public class LacRequest {
List<String> text;//请求内容
Integer batch_size = 1;
Boolean use_gpu = false;
String test_text;//请求内容
String task_type = "lac_word";
}

View File

@ -8,6 +8,7 @@ import com.qiuguo.iot.third.nlp.INlp;
import com.qiuguo.iot.third.nlp.Nlp;
import com.qiuguo.iot.third.nlp.NlpKey;
import com.qiuguo.iot.third.nlp.lac.HubLacRequest;
import com.qiuguo.iot.third.nlp.lac.LacRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
@ -37,8 +38,8 @@ public class LacNlpService implements INlp {
// @Value("${lac.url}")
// private String url;
private Mono<JSONObject> getNlpFromLac(LacRequest request){
return WebClientUtils.post(SpringUtil.getProperty("lac.url"), (JSONObject) JSONObject.toJSON(request)).map(
private Mono<JSONObject> getNlpFromHubLac(HubLacRequest request){
return WebClientUtils.post(SpringUtil.getProperty("lac.hub.url"), (JSONObject) JSONObject.toJSON(request)).map(
jsonObject -> {
if (!Objects.equals(jsonObject.getInteger("status"), 0)) {
throw new RuntimeException(jsonObject.getString("info"));
@ -48,14 +49,52 @@ public class LacNlpService implements INlp {
);
}
private Mono<JSONObject> getNlpFromSuanFaLac(LacRequest request){
return WebClientUtils.post(SpringUtil.getProperty("lac.suanfa.url"), (JSONObject) JSONObject.toJSON(request)).map(
jsonObject -> {
if (!Objects.equals(jsonObject.getInteger("status_code"), 0)) {
throw new RuntimeException(jsonObject.getString("info"));
}
return jsonObject;
}
);
}
@Override
public Mono<Nlp> geSingletNlp(String value) {
return getSuanFaLac(value);
}
/**
* 算法提供的lac调用
* @param value
* @return
*/
private Mono<Nlp> getSuanFaLac(String value){
LacRequest request = new LacRequest();
request.setTest_text(value);
Mono<JSONObject> mono = getNlpFromSuanFaLac(request);
Mono<Nlp> nlpKeyMono = mono.map(jsonObject -> {
Nlp nlp = new Nlp();
nlp.setKeys(getKeys(jsonObject));
return nlp;
});
return nlpKeyMono;
}
/**
* 百度Hub提供的调用
* @param value
* @return
*/
private Mono<Nlp> getHubFaLac(String value){
List<String> text = new ArrayList<>();
text.add(value);
LacRequest request = new LacRequest();
HubLacRequest request = new HubLacRequest();
request.setText(text);
Mono<JSONObject> mono = getNlpFromLac(request);
Mono<JSONObject> mono = getNlpFromHubLac(request);
Mono<Nlp> nlpKeyMono = mono.map(jsonObject -> {
JSONArray jsonArray = jsonObject.getJSONArray("results");
Nlp nlp = new Nlp();
@ -69,9 +108,9 @@ public class LacNlpService implements INlp {
@Override
public Mono<List<Nlp>> getNlp(List<String> values) {
LacRequest request = new LacRequest();
HubLacRequest request = new HubLacRequest();
request.setText(values);
Mono<JSONObject> mono = getNlpFromLac(request);
Mono<JSONObject> mono = getNlpFromHubLac(request);
Mono<List<Nlp>> nlpKeyFlux = mono.map(jsonObject -> {
JSONArray jsonArray = jsonObject.getJSONArray("results");
List<Nlp> nlps = new ArrayList<>();

View File

@ -15,4 +15,9 @@ public class BoxMessageResp extends BaseMessageResp {
* U3D動作
*/
ActionResp action;
/**
* 天气
*/
WeatherResp weather;
}

View File

@ -0,0 +1,19 @@
package com.qiuguo.iot.box.websocket.api.domain.box.resp;
import lombok.Data;
@Data
public class WeatherResp {
/**
* 城市
*/
String weatherLocal;
/**
* 气温
*/
String weatherTemperature;
/**
* 具体天气U3D根据字符串匹配图标
*/
String WeatherIcon;
}

View File

@ -3,10 +3,12 @@ package com.qiuguo.iot.box.websocket.api.handler;
import com.alibaba.fastjson.JSONObject;
import com.qiuguo.iot.base.enums.*;
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.ActionResp;
import com.qiuguo.iot.box.websocket.api.domain.box.resp.BoxMessageResp;
import com.qiuguo.iot.box.websocket.api.domain.box.resp.WeatherResp;
import com.qiuguo.iot.data.constants.YunxiRabbitConst;
import com.qiuguo.iot.data.entity.device.DeviceUserBindEntity;
import com.qiuguo.iot.data.entity.device.DeviceUserTalkRecordEntity;
@ -152,10 +154,11 @@ public class BaseWebSocketProcess {
int n = old.indexOf("\\n");
int m = Math.max(Math.max(Math.max(Math.max(d, j), Math.max(a, b)), c), n);
if(m > 0){
//清空
sb.setLength(0);
sb.append(old.substring(m));
old = old.substring(0, m);
//清空
normalSendMsg(baseSession, old, AskTypeEnum.TTS.getCode());
}else{
sb.append(message);
@ -344,6 +347,19 @@ public class BaseWebSocketProcess {
+ item.getNarrative().replace("每 km / h", "千米每小时")
+ ",空气质量" + item.getAir_level()
+ ",湿度" + item.getHumidity() + ",最低气温" + item.getTem2() + "°C";
if(this instanceof BoxWebSocketHandler){
WeatherResp weatherResp = new WeatherResp();
weatherResp.setWeatherLocal(t.getCity());
weatherResp.setWeatherTemperature(item.getTem1());
weatherResp.setWeatherIcon(item.getWea());
BoxMessageResp resp = new BoxMessageResp();
resp.setType(action.getSystemTalkAnswerConfigEntity().getAnswerType());
resp.setText(msg);
resp.setWeather(weatherResp);
sendMessage(action, baseSession, resp);
return t;
}
}else{
msg = action.getSystemTalkAnswerConfigEntity().getAnswerValueFaild();
@ -509,13 +525,13 @@ public class BaseWebSocketProcess {
private void sendMessage(Action action, BaseSession baseSession, String message, Integer type){
BoxMessageResp resp = new BoxMessageResp();
BaseMessageResp resp = new BaseMessageResp();
resp.setType(type);
resp.setText(message);
sendMessage(action, baseSession, resp);
}
private void sendMessage(Action action, BaseSession baseSession, BoxMessageResp resp){
private void sendMessage(Action action, BaseSession baseSession, BaseMessageResp resp){
DeviceUserTalkRecordEntity talkRecord = new DeviceUserTalkRecordEntity();
talkRecord.setAskType(resp.getType());
talkRecord.setAskValue(action.getAsk());

View File

@ -19,6 +19,7 @@ import com.qiuguo.iot.data.service.device.DeviceInfoService;
import com.qiuguo.iot.third.nlp.action.Actions;
import com.qiuguo.iot.third.service.NlpService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.ReactiveStringRedisTemplate;
import org.springframework.data.redis.core.ReactiveValueOperations;
@ -67,25 +68,28 @@ public class BoxWebSocketHandler extends BaseWebSocketProcess implements WebSock
Long userId = Long.parseLong(headers.get("userId").get(0));
//
BoxSession boxSession = new BoxSession();
boxSession.setSn(sn);
boxSession.setCustomerIP(ip);
boxSession.setSession(session);
boxSession.setUserId(userId);
boxSession.setLogId(headers.get(LogMdcConfiguration.PRINT_LOG_ID).get(0));
log.info("登录成功SN:{}", sn);
Mono<Void> input = session.receive().map(webSocketMessage ->{
//MDC.put(LogMdcConfiguration.PRINT_LOG_ID, getBoxSessionWithSn().getLogId());
MDC.put(LogMdcConfiguration.PRINT_LOG_ID, boxSession.getLogId());
String text = webSocketMessage.getPayloadAsText();
log.info("设备端收到消息:{}", text);
BoxTalkMessage boxTalkMessage = JSONObject.parseObject(text, BoxTalkMessage.class);
BoxSession boxSession = getBoxSessionWithSn(boxTalkMessage.getSn());
BoxSession boxSession1 = getBoxSessionWithSn(boxTalkMessage.getSn());
if(!boxSession.equals(boxSession1)){
log.info("消息发送异常或者未验签就收到信息不是同一个链接。可能传错SN");
closeSendMsg(boxSession, "请等待验签结束或者SN可能错误", AskTypeEnum.TTS.getCode());
return Mono.empty();
}
nlpService.getActionWithLacSingle(boxSession.getUserId(), boxTalkMessage.getMessage()).defaultIfEmpty(new Actions()).map(actions -> {
//处理
if(boxSession == null){
log.info("未匹配到用户session可能传错用户id");
session.close().subscribe();
}else{
processAction(actions, userId, boxSession);
}
processAction(actions, userId, boxSession);
return Mono.empty();
}).subscribe();
@ -94,12 +98,7 @@ public class BoxWebSocketHandler extends BaseWebSocketProcess implements WebSock
return Mono.empty();
}).then();
BoxSession boxSession = new BoxSession();
boxSession.setSn(sn);
boxSession.setCustomerIP(ip);
boxSession.setSession(session);
boxSession.setUserId(userId);
boxSession.setLogId(headers.get(LogMdcConfiguration.PRINT_LOG_ID).get(0));
//校验
checkToken(boxSession, sn, linkTime, signature, userId);

View File

@ -16,6 +16,7 @@ import com.qiuguo.iot.data.service.device.DeviceUserBindService;
import com.qiuguo.iot.third.nlp.action.Actions;
import com.qiuguo.iot.third.service.NlpService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
@ -63,14 +64,24 @@ public class CustomerWebSocketHandler extends BaseWebSocketProcess implements We
return session.close();
}
String ip = headers.get(LogWebFilter.HEAD_IP).get(0);
BaseSession userSession = new BaseSession();
userSession.setUserId(userId);
userSession.setSession(session);
userSession.setCustomerIP(ip);
userSession.setSessionType(YesNo.YES.getCode());
userSession.setLogId(headers.get(LogMdcConfiguration.PRINT_LOG_ID).get(0));
log.info("用户成功userId:{}", userId);
Mono<Void> input = session.receive().map(webSocketMessage ->{
//MDC.put(LogMdcConfiguration.PRINT_LOG_ID, getBoxSessionWithSn().getLogId());
MDC.put(LogMdcConfiguration.PRINT_LOG_ID, userSession.getLogId());
String text = webSocketMessage.getPayloadAsText();
log.info("收到用户消息:{}", text);
UserTalkMessage userTalkMessage = JSONObject.parseObject(text, UserTalkMessage.class);
BaseSession userSession = getUserSessionWithUserId(userTalkMessage.getUserId());
BaseSession userSession1 = getUserSessionWithUserId(userTalkMessage.getUserId());
if(!userSession.equals(userSession1)){
log.info("消息发送异常或者未验签就收到信息不是同一个链接。可能传错用户ID");
closeSendMsg(userSession, "请等待验签结束或者用户ID可能错误", AskTypeEnum.TTS.getCode());
return Mono.empty();
}
nlpService.getActionWithLacSingle(userSession.getUserId(), userTalkMessage.getMessage())
.defaultIfEmpty(new Actions()).map(actions -> {
//处理
@ -88,12 +99,7 @@ public class CustomerWebSocketHandler extends BaseWebSocketProcess implements We
//MDC.remove(LogMdcConfiguration.PRINT_LOG_ID);
return Mono.empty();
}).then();
BaseSession userSession = new BaseSession();
userSession.setUserId(userId);
userSession.setSession(session);
userSession.setCustomerIP(ip);
userSession.setSessionType(YesNo.YES.getCode());
userSession.setLogId(headers.get(LogMdcConfiguration.PRINT_LOG_ID).get(0));
checkToken(userSession, type, token, userId);

View File

@ -41,6 +41,10 @@ qiuguo:
checktoken:
url: https://exper.qiuguojihua.com/data/api.auth.center/get
lac:
url: http://192.168.8.175:8866/predict/lac
hub:
url: http://192.168.8.175:8866/predict/lac
suanfa:
url: http://192.168.8.211:6000/qg_human/lac_word
Ali:
qianwen: 'sk-8d64677afaf6404cb83ce1910b5b2558'