车辆上下线连接mqtt客户端
parent
320fe5c311
commit
8fd9aea58a
|
@ -6,10 +6,7 @@ import com.muyu.domain.resp.VehicleInstanceResp;
|
|||
import com.muyu.service.VehicleInstanceService;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
@ -36,4 +33,26 @@ public class VehicleInstanceController {
|
|||
List<VehicleInstanceResp> list = vehicleInstanceService.queryList(vehicleInstanceListReq);
|
||||
return Result.success(list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 车辆连接初始化
|
||||
* @param vin vin
|
||||
* @return 初始化
|
||||
*/
|
||||
@PostMapping("/client/init/{vin}")
|
||||
public Result<String> vehicleClientInit(@PathVariable("vin") String vin){
|
||||
this.vehicleInstanceService.vehicleClientInit(vin);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 车辆连接初始化
|
||||
* @param vin vin
|
||||
* @return 初始化
|
||||
*/
|
||||
@PostMapping("/client/close/{vin}")
|
||||
public Result<String> vehicleClientClose(@PathVariable("vin") String vin){
|
||||
this.vehicleInstanceService.vehicleClientClose(vin);
|
||||
return Result.success();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,9 +22,15 @@ public class VehicleInstanceResp {
|
|||
*/
|
||||
private String vin;
|
||||
|
||||
/**
|
||||
* 是否在线
|
||||
*/
|
||||
public boolean isOnline;
|
||||
|
||||
public static VehicleInstanceResp instanceBuild (VehicleInstance vehicleInstance) {
|
||||
return VehicleInstanceResp.builder()
|
||||
.vin(vehicleInstance.getVin())
|
||||
.isOnline(vehicleInstance.isOnline())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,4 +32,16 @@ public interface VehicleInstanceService {
|
|||
* @return 车辆对象
|
||||
*/
|
||||
List<VehicleInstanceResp> queryList (VehicleInstanceListReq vehicleInstanceListReq);
|
||||
|
||||
/**
|
||||
* 车辆客户端初始化
|
||||
* @param vin vin
|
||||
*/
|
||||
void vehicleClientInit (String vin);
|
||||
|
||||
/**
|
||||
* 车辆客户端关闭
|
||||
* @param vin vin
|
||||
*/
|
||||
void vehicleClientClose (String vin);
|
||||
}
|
||||
|
|
|
@ -50,7 +50,7 @@ public class VehicleInstanceServiceImpl implements VehicleInstanceService {
|
|||
.clientId(vin)
|
||||
.build();
|
||||
vehicleInstance.setMqttProperties(mqttProperties);
|
||||
vehicleInstance.initCline();
|
||||
vehicleInstance.initClient();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -68,4 +68,36 @@ public class VehicleInstanceServiceImpl implements VehicleInstanceService {
|
|||
.toList();
|
||||
}
|
||||
|
||||
/**
|
||||
* 车辆客户端初始化
|
||||
*
|
||||
* @param vin vin
|
||||
*/
|
||||
@Override
|
||||
public void vehicleClientInit (String vin) {
|
||||
VehicleInstance vehicleInstance = LocalContainer.getVehicleInstance(vin);
|
||||
if (vehicleInstance == null){
|
||||
throw new RuntimeException("没有【"+vin+"】车辆");
|
||||
}
|
||||
vehicleInstance.setMqttProperties(
|
||||
MqttProperties.builder()
|
||||
.broker("tcp://fluxmq.muyu.icu:1883")
|
||||
.topic("test")
|
||||
.clientId(vin)
|
||||
.build()
|
||||
);
|
||||
vehicleInstance.initClient();
|
||||
}
|
||||
|
||||
/**
|
||||
* 车辆客户端关闭
|
||||
*
|
||||
* @param vin vin
|
||||
*/
|
||||
@Override
|
||||
public void vehicleClientClose (String vin) {
|
||||
VehicleInstance vehicleInstance = LocalContainer.getVehicleInstance(vin);
|
||||
vehicleInstance.closeClient();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,21 +1,17 @@
|
|||
package com.muyu.vehicle;
|
||||
|
||||
import com.alibaba.fastjson2.JSONArray;
|
||||
import com.muyu.common.ThreadPool;
|
||||
import com.muyu.domain.Vehicle;
|
||||
import com.muyu.domain.model.PositionModel;
|
||||
import com.muyu.utils.VehicleUtils;
|
||||
import com.muyu.vehicle.core.LocalContainer;
|
||||
import com.muyu.vehicle.model.VehicleData;
|
||||
import com.muyu.vehicle.model.properties.MqttProperties;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
import static java.lang.Thread.sleep;
|
||||
|
||||
|
@ -32,16 +28,18 @@ public class Test {
|
|||
private static MqttProperties mqttProperties = MqttProperties.builder()
|
||||
.broker("tcp://fluxmq.muyu.icu:1883")
|
||||
.topic("test")
|
||||
.username("123456")
|
||||
.password("AJLJWIEJLASDJOWQES")
|
||||
.build();
|
||||
|
||||
public static void main(String[] args) {
|
||||
log.info("开始生成VIN");
|
||||
long genVinStartTime = System.currentTimeMillis();
|
||||
List<String> list = new ArrayList<>();
|
||||
// list.add("VIN12345678912345");
|
||||
for (int i = 0; i < 10; i++) {
|
||||
list.add(VehicleUtils.genVin());
|
||||
}
|
||||
list.add("VIN12345678912345");
|
||||
// for (int i = 0; i < 10; i++) {
|
||||
// list.add(VehicleUtils.genVin());
|
||||
// }
|
||||
log.info("生成VIN结束:[{}MS]", System.currentTimeMillis()-genVinStartTime);
|
||||
log.info("开始创建车辆");
|
||||
long initVehicleStartTime = System.currentTimeMillis();
|
||||
|
@ -94,7 +92,7 @@ public class Test {
|
|||
log.info("构建车辆实例");
|
||||
vehicleInstance.setVehicle(vehicle);
|
||||
vehicleInstance.setVehicleData(VehicleData.vehicleBuild(vehicle));
|
||||
vehicleInstance.initCline();
|
||||
vehicleInstance.initClient();
|
||||
log.info("构建车辆客户端");
|
||||
LocalContainer.setVehicleInstance(vehicleInstance);
|
||||
}
|
||||
|
|
|
@ -116,7 +116,7 @@ public class VehicleInstance {
|
|||
/**
|
||||
* 初始化客户端
|
||||
*/
|
||||
public void initCline() {
|
||||
public void initClient () {
|
||||
try {
|
||||
client = new MqttClient(mqttProperties.getBroker(), mqttProperties.getClientId(), new MemoryPersistence());
|
||||
// 连接参数
|
||||
|
@ -137,6 +137,34 @@ public class VehicleInstance {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否连接在线
|
||||
* @return 在线返回true,不在线为false
|
||||
*/
|
||||
public boolean isOnline () {
|
||||
if (this.client == null){
|
||||
return false;
|
||||
}
|
||||
return this.client.isConnected();
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭连接
|
||||
*/
|
||||
public void closeClient(){
|
||||
if (this.client != null){
|
||||
try {
|
||||
// 断开连接
|
||||
this.client.disconnect();
|
||||
// 关闭连接
|
||||
this.client.close();
|
||||
log.info("车辆:[{}] 客户端下线成功", getVin());
|
||||
} catch (MqttException e) {
|
||||
log.error("车辆:[{}] 客户端关闭异常:[{}]",getVin(), e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化车辆路线
|
||||
* @param positionModelList 路线集合
|
||||
|
@ -247,28 +275,4 @@ public class VehicleInstance {
|
|||
this.vehicleData.setGear(gear);
|
||||
}
|
||||
|
||||
/*public static void main (String[] args) {
|
||||
PositionModel lastPositionModel = PositionModel.builder()
|
||||
.longitude("116.664053")
|
||||
.latitude("39.531791")
|
||||
.build();
|
||||
PositionModel currentPositionModel = PositionModel.builder()
|
||||
.longitude("116.655091")
|
||||
.latitude("39.52091")
|
||||
.build();
|
||||
// 两点之间的距离
|
||||
BigDecimal distance = VehicleUtils.distance(lastPositionModel, currentPositionModel);
|
||||
|
||||
// 当前电量减少
|
||||
// 电池浮动
|
||||
BigDecimal batteryFloat = VehicleUtils.batteryFloat();
|
||||
BigDecimal divide = distance.divide(SystemConstant.hundredKilometers).setScale(4, RoundingMode.HALF_UP);
|
||||
BigDecimal multiply = SystemConstant.powerConsumption.multiply(divide)
|
||||
.multiply(batteryFloat)
|
||||
.setScale(2, RoundingMode.HALF_UP);
|
||||
System.out.println("移动距离:" + distance);
|
||||
System.out.println("浮动量:" + batteryFloat);
|
||||
System.out.println("百公里占比:" + divide);
|
||||
System.out.println("当前减少电池量:" + multiply);
|
||||
}*/
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue