报文上报
parent
4c978050f0
commit
4cf4047a86
|
@ -17,4 +17,14 @@ public class SystemConstant {
|
|||
public final static BigDecimal powerConsumption = new BigDecimal(10000);
|
||||
|
||||
public final static BigDecimal hundredKilometers = new BigDecimal(100);
|
||||
|
||||
/**
|
||||
* 报文起始位
|
||||
*/
|
||||
public static final String MSG_START = "7E ";
|
||||
|
||||
/**
|
||||
* 报文结束位
|
||||
*/
|
||||
public static final String MSG_END = "7E";
|
||||
}
|
||||
|
|
|
@ -0,0 +1,44 @@
|
|||
package com.muyu.utils;
|
||||
|
||||
/**
|
||||
* 校验位计算
|
||||
*/
|
||||
public class CalculateCheckDigit {
|
||||
|
||||
/**
|
||||
* 不去空格
|
||||
* @param sHex
|
||||
* @return
|
||||
*/
|
||||
public static String makeCheck(String sHex){
|
||||
return makeCheckSum(sHex.replace(" ", ""));
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算校验位 ,返回十六进制校验位
|
||||
* */
|
||||
private static String makeCheckSum(String data) {
|
||||
int dSum = 0;
|
||||
int length = data.length();
|
||||
int index = 0;
|
||||
// 遍历十六进制,并计算总和
|
||||
while (index < length) {
|
||||
// 截取2位字符
|
||||
String s = data.substring(index, index + 2);
|
||||
// 十六进制转成十进制 , 并计算十进制的总和
|
||||
dSum += Integer.parseInt(s, 16);
|
||||
index = index + 2;
|
||||
}
|
||||
|
||||
// 用256取余,十六进制最大是FF,FF的十进制是255
|
||||
int mod = dSum % 256;
|
||||
// 余数转成十六进制
|
||||
String checkSumHex = Integer.toHexString(mod);
|
||||
length = checkSumHex.length();
|
||||
if (length < 2) {
|
||||
// 校验位不足两位的,在前面补0
|
||||
checkSumHex = "0" + checkSumHex;
|
||||
}
|
||||
return checkSumHex;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package com.muyu.utils;
|
||||
|
||||
public class ConversionUtil {
|
||||
|
||||
/**
|
||||
* 字符串转化成为16进制字符串
|
||||
* @param s
|
||||
* @return
|
||||
*/
|
||||
public static String strToSixteen(String s) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int length = s.length();
|
||||
for (int i = 0; i < length; i++) {
|
||||
int ch = s.charAt(i);
|
||||
String s4 = Integer.toHexString(ch);
|
||||
sb.append(s4 + " ");
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
File diff suppressed because one or more lines are too long
|
@ -4,6 +4,8 @@ import com.muyu.common.SystemConstant;
|
|||
import com.muyu.common.ThreadPool;
|
||||
import com.muyu.domain.Vehicle;
|
||||
import com.muyu.domain.model.PositionModel;
|
||||
import com.muyu.utils.CalculateCheckDigit;
|
||||
import com.muyu.utils.ConversionUtil;
|
||||
import com.muyu.utils.VehicleUtils;
|
||||
import com.muyu.vehicle.model.VehicleData;
|
||||
import com.muyu.vehicle.model.properties.MqttProperties;
|
||||
|
@ -20,11 +22,12 @@ import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
|
|||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
|
||||
import static com.muyu.common.SystemConstant.powerConsumption;
|
||||
import static com.muyu.common.SystemConstant.*;
|
||||
|
||||
/**
|
||||
* @author DongZeLiang
|
||||
|
@ -87,7 +90,16 @@ public class VehicleInstance {
|
|||
return this.vehicle.getVin();
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送消息
|
||||
* @param msg 消息
|
||||
*/
|
||||
public void sendMsg(String msg) {
|
||||
//得到16进制报文
|
||||
String sHex = ConversionUtil.strToSixteen(msg);
|
||||
//计算校验和
|
||||
String makeCheck = CalculateCheckDigit.makeCheck(sHex);
|
||||
msg = MSG_START + sHex + makeCheck + " " + MSG_END;
|
||||
// 创建消息并设置 QoS
|
||||
MqttMessage message = new MqttMessage(msg.getBytes());
|
||||
message.setQos(this.mqttProperties.getQos());
|
||||
|
@ -99,6 +111,9 @@ public class VehicleInstance {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化客户端
|
||||
*/
|
||||
public void initCline() {
|
||||
try {
|
||||
client = new MqttClient(mqttProperties.getBroker(), mqttProperties.getClientId(), new MemoryPersistence());
|
||||
|
@ -120,6 +135,14 @@ public class VehicleInstance {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化车辆路线
|
||||
* @param positionModelList 路线集合
|
||||
*/
|
||||
public void settingPosition(List<PositionModel> positionModelList){
|
||||
positionModelList.forEach(positionQueue::offer);
|
||||
log.info("车辆:{} 设置路径成功", this.getVin());
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化线程
|
||||
|
@ -167,17 +190,18 @@ public class VehicleInstance {
|
|||
/**
|
||||
* 模拟车辆数据
|
||||
*/
|
||||
public void imitateData() {
|
||||
public String imitateData() {
|
||||
String gear = this.vehicleData.getGear();
|
||||
if (!gear.equals("D")){
|
||||
// TODO 不为D档,则无法启动车辆
|
||||
if (!"D".equals(gear)){
|
||||
log.info("车辆不是动车档位,不进行模拟数据");
|
||||
return null;
|
||||
}
|
||||
// 获取上一次定位点
|
||||
PositionModel lastPositionModel = this.lastPosition;
|
||||
PositionModel lastPositionModel = this.lastPosition == null ? positionQueue.poll() : this.lastPosition;
|
||||
// 获取当前定位点
|
||||
PositionModel currentPositionModel = positionQueue.poll();
|
||||
if (currentPositionModel == null) {
|
||||
// TODO 表示当前定位点已经跑完,需要其他操作
|
||||
return "表示当前定位点已经跑完,需要其他操作";
|
||||
}
|
||||
// 两点之间的距离
|
||||
BigDecimal distance = VehicleUtils.distance(lastPositionModel, currentPositionModel);
|
||||
|
@ -205,7 +229,18 @@ public class VehicleInstance {
|
|||
vehicleData.setSpeed(
|
||||
distance.divide(new BigDecimal(10)).multiply(new BigDecimal("3.6")).setScale(2, RoundingMode.HALF_UP).toString()
|
||||
);
|
||||
vehicleData.imitateBase();
|
||||
vehicleData.imitateMotor();
|
||||
vehicleData.imitateBatteryPack();
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更改车辆档位
|
||||
* @param d
|
||||
*/
|
||||
public void setGear (String gear) {
|
||||
this.vehicleData.setGear(gear);
|
||||
}
|
||||
|
||||
/*public static void main (String[] args) {
|
||||
|
|
|
@ -70,7 +70,7 @@ public class VehicleData {
|
|||
/**
|
||||
* 档位
|
||||
*/
|
||||
private String gear;
|
||||
private String gear = "P";
|
||||
|
||||
/**
|
||||
* 加速踏板行程值
|
||||
|
@ -180,87 +180,87 @@ public class VehicleData {
|
|||
/**
|
||||
* 车辆状态
|
||||
*/
|
||||
private int vehicleStatus;
|
||||
private int vehicleStatus = 1;
|
||||
|
||||
/**
|
||||
* 充电状态
|
||||
*/
|
||||
private int chargingStatus;
|
||||
private int chargingStatus = 1;
|
||||
|
||||
/**
|
||||
* 运行状态
|
||||
*/
|
||||
private int operatingStatus;
|
||||
private int operatingStatus = 1;
|
||||
|
||||
/**
|
||||
* SOC
|
||||
*/
|
||||
private int socStatus;
|
||||
private int socStatus = 1;
|
||||
|
||||
/**
|
||||
* 可充电储能装置工作状态
|
||||
*/
|
||||
private int chargingEnergyStorageStatus;
|
||||
private int chargingEnergyStorageStatus = 1;
|
||||
|
||||
/**
|
||||
* 驱动电机状态
|
||||
*/
|
||||
private int driveMotorStatus;
|
||||
private int driveMotorStatus = 1;
|
||||
|
||||
/**
|
||||
* 定位是否有效
|
||||
*/
|
||||
private int positionStatus;
|
||||
private int positionStatus = 1;
|
||||
|
||||
/**
|
||||
* EAS(汽车防盗系统)状态
|
||||
*/
|
||||
private int easStatus;
|
||||
private int easStatus = 1;
|
||||
|
||||
/**
|
||||
* PTC(电动加热器)状态
|
||||
*/
|
||||
private int ptcStatus;
|
||||
private int ptcStatus = 1;
|
||||
|
||||
/**
|
||||
* EPS(电动助力系统)状态
|
||||
*/
|
||||
private int epsStatus;
|
||||
private int epsStatus = 1;
|
||||
|
||||
/**
|
||||
* ABS(防抱死)状态
|
||||
*/
|
||||
private int absStatus;
|
||||
private int absStatus = 1;
|
||||
|
||||
/**
|
||||
* MCU(电机/逆变器)状态
|
||||
*/
|
||||
private int mcuStatus;
|
||||
private int mcuStatus = 1;
|
||||
|
||||
/**
|
||||
* 动力电池加热状态
|
||||
*/
|
||||
private int heatingStatus;
|
||||
private int heatingStatus = 1;
|
||||
|
||||
/**
|
||||
* 动力电池当前状态
|
||||
*/
|
||||
private int batteryStatus;
|
||||
private int batteryStatus = 1;
|
||||
|
||||
/**
|
||||
* 动力电池保温状态
|
||||
*/
|
||||
private int batteryInsulationStatus;
|
||||
private int batteryInsulationStatus = 1;
|
||||
|
||||
/**
|
||||
* DCDC(电力交换系统)状态
|
||||
*/
|
||||
private int dcdcStatus;
|
||||
private int dcdcStatus = 1;
|
||||
|
||||
/**
|
||||
* CHG(充电机)状态
|
||||
*/
|
||||
private int chgStatus;
|
||||
private int chgStatus = 1;
|
||||
|
||||
/**
|
||||
* 车辆状态 报文
|
||||
|
@ -279,6 +279,8 @@ public class VehicleData {
|
|||
StringBuilder sb = new StringBuilder();
|
||||
//第一位VIN
|
||||
sb.append(vin);
|
||||
// 当前时间戳
|
||||
sb.append(System.currentTimeMillis());
|
||||
//第二位经度 longitude latitude
|
||||
sb.append(getValue(longitude ,11));
|
||||
//第三位维度 longitude latitude
|
||||
|
|
|
@ -23,18 +23,27 @@ public class VehicleThread implements Runnable {
|
|||
|
||||
@Override
|
||||
public void run() {
|
||||
if (!isStop){
|
||||
if (!isPaused){
|
||||
log.info("{} - 上报数据", this.vehicleInstance.getVin());
|
||||
this.vehicleInstance.sendMsg(
|
||||
String.format("%s - 上报数据: %s", this.vehicleInstance.getVin(), this.vehicleInstance.getVehicleData().getMsg())
|
||||
);
|
||||
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());
|
||||
log.info("终止模拟和上报:[{}]", this.vehicleInstance.getVin());
|
||||
vehicleInstance.cancelExecution();
|
||||
}
|
||||
}else {
|
||||
log.info("终止模拟和上报:[{}]", this.vehicleInstance.getVin());
|
||||
vehicleInstance.cancelExecution();
|
||||
}catch (Throwable throwable){
|
||||
log.error("车辆模拟输出错误:{}", this.vehicleInstance.getVin(), throwable);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue