Jelajahi Sumber

实现围栏闯禁事件融合功能

- 新增EvtStatus和EvtType枚举类
- 修改BdFenceVioEvt实体类,增加evtStatus字段
- 更新BdFenceVioEvtMapper,添加evtStatus相关SQL语句
- 重构EvtFusionEngine接口,修改check方法参数
- 实现FenceBreakInEngine,完成围栏闯禁事件处理逻辑
- 调整generateEvt方法,增加fenceId和fenceName字段
chen.cheng 9 bulan lalu
induk
melakukan
70a4492b63

+ 10 - 0
bd-location/src/main/java/com/ruoyi/bd/domain/BdFenceVioEvt.java

@@ -43,6 +43,8 @@ public class BdFenceVioEvt extends BaseEntity
     @Excel(name = "围栏id")
     private Long fenceId;
 
+    private String evtStatus;
+
     public void setId(Long id)
     {
         this.id = id;
@@ -107,6 +109,14 @@ public class BdFenceVioEvt extends BaseEntity
         return fenceId;
     }
 
+    public String getEvtStatus() {
+        return evtStatus;
+    }
+
+    public void setEvtStatus(String evtStatus) {
+        this.evtStatus = evtStatus;
+    }
+
     @Override
     public String toString() {
         return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)

+ 1 - 1
bd-location/src/main/java/com/ruoyi/bd/service/engine/EvtFusionEngine.java

@@ -55,7 +55,7 @@ public abstract class EvtFusionEngine {
         messageQueue.offer(msg);
     }
 
-    public abstract Boolean check(LocationInfo locationInfo, LocationInfo msg);
+    public abstract Boolean check(LocationInfo older, LocationInfo newer);
 
     public abstract String getKey(LocationInfo msg);
 

+ 40 - 6
bd-location/src/main/java/com/ruoyi/bd/service/engine/impl/FenceBreakInEngine.java

@@ -1,12 +1,17 @@
 package com.ruoyi.bd.service.engine.impl;
 
+import com.alibaba.fastjson2.JSON;
 import com.alibaba.fastjson2.JSONObject;
 import com.ruoyi.bd.domain.BdFenceInfo;
+import com.ruoyi.bd.domain.BdFenceVioEvt;
 import com.ruoyi.bd.service.IBdFenceInfoService;
+import com.ruoyi.bd.service.IBdFenceVioEvtService;
 import com.ruoyi.bd.service.engine.EvtFusionEngine;
 import com.ruoyi.bd.service.engine.LocationInfo;
 import com.ruoyi.common.BDConst;
 import com.ruoyi.common.core.redis.RedisCache;
+import com.ruoyi.common.enums.EvtStatus;
+import com.ruoyi.common.enums.EvtType;
 import com.ruoyi.common.utils.geo.GeoUtils;
 import net.dreamlu.iot.mqtt.spring.client.MqttClientTemplate;
 import org.apache.commons.lang3.ObjectUtils;
