wenhongquan 2 luni în urmă
părinte
comite
00560ec562

+ 35 - 5
ruoyi-admin/src/main/java/org/dromara/web/task/KafkaMessageConsumer.java

@@ -11,15 +11,14 @@ import cn.hutool.json.JSONObject;
 import cn.hutool.json.JSONUtil;
 import org.apache.kafka.clients.consumer.ConsumerRecord;
 import org.dromara.common.core.utils.SpringUtils;
+import org.dromara.system.domain.bo.SysDictDataBo;
 import org.dromara.system.domain.bo.TblDeviceBo;
 import org.dromara.system.domain.bo.TblEventBo;
+import org.dromara.system.domain.vo.SysDictDataVo;
 import org.dromara.system.domain.vo.SysOssVo;
 import org.dromara.system.domain.vo.TblDeviceVo;
 import org.dromara.system.domain.vo.TblEventVo;
-import org.dromara.system.service.ISysConfigService;
-import org.dromara.system.service.ISysOssService;
-import org.dromara.system.service.ITblDeviceService;
-import org.dromara.system.service.ITblEventService;
+import org.dromara.system.service.*;
 import org.dromara.system.utils.Utils;
 import org.slf4j.Logger;
 import org.springframework.beans.factory.annotation.Value;
@@ -39,10 +38,15 @@ public class KafkaMessageConsumer {
     private Map<String, Long> eventMap = new java.util.HashMap<>();
     private Map<String, Long> eventSaveMap = new java.util.HashMap<>();
 
+    private Map<String, String> eventLevelMap = new java.util.HashMap<>();
+
     private String finalEventTime = "200";
     private String finalEventChangeTime = "400";
 
 
+
+
+
     @Value("${user.ffmpegpath}")
     private String ffmpegFilePath = "/usr/local/bin/ffmpeg";
     @Scheduled(cron = "*/1 * * * * *")
@@ -111,6 +115,9 @@ public class KafkaMessageConsumer {
                 for (int j = 0; j < envlist.size(); j++) {
                     JSONObject envitem = envlist.getJSONObject(j);
                     String key = envitem.getInt("EvtType")+msg.getStr("DevNo")+"源驶科技";
+                    envitem.set("lx", getEventName(envitem.getInt("EvtType")));
+
+
                     try{
                         if(eventMap.get(key)!=null){
                             if((DateUtil.current()-eventMap.get(key))<Integer.parseInt(finalEventTime)*1000){
@@ -121,10 +128,21 @@ public class KafkaMessageConsumer {
                     }
                     eventMap.put(key,DateUtil.current());
                     envitem.set("from","源驶科技");
-                    envitem.set("lx", getEventName(envitem.getInt("EvtType")));
+
                     TblEventBo tblEventBo = new TblEventBo();
                     tblEventBo.setCreateTime(DateUtil.date(msg.getLong("Timestamp")));
                     tblEventBo.setExt2(JSONUtil.toJsonStr(envitem));
+
+                        //设置类型
+                        try{
+                            eventLevelMap.keySet().forEach(key1 -> {
+                               if(key1.contains(envitem.get("lx").toString())){
+                                   tblEventBo.setLevel(eventLevelMap.get(key1));
+                               }
+                            });
+                        } catch (Exception e) {}
+
+
                     List<String> urls = new ArrayList<>();
                     //下载图片
                     String path =envitem.getStr("EventImagePath");
@@ -179,6 +197,16 @@ public class KafkaMessageConsumer {
                 }
             }
         });
+
+        SysDictDataBo sysDictDataBo = new SysDictDataBo();
+        sysDictDataBo.setDictType("event_level");
+        List<SysDictDataVo> listv = SpringUtils.getBean(ISysDictDataService.class).selectDictDataList(sysDictDataBo);
+        eventLevelMap = new java.util.HashMap<>();
+        listv.forEach(item ->{
+            eventLevelMap.put(item.getRemark(),item.getDictValue());
+        });
+
+
         message.forEach(consumerRecord ->{
 
             if(consumerRecord.topic().equals("zs_events_r2p3")){
@@ -314,6 +342,8 @@ public class KafkaMessageConsumer {
             case 8388608: return "高温";
             case 134217729: return "行人闯红灯";
             case 134217730: return "非机动车闯红灯";
+            case 131073: return "车辆侧翻";
+            case 131074: return "机动车与行人发生碰撞";
             default: return null; // 可选:返回默认值如 "未知事件"
         }
     }

+ 1 - 1
ruoyi-admin/src/main/resources/application-dev.yml

@@ -49,7 +49,7 @@ spring:
           driverClassName: com.mysql.cj.jdbc.Driver
           # jdbc 所有参数配置参考 https://lionli.blog.csdn.net/article/details/122018562
           # rewriteBatchedStatements=true 批处理优化 大幅提升批量插入更新删除性能(对数据库有性能损耗 使用批量操作应考虑性能问题)
-          url: jdbc:mysql://${DATABASE_ADDR:200.200.19.121:31000}/jtjai?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&autoReconnect=true&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true&nullCatalogMeansCurrent=true
+          url: jdbc:mysql://${DATABASE_ADDR:127.0.0.1:8534}/jtjai?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&autoReconnect=true&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true&nullCatalogMeansCurrent=true
           username: ${DATABASE_USER:root}
           password: ${DATABASE_PASSWORD:root}
 #        # 从库数据源

+ 1 - 0
ruoyi-admin/src/main/resources/application.yml

@@ -47,6 +47,7 @@ user:
   fileUrl: http://jtjai.xt.wenhq.top:8083/api/oss/local/upload/
   difyUrl: ${difiurl:http://172.16.5.83/v1}
   difykey: ${difykey:app-bam2jWvQRd16MnfFaaGWK0Lj}
+  difykey1: ${difykey1:app-bam2jWvQRd16MnfFaaGWK0Lj}
   ffmpegpath: ${ffmpegpath:/Users/wenhongquan/miniforge3/bin/ffmpeg}
   hls:
     # 直播流保存路径

+ 47 - 5
ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/controller/TblEventController.java

@@ -8,10 +8,13 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.stream.Collectors;
 
+import cn.hutool.core.codec.Base64Decoder;
 import cn.hutool.core.io.FileUtil;
 import cn.hutool.core.util.StrUtil;
 import cn.hutool.json.JSONArray;
+import cn.hutool.json.JSONObject;
 import cn.hutool.json.JSONUtil;
 import lombok.RequiredArgsConstructor;
 import jakarta.servlet.http.HttpServletResponse;
@@ -61,6 +64,13 @@ public class TblEventController extends BaseController {
     String FileBASE_URL = "http://192.168.39.17:9000/v1";
 
 
+    @Value("${user.difykey}")
+    String difykey = "";
+
+    @Value("${user.difykey1}")
+    String difykey1 = "";
+
+
 
 
     /**
@@ -162,9 +172,7 @@ public class TblEventController extends BaseController {
         try{
             data.put("事件类型", JSONUtil.parseObj(tblEventVo.getExt2()).getStr("lx"));
         }catch (Exception e) {}
-        try{
-            data.put("事件来源", JSONUtil.parseObj(tblEventVo.getExt2()).getStr("from"));
-        }catch (Exception e) {}
+
         try{
             TblDeviceVo tblDeviceVo = tblDeviceService.queryById(JSONUtil.parseObj(tblEventVo.getExt2()).getJSONObject("deviceVo").getLong("id"));
             if(tblDeviceVo!=null && tblDeviceVo.getExt1()!=null){
@@ -174,6 +182,40 @@ public class TblEventController extends BaseController {
             }
         }catch (Exception e) {}
 
+        try{
+            data.put("事件来源", JSONUtil.parseObj(tblEventVo.getExt2()).getStr("from"));
+
+            data.put("事件首报", Base64Decoder.decodeStr(JSONUtil.parseObj(tblEventVo.getExt2()).getStr("firstContent")));
+            data.put("事件核实", JSONUtil.parseObj(tblEventVo.getExt2()).getJSONArray("reports").stream().map(it->{
+                if(it!=null && it instanceof JSONObject){
+                    return ((JSONObject) it).getStr("dept") + ":" + ((JSONObject) it).getStr("content");
+                }
+                return "";
+            }).collect(Collectors.joining(",")));
+
+            String  eventString = JSONUtil.toJsonStr( data);
+            Map<String, Object> workflowInputs = new HashMap<>();
+
+            workflowInputs.put("report", data.get("事件首报"));
+            workflowInputs.put("event",  data.get("事件核实"));
+            try {
+                String workflowResult = difyUtil.runWorkflow(
+                    null,
+                    workflowInputs,
+                    "blocking",
+                    "user-12",difykey1
+                );
+                System.out.println("工作流结果: " + workflowResult);
+                return R.ok(workflowResult);
+
+            }catch (Exception e){}
+
+
+
+        }catch (Exception e) {}
+
+
+
 
         //获取事件图片
         List fileInput = new ArrayList();
@@ -184,7 +226,7 @@ public class TblEventController extends BaseController {
                 if(image.toString().contains("mp4")) continue;
                 try {
                     File file = storage.getLocalEnvFile(image);
-                    String imageUrl = difyUtil.uploadFile(file.getAbsolutePath(),"user-12");
+                    String imageUrl = difyUtil.uploadFile(file.getAbsolutePath(),"user-12",difykey);
                     String imgid = JSONUtil.parseObj(imageUrl).getStr("id");
                     Map<String, Object> fileItem = new HashMap<>();
                     fileItem.put("transfer_method", "local_file");
@@ -207,7 +249,7 @@ public class TblEventController extends BaseController {
                 null,
                 workflowInputs,
                 "blocking",
-                "user-12"
+                "user-12",difykey
             );
             System.out.println("工作流结果: " + workflowResult);
             return R.ok(workflowResult);

+ 18 - 6
ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/utils/DifyUtil.java

@@ -31,7 +31,7 @@ public class DifyUtil {
      * @return API响应结果
      * @throws IOException IO异常
      */
-    public  String uploadFile(String filePath,String userid) throws IOException {
+    public  String uploadFile(String filePath,String userid,String key) throws IOException {
         String endpoint = "/files/upload";
 //        HttpURLConnection connection = null;
         File file = new File(filePath);
@@ -40,10 +40,14 @@ public class DifyUtil {
         //文件上传只需将参数中的键指定(默认file),值设为文件对象即可,对于使用者来说,文件上传与普通表单提交并无区别
         paramMap.put("file", file);
         paramMap.put("user", userid);
+        if(key==null){
+            key = API_KEY;
+        }
+
 
 
         String result2 = HttpRequest.post(API_BASE_URL + endpoint)
-            .header(Header.AUTHORIZATION,  "Bearer " + API_KEY)
+            .header(Header.AUTHORIZATION,  "Bearer " + key)
             .form(paramMap)//表单内容
             .timeout(-1)//超时,毫秒
             .execute().body();
@@ -102,15 +106,19 @@ public class DifyUtil {
      * @return API响应结果
      * @throws IOException IO异常
      */
-    public  String chat(String model, String prompt, Map<String, Object> params) throws IOException {
+    public  String chat(String model, String prompt, Map<String, Object> params,String key) throws IOException {
         String endpoint = "/chat/completions";
         HttpURLConnection connection = null;
 
+        if(key==null){
+            key = API_KEY;
+        }
+
         try {
             URL url = new URL(API_BASE_URL + endpoint);
             connection = (HttpURLConnection) url.openConnection();
             connection.setRequestMethod("POST");
-            connection.setRequestProperty("Authorization", "Bearer " + API_KEY);
+            connection.setRequestProperty("Authorization", "Bearer " + key);
             connection.setRequestProperty("Content-Type", "application/json");
             connection.setDoOutput(true);
 
@@ -152,7 +160,7 @@ public class DifyUtil {
  * @return API响应结果
  * @throws IOException IO异常
  */
-public  String runWorkflow(String workflowId, Map<String, Object> inputs, String responseMode, String user) throws IOException {
+public  String runWorkflow(String workflowId, Map<String, Object> inputs, String responseMode, String user,String key) throws IOException {
     String endpoint = "/workflows/run";
     if(workflowId==null){
         endpoint = "/workflows/run" ;
@@ -161,11 +169,15 @@ public  String runWorkflow(String workflowId, Map<String, Object> inputs, String
     }
     HttpURLConnection connection = null;
 
+    if(key==null){
+        key = API_KEY;
+    }
+
     try {
         URL url = new URL(API_BASE_URL + endpoint);
         connection = (HttpURLConnection) url.openConnection();
         connection.setRequestMethod("POST");
-        connection.setRequestProperty("Authorization", "Bearer " + API_KEY);
+        connection.setRequestProperty("Authorization", "Bearer " + key);
         connection.setRequestProperty("Content-Type", "application/json");
         connection.setDoOutput(true);