Vehicle/src/main/java/com/muyu/vehicle/VehicleInstance.java

160 lines
3.9 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.muyu.vehicle;
import com.muyu.common.ThreadPool;
import com.muyu.domain.Vehicle;
import com.muyu.vehicle.model.VehicleData;
import com.muyu.vehicle.model.properties.MqttProperties;
import com.muyu.vehicle.thread.VehicleThread;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import java.util.Objects;
import java.util.concurrent.ScheduledFuture;
import static java.lang.Thread.sleep;
/**
* @author DongZeLiang
* @version 1.0
* @description 车辆实例
* @date 2023/11/16
*/
@Data
@Log4j2
@Builder
@AllArgsConstructor
public class VehicleInstance {
/**
* 车辆
*/
private Vehicle vehicle;
/**
* 实例数据
*/
private VehicleData vehicleData;
/**
* 车辆工作线程
*/
private VehicleThread vehicleThread;
/**
* 线程提交回调
*/
private ScheduledFuture<?> scheduledFuture;
/**
* 链接上报
*/
private MqttClient client = null;
/**
* Mqtt配置
*/
private MqttProperties mqttProperties;
public VehicleInstance(MqttProperties mqttProperties) {
this.mqttProperties = mqttProperties;
}
/***
* 获取当前车辆VIN
* @return VIN
*/
public String getVin(){
return this.vehicle.getVin();
}
public void sendMsg(String msg){
// 创建消息并设置 QoS
MqttMessage message = new MqttMessage(msg.getBytes());
message.setQos(this.mqttProperties.getQos());
// 发布消息
try {
client.publish(this.mqttProperties.getTopic(), message);
} catch (MqttException e) {
throw new RuntimeException(e);
}
}
public void initCline(){
try {
client = new MqttClient(mqttProperties.getBroker(), mqttProperties.getClientId(), new MemoryPersistence());
// 连接参数
MqttConnectOptions options = new MqttConnectOptions();
// 设置用户名和密码
if (Objects.nonNull(mqttProperties.getUsername()) && Objects.nonNull(mqttProperties.getPassword())) {
options.setUserName(mqttProperties.getUsername());
options.setPassword(mqttProperties.getPassword().toCharArray());
}
options.setConnectionTimeout(60);
options.setKeepAliveInterval(60);
// 连接
client.connect(options);
log.info("mqtt初始化成功");
} catch (MqttException e) {
log.error("mqtt链接服务器异常{}", e.getMessage(), e);
throw new RuntimeException(e);
}
}
/**
* 初始化线程
*/
public void initVehicleThread(){
VehicleThread vehicleThread = new VehicleThread();
vehicleThread.setVehicleInstance(this);
this.setVehicleThread(vehicleThread);
this.vehicleThread.pause();
ScheduledFuture<?> scheduledFuture = ThreadPool.submit(vehicleThread);
this.setScheduledFuture(scheduledFuture);
log.info("初始化车辆上报模拟线程开始:[{}]", this.getVin());
}
/**
* 开始上报线程
*/
public void startSend(){
this.vehicleThread.resume();
}
/**
* 暂停上报线程
*/
public void pauseSend(){
this.vehicleThread.pause();
}
/**
* 结束发送
*/
public void stopSend(){
this.vehicleThread.stop();
}
/**
* 取消执行
*/
public void cancelExecution(){
scheduledFuture.cancel(true);
this.vehicleThread = null;
this.scheduledFuture = null;
}
}