zhoufan 6 mesi fa
parent
commit
fc4a6a7879

+ 6 - 0
midware-entity/src/main/java/midware/entity/database/rep/Ai.java

@@ -25,6 +25,12 @@ public class Ai {
25 25
     /** 内容 */
26 26
     //@ApiModelProperty("内容")
27 27
     private String result;
28
+    /** 意图id */
29
+    //@ApiModelProperty("意图id")
30
+    private Integer nluId;
31
+    /** 意图名称 */
32
+    //@ApiModelProperty("意图名称")
33
+    private String nluName;
28 34
     /** 创建时间 */
29 35
     //@ApiModelProperty("创建时间")
30 36
     private Date createTime;

+ 164 - 78
midware-service/src/main/java/midware/service/eslclient/EslEventListenerAi.java

@@ -3,6 +3,7 @@ package midware.service.eslclient;
3 3
 import com.alibaba.fastjson2.JSON;
4 4
 import lombok.extern.slf4j.Slf4j;
5 5
 import midware.entity.database.rep.Ai;
6
+import midware.service.eslclient.entity.Channel;
6 7
 import midware.service.init.EslClientService;
7 8
 import midware.util.enums.EslEventEnum;
8 9
 import midware.util.helper.HttpHelper;
@@ -88,6 +89,105 @@ public class EslEventListenerAi implements IEslEventListener {
88 89
     public void backgroundJobResultReceived(EslEvent event) {}
89 90
 
90 91
     //ai回答
92
+//    private void aiAnswer(String chanId, String text) {
93
+//        EslClientService eslClientService = SpringHelper.getBean(EslClientService.class);
94
+//        eslClientService.playBreak(chanId);
95
+//        eslClientService.playBack("/home/wav/zyyt/好的.wav", chanId);
96
+//
97
+//        Ai ai = new Ai();
98
+//        ai.setUuid(chanId);
99
+//        ai.setType(1);
100
+//        ai.setResult(text);
101
+//        ai.setCreateTime(new Date());
102
+//        synchronized (EslCommon.ais) {
103
+//            EslCommon.ais.add(ai);
104
+//        }
105
+//        List<String> stops = Arrays.asList("停", "停止", "暂停", "闭嘴", "住口", "别说了", "行了", "好了", "够了", "烦死了", "打住", "跳过", "不需要了", "等一下", "不用说了");
106
+//        List<String> agains = Arrays.asList("再听一遍", "重听", "重庆", "你听", "没有听清", "听不清", "再说一遍");
107
+//        String zw = text.replaceAll("[^\\u4e00-\\u9fa5]", "");
108
+//        if (text.contains("人工")) {//人工
109
+//            stopAsr(chanId, eslClientService);
110
+//            eslClientService.playBack("/home/wav/zyyt/转人工.wav", chanId);
111
+//            try {
112
+//                Thread.sleep(3000);
113
+//                eslClientService.transferQueue(chanId);
114
+//            } catch (InterruptedException e) {
115
+//                throw new RuntimeException(e);
116
+//            }
117
+//        } else if (agains.contains(zw)) {//重听
118
+//            eslClientService.playBack(EslCommon.getSessionById(chanId).getVideoPath(), chanId);
119
+//            try {
120
+//                Thread.sleep(2000);
121
+//                resumeAsr(chanId, eslClientService);
122
+//            } catch (InterruptedException e) {
123
+//                throw new RuntimeException(e);
124
+//            }
125
+//        } else if (stops.contains(zw)) {//停止
126
+//            resumeAsr(chanId, eslClientService);
127
+//        } else {
128
+//            if (text.length() > 5) {
129
+//                String rt = "无效输入";
130
+//                Map<String, String> ps = new HashMap<>();
131
+////                ps.put("text",text);
132
+////                String result = HttpHelper.post("http://192.168.1.7:3000/predict", JSON.toJSONString(ps));
133
+////                if (StringHelper.isNotEmpty(result)) {
134
+////                    Map map = JSON.parseObject(result, Map.class);
135
+////                    if (map.get("success").toString().equals("true")) {
136
+////                        List<Map> maps = JSON.parseArray(map.get("predictions").toString(), Map.class);
137
+////                        Map rtMap = maps.get(0);
138
+////                        rt = rtMap.get("label").toString();
139
+////                        float bl = Float.parseFloat(rtMap.get("confidence").toString());
140
+////                        if (bl >= 0.4) rt = "无效输入(" + rt + "-" + bl + ")";
141
+////                    }
142
+////                }
143
+//                ps.put("sender", String.valueOf(System.currentTimeMillis()));
144
+//                ps.put("message", text);
145
+//                String url=SpringHelper.getRequiredProperty("common.rasa");
146
+//                String result = HttpHelper.post(url, JSON.toJSONString(ps));
147
+//                if (StringHelper.isNotEmpty(result)) {
148
+//                    List<Map> maps = JSON.parseArray(result, Map.class);
149
+//                    rt = maps.get(0).get("text").toString();
150
+//                }
151
+//                if (!rt.startsWith("无效输入")) {
152
+//                    String path = "/home/wav/zyyt/" + rt + ".wav";
153
+//                    EslCommon.getSessionById(chanId).setVideoPath(path);
154
+//                    eslClientService.playBack(path, chanId);
155
+//                    try {
156
+//                        Thread.sleep(2000);
157
+//                        resumeAsr(chanId, eslClientService);
158
+//                    } catch (InterruptedException e) {
159
+//                        throw new RuntimeException(e);
160
+//                    }
161
+//                } else {
162
+//                    eslClientService.playBack("/home/wav/zyyt/无法回答转人工.wav", chanId);
163
+//                    stopAsr(chanId, eslClientService);
164
+//                    try {
165
+//                        Thread.sleep(5000);
166
+//                        eslClientService.transferQueue(chanId);
167
+//                    } catch (InterruptedException e) {
168
+//                        throw new RuntimeException(e);
169
+//                    }
170
+//                }
171
+//                Ai ai1 = new Ai();
172
+//                ai1.setUuid(chanId);
173
+//                ai1.setType(2);
174
+//                ai1.setResult(rt);
175
+//                ai1.setCreateTime(new Date());
176
+//                synchronized (EslCommon.ais) {
177
+//                    EslCommon.ais.add(ai1);
178
+//                }
179
+//            } else {
180
+//                eslClientService.playBack("/home/wav/zyyt/时间太短请重说.wav", chanId);
181
+//                try {
182
+//                    Thread.sleep(2000);
183
+//                    resumeAsr(chanId, eslClientService);
184
+//                } catch (InterruptedException e) {
185
+//                    throw new RuntimeException(e);
186
+//                }
187
+//            }
188
+//        }
189
+//    }
190
+
91 191
     private void aiAnswer(String chanId, String text) {
92 192
         EslClientService eslClientService = SpringHelper.getBean(EslClientService.class);
93 193
         eslClientService.playBreak(chanId);
@@ -98,92 +198,78 @@ public class EslEventListenerAi implements IEslEventListener {
98 198
         ai.setType(1);
99 199
         ai.setResult(text);
100 200
         ai.setCreateTime(new Date());
101
-        synchronized (EslCommon.ais) {
102
-            EslCommon.ais.add(ai);
103
-        }
104
-        List<String> stops = Arrays.asList("停", "停止", "暂停", "闭嘴", "住口", "别说了", "行了", "好了", "够了", "烦死了", "打住", "跳过", "不需要了", "等一下", "不用说了");
105
-        List<String> agains = Arrays.asList("再听一遍", "重听", "重庆", "你听", "没有听清", "听不清", "再说一遍");
106
-        String zw = text.replaceAll("[^\\u4e00-\\u9fa5]", "");
107
-        if (text.contains("人工")) {//人工
108
-            stopAsr(chanId, eslClientService);
109
-            eslClientService.playBack("/home/wav/zyyt/转人工.wav", chanId);
110
-            try {
111
-                Thread.sleep(3000);
112
-                eslClientService.transferQueue(chanId);
113
-            } catch (InterruptedException e) {
114
-                throw new RuntimeException(e);
115
-            }
116
-        } else if (agains.contains(zw)) {//重听
117
-            eslClientService.playBack(EslCommon.getSessionById(chanId).getVideoPath(), chanId);
118
-            try {
119
-                Thread.sleep(2000);
120
-                resumeAsr(chanId, eslClientService);
121
-            } catch (InterruptedException e) {
122
-                throw new RuntimeException(e);
201
+
202
+        if (text.length() > 5) {
203
+            String rt = "无效输入";
204
+            Map<String, String> ps = new HashMap<>();
205
+            ps.put("sender", String.valueOf(System.currentTimeMillis()));
206
+            ps.put("message", text);
207
+            String url = SpringHelper.getRequiredProperty("common.rasa");
208
+            String result = HttpHelper.post(url, JSON.toJSONString(ps));
209
+            if (StringHelper.isNotEmpty(result)) {
210
+                List<Map> maps = JSON.parseArray(result, Map.class);
211
+                rt = maps.get(0).get("text").toString();
123 212
             }
124
-        } else if (stops.contains(zw)) {//停止
125
-            resumeAsr(chanId, eslClientService);
126
-        } else {
127
-            if (text.length() > 5) {
128
-                String rt = "无效输入";
129
-                Map<String, String> ps = new HashMap<>();
130
-//                ps.put("text",text);
131
-//                String result = HttpHelper.post("http://192.168.1.7:3000/predict", JSON.toJSONString(ps));
132
-//                if (StringHelper.isNotEmpty(result)) {
133
-//                    Map map = JSON.parseObject(result, Map.class);
134
-//                    if (map.get("success").toString().equals("true")) {
135
-//                        List<Map> maps = JSON.parseArray(map.get("predictions").toString(), Map.class);
136
-//                        Map rtMap = maps.get(0);
137
-//                        rt = rtMap.get("label").toString();
138
-//                        float bl = Float.parseFloat(rtMap.get("confidence").toString());
139
-//                        if (bl >= 0.4) rt = "无效输入(" + rt + "-" + bl + ")";
140
-//                    }
141
-//                }
142
-                ps.put("sender", String.valueOf(System.currentTimeMillis()));
143
-                ps.put("message", text);
144
-                String url=SpringHelper.getRequiredProperty("common.rasa");
145
-                String result = HttpHelper.post(url, JSON.toJSONString(ps));
146
-                if (StringHelper.isNotEmpty(result)) {
147
-                    List<Map> maps = JSON.parseArray(result, Map.class);
148
-                    rt = maps.get(0).get("text").toString();
149
-                }
150
-                if (!rt.startsWith("无效输入")) {
151
-                    String path = "/home/wav/zyyt/" + rt + ".wav";
152
-                    EslCommon.getSessionById(chanId).setVideoPath(path);
153
-                    eslClientService.playBack(path, chanId);
154
-                    try {
155
-                        Thread.sleep(2000);
156
-                        resumeAsr(chanId, eslClientService);
157
-                    } catch (InterruptedException e) {
158
-                        throw new RuntimeException(e);
159
-                    }
160
-                } else {
161
-                    eslClientService.playBack("/home/wav/zyyt/无法回答转人工.wav", chanId);
162
-                    stopAsr(chanId, eslClientService);
163
-                    try {
164
-                        Thread.sleep(5000);
165
-                        eslClientService.transferQueue(chanId);
166
-                    } catch (InterruptedException e) {
167
-                        throw new RuntimeException(e);
168
-                    }
169
-                }
170
-                Ai ai1 = new Ai();
171
-                ai1.setUuid(chanId);
172
-                ai1.setType(2);
173
-                ai1.setResult(rt);
174
-                ai1.setCreateTime(new Date());
175
-                synchronized (EslCommon.ais) {
176
-                    EslCommon.ais.add(ai1);
213
+            if (!rt.startsWith("无效输入")) {
214
+                String[] rts = rt.split("\\|");
215
+                ai.setNluId(Integer.valueOf(rts[0]));
216
+                ai.setNluName(rts[1]);
217
+                switch (rts[2]) {
218
+                    case "1"://放音
219
+                        eslClientService.playBack(rts[3], chanId);
220
+                        try {
221
+                            Thread.sleep(2000);
222
+                            resumeAsr(chanId, eslClientService);
223
+                        } catch (InterruptedException e) {
224
+                            throw new RuntimeException(e);
225
+                        }
226
+                        break;
227
+                    case "2"://转人工
228
+                        stopAsr(chanId, eslClientService);
229
+                        eslClientService.playBack("/home/wav/zyyt/转人工.wav", chanId);
230
+                        try {
231
+                            Thread.sleep(3000);
232
+                            eslClientService.transferQueue(chanId);
233
+                        } catch (InterruptedException e) {
234
+                            throw new RuntimeException(e);
235
+                        }
236
+                        break;
237
+                    case "3"://api
238
+                        break;
239
+                    case "4"://挂机
240
+                        eslClientService.killApp(chanId);
241
+                        break;
242
+                    case "5"://转移
243
+                        Channel cl = EslCommon.getChanById(chanId);
244
+                        eslClientService.transfer(chanId, cl.getNumber(), rts[3], "");
245
+                        break;
177 246
                 }
247
+
178 248
             } else {
179
-                eslClientService.playBack("/home/wav/zyyt/时间太短请重说.wav", chanId);
249
+                eslClientService.playBack("/home/wav/zyyt/无法回答转人工.wav", chanId);
250
+                stopAsr(chanId, eslClientService);
180 251
                 try {
181
-                    Thread.sleep(2000);
182
-                    resumeAsr(chanId, eslClientService);
252
+                    Thread.sleep(5000);
253
+                    eslClientService.transferQueue(chanId);
183 254
                 } catch (InterruptedException e) {
184 255
                     throw new RuntimeException(e);
185 256
                 }
186 257
             }
258
+
259
+            ai.setType(2);
260
+            ai.setResult(rt);
261
+            ai.setCreateTime(new Date());
262
+            synchronized (EslCommon.ais) {
263
+                EslCommon.ais.add(ai);
264
+            }
265
+        } else {
266
+            eslClientService.playBack("/home/wav/zyyt/时间太短请重说.wav", chanId);
267
+            try {
268
+                Thread.sleep(2000);
269
+                resumeAsr(chanId, eslClientService);
270
+            } catch (InterruptedException e) {
271
+                throw new RuntimeException(e);
272
+            }
187 273
         }
188 274
     }
189 275