@@ -25,12 +30,23 @@ public class FenceBreakInEngine extends EvtFusionEngine {
     @Autowired
     private IBdFenceInfoService fenceInfoService;
 
+    @Autowired
+    private IBdFenceVioEvtService vioEvtService;
+
     @Resource
     private RedisCache redisCache;
 
     @Autowired
     private MqttClientTemplate client;
 
+    /**
+     * The constant MAX_EVT_TIME_GAP.
+     * 单位:秒
+     *
+     * @author chen.cheng
+     */
+    private static final int MAX_EVT_TIME_GAP = 300;
+
     @PostConstruct
     public void init() {
         this.setEngineName("围栏闯禁事件融合");
@@ -45,30 +61,48 @@ public class FenceBreakInEngine extends EvtFusionEngine {
     }
 
     @Override
-    public Boolean check(LocationInfo locationInfo, LocationInfo msg) {
-        return null;
+    public Boolean check(LocationInfo older, LocationInfo newer) {
+        long gap = newer.getSrcTimestamp() - older.getSrcTimestamp();
+        // 将gap 转换为秒
+        gap = gap / 1000;
+        return gap <= MAX_EVT_TIME_GAP;
     }
 
     @Override
     public String getKey(LocationInfo msg) {
-        return null;
+        return BDConst.REDIS_KEY.FENCE_BREAK_IN_KEY + msg.getMsg().getString("deviceId");
     }
 
     @Override
     public void getBizId(LocationInfo msg) {
-
+        BdFenceVioEvt bdFenceVioEvt = new BdFenceVioEvt();
+        bdFenceVioEvt.setFenceId(msg.getMsg().getLong("fenceId"));
+        bdFenceVioEvt.setLat(msg.getLatitude());
+        bdFenceVioEvt.setLng(msg.getLongitude());
+        bdFenceVioEvt.setEvtStatus(EvtStatus.NEW.getCode());
+        bdFenceVioEvt.setEvtType(EvtType.FENCE_IN.getCode());
+        bdFenceVioEvt.setEvtDesc(String.format("围栏闯禁: %s", msg.getMsg().getString("fenceName")));
+        vioEvtService.insertBdFenceVioEvt(bdFenceVioEvt);
+        msg.setBizId(bdFenceVioEvt.getId().toString());
     }
 
     @Override
     public void processOlderData(LocationInfo msg) {
-
+        vioEvtService.updateBdFenceVioEvt(new BdFenceVioEvt() {
+            {
+                setFenceId(msg.getMsg().getLong("fenceId"));
+                setEvtStatus(EvtStatus.DISAPPEAR.getCode());
+            }
+        });
     }
 
     public void generateEvt(JSONObject msg, byte[] payload) {
         List<BdFenceInfo> cacheList = redisCache.getCacheList(BDConst.REDIS_KEY.FENCE);
         for (BdFenceInfo fenceInfo : cacheList) {
             if (GeoUtils.isPointInGeoFence(fenceInfo.getPolygon(), msg.getString("longitude"), msg.getString("latitude"))) {
-                client.publish(BDConst.MQTT_TOPIC.EVT_LOCATION_TOPIC, payload);
+                msg.put("fenceId", fenceInfo.getId());
+                msg.put("fenceName", fenceInfo.getDefenceName());
+                client.publish(BDConst.MQTT_TOPIC.EVT_LOCATION_TOPIC, JSON.toJSONBytes(msg));
             }
         }
     }

+ 21 - 0
bd-location/src/main/java/com/ruoyi/common/enums/EvtStatus.java

@@ -0,0 +1,21 @@
+package com.ruoyi.common.enums;
+
+public enum EvtStatus {
+    NEW("0", "新增"), DISAPPEAR("1", "消散");
+
+    private final String code;
+    private final String info;
+
+    EvtStatus(String code, String info) {
+        this.code = code;
+        this.info = info;
+    }
+
+    public String getCode() {
+        return code;
+    }
+
+    public String getInfo() {
+        return info;
+    }
+}

+ 21 - 0
bd-location/src/main/java/com/ruoyi/common/enums/EvtType.java

@@ -0,0 +1,21 @@
+package com.ruoyi.common.enums;
+
+public enum EvtType {
+    FENCE_IN("1", "围栏闯禁");
+
+    private final String code;
+    private final String info;
+
+    EvtType(String code, String info) {
+        this.code = code;
+        this.info = info;
+    }
+
+    public String getCode() {
+        return code;
+    }
+
+    public String getInfo() {
+        return info;
+    }
+}

+ 43 - 24
bd-location/src/main/resources/mapper/bd/BdFenceVioEvtMapper.xml

@@ -1,35 +1,49 @@
 <?xml version="1.0" encoding="UTF-8" ?>
 <!DOCTYPE mapper
-PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
-"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
+        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="com.ruoyi.bd.mapper.BdFenceVioEvtMapper">
-    
+
     <resultMap type="BdFenceVioEvt" id="BdFenceVioEvtResult">
-        <result property="id"    column="id"    />
-        <result property="evtKey"    column="evt_key"    />
-        <result property="evtType"    column="evt_type"    />
-        <result property="evtDesc"    column="evt_desc"    />
-        <result property="lng"    column="lng"    />
-        <result property="lat"    column="lat"    />
-        <result property="fenceId"    column="fence_id"    />
-        <result property="updateTime"    column="update_time"    />
-        <result property="createTime"    column="create_time"    />
-        <result property="createBy"    column="create_by"    />
-        <result property="updateBy"    column="update_by"    />
+        <result property="id" column="id"/>
+        <result property="evtKey" column="evt_key"/>
+        <result property="evtType" column="evt_type"/>
+        <result property="evtDesc" column="evt_desc"/>
+        <result property="lng" column="lng"/>
+        <result property="lat" column="lat"/>
+        <result property="fenceId" column="fence_id"/>
+        <result property="evtStatus" column="evt_status"/>
+        <result property="updateTime" column="update_time"/>
+        <result property="createTime" column="create_time"/>
+        <result property="createBy" column="create_by"/>
+        <result property="updateBy" column="update_by"/>
     </resultMap>
 
     <sql id="selectBdFenceVioEvtVo">
-        select id, evt_key, evt_type, evt_desc, lng, lat, fence_id, update_time, create_time, create_by, update_by from bd_fence_vio_evt
+        select id,
+               evt_key,
+               evt_type,
+               evt_desc,
+               lng,
+               lat,
+               fence_id,
+               evt_status,
+               update_time,
+               create_time,
+               create_by,
+               update_by
+        from bd_fence_vio_evt
     </sql>
 
     <select id="selectBdFenceVioEvtList" parameterType="BdFenceVioEvt" resultMap="BdFenceVioEvtResult">
         <include refid="selectBdFenceVioEvtVo"/>
-        <where>  
-            <if test="evtType != null  and evtType != ''"> and evt_type = #{evtType}</if>
-            <if test="evtDesc != null  and evtDesc != ''"> and evt_desc = #{evtDesc}</if>
+        <where>
+            <if test="evtType != null  and evtType != ''">and evt_type = #{evtType}</if>
+            <if test="evtDesc != null  and evtDesc != ''">and evt_desc = #{evtDesc}</if>
+            <if test="evtStatus !=null and evtStatus!=''">and evt_status = #{evtStatus}</if>
         </where>
     </select>
-    
+
     <select id="selectBdFenceVioEvtById" parameterType="Long" resultMap="BdFenceVioEvtResult">
         <include refid="selectBdFenceVioEvtVo"/>
         where id = #{id}
@@ -44,11 +58,12 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
             <if test="lng != null">lng,</if>
             <if test="lat != null">lat,</if>
             <if test="fenceId != null">fence_id,</if>
+            <if test="evtStatus !=null">evt_status,</if>
             <if test="updateTime != null">update_time,</if>
             <if test="createTime != null">create_time,</if>
             <if test="createBy != null">create_by,</if>
             <if test="updateBy != null">update_by,</if>
-         </trim>
+        </trim>
         <trim prefix="values (" suffix=")" suffixOverrides=",">
             <if test="evtKey != null">#{evtKey},</if>
             <if test="evtType != null">#{evtType},</if>
@@ -56,11 +71,12 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
             <if test="lng != null">#{lng},</if>
             <if test="lat != null">#{lat},</if>
             <if test="fenceId != null">#{fenceId},</if>
+            <if test="evtStatus !=null">#{evtStatus},</if>
             <if test="updateTime != null">#{updateTime},</if>
             <if test="createTime != null">#{createTime},</if>
             <if test="createBy != null">#{createBy},</if>
             <if test="updateBy != null">#{updateBy},</if>
-         </trim>
+        </trim>
     </insert>
 
     <update id="updateBdFenceVioEvt" parameterType="BdFenceVioEvt">
@@ -72,6 +88,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
             <if test="lng != null">lng = #{lng},</if>
             <if test="lat != null">lat = #{lat},</if>
             <if test="fenceId != null">fence_id = #{fenceId},</if>
+            <if test="evtStatus !=null">evt_status = #{evtStatus},</if>
             <if test="updateTime != null">update_time = #{updateTime},</if>
             <if test="createTime != null">create_time = #{createTime},</if>
             <if test="createBy != null">create_by = #{createBy},</if>
@@ -81,13 +98,15 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
     </update>
 
     <delete id="deleteBdFenceVioEvtById" parameterType="Long">
-        delete from bd_fence_vio_evt where id = #{id}
+        delete
+        from bd_fence_vio_evt
+        where id = #{id}
     </delete>
 
     <delete id="deleteBdFenceVioEvtByIds" parameterType="String">
-        delete from bd_fence_vio_evt where id in 
+        delete from bd_fence_vio_evt where id in
         <foreach item="id" collection="array" open="(" separator="," close=")">
             #{id}
         </foreach>
     </delete>
-</mapper>
+</mapper>

+ 1 - 1
ruoyi-ui/.env.development

@@ -7,6 +7,6 @@ ENV = 'development'
 # 若依管理系统/开发环境
 VUE_APP_BASE_API = '/dev-api'
 
-VUE_APP_BASE_URL = 'http://127.0.0.1:18080/tfc'
+VUE_APP_BASE_URL = 'https://www.lj-info.com:8090/prod-api'
 # 路由懒加载
 VUE_CLI_BABEL_TRANSPILE_MODULES = true