From 01eb7762152226a1608fed58cfa09cef5868ad95 Mon Sep 17 00:00:00 2001 From: dongzeliang <2746733890@qq.com> Date: Wed, 6 Dec 2023 15:35:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=9A=E5=8A=A1=E7=BB=93=E6=9E=84=E6=8B=86?= =?UTF-8?q?=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../muyu/controller/VehicleController.java | 12 +- .../controller/VehicleInstanceController.java | 54 ---- .../controller/VehicleUnifiedController.java | 80 ++++++ .../java/com/muyu/domain/model/TaskModel.java | 21 +- .../com/muyu/domain/resp/UnifiedTaskResp.java | 3 - .../muyu/service/VehicleInstanceService.java | 31 --- .../java/com/muyu/service/VehicleService.java | 6 + .../muyu/service/VehicleUnifiedService.java | 45 +++ .../muyu/service/impl/VechileServiceImpl.java | 23 ++ .../impl/VehicleInstanceServiceImpl.java | 225 --------------- .../impl/VehicleUnifiedServiceImpl.java | 263 ++++++++++++++++++ .../com/muyu/vehicle/VehicleInstance.java | 8 + .../com/muyu/vehicle/core/LocalContainer.java | 8 + 13 files changed, 450 insertions(+), 329 deletions(-) create mode 100644 src/main/java/com/muyu/controller/VehicleUnifiedController.java create mode 100644 src/main/java/com/muyu/service/VehicleUnifiedService.java create mode 100644 src/main/java/com/muyu/service/impl/VehicleUnifiedServiceImpl.java diff --git a/src/main/java/com/muyu/controller/VehicleController.java b/src/main/java/com/muyu/controller/VehicleController.java index 6835680..3fa3d27 100644 --- a/src/main/java/com/muyu/controller/VehicleController.java +++ b/src/main/java/com/muyu/controller/VehicleController.java @@ -39,7 +39,7 @@ public class VehicleController { } /** * 根据输入的数量进行车辆随机生成 - * @param sum 车辆总数 + * @param vehicleCreateAddReq 车辆总数 * @return 结果集 */ @PostMapping("/create") @@ -49,4 +49,14 @@ public class VehicleController { } + /** + * 删除车辆 + * @param vin 车辆总数 + * @return 结果集 + */ + @DeleteMapping("/{vin}") + public Result delete(@PathVariable("vin") String vin){ + this.vehicleService.delete(vin); + return Result.success(null,"删除成功"); + } } diff --git a/src/main/java/com/muyu/controller/VehicleInstanceController.java b/src/main/java/com/muyu/controller/VehicleInstanceController.java index 20a2968..c1bc146 100644 --- a/src/main/java/com/muyu/controller/VehicleInstanceController.java +++ b/src/main/java/com/muyu/controller/VehicleInstanceController.java @@ -116,58 +116,4 @@ public class VehicleInstanceController { this.vehicleInstanceService.editStatus(vin, statusKey, statusValue); return Result.success(); } - - /** - * 一键上线 - */ - @PostMapping("/unified/online") - public Result unifiedOnline(){ - this.vehicleInstanceService.unifiedOnline(); - return Result.success(null,"已成功发布一键上线任务"); - } - - /** - * 一键离线 - */ - @PostMapping("/unified/offline") - public Result unifiedOffline(){ - this.vehicleInstanceService.unifiedOffline(); - return Result.success(null,"已成功发布一键离线任务"); - } - - /** - * 一键上报 - */ - @PostMapping("/unified/send") - public Result unifiedSend(){ - this.vehicleInstanceService.unifiedSend(); - return Result.success(null,"已成功发布一键上报任务"); - } - - /** - * 一键重置路径 - */ - @PostMapping("/unified/position") - public Result unifiedPosition(){ - this.vehicleInstanceService.unifiedPosition(); - return Result.success(null,"已成功发布一键上报任务"); - } - - /** - * 一键取消上报 - */ - @PostMapping("/unified/stop") - public Result unifiedStop(){ - this.vehicleInstanceService.unifiedStop(); - return Result.success(null,"已成功发布取消上报任务"); - } - - /** - * 获取一键执行状态 - * @return 执行结果 - */ - @GetMapping("/unified/status") - public Result unifiedStatus(){ - return Result.success(this.vehicleInstanceService.unifiedStatus()); - } } diff --git a/src/main/java/com/muyu/controller/VehicleUnifiedController.java b/src/main/java/com/muyu/controller/VehicleUnifiedController.java new file mode 100644 index 0000000..816540d --- /dev/null +++ b/src/main/java/com/muyu/controller/VehicleUnifiedController.java @@ -0,0 +1,80 @@ +package com.muyu.controller; + +import com.muyu.common.Result; +import com.muyu.domain.resp.UnifiedTaskResp; +import com.muyu.service.VehicleUnifiedService; +import lombok.extern.log4j.Log4j2; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author DongZeLiang + * @version 1.0 + * @description 车辆一键操作控制层 + * @date 2023/12/6 + */ +@Log4j2 +@RestController +@RequestMapping("/vehicle/unified") +public class VehicleUnifiedController { + + @Autowired + private VehicleUnifiedService vehicleUnifiedService; + + /** + * 一键上线 + */ + @PostMapping("/online") + public Result unifiedOnline(){ + this.vehicleUnifiedService.unifiedOnline(); + return Result.success(null,"已成功发布一键上线任务"); + } + + /** + * 一键离线 + */ + @PostMapping("/offline") + public Result unifiedOffline(){ + this.vehicleUnifiedService.unifiedOffline(); + return Result.success(null,"已成功发布一键离线任务"); + } + + /** + * 一键上报 + */ + @PostMapping("/send") + public Result unifiedSend(){ + this.vehicleUnifiedService.unifiedSend(); + return Result.success(null,"已成功发布一键上报任务"); + } + + /** + * 一键重置路径 + */ + @PostMapping("/position") + public Result unifiedPosition(){ + this.vehicleUnifiedService.unifiedPosition(); + return Result.success(null,"已成功发布一键上报任务"); + } + + /** + * 一键取消上报 + */ + @PostMapping("/stop") + public Result unifiedStop(){ + this.vehicleUnifiedService.unifiedStop(); + return Result.success(null,"已成功发布取消上报任务"); + } + + /** + * 获取一键执行状态 + * @return 执行结果 + */ + @GetMapping("/status") + public Result unifiedStatus(){ + return Result.success(this.vehicleUnifiedService.unifiedStatus()); + } +} diff --git a/src/main/java/com/muyu/domain/model/TaskModel.java b/src/main/java/com/muyu/domain/model/TaskModel.java index e385d12..3ea722a 100644 --- a/src/main/java/com/muyu/domain/model/TaskModel.java +++ b/src/main/java/com/muyu/domain/model/TaskModel.java @@ -6,6 +6,7 @@ import lombok.Data; import lombok.NoArgsConstructor; import lombok.extern.log4j.Log4j2; +import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; @@ -27,23 +28,16 @@ public class TaskModel { */ private final AtomicBoolean unifiedStatus = new AtomicBoolean(Boolean.FALSE); + private CountDownLatch countDownLatch ; + /** * 任务是否可以执行 * @return 是否有任务执行 */ public boolean isExecution(){ - // 为true表示任务在执行 - if (unifiedStatus.get()){ - // 就算状态为true若执行线程为null或者线程为不活跃也可以执行也可以执行任务 - return this.currentThread == null || !this.currentThread.isAlive(); - } - return true; + return !unifiedStatus.get(); } - /** - * 当前线程 - */ - private Thread currentThread ; /** * 任务名称 */ @@ -60,18 +54,16 @@ public class TaskModel { /** * 提交当前线程任务 - * @param task 线程任务 * @param taskName 任务名称 * @param taskExecutionSum 任务总数 */ - public void submit(String taskName,Integer taskExecutionSum, Thread task){ + public void submit(String taskName,Integer taskExecutionSum){ if (!this.isExecution()){ throw new RuntimeException("["+this.taskName+"]的任务正在进行中,请等待任务执行完成再次发布一键任务"); } unifiedStatus.set(Boolean.TRUE); - this.currentThread = task; - this.currentThread.start(); this.taskName = taskName; + this.countDownLatch = new CountDownLatch(taskExecutionSum); this.taskExecutionSum = taskExecutionSum; this.taskSuccessSum = new AtomicInteger(); this.taskErrorSum = new AtomicInteger(); @@ -84,7 +76,6 @@ public class TaskModel { */ public void down(){ log.info("[{}]任务执行结束,耗时:[{}]MS", this.taskName, System.currentTimeMillis() - taskStartTime); - this.currentThread = null; this.taskName = null; this.taskExecutionSum = 0; this.taskSuccessSum.set(0); diff --git a/src/main/java/com/muyu/domain/resp/UnifiedTaskResp.java b/src/main/java/com/muyu/domain/resp/UnifiedTaskResp.java index 8f03652..2ca55d1 100644 --- a/src/main/java/com/muyu/domain/resp/UnifiedTaskResp.java +++ b/src/main/java/com/muyu/domain/resp/UnifiedTaskResp.java @@ -5,9 +5,6 @@ import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; -import java.util.concurrent.atomic.AtomicBoolean; -import java.util.concurrent.atomic.AtomicInteger; - /** * @author DongZeLiang * @version 1.0 diff --git a/src/main/java/com/muyu/service/VehicleInstanceService.java b/src/main/java/com/muyu/service/VehicleInstanceService.java index dbbbfef..ed2de0e 100644 --- a/src/main/java/com/muyu/service/VehicleInstanceService.java +++ b/src/main/java/com/muyu/service/VehicleInstanceService.java @@ -71,35 +71,4 @@ public interface VehicleInstanceService { */ void editStatus (String vin, String statusKey, Integer statusValue); - - /** - * 一键上线 - */ - public void unifiedOnline(); - - /** - * 一键离线 - */ - public void unifiedOffline(); - - /** - * 一键上报 - */ - public void unifiedSend(); - - /** - * 一键重置路径 - */ - void unifiedPosition (); - - /** - * 一键取消上报 - */ - public void unifiedStop(); - - /** - * 一键执行状态 - * @return 一键执行状态 - */ - UnifiedTaskResp unifiedStatus(); } diff --git a/src/main/java/com/muyu/service/VehicleService.java b/src/main/java/com/muyu/service/VehicleService.java index 8a91bd1..5029388 100644 --- a/src/main/java/com/muyu/service/VehicleService.java +++ b/src/main/java/com/muyu/service/VehicleService.java @@ -29,4 +29,10 @@ public interface VehicleService extends IService { * 同步数据库 */ void syncDb(); + + /** + * 通过VIN删除车辆 + * @param vin vin + */ + void delete(String vin); } diff --git a/src/main/java/com/muyu/service/VehicleUnifiedService.java b/src/main/java/com/muyu/service/VehicleUnifiedService.java new file mode 100644 index 0000000..dad263b --- /dev/null +++ b/src/main/java/com/muyu/service/VehicleUnifiedService.java @@ -0,0 +1,45 @@ +package com.muyu.service; + +import com.muyu.domain.resp.UnifiedTaskResp; + +/** + * @author DongZeLiang + * @version 1.0 + * @description 一键设置业务层 + * @date 2023/12/6 + */ +public interface VehicleUnifiedService { + + + + /** + * 一键上线 + */ + public void unifiedOnline(); + + /** + * 一键离线 + */ + public void unifiedOffline(); + + /** + * 一键上报 + */ + public void unifiedSend(); + + /** + * 一键重置路径 + */ + void unifiedPosition (); + + /** + * 一键取消上报 + */ + public void unifiedStop(); + + /** + * 一键执行状态 + * @return 一键执行状态 + */ + UnifiedTaskResp unifiedStatus(); +} diff --git a/src/main/java/com/muyu/service/impl/VechileServiceImpl.java b/src/main/java/com/muyu/service/impl/VechileServiceImpl.java index d34fd71..2637086 100644 --- a/src/main/java/com/muyu/service/impl/VechileServiceImpl.java +++ b/src/main/java/com/muyu/service/impl/VechileServiceImpl.java @@ -115,5 +115,28 @@ public class VechileServiceImpl extends ServiceImpl impl } } + /** + * 通过VIN删除车辆 + * + * @param vin vin + */ + @Override + public void delete(String vin) { + VehicleInstance vehicleInstance = LocalContainer.getVehicleInstance(vin); + // 先判断车辆是否在上报数据,上报则停止 + if (vehicleInstance.isSend()){ + vehicleInstance.stopSend(); + } + // 判断车辆是否在线,若在线则让车辆下线 + if (vehicleInstance.isOnline()){ + vehicleInstance.closeClient(); + } + // 进行缓存删除 + LocalContainer.removeByVin(vin); + // 删除数据库 + this.removeById(vin); + } + + } diff --git a/src/main/java/com/muyu/service/impl/VehicleInstanceServiceImpl.java b/src/main/java/com/muyu/service/impl/VehicleInstanceServiceImpl.java index 1ec0097..3b5da8a 100644 --- a/src/main/java/com/muyu/service/impl/VehicleInstanceServiceImpl.java +++ b/src/main/java/com/muyu/service/impl/VehicleInstanceServiceImpl.java @@ -213,229 +213,4 @@ public class VehicleInstanceServiceImpl implements VehicleInstanceService { // private final AtomicBoolean unifiedStatus = new AtomicBoolean(Boolean.TRUE); - /** - * 任务执行模型 - */ - private final TaskModel taskModel = new TaskModel(); - - /** - * 一键上线 - */ - @Override - public void unifiedOnline () { - // 获取离线车辆 - List vinList - = LocalContainer.getOfflineVehicleInstance().stream().map(VehicleInstance::getVin).toList(); - CountDownLatch countDownLatch = new CountDownLatch(vinList.size()); - // 筛选出离线车辆并使用并行流进行上线操作 - for (String vin : vinList) { - FixedThreadPool.submit( - new Thread(() -> { - try { - this.vehicleClientInit(vin); - taskModel.incrementSuccess(); - } catch (Exception exception) { - exception.printStackTrace(); - log.error("车辆上线异常:{}", exception.getMessage()); - taskModel.incrementError(); - } - countDownLatch.countDown(); - }) - ); - } - new Thread(() -> { - boolean await = false; - do { - try { - await = countDownLatch.await(5, TimeUnit.SECONDS); - - log.info("等待一轮训,还剩余:[{}]", countDownLatch.getCount()); - } catch (InterruptedException e) { - log.error("等待异常:{}", e.getMessage(), e); - await = true; - } - } while (!await); - taskModel.down(); - }, "一键上线").start(); - } - - /** - * 一键离线 - */ - @Override - public void unifiedOffline () { - List onlineVehicleInstanceList = LocalContainer.getOnlineVehicleInstance(); - Thread taskThread = new Thread(() -> { - try { - // 筛选出在线车辆使用并行流操作先停止车辆上报动作再进行车辆离线操作 - onlineVehicleInstanceList - .stream() - .parallel() - .forEach(vehicleInstance -> { - try { - vehicleInstance.stopSend(); - vehicleInstance.closeClient(); - taskModel.incrementSuccess(); - }catch (Exception exception){ - log.error("车辆离线异常:{}", exception.getMessage()); - taskModel.incrementError(); - } - }); - } catch (Exception exception) { - log.error("车辆一键离线报错:{}", exception.getMessage(), exception); - } - taskModel.down(); - }); - taskModel.submit("一键离线", onlineVehicleInstanceList.size(), taskThread); - - } - - /** - * 一键上报 - */ - @Override - public void unifiedSend () { - List vehicleInstanceList = LocalContainer.getOnlineVehicleInstance(); - if (vehicleInstanceList.isEmpty()){ - throw new RuntimeException("还没有车辆连接到服务器,请先让车辆上线"); - } - // 获取到所有路径 - List positionRouteInfoList = positionRouteService.list(); - // 路径长度 - int positionSize = positionRouteInfoList.size(); - // 随机数 - Random random = new Random(); - Thread taskThread = new Thread(() -> { - try { - - vehicleInstanceList - .stream() - .parallel() - .forEach(vehicleInstance -> { - try { - // 随机一个路径结果 - int positionIndex = random.nextInt(0, positionSize); - PositionRouteInfo positionRouteInfo = positionRouteInfoList.get(positionIndex); - String positionCode = positionRouteInfo.getName(); - List positionModelList = JSONArray.parseArray(positionRouteInfo.getRouteData(), String.class) - .stream() - .map(PositionModel::strBuild) - .toList(); - // 设置车辆路径 - vehicleInstance.settingPosition(positionModelList); - vehicleInstance.setPositionCode(positionCode); - // 设置车辆档位 - vehicleInstance.setGear("D"); - // 开启线程进行上报 - if (vehicleInstance.getVehicleThread() == null) { - vehicleInstance.initVehicleThread(); - } - vehicleInstance.startSend(); - taskModel.incrementSuccess(); - }catch (Exception exception){ - log.info("车辆设置一键上报失败:{}", exception.getMessage()); - taskModel.incrementError(); - } - }); - } catch (Exception exception) { - log.error("车辆一键上报报错:{}", exception.getMessage(), exception); - } - taskModel.down(); - }); - taskModel.submit("一键上报", vehicleInstanceList.size(),taskThread); - - } - - /** - * 一键重置路径 - */ - @Override - public void unifiedPosition () { - List vehicleInstanceList = LocalContainer.getOnlineVehicleInstance(); - Thread taskThread = new Thread(() -> { - try { - // 获取到所有路径 - List positionRouteInfoList = positionRouteService.list(); - // 路径长度 - int positionSize = positionRouteInfoList.size(); - // 随机数 - Random random = new Random(); - - vehicleInstanceList - .stream() - .parallel() - .forEach(vehicleInstance -> { - try { - // 随机一个路径结果 - int positionIndex = random.nextInt(0, positionSize); - PositionRouteInfo positionRouteInfo = positionRouteInfoList.get(positionIndex); - String positionCode = positionRouteInfo.getName(); - List positionModelList = JSONArray.parseArray(positionRouteInfo.getRouteData(), String.class) - .stream() - .map(PositionModel::strBuild) - .toList(); - // 设置车辆路径 - vehicleInstance.settingPosition(positionModelList); - vehicleInstance.setPositionCode(positionCode); - taskModel.incrementSuccess(); - } catch (Exception exception) { - log.error("车辆设置路线异常:[{}]", exception.getMessage()); - taskModel.incrementError(); - } - }); - } catch (Exception exception) { - log.error("车辆一键重置路径报错:{}", exception.getMessage(), exception); - } - taskModel.down(); - }); - taskModel.submit("一键重置路径",vehicleInstanceList.size(), taskThread); - } - - /** - * 一键取消上报 - */ - @Override - public void unifiedStop () { - List onlineVehicleInstanceList = LocalContainer.getOnlineVehicleInstance(); - Thread taskThread = new Thread(() -> { - try { - LocalContainer.getOnlineVehicleInstance() - .stream() - .parallel() - .forEach(vehicleInstance -> { - try { - vehicleInstance.stopSend(); - taskModel.incrementSuccess(); - }catch (Exception exception){ - log.info("车辆一键取消上报发生错误:{}", exception.getMessage()); - taskModel.incrementError(); - } - }); - taskModel.down(); - } catch (Exception exception) { - log.error("车辆一键取消上报报错:{}", exception.getMessage(), exception); - } - }); - - taskModel.submit("一键取消上报", onlineVehicleInstanceList.size(), taskThread); - } - - /** - * 一键执行状态 - * - * @return 一键执行状态 - */ - @Override - public UnifiedTaskResp unifiedStatus() { - boolean unifiedStatus = this.taskModel.getUnifiedStatus().get(); - return UnifiedTaskResp.builder() - .unifiedStatus(unifiedStatus) - .taskErrorSum(this.taskModel.getErrorSum()) - .taskExecutionSum(this.taskModel.getTaskExecutionSum()) - .taskSuccessSum(this.taskModel.getSuccessSum()) - .taskName(this.taskModel.getTaskName()) - .taskStartTime(System.currentTimeMillis() - this.taskModel.getTaskStartTime()) - .build(); - } - } diff --git a/src/main/java/com/muyu/service/impl/VehicleUnifiedServiceImpl.java b/src/main/java/com/muyu/service/impl/VehicleUnifiedServiceImpl.java new file mode 100644 index 0000000..f5bb15d --- /dev/null +++ b/src/main/java/com/muyu/service/impl/VehicleUnifiedServiceImpl.java @@ -0,0 +1,263 @@ +package com.muyu.service.impl; + +import com.alibaba.fastjson2.JSONArray; +import com.muyu.common.pool.FixedThreadPool; +import com.muyu.domain.PositionRouteInfo; +import com.muyu.domain.model.PositionModel; +import com.muyu.domain.model.TaskModel; +import com.muyu.domain.resp.UnifiedTaskResp; +import com.muyu.service.PositionRouteService; +import com.muyu.service.VehicleInstanceService; +import com.muyu.service.VehicleUnifiedService; +import com.muyu.vehicle.VehicleInstance; +import com.muyu.vehicle.core.LocalContainer; +import lombok.extern.log4j.Log4j2; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Random; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +/** + * @author DongZeLiang + * @version 1.0 + * @description 一键设置业务实现层 + * @date 2023/12/6 + */ +@Log4j2 +@Service +public class VehicleUnifiedServiceImpl implements VehicleUnifiedService { + + @Autowired + private VehicleInstanceService vehicleInstanceService; + + @Autowired + private PositionRouteService positionRouteService; + + + /** + * 任务执行模型 + */ + private final TaskModel taskModel = new TaskModel(); + + /** + * 一键上线 + */ + @Override + public void unifiedOnline () { + // 获取离线车辆 + List vinList + = LocalContainer.getOfflineVehicleInstance().stream().map(VehicleInstance::getVin).toList(); + CountDownLatch countDownLatch = new CountDownLatch(vinList.size()); + // 筛选出离线车辆并使用并行流进行上线操作 + for (String vin : vinList) { + FixedThreadPool.submit( + new Thread(() -> { + try { + vehicleInstanceService.vehicleClientInit(vin); + taskModel.incrementSuccess(); + } catch (Exception exception) { + log.error("车辆上线异常:{}", exception.getMessage(), exception); + taskModel.incrementError(); + } + countDownLatch.countDown(); + }) + ); + } + new Thread(() -> { + boolean await = false; + do { + try { + await = countDownLatch.await(5, TimeUnit.SECONDS); + + log.info("等待一轮训,还剩余:[{}]", countDownLatch.getCount()); + } catch (InterruptedException e) { + log.error("等待异常:{}", e.getMessage(), e); + await = true; + } + } while (!await); + taskModel.down(); + }, "一键上线").start(); + } + + /** + * 一键离线 + */ + @Override + public void unifiedOffline () { + List onlineVehicleInstanceList = LocalContainer.getOnlineVehicleInstance(); + Thread taskThread = new Thread(() -> { + try { + // 筛选出在线车辆使用并行流操作先停止车辆上报动作再进行车辆离线操作 + onlineVehicleInstanceList + .stream() + .parallel() + .forEach(vehicleInstance -> { + try { + vehicleInstance.stopSend(); + vehicleInstance.closeClient(); + taskModel.incrementSuccess(); + }catch (Exception exception){ + log.error("车辆离线异常:{}", exception.getMessage()); + taskModel.incrementError(); + } + }); + } catch (Exception exception) { + log.error("车辆一键离线报错:{}", exception.getMessage(), exception); + } + taskModel.down(); + }); +// taskModel.submit("一键离线", onlineVehicleInstanceList.size(), taskThread); + + } + + /** + * 一键上报 + */ + @Override + public void unifiedSend () { + List vehicleInstanceList = LocalContainer.getOnlineVehicleInstance(); + if (vehicleInstanceList.isEmpty()){ + throw new RuntimeException("还没有车辆连接到服务器,请先让车辆上线"); + } + // 获取到所有路径 + List positionRouteInfoList = positionRouteService.list(); + // 路径长度 + int positionSize = positionRouteInfoList.size(); + // 随机数 + Random random = new Random(); + Thread taskThread = new Thread(() -> { + try { + + vehicleInstanceList + .stream() + .parallel() + .forEach(vehicleInstance -> { + try { + // 随机一个路径结果 + int positionIndex = random.nextInt(0, positionSize); + PositionRouteInfo positionRouteInfo = positionRouteInfoList.get(positionIndex); + String positionCode = positionRouteInfo.getName(); + List positionModelList = JSONArray.parseArray(positionRouteInfo.getRouteData(), String.class) + .stream() + .map(PositionModel::strBuild) + .toList(); + // 设置车辆路径 + vehicleInstance.settingPosition(positionModelList); + vehicleInstance.setPositionCode(positionCode); + // 设置车辆档位 + vehicleInstance.setGear("D"); + // 开启线程进行上报 + if (vehicleInstance.getVehicleThread() == null) { + vehicleInstance.initVehicleThread(); + } + vehicleInstance.startSend(); + taskModel.incrementSuccess(); + }catch (Exception exception){ + log.info("车辆设置一键上报失败:{}", exception.getMessage()); + taskModel.incrementError(); + } + }); + } catch (Exception exception) { + log.error("车辆一键上报报错:{}", exception.getMessage(), exception); + } + taskModel.down(); + }); +// taskModel.submit("一键上报", vehicleInstanceList.size(),taskThread); + + } + + /** + * 一键重置路径 + */ + @Override + public void unifiedPosition () { + List vehicleInstanceList = LocalContainer.getOnlineVehicleInstance(); + Thread taskThread = new Thread(() -> { + try { + // 获取到所有路径 + List positionRouteInfoList = positionRouteService.list(); + // 路径长度 + int positionSize = positionRouteInfoList.size(); + // 随机数 + Random random = new Random(); + + vehicleInstanceList + .stream() + .parallel() + .forEach(vehicleInstance -> { + try { + // 随机一个路径结果 + int positionIndex = random.nextInt(0, positionSize); + PositionRouteInfo positionRouteInfo = positionRouteInfoList.get(positionIndex); + String positionCode = positionRouteInfo.getName(); + List positionModelList = JSONArray.parseArray(positionRouteInfo.getRouteData(), String.class) + .stream() + .map(PositionModel::strBuild) + .toList(); + // 设置车辆路径 + vehicleInstance.settingPosition(positionModelList); + vehicleInstance.setPositionCode(positionCode); + taskModel.incrementSuccess(); + } catch (Exception exception) { + log.error("车辆设置路线异常:[{}]", exception.getMessage()); + taskModel.incrementError(); + } + }); + } catch (Exception exception) { + log.error("车辆一键重置路径报错:{}", exception.getMessage(), exception); + } + taskModel.down(); + }); +// taskModel.submit("一键重置路径",vehicleInstanceList.size(), taskThread); + } + + /** + * 一键取消上报 + */ + @Override + public void unifiedStop () { + List onlineVehicleInstanceList = LocalContainer.getOnlineVehicleInstance(); + Thread taskThread = new Thread(() -> { + try { + LocalContainer.getOnlineVehicleInstance() + .stream() + .parallel() + .forEach(vehicleInstance -> { + try { + vehicleInstance.stopSend(); + taskModel.incrementSuccess(); + }catch (Exception exception){ + log.info("车辆一键取消上报发生错误:{}", exception.getMessage()); + taskModel.incrementError(); + } + }); + taskModel.down(); + } catch (Exception exception) { + log.error("车辆一键取消上报报错:{}", exception.getMessage(), exception); + } + }); + +// taskModel.submit("一键取消上报", onlineVehicleInstanceList.size(), taskThread); + } + + /** + * 一键执行状态 + * + * @return 一键执行状态 + */ + @Override + public UnifiedTaskResp unifiedStatus() { + boolean unifiedStatus = this.taskModel.getUnifiedStatus().get(); + return UnifiedTaskResp.builder() + .unifiedStatus(unifiedStatus) + .taskErrorSum(this.taskModel.getErrorSum()) + .taskExecutionSum(this.taskModel.getTaskExecutionSum()) + .taskSuccessSum(this.taskModel.getSuccessSum()) + .taskName(this.taskModel.getTaskName()) + .taskStartTime(System.currentTimeMillis() - this.taskModel.getTaskStartTime()) + .build(); + } +} diff --git a/src/main/java/com/muyu/vehicle/VehicleInstance.java b/src/main/java/com/muyu/vehicle/VehicleInstance.java index 30a8d46..2f5c857 100644 --- a/src/main/java/com/muyu/vehicle/VehicleInstance.java +++ b/src/main/java/com/muyu/vehicle/VehicleInstance.java @@ -158,6 +158,14 @@ public class VehicleInstance { return this.client.isConnected(); } + /** + * 是否建立车辆模拟线程 + * @return 建立返回true不建立返回false + */ + public boolean isSend(){ + return this.vehicleThread != null; + } + /** * 关闭连接 */ diff --git a/src/main/java/com/muyu/vehicle/core/LocalContainer.java b/src/main/java/com/muyu/vehicle/core/LocalContainer.java index 658c2b4..80e503a 100644 --- a/src/main/java/com/muyu/vehicle/core/LocalContainer.java +++ b/src/main/java/com/muyu/vehicle/core/LocalContainer.java @@ -70,4 +70,12 @@ public class LocalContainer { public static List getOfflineVehicleInstance(){ return vehicleDataMap.values().stream().filter(vehicleInstance -> !vehicleInstance.isOnline()).toList(); } + + /** + * 通过VIN删除 + * @param vin 车辆VIN + */ + public static void removeByVin(String vin) { + vehicleDataMap.remove(vin); + } }