瀏覽代碼

gps超速事件

Memory_LG 10 月之前
父節點
當前提交
1a9cf141bb

+ 30 - 0
src/main/java/com/sooka/sponest/data/housingconstruction/domain/eventEntity/GpsEntity.java

@@ -0,0 +1,30 @@
+package com.sooka.sponest.data.housingconstruction.domain.eventEntity;
+
+import lombok.*;
+
+@Setter
+@Getter
+@NoArgsConstructor
+@AllArgsConstructor
+@ToString
+public class GpsEntity {
+    private Long id;
+    private String deviceid;
+    private Double maxspeed;
+    private Double tripdistance;
+    private String triptime;
+    private String starttime;
+    private String endtime;
+    private String parktime;
+    private Double averagespeed;
+    private String slat;
+    private String slon;
+    private String elat;
+    private String elon;
+    private String trackstarttime;
+    private String trackendtime;
+    private String fuelreport;
+    private String addressmap;
+    private String createTime;
+
+}

+ 3 - 0
src/main/java/com/sooka/sponest/data/housingconstruction/domain/eventEntity/StaticEntity.java

@@ -17,6 +17,9 @@ public class StaticEntity {
     public static final String ENTERPRISE_NAME = "企业运行预警";
     public static final String INSPECT_CODE = "1207";
     public static final String INSPECT_NAME = "气瓶存疑预警";
+    public static final String SPEED_CODE = "802";
+    public static final String SPEED_NAME = "车辆超速预警";
+
 
 
 

+ 137 - 9
src/main/java/com/sooka/sponest/data/housingconstruction/handler/TaskHandler.java

@@ -1,32 +1,43 @@
 package com.sooka.sponest.data.housingconstruction.handler;
 
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import com.ruoyi.common.core.utils.SpringUtils;
+import com.ruoyi.common.core.web.domain.AjaxResult;
 import com.ruoyi.common.security.utils.DictUtils;
+import com.ruoyi.system.api.RemoteConfigService;
 import com.ruoyi.system.api.domain.SysDictData;
 import com.sooka.sponest.data.housingconstruction.domain.BO.SteelcylinderBO;
 import com.sooka.sponest.data.housingconstruction.domain.CenterdataTHousingconstructionEnterprise;
 import com.sooka.sponest.data.housingconstruction.domain.CenterdataTHousingconstructionEnterpriseproductionreport;
 import com.sooka.sponest.data.housingconstruction.domain.CenterdataTHousingconstructionSteelcylinder;
+import com.sooka.sponest.data.housingconstruction.domain.eventEntity.GpsEntity;
 import com.sooka.sponest.data.housingconstruction.domain.eventEntity.SensorEventVo;
 import com.sooka.sponest.data.housingconstruction.domain.eventEntity.StaticEntity;
+import com.sooka.sponest.data.housingconstruction.service.ICenterdataTHousingconstructionCarService;
 import com.sooka.sponest.data.housingconstruction.service.ICenterdataTHousingconstructionEnterpriseService;
 import com.sooka.sponest.data.housingconstruction.service.ICenterdataTHousingconstructionEnterpriseproductionreportService;
 import com.sooka.sponest.data.housingconstruction.service.ICenterdataTHousingconstructionSteelcylinderService;
+import org.apache.commons.collections4.MapUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.scheduling.annotation.Scheduled;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 
 import java.time.LocalDateTime;
 import java.time.ZoneId;
 import java.time.temporal.ChronoUnit;
 import java.util.Date;
 import java.util.List;
+import java.util.Map;
+import java.util.Optional;
 
 @RestController
 @RequestMapping("/taskHandler")
 public class TaskHandler {
 
+    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
+
     @Autowired
     private ICenterdataTHousingconstructionEnterpriseproductionreportService reportService;
 
@@ -37,6 +48,9 @@ public class TaskHandler {
     private ICenterdataTHousingconstructionSteelcylinderService steelcylinderService;
 
     @Autowired
+    private ICenterdataTHousingconstructionCarService carService;
+
+    @Autowired
     private SendEvent sendEvent;
 
     /**
@@ -66,7 +80,7 @@ public class TaskHandler {
                 //遍历业务结果集
                 for (CenterdataTHousingconstructionEnterpriseproductionreport report : dataList) {
                     //如果企业id相同                                          并且  文件类型相同
-                    if(report.getEnterpriseId().equals(enterprise.getId())  && report.getReportType().equals(sysDictData.getDictValue())){
+                    if (report.getEnterpriseId().equals(enterprise.getId()) && report.getReportType().equals(sysDictData.getDictValue())) {
                         //获取文件上传时间 + 阈值30天 与当前时间做比较
                         //获取数据的上传时间
                         Date createTime = report.getCreateTime();
@@ -84,14 +98,14 @@ public class TaskHandler {
                         break;
                     }
                 }
-                if(flag){
+                if (flag) {
                     enterpriseStr.append(sysDictData.getDictLabel()).append(",");
                 }
             }
-            if(enterpriseStr.length() > 0){
-                enterpriseStr.deleteCharAt(enterpriseStr.length() -1);
+            if (enterpriseStr.length() > 0) {
+                enterpriseStr.deleteCharAt(enterpriseStr.length() - 1);
                 enterpriseStr.append(")发生%s, 超过30天未上传, 请及时处理!%s%s");
-                enterpriseStr.insert(0,enterprise.getEnterpriseName() +"的(");
+                enterpriseStr.insert(0, enterprise.getEnterpriseName() + "的(");
                 //创建事件实体类
                 SensorEventVo sensorEventVo = new SensorEventVo(
                         enterprise.getLongitude(),
@@ -187,4 +201,118 @@ public class TaskHandler {
 
     }
 
+    @PostMapping("/sendGpsEvent")
+    public AjaxResult sendGpsEvent(@RequestBody String jsonObj) {
+        logger.info("接受的参数为:==》{}", jsonObj);
+        if (jsonObj.isEmpty()) {
+            return AjaxResult.error("发送数据为空");
+        }
+        JSONObject jsonObject = JSONObject.parseObject(jsonObj);
+        //获取设定的阈值
+        double speedThreshold = Double.parseDouble(SpringUtils.getBean(RemoteConfigService.class).remotegetConfigKey("speed_threshold").getData());
+        logger.info("设定的阈值{}", speedThreshold);
+
+        String address = getAddressMap(jsonObject, speedThreshold);
+        logger.info("事件发生地点信息{}",address);
+
+        Map<String, Object> carInfo = carService.getCarInfoByDeviceId(jsonObject.getString("deviceid"));
+        logger.info("车辆信息{}",carInfo);
+
+        String eventSlatSlon = getEventSlatSlon(jsonObject);
+        logger.info("事件起点经纬度{}",eventSlatSlon);
+
+        //创建事件实体
+        SensorEventVo sensorEventVo = new SensorEventVo();
+        sensorEventVo.setEventType("8");
+        sensorEventVo.setLongitude("false".equals(eventSlatSlon)?"":eventSlatSlon.split("_")[0]);
+        sensorEventVo.setLatitude("false".equals(eventSlatSlon)?"":eventSlatSlon.split("_")[1]);
+        sensorEventVo.setSensorId(jsonObject.getString("deviceid"));
+        sensorEventVo.setReportTime(new Date());
+        sensorEventVo.setAddress(address);
+        sensorEventVo.setReportor(MapUtils.getString(carInfo, "carNumber"));
+        StringBuilder template = new StringBuilder();
+        template.append(MapUtils.getString(carInfo, "enterpriseName"))
+                .append("的").append(MapUtils.getString(carInfo, "driverName"))
+                .append("驾驶").append(MapUtils.getString(carInfo, "carNumber"))
+                .append("车辆").append("发生%s")
+                .append(",设定的阈值为%skm/h");
+        System.out.println("事件推送实体 =》 " + sensorEventVo);
+        //发送事件
+        sendEvent.sendEvent(template.toString(), StaticEntity.SPEED_NAME, String.valueOf(speedThreshold), "", sensorEventVo, StaticEntity.SPEED_CODE);
+
+        return AjaxResult.success();
+    }
+
+    private String getAddressMap(JSONObject jsonObject, double speedThreshold) {
+        StringBuilder addressResult = new StringBuilder();
+        //获取车速集合
+        JSONArray totaltrips = jsonObject.getJSONArray("totaltrips");
+
+        //获取事件发生地点对象
+        JSONObject addressmap = jsonObject.getJSONObject("addressmap");
+
+        List<GpsEntity> gpsEntities = JSONArray.parseArray(totaltrips.toJSONString(), GpsEntity.class);
+        gpsEntities.forEach(gps -> {
+            System.out.println("gps = " + gps);
+            double v = gps.getMaxspeed() / 1000;
+            if (v > speedThreshold) {
+                addressResult.append("在").append(getAddressStr(gps, addressmap, String.valueOf(v))).append(";");
+            }
+
+        });
+        return addressResult.toString();
+    }
+
+    //根据每条车速信息,获取发生地点范围
+    private String getAddressStr(GpsEntity entity, JSONObject addressmap, String maxSpeed) {
+        //超速开始
+        String mapKeyStart = getStr(entity.getSlat()) + "_" + getStr(entity.getSlon());
+        //超速结束
+        String mapKeyEnd = getStr(entity.getElat()) + "_" + getStr(entity.getElon());
+        return "{" + addressmap.getString(mapKeyStart) + ">>>" + addressmap.getString(mapKeyEnd) + "}区间最高时速为" + maxSpeed +"km/h";
+    }
+
+    /**
+     * @param str 需要截取的数字
+     */
+    public String getStr(String str) {
+        // 判断是整数还是小数,如果是整数,则小数点后用0补位
+        if (!str.contains(".")) {
+            String b = ".";
+            for (int a = 0; a < 5; a++) {
+                b = b + "0";
+            }
+            return str + b;
+        }
+        // 获取小数点的位置
+        int index = str.indexOf(".");
+        String beforeNum = str.substring(0, index + 1);
+        // 获取小数点后面是否有num位,不足用0补位
+        String afterNum = str.replace(beforeNum, "");
+        // 判断小数点后位数是否大于保留的位数
+        if (afterNum.length() <= 5) {
+            for (int a = afterNum.length(); a < 5; a++) {
+                afterNum = afterNum + "0";
+            }
+        } else {
+            afterNum = afterNum.substring(0, 5);
+        }
+        return beforeNum + afterNum;
+    }
+
+    private String getEventSlatSlon(JSONObject jsonObject){
+        JSONArray totaltrips = jsonObject.getJSONArray("totaltrips");
+        List<GpsEntity> gpsEntities = JSONArray.parseArray(totaltrips.toJSONString(), GpsEntity.class);
+        // 找到 startTime 最小的 GpsEntity
+        Optional<GpsEntity> minStartTimeEntity = gpsEntities.stream()
+                .min((e1, e2) -> e1.getStarttime().compareTo(e2.getStarttime()));
+
+        // 如果存在这样的实体,拼接 Slat 和 Slon
+        if (minStartTimeEntity.isPresent()) {
+            GpsEntity entity = minStartTimeEntity.get();
+            return entity.getSlat() + "_" + entity.getSlon();
+        } else {
+            return "false";
+        }
+    }
 }

+ 2 - 0
src/main/java/com/sooka/sponest/data/housingconstruction/mapper/CenterdataTHousingconstructionCarMapper.java

@@ -68,4 +68,6 @@ public interface CenterdataTHousingconstructionCarMapper {
     * @date 2024/9/12 下午2:37
     */
     public List<Map<String,Object>> getCarList(CenterdataTHousingconstructionCar car);
+
+    Map<String, Object> getCarInfoByDeviceId(String deviceId);
 }

+ 3 - 0
src/main/java/com/sooka/sponest/data/housingconstruction/service/ICenterdataTHousingconstructionCarService.java

@@ -4,6 +4,7 @@ package com.sooka.sponest.data.housingconstruction.service;
 import com.sooka.sponest.data.housingconstruction.domain.CenterdataTHousingconstructionCar;
 
 import java.util.List;
+import java.util.Map;
 
 /**
  * 车辆数据Service接口
@@ -59,4 +60,6 @@ public interface ICenterdataTHousingconstructionCarService {
      * @return 结果
      */
     public int deleteCenterdataTHousingconstructionCarById(String id);
+
+    Map<String, Object> getCarInfoByDeviceId(String deviceId);
 }

+ 6 - 0
src/main/java/com/sooka/sponest/data/housingconstruction/service/impl/CenterdataTHousingconstructionCarServiceImpl.java

@@ -12,6 +12,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
 import java.util.List;
+import java.util.Map;
 
 /**
  * 车辆数据Service业务层处理
@@ -98,4 +99,9 @@ public class CenterdataTHousingconstructionCarServiceImpl extends BaseServiceImp
     public int deleteCenterdataTHousingconstructionCarById(String id) {
         return centerdataTHousingconstructionCarMapper.deleteCenterdataTHousingconstructionCarById(id);
     }
+
+    @Override
+    public Map<String, Object> getCarInfoByDeviceId(String deviceId) {
+        return centerdataTHousingconstructionCarMapper.getCarInfoByDeviceId(deviceId);
+    }
 }

+ 10 - 0
src/main/resources/mapper/housingconstruction/CenterdataTHousingconstructionCarMapper.xml

@@ -171,4 +171,14 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
             ${params.dataScope}
         </where>
     </select>
+
+    <select id="getCarInfoByDeviceId" parameterType="string" resultType="map">
+        select
+            enterprise_name enterpriseName,
+            car_number carNumber,
+            manager_name managerName,
+            driver_name driverName,
+            escort_name escortName
+        from centerdata_t_housingconstruction_car where speed = #{deviceId}
+    </select>
 </mapper>