From a144a4d96ab8ec250ef96075d26913e467d73903 Mon Sep 17 00:00:00 2001 From: JangCan <2862008188@qq.com> Date: Wed, 10 Apr 2024 22:40:27 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=E6=95=85=E9=9A=9C=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E5=AD=98=E5=82=A8redis,mysql?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/zhilian/business/domain/BreakLog.java | 6 +- .../business/remote/RemoteBreakService.java | 5 +- .../factory/RemoteBreakFallbackFactory.java | 12 +- .../com/zhilian/common/resolver/ErrorCar.java | 23 -- .../domain/ResolverMalfunctionLog.java | 47 --- .../service/impl/BreakLogServiceImpl.java | 1 + .../controller/MalfunctionController.java | 33 -- .../resolver/mapper/MalfunctionMapper.java | 11 - .../resolver/service/MalfunctionService.java | 9 - .../service/impl/MalfunctionServiceImpl.java | 23 -- .../GeofenceEventServiceImpl.java | 58 ++-- .../MalfunctionEventServiceImpl.java | 325 ++++++++---------- .../mapper/resolver/MalfunctionMapper.xml | 11 - 13 files changed, 202 insertions(+), 362 deletions(-) delete mode 100644 zhilian-common/zhilian-common-resolver/src/main/java/com/zhilian/common/resolver/ErrorCar.java delete mode 100644 zhilian-common/zhilian-common-resolver/src/main/java/com/zhilian/common/resolver/domain/ResolverMalfunctionLog.java delete mode 100644 zhilian-modules/zhilian-resolver/src/main/java/com/zhilian/resolver/controller/MalfunctionController.java delete mode 100644 zhilian-modules/zhilian-resolver/src/main/java/com/zhilian/resolver/mapper/MalfunctionMapper.java delete mode 100644 zhilian-modules/zhilian-resolver/src/main/java/com/zhilian/resolver/service/MalfunctionService.java delete mode 100644 zhilian-modules/zhilian-resolver/src/main/java/com/zhilian/resolver/service/impl/MalfunctionServiceImpl.java delete mode 100644 zhilian-modules/zhilian-resolver/src/main/resources/mapper/resolver/MalfunctionMapper.xml diff --git a/zhilian-common/zhilian-common-business/src/main/java/com/zhilian/business/domain/BreakLog.java b/zhilian-common/zhilian-common-business/src/main/java/com/zhilian/business/domain/BreakLog.java index 9bd9d92..8c90a67 100644 --- a/zhilian-common/zhilian-common-business/src/main/java/com/zhilian/business/domain/BreakLog.java +++ b/zhilian-common/zhilian-common-business/src/main/java/com/zhilian/business/domain/BreakLog.java @@ -2,6 +2,8 @@ package com.zhilian.business.domain; import lombok.Data; +import java.util.Date; + /** * @ClassName BreakLog * @Description TODO @@ -20,9 +22,9 @@ public class BreakLog { /** 故障类型 */ private String breakType; /** 结束报警时间 */ - private String breakTime; + private Date breakTime; /** 开始报警时间 */ - private String breakDate; + private Date breakDate; /** 是否报警 */ private String breakState; /** 逻辑删除 */ diff --git a/zhilian-common/zhilian-common-business/src/main/java/com/zhilian/business/remote/RemoteBreakService.java b/zhilian-common/zhilian-common-business/src/main/java/com/zhilian/business/remote/RemoteBreakService.java index 23409d5..0f06ea5 100644 --- a/zhilian-common/zhilian-common-business/src/main/java/com/zhilian/business/remote/RemoteBreakService.java +++ b/zhilian-common/zhilian-common-business/src/main/java/com/zhilian/business/remote/RemoteBreakService.java @@ -1,6 +1,7 @@ package com.zhilian.business.remote; import com.zhilian.business.domain.Break; +import com.zhilian.business.domain.BreakLog; import com.zhilian.business.domain.BusinessBreak; import com.zhilian.business.remote.factory.RemoteBreakFallbackFactory; import com.zhilian.common.core.constant.ServiceNameConstants; @@ -15,8 +16,8 @@ public interface RemoteBreakService { /** * 新增故障 */ - @PostMapping - public Result add(@RequestBody BusinessBreak businessBreak); + @PostMapping("logAdd") + public Result logAdd(@RequestBody BreakLog breakLog); } diff --git a/zhilian-common/zhilian-common-business/src/main/java/com/zhilian/business/remote/factory/RemoteBreakFallbackFactory.java b/zhilian-common/zhilian-common-business/src/main/java/com/zhilian/business/remote/factory/RemoteBreakFallbackFactory.java index f12ee6b..3fdd31f 100644 --- a/zhilian-common/zhilian-common-business/src/main/java/com/zhilian/business/remote/factory/RemoteBreakFallbackFactory.java +++ b/zhilian-common/zhilian-common-business/src/main/java/com/zhilian/business/remote/factory/RemoteBreakFallbackFactory.java @@ -1,5 +1,6 @@ package com.zhilian.business.remote.factory; +import com.zhilian.business.domain.BreakLog; import com.zhilian.business.domain.BusinessBreak; import com.zhilian.business.remote.RemoteBreakService; import com.zhilian.common.core.domain.Result; @@ -13,10 +14,17 @@ public class RemoteBreakFallbackFactory implements FallbackFactory split){ - if(split.size()<4){ + /** + * 判断点是否在矩形内(射线法) + * + * @param pointLat 纬度 + * @param pointLng 经度 + * @param coordinates 矩形顶点坐标数组,格式为:[纬度1,经度1,纬度2,经度2,纬度3,经度3] + * @return 如果点在矩形内,返回true;否则返回false + */ + private boolean isWithinFence(double pointLat, double pointLng, List coordinates) { + if (coordinates.size() < 4) { throw new IllegalArgumentException("不符合条件"); } @@ -64,14 +73,23 @@ public class GeofenceEventServiceImpl implements ResolverEventService { double[] latitudes = new double[4]; for (int i = 0; i < 4; i++) { - String[] coordinates = split.get(i).split(","); - latitudes[i] = parseDouble(coordinates[0]); - longitudes[i] = parseDouble(coordinates[1]); + String[] coordinate = coordinates.get(i).split(","); + latitudes[i] = parseDouble(coordinate[0]); + longitudes[i] = parseDouble(coordinate[1]); } - return isPointInRectangle(pointLat,pointLat,latitudes,longitudes); + return isPointInRectangle(pointLat, pointLng, latitudes, longitudes); } + /** + * 判断点是否在矩形内(射线法) + * + * @param pointLat 纬度 + * @param pointLng 经度 + * @param latitudes 矩形顶点纬度数组 + * @param longitudes 矩形顶点经度数组 + * @return 如果点在矩形内,返回true;否则返回false + */ private boolean isPointInRectangle(double pointLat, double pointLng, double[] latitudes, double[] longitudes) { // 根据矩形顶点顺序确定最小和最大纬度、经度 double minLatitude = Arrays.stream(latitudes).min().orElseThrow(); diff --git a/zhilian-modules/zhilian-resolver/src/main/java/com/zhilian/resolver/service/impl/eventMalfunction/MalfunctionEventServiceImpl.java b/zhilian-modules/zhilian-resolver/src/main/java/com/zhilian/resolver/service/impl/eventMalfunction/MalfunctionEventServiceImpl.java index cf333d4..ac5137b 100644 --- a/zhilian-modules/zhilian-resolver/src/main/java/com/zhilian/resolver/service/impl/eventMalfunction/MalfunctionEventServiceImpl.java +++ b/zhilian-modules/zhilian-resolver/src/main/java/com/zhilian/resolver/service/impl/eventMalfunction/MalfunctionEventServiceImpl.java @@ -1,10 +1,7 @@ package com.zhilian.resolver.service.impl.eventMalfunction; - -import com.alibaba.fastjson.JSON; -import com.zhilian.business.domain.BusinessBreak; +import com.zhilian.business.domain.BreakLog; import com.zhilian.business.remote.RemoteBreakService; import com.zhilian.common.redis.service.RedisService; -import com.zhilian.common.resolver.domain.ResolverMalfunctionLog; import com.zhilian.common.resolver.domain.ResolverReportData; import com.zhilian.resolver.mapper.UserMapper; import com.zhilian.resolver.service.ResolverEventService; @@ -14,13 +11,15 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.Date; +import java.util.HashSet; +import java.util.Set; import java.util.concurrent.TimeUnit; /** - * @ClassName MalfunctionEventServiceImpl - * @Description 故障事件服务 - * @Author Can.J - * @Date 2024/4/4 10:00 + * @ClassName MalfunctionEventServiceImpl + * @Description 故障事件服务 + * @Author Can.J + * @Date 2024/4/4 10:00 */ @Slf4j @Service("malfunction") @@ -46,185 +45,153 @@ public class MalfunctionEventServiceImpl implements ResolverEventService { @Override public void execute(ResolverReportData vehicleData) { log.info("故障事件开始执行"); - if(isAnyFieldZero(vehicleData)){ - resolverReportInfoService.saveDataToDatabaseByMalfunction(vehicleData); + if(isAnyFieldZero(vehicleData)){ + resolverReportInfoService.saveDataToDatabaseByMalfunction(vehicleData); - BusinessBreak businessBreak = new BusinessBreak(); - businessBreak.setBreakVin(vehicleData.getVin()); + BreakLog breakLog = new BreakLog(); + breakLog.setBreakVin(vehicleData.getVin()); - //测试故障日志添加 - ResolverMalfunctionLog resolverMalfunctionLog = new ResolverMalfunctionLog(); - resolverMalfunctionLog.setMalfunctionVin(vehicleData.getVin()); + // * 车辆状态 1:正常 0:故障 + if(vehicleData.getVehicleStatus()==0){ + breakLog.setBreakCode("CL001"); + breakLog.setBreakType("车体故障"); + breakLog.setBreakState("1"); + } - // * 车辆状态 1:正常 0:故障 - if(vehicleData.getVehicleStatus()==0){ - businessBreak.setBreakCode("CL001"); - businessBreak.setBreakDate(new Date()); - businessBreak.setBreakType("车体故障"); - businessBreak.setBreakState("1"); - remoteBreakService.add(businessBreak); + // * 充电状态 1:正常 0:故障 + // private int chargingStatus; + if(vehicleData.getChargingStatus()==0){ + breakLog.setBreakCode("CD001"); + breakLog.setBreakType("电池故障"); + breakLog.setBreakState("1"); + } - //测试故障日志添加 - resolverMalfunctionLog.setMalfunctionCode("CL001"); + // * 运行状态 1:正常 0:故障 + if(vehicleData.getOperatingStatus()==0){ + breakLog.setBreakCode("DW001"); + breakLog.setBreakType("行驶故障"); + breakLog.setBreakState("1"); + } + + // * SOC 状态 1:正常 0:故障 + if(vehicleData.getSocStatus()==0){ + breakLog.setBreakCode("SOC001"); + breakLog.setBreakType("电池故障"); + breakLog.setBreakState("1"); + } + + // 可充电储能装置工作状态 1:正常 0:故障; + if(vehicleData.getChargingEnergyStorageStatus()==0){ + breakLog.setBreakCode("CDC001"); + breakLog.setBreakType("电池故障"); + breakLog.setBreakState("1"); + } + + // 驱动电机状态 1:正常 0:故障 + if(vehicleData.getDriveMotorStatus()==0){ + breakLog.setBreakCode("DJ001"); + breakLog.setBreakType("电机故障"); + breakLog.setBreakState("1"); + } + + // 定位是否有效 1:有效 0:无效 + if(vehicleData.getPositionStatus()==0){ + breakLog.setBreakCode("SOC001"); + breakLog.setBreakType("定位故障"); + breakLog.setBreakState("1"); + } + + // * EAS(汽车防盗系统)状态 1:正常 0:故障 + if(vehicleData.getEasStatus()==0){ + breakLog.setBreakCode("EAS001"); + breakLog.setBreakType("EAS故障"); + breakLog.setBreakState("1"); + } + + // * PTC(电动加热器)状态 1:正常 0:故障 + if(vehicleData.getPtcStatus()==0){ + breakLog.setBreakCode("PTC001"); + breakLog.setBreakType("加热故障"); + breakLog.setBreakState("1"); + } + + // * EPS(电动助力系统)状态 1:正常 0:故障 + if(vehicleData.getEpsStatus()==0){ + breakLog.setBreakCode("EPS001"); + breakLog.setBreakType("电动助力故障"); + breakLog.setBreakState("1"); + } + + // * ABS(防抱死)状态 1:正常 0:故障 + if(vehicleData.getAbsStatus()==0){ + breakLog.setBreakCode("ABS001"); + breakLog.setBreakType("ABS故障"); + breakLog.setBreakState("1"); + } + + // * MCU(电机/逆变器)状态 1:正常 0:故障 + if(vehicleData.getMcuStatus()==0){ + breakLog.setBreakCode("MCU001"); + breakLog.setBreakType("MCU故障"); + breakLog.setBreakState("1"); + } + + // * 动力电池加热状态 1:正常 0:故障 + if(vehicleData.getHeatingStatus()==0){ + breakLog.setBreakCode("DC002"); + breakLog.setBreakType("电池故障"); + breakLog.setBreakState("1"); + } + + // * 动力电池当前状态 1:正常 0:故障 + if(vehicleData.getBatteryStatus()==0){ + breakLog.setBreakCode("DC003"); + breakLog.setBreakType("电池故障"); + breakLog.setBreakState("1"); + } + + // * 动力电池保温状态 1:正常 0:故障 + if(vehicleData.getBatteryInsulationStatus()==0){ + breakLog.setBreakCode("DC004"); + breakLog.setBreakType("电池故障"); + breakLog.setBreakState("1"); + } + + // * DCDC(电力交换系统) 状态 1:正常 0:故障 + if(vehicleData.getDcdcStatus()==0){ + breakLog.setBreakCode("DCDC005"); + breakLog.setBreakType("电力故障"); + breakLog.setBreakState("1"); + } + + // * CHG(充电机)状态 1:正常 0:故障 + if(vehicleData.getChgStatus()==0){ + breakLog.setBreakCode("CDJ005"); + breakLog.setBreakType("电力故障"); + breakLog.setBreakState("1"); + + } + + HashSet breakHash = new HashSet<>(); + breakHash.add(breakLog); - } - - // * 充电状态 1:正常 0:故障 - // private int chargingStatus; - if(vehicleData.getChargingStatus()==0){ - businessBreak.setBreakCode("CD001"); - businessBreak.setBreakDate(new Date()); - businessBreak.setBreakType("电池故障"); - businessBreak.setBreakState("1"); - remoteBreakService.add(businessBreak); - } - - // * 运行状态 1:正常 0:故障 - if(vehicleData.getOperatingStatus()==0){ - businessBreak.setBreakCode("DW001"); - businessBreak.setBreakDate(new Date()); - businessBreak.setBreakType("行驶故障"); - businessBreak.setBreakState("1"); - remoteBreakService.add(businessBreak); - } - - // * SOC 状态 1:正常 0:故障 - if(vehicleData.getSocStatus()==0){ - businessBreak.setBreakCode("SOC001"); - businessBreak.setBreakDate(new Date()); - businessBreak.setBreakType("电池故障"); - businessBreak.setBreakState("1"); - remoteBreakService.add(businessBreak); - } - - // 可充电储能装置工作状态 1:正常 0:故障; - if(vehicleData.getChargingEnergyStorageStatus()==0){ - businessBreak.setBreakCode("CDC001"); - businessBreak.setBreakDate(new Date()); - businessBreak.setBreakType("电池故障"); - businessBreak.setBreakState("1"); - remoteBreakService.add(businessBreak); - } - - // 驱动电机状态 1:正常 0:故障 - if(vehicleData.getDriveMotorStatus()==0){ - businessBreak.setBreakCode("DJ001"); - businessBreak.setBreakDate(new Date()); - businessBreak.setBreakType("电机故障"); - businessBreak.setBreakState("1"); - remoteBreakService.add(businessBreak); - } - - // 定位是否有效 1:有效 0:无效 - if(vehicleData.getPositionStatus()==0){ - businessBreak.setBreakCode("SOC001"); - businessBreak.setBreakDate(new Date()); - businessBreak.setBreakType("定位故障"); - businessBreak.setBreakState("1"); - remoteBreakService.add(businessBreak); - } - - // * EAS(汽车防盗系统)状态 1:正常 0:故障 - if(vehicleData.getEasStatus()==0){ - businessBreak.setBreakCode("EAS001"); - businessBreak.setBreakDate(new Date()); - businessBreak.setBreakType("EAS故障"); - businessBreak.setBreakState("1"); - remoteBreakService.add(businessBreak); - } - - // * PTC(电动加热器)状态 1:正常 0:故障 - if(vehicleData.getPtcStatus()==0){ - businessBreak.setBreakCode("PTC001"); - businessBreak.setBreakDate(new Date()); - businessBreak.setBreakType("加热故障"); - businessBreak.setBreakState("1"); - remoteBreakService.add(businessBreak); - } - - // * EPS(电动助力系统)状态 1:正常 0:故障 - if(vehicleData.getEpsStatus()==0){ - businessBreak.setBreakCode("EPS001"); - businessBreak.setBreakDate(new Date()); - businessBreak.setBreakType("电动助力故障"); - businessBreak.setBreakState("1"); - remoteBreakService.add(businessBreak); - } - - // * ABS(防抱死)状态 1:正常 0:故障 - if(vehicleData.getAbsStatus()==0){ - businessBreak.setBreakCode("ABS001"); - businessBreak.setBreakDate(new Date()); - businessBreak.setBreakType("ABS故障"); - businessBreak.setBreakState("1"); - remoteBreakService.add(businessBreak); - } - - // * MCU(电机/逆变器)状态 1:正常 0:故障 - if(vehicleData.getMcuStatus()==0){ - businessBreak.setBreakCode("MCU001"); - businessBreak.setBreakDate(new Date()); - businessBreak.setBreakType("MCU故障"); - businessBreak.setBreakState("1"); - remoteBreakService.add(businessBreak); - } - - // * 动力电池加热状态 1:正常 0:故障 - if(vehicleData.getHeatingStatus()==0){ - businessBreak.setBreakCode("DC002"); - businessBreak.setBreakDate(new Date()); - businessBreak.setBreakType("电池故障"); - businessBreak.setBreakState("1"); - remoteBreakService.add(businessBreak); - } - - // * 动力电池当前状态 1:正常 0:故障 - if(vehicleData.getBatteryStatus()==0){ - businessBreak.setBreakCode("DC003"); - businessBreak.setBreakDate(new Date()); - businessBreak.setBreakType("电池故障"); - businessBreak.setBreakState("1"); - remoteBreakService.add(businessBreak); - } - - // * 动力电池保温状态 1:正常 0:故障 - if(vehicleData.getBatteryInsulationStatus()==0){ - businessBreak.setBreakCode("DC004"); - businessBreak.setBreakDate(new Date()); - businessBreak.setBreakType("电池故障"); - businessBreak.setBreakState("1"); - remoteBreakService.add(businessBreak); - } - - // * DCDC(电力交换系统) 状态 1:正常 0:故障 - if(vehicleData.getDcdcStatus()==0){ - businessBreak.setBreakCode("DCDC005"); - businessBreak.setBreakDate(new Date()); - businessBreak.setBreakType("电力故障"); - businessBreak.setBreakState("1"); - remoteBreakService.add(businessBreak); - } - - // * CHG(充电机)状态 1:正常 0:故障 - if(vehicleData.getChgStatus()==0){ - businessBreak.setBreakCode("CDJ005"); - businessBreak.setBreakDate(new Date()); - businessBreak.setBreakType("电力故障"); - businessBreak.setBreakState("1"); - remoteBreakService.add(businessBreak); - } - - - - -// String key = "breakdown"; - String value = JSON.toJSONString(businessBreak); - redisService.setCacheObject("breakdown:gz:"+vehicleData.getVin(),value); - long expireTime = 30; - redisService.expire("breakdown:gz:"+vehicleData.getVin(), expireTime, TimeUnit.MINUTES); + redisService.setCacheSet("breakdown:gz::"+vehicleData.getVin(),breakHash); + Set breakLogsFromRedis = redisService.getCacheSet("breakdown:gz::" + vehicleData.getVin()); + if(breakLogsFromRedis !=null && !breakLogsFromRedis.isEmpty()){ + // 将Redis中的数据遍历,逐条插入数据库 + for (BreakLog breakLogs : breakLogsFromRedis) { + remoteBreakService.logAdd(breakLogs); } + }else { + log.info("没有找到对应VIN的故障记录在Redis中,无需同步到数据库"); + } + + long expireTime = 30; + redisService.expire("breakdown:gz:"+vehicleData.getVin(), expireTime, TimeUnit.MINUTES); + + } log.info("故障事件结束"); diff --git a/zhilian-modules/zhilian-resolver/src/main/resources/mapper/resolver/MalfunctionMapper.xml b/zhilian-modules/zhilian-resolver/src/main/resources/mapper/resolver/MalfunctionMapper.xml deleted file mode 100644 index 1db4d72..0000000 --- a/zhilian-modules/zhilian-resolver/src/main/resources/mapper/resolver/MalfunctionMapper.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - INSERT INTO `vehicle-resolver`.`resolver_malfunction_log` (`malfunction_code`, `malfunction_vin`, `start_time`, `break_type`, `end_time`, `malfunction_tag`) - VALUES (#{malfunctionCode}, #{malfunctionVin}, #{startTime}, #{breakType}, #{endTime}, #{malfunctionTag}); - -