NetworkingCar/src/main/java/com/muyu/vehicle/thread/VehicleThread.java

77 lines
1.9 KiB
Java

package com.muyu.vehicle.thread;
import com.alibaba.fastjson2.JSONObject;
import com.muyu.vehicle.VehicleInstance;
import lombok.extern.log4j.Log4j2;
@Log4j2
public class VehicleThread implements Runnable {
/**
* 是否停止线程
*/
private volatile boolean isStop = false;
/**
* 设置是否暂停
*/
private volatile boolean isPaused;
/**
* 车辆实例对象
*/
private VehicleInstance vehicleInstance;
@Override
public void run() {
try {
if (!isStop){
if (!isPaused){
log.info("{} - 上报数据", this.vehicleInstance.getVin());
String imitateResult = this.vehicleInstance.imitateData();
if (imitateResult == null){
this.vehicleInstance.sendMsg(
this.vehicleInstance.getVehicleData().getMsg()
);
}else {
log.warn("车辆[{}]数据模拟:{}", this.vehicleInstance.getVin(), imitateResult);
}
}else {
log.info("暂停模拟和上报:[{}]", this.vehicleInstance.getVin());
}
}else {
log.info("终止模拟和上报:[{}]", this.vehicleInstance.getVin());
vehicleInstance.cancelExecution();
}
}catch (Throwable throwable){
log.error("车辆模拟输出错误:{}", this.vehicleInstance.getVin(), throwable);
}
}
/**
* 暂停线程
*/
public void pause() {
isPaused = true;
}
/**
* 开始线程
*/
public void resume() {
isPaused = false;
}
/**
* 停止方法
*/
public void stop(){
this.isStop = true;
}
public void setVehicleInstance(VehicleInstance vehicleInstance) {
this.vehicleInstance = vehicleInstance;
}
}