diff --git a/.gitignore b/.gitignore index 5ff6309..b987916 100644 --- a/.gitignore +++ b/.gitignore @@ -4,13 +4,11 @@ target/ !**/src/test/**/target/ ### IntelliJ IDEA ### -.idea/modules.xml -.idea/jarRepositories.xml -.idea/compiler.xml -.idea/libraries/ +/.idea *.iws *.iml *.ipr +/logs ### Eclipse ### .apt_generated @@ -35,4 +33,4 @@ build/ .vscode/ ### Mac OS ### -.DS_Store \ No newline at end of file +.DS_Store diff --git a/src/main/java/com/muyu/vehicle/VehicleData.java b/src/main/java/com/muyu/vehicle/VehicleData.java index 511abdd..7c48f45 100644 --- a/src/main/java/com/muyu/vehicle/VehicleData.java +++ b/src/main/java/com/muyu/vehicle/VehicleData.java @@ -1,11 +1,14 @@ package com.muyu.vehicle; +import com.muyu.domain.Vehicle; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; +import java.math.BigDecimal; + /** * @author 牧鱼 * @Classname VehicleData @@ -44,7 +47,7 @@ public class VehicleData { /** * 里程 */ - private String mileage; + private BigDecimal mileage; /** * 总电压 @@ -114,7 +117,12 @@ public class VehicleData { /** * 动力电池剩余电量SOC */ - private String remainingBattery; + private BigDecimal remainingBattery; + + /** + * 电池总容量 + */ + private BigDecimal batteryLevel; /** * 当前状态允许的最大反馈功率 @@ -275,7 +283,7 @@ public class VehicleData { //车速 sb.append(getValue(speed,6)); //总里程 - sb.append(getValue(mileage,11)); + sb.append(getValue(mileage.toString(),11)); // 总电压 sb.append(getValue(voltage,6)); //总电流 @@ -303,7 +311,7 @@ public class VehicleData { //电机电流 sb.append(getValue(motorCurrent,8)); //动力电池剩余电量SOC - sb.append(getValue(remainingBattery,6)); + sb.append(getValue(remainingBattery.toString(),6)); //当前状态允许的最大反馈功率 sb.append(getValue(maximumFeedbackPower,6)); //当前状态允许最大放电功率 @@ -370,6 +378,20 @@ public class VehicleData { return val; } + /** + * 汽车对象构造企业VIN + * @param vehicle 汽车对象 + * @return 汽车数据对象 + */ + public static VehicleData vehicleBuild (Vehicle vehicle) { + return VehicleData.builder() + .vin(vehicle.getVin()) + .remainingBattery(vehicle.getRemainingBattery()) + .batteryLevel(vehicle.getBatteryLevel()) + .mileage(vehicle.getTotalMileage()) + .build(); + } + /** VIN vin; diff --git a/src/main/java/com/muyu/vehicle/core/Config.java b/src/main/java/com/muyu/vehicle/core/Config.java deleted file mode 100644 index d238064..0000000 --- a/src/main/java/com/muyu/vehicle/core/Config.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.muyu.vehicle.core; - -/** - * @author DongZeLiang - * @version 1.0 - * @description 配置 - * @date 2023/11/9 - */ -public class Config { -} diff --git a/src/main/java/com/muyu/vehicle/core/LocalContainer.java b/src/main/java/com/muyu/vehicle/core/LocalContainer.java new file mode 100644 index 0000000..dd9cc39 --- /dev/null +++ b/src/main/java/com/muyu/vehicle/core/LocalContainer.java @@ -0,0 +1,32 @@ +package com.muyu.vehicle.core; + +import com.muyu.domain.Vehicle; +import com.muyu.vehicle.VehicleData; + +import java.util.HashMap; + +/** + * @author DongZl + * @description: 车辆容器 + * @Date 2023-11-16 下午 02:30 + */ +public class LocalContainer { + + /** + * 车辆容器 + */ + public static final HashMap vehicleDataMap = new HashMap<>(); + + /** + * 添加车辆 + * @param vehicle 车辆信息 + */ + public static void setVehicle(Vehicle vehicle){ + String vin = vehicle.getVin(); + VehicleData vehicleData = vehicleDataMap.get(vin); + if (vehicleData == null){ + vehicleDataMap.put(vin,VehicleData.vehicleBuild(vehicle)); + } + } + +} diff --git a/src/main/java/com/muyu/vehicle/core/VehicleConfiguration.java b/src/main/java/com/muyu/vehicle/core/VehicleConfiguration.java new file mode 100644 index 0000000..f2c6818 --- /dev/null +++ b/src/main/java/com/muyu/vehicle/core/VehicleConfiguration.java @@ -0,0 +1,53 @@ +package com.muyu.vehicle.core; + +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.muyu.domain.Vehicle; +import com.muyu.service.VehicleService; +import com.muyu.vehicle.VehicleData; +import lombok.AllArgsConstructor; +import lombok.extern.log4j.Log4j2; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; +import org.springframework.context.annotation.Configuration; + +import javax.annotation.PostConstruct; +import java.util.List; + +/** + * @author DongZeLiang + * @version 1.0 + * @description 配置 + * @date 2023/11/9 + */ +@Log4j2 +@Configuration +@AllArgsConstructor +public class VehicleConfiguration implements ApplicationRunner { + + private final VehicleService vehicleService; + + /** + * 初始化加载汽车数据到内存当中 + */ + public void vehicleInit(){ + long startTime = System.currentTimeMillis(); + int page = 0, pageSize = 1000; + log.info("初始开始,批量从数据库当中加载数据到内存当中,每次[{}]条", pageSize); + while (true){ + Page vehiclePage = vehicleService.page(new Page(page++, pageSize)); + List records = vehiclePage.getRecords(); + records.forEach(LocalContainer::setVehicle); + log.info("第[{}]页,[{}]条", page, records.size()); + if (records.size() < pageSize){ + break; + } + } + log.info("数据加载完成,耗时:{}MS", System.currentTimeMillis() - startTime); + } + + @Override + public void run (ApplicationArguments args) throws Exception { + this.vehicleInit(); + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index db303b8..5020113 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -10,8 +10,8 @@ spring: # jdbc:h2:mem:testDB;DB_CLOSE_DELAY=-1 url: jdbc:h2:file:~/vehicle/db;AUTO_SERVER=TRUE;DB_CLOSE_DELAY=-1 driver-class-name: org.h2.Driver - # 开启这个配置就可以通过 web 页面访问了,例如:http://localhost:8080/springboot-h2/h2-console h2: + # 开启这个配置就可以通过 web 页面访问了,例如:http://localhost:8080/springboot-h2/h2-console console: enabled: true settings: @@ -45,11 +45,10 @@ mybatis-plus: column-underline: true logic-delete-value: -1 logic-not-delete-value: 0 - banner: false #原生配置 configuration: # 打印sql - log-impl: org.apache.ibatis.logging.stdout.StdOutImpl +# log-impl: org.apache.ibatis.logging.stdout.StdOutImpl map-underscore-to-camel-case: true cache-enabled: false call-setters-on-nulls: true @@ -65,7 +64,7 @@ logging: web: ERROR file: path: ./logs - name: './logs/springboot-h2.log' + name: './logs/vehicle.log' pattern: file: '%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}:%L - %msg%n' console: '%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}:%L - %msg%n' diff --git a/src/main/resources/data.sql b/src/main/resources/data.sql index 7c06f43..fd6eeb2 100644 --- a/src/main/resources/data.sql +++ b/src/main/resources/data.sql @@ -1,4 +1,2 @@ insert into t_user(id, username, pwd, create_time, update_time) values (0,'zhhangsan','1222', {ts '2022-07-27 18:47:52.69'}, {ts '2022-07-27 18:47:52.69'}) - -insert into vehicle(id,vin, create_time) values (1,'vin123456789123456',{ts '2022-07-27 18:47:52.69'})