[select] 通义千问回调成功
This commit is contained in:
parent
f5f7b0a526
commit
cda40fadcc
@ -27,4 +27,6 @@ public class RedisConstans {
|
||||
public static String IOT_TOKEN = "iot_token:";
|
||||
|
||||
public static String USER_BOX_INFO = "user_box_info:";
|
||||
|
||||
public static String TY_QUEUE_LIST = "ty_queue_list:"; //通义千问的前缀
|
||||
}
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
package com.qiuguo.iot.base.utils;
|
||||
|
||||
import cn.hutool.core.math.Money;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
@ -24,28 +25,29 @@ public class WebClientUtils {
|
||||
|
||||
public static Mono<JSONObject> get(String url, Map<String, String> headers) {
|
||||
if(headers == null || headers.size() == 0) {
|
||||
return webClient.get().uri(url).retrieve().bodyToMono(JSONObject.class);
|
||||
return webClient.get().uri(url).retrieve().bodyToMono(String.class).flatMap(s-> Mono.just(JSONObject.parseObject(s)));
|
||||
}else{
|
||||
return webClient.get().uri(url).headers(httpHeaders -> {
|
||||
for (String key:headers.keySet()
|
||||
) {
|
||||
httpHeaders.set(key, headers.get(key));
|
||||
}
|
||||
}).retrieve().bodyToMono(JSONObject.class);
|
||||
}).retrieve().bodyToMono(String.class).flatMap(s-> Mono.just(JSONObject.parseObject(s)));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static Mono<JSONObject> post(String url, JSONObject body, Map<String, String> headers) {
|
||||
if(headers == null || headers.size() == 0) {
|
||||
return webClient.post().uri(url).bodyValue(body.toString()).retrieve().bodyToMono(JSONObject.class);
|
||||
return webClient.post().uri(url).bodyValue(body.toString()).retrieve().bodyToMono(String.class).flatMap(s->
|
||||
Mono.just(JSONObject.parseObject(s)));
|
||||
}else{
|
||||
return webClient.post().uri(url).bodyValue(body.toString()).headers(httpHeaders -> {
|
||||
for (String key:headers.keySet()
|
||||
) {
|
||||
httpHeaders.set(key, headers.get(key));
|
||||
}
|
||||
}).retrieve().bodyToMono(JSONObject.class);
|
||||
}).retrieve().bodyToMono(String.class).flatMap(s-> Mono.just(JSONObject.parseObject(s)));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -0,0 +1,95 @@
|
||||
package com.qiuguo.iot.user.api.controller.tongyi;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.qiuguo.iot.base.constans.RedisConstans;
|
||||
import com.qiuguo.iot.base.utils.WebClientUtils;
|
||||
import com.qiuguo.iot.user.api.rest.tongyi.QGResponse;
|
||||
import com.qiuguo.iot.user.api.rest.tongyi.TongYiCommunicationRest;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.data.redis.core.ReactiveStringRedisTemplate;
|
||||
import org.springframework.data.redis.core.ReactiveValueOperations;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/TY")
|
||||
@Slf4j
|
||||
public class TYController {
|
||||
|
||||
@Resource
|
||||
private ReactiveStringRedisTemplate reactiveStringRedisTemplate;
|
||||
|
||||
|
||||
@PostMapping("communication")
|
||||
public Mono<QGResponse.Data> communication(@RequestBody TongYiCommunicationRest rest){
|
||||
ReactiveValueOperations<String, String> operations = reactiveStringRedisTemplate.opsForValue();
|
||||
return operations.get(RedisConstans.TY_QUEUE_LIST+rest.getDeviceId()).defaultIfEmpty("").flatMap(res -> {
|
||||
JSONArray objects = JSONArray.parseArray(res);
|
||||
List<TongYiCommunicationRest> list = new LinkedList<>();
|
||||
try {
|
||||
if (!ObjectUtils.isEmpty(objects)){
|
||||
list = JSONObject.parseArray(res, TongYiCommunicationRest.class);
|
||||
if (rest.getIsWhiteList() == 0) {
|
||||
//是白名单,插入头部
|
||||
log.info("收到白名单对应信息,接收参数为:{}", rest);
|
||||
list.add(0,rest);
|
||||
}
|
||||
}
|
||||
//不是白名单插入尾部
|
||||
list.add(rest);
|
||||
operations.set(RedisConstans.TY_QUEUE_LIST+rest.getDeviceId(), JSONArray.toJSONString(list)).subscribe();
|
||||
//通知Redis让Redis进行消费
|
||||
HashMap<String, Object> map = new HashMap<>();
|
||||
map.put("task_type", "qwen");
|
||||
map.put("text", rest.getText());
|
||||
return httpTY(map) ;
|
||||
}catch (Exception e){
|
||||
log.error("调用异常:");
|
||||
return Mono.just(new QGResponse().getData());
|
||||
}finally {
|
||||
reactiveStringRedisTemplate.delete(RedisConstans.TY_QUEUE_LIST+rest.getDeviceId()).subscribe();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static Mono<QGResponse.Data> httpTY(HashMap<String, Object> map){
|
||||
|
||||
return WebClientUtils.post("http://192.168.8.211:5010/qg_human/qwen", new JSONObject(map)).flatMap(jsonObject -> {
|
||||
long time = new Date().getTime();
|
||||
QGResponse qgResponse = jsonObject.toJavaObject(QGResponse.class);
|
||||
log.info("发起TY请求,响应时间时间为:{}", new Date().getTime()-time);
|
||||
if (qgResponse.getStatusCode() == 0) {
|
||||
//成功
|
||||
log.info("通知成功:"+ qgResponse.getData());
|
||||
return Mono.just(qgResponse.getData());
|
||||
}
|
||||
//失败
|
||||
log.info("通知失败:"+ map);
|
||||
|
||||
return Mono.just(qgResponse.getData());
|
||||
});
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
HashMap<String, Object> map = new HashMap<>();
|
||||
map.put("task_type", "qwen");
|
||||
map.put("text", "你好");
|
||||
try {
|
||||
Mono<QGResponse.Data> qgResponseMono = httpTY(map);
|
||||
System.out.println(qgResponseMono.block());
|
||||
}catch (Exception e) {
|
||||
log.info("异常:{}", e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,9 +1,9 @@
|
||||
package com.qiuguo.iot.user.api.rest;
|
||||
package com.qiuguo.iot.user.api.rest.room;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class RoomInitResp {
|
||||
public class RoomInitRest {
|
||||
//房间名称
|
||||
private String roomName;
|
||||
}
|
||||
@ -0,0 +1,39 @@
|
||||
package com.qiuguo.iot.user.api.rest.tongyi;
|
||||
|
||||
import lombok.Data;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
@Data
|
||||
public class QGResponse {
|
||||
@JsonProperty("status_code")
|
||||
private int statusCode;
|
||||
|
||||
private String message;
|
||||
|
||||
private Data data;
|
||||
|
||||
@JsonProperty("task_type")
|
||||
private String taskType;
|
||||
|
||||
@JsonProperty("run_time")
|
||||
private double runTime;
|
||||
|
||||
private String version;
|
||||
|
||||
// 添加构造方法、getter和setter方法
|
||||
public static class Data {
|
||||
private String resut;
|
||||
|
||||
private int code;
|
||||
|
||||
public String getResut() {
|
||||
return resut;
|
||||
}
|
||||
|
||||
public void setResut(String resut) {
|
||||
this.resut = resut;
|
||||
}
|
||||
|
||||
// 添加getter和setter方法
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
package com.qiuguo.iot.user.api.rest.tongyi;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class TongYiCommunicationRest {
|
||||
private Integer isWhiteList;//是否为白名单
|
||||
private String text;//消息
|
||||
private String deviceId;//设备序列号
|
||||
private String status; //状态,0:已完成,1.未发起,2.发起中
|
||||
}
|
||||
@ -1,40 +1,17 @@
|
||||
package com.qiuguo.iot.customer.http.api.mysql;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import org.springframework.util.ResourceUtils;
|
||||
import org.springframework.util.StopWatch;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class MysqlMain {
|
||||
|
||||
public static String package_name = "com.qiuguo.iot.data.entity";
|
||||
public static String package_name_model = "com.chengyi.user.dao";
|
||||
/*
|
||||
|
||||
//public static String save_path = "C:\\Users\\linzi\\Desktop\\mysql";
|
||||
|
||||
//public static String save_path ="C:\\workspace\\life";
|
||||
public static String save_path = "C:\\workspace\\ideaworkspace\\api";
|
||||
|
||||
public static String mysql_url = "jdbc:mysql://rr-bp1ih900km0q3e0ur.mysql.rds.aliyuncs.com:3306";
|
||||
public static String pre = "";
|
||||
public static String mysql_dbname = "igo";
|
||||
public static String mysql_username = "igo_readonly";
|
||||
public static String mysql_password = "qEJMAwIjY45F";
|
||||
*/
|
||||
|
||||
|
||||
//public static String save_path = "C:\\Users\\linzi\\Desktop\\mysql";
|
||||
|
||||
// public static String save_path ="C:\\workspace\\life";
|
||||
public static String save_path = System.getProperty("user.dir");
|
||||
|
||||
public static String mysql_url = "jdbc:mysql://192.168.8.146:30416/qiuguo_iot?useSSL=false&serverZoneId=Asia/Shanghai";
|
||||
@ -109,21 +86,10 @@ public class MysqlMain {
|
||||
}
|
||||
|
||||
|
||||
/*for (int i = 0; i < list2.size(); i++) {
|
||||
MysqlUtilTable2Mapper.printDao(list2.get(i));
|
||||
}
|
||||
for (int i = 0; i < list2.size(); i++) {
|
||||
MysqlUtilTable2XML.printXMLForMap(list2.get(i));
|
||||
}*/
|
||||
|
||||
for (int i = 0; i < list2.size(); i++) {
|
||||
MysqlUtilTable2Service.printReactorService(list2.get(i));
|
||||
//MysqlUtilTable2Service.printServiceImpl(list2.get(i));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user