IotDB_kafka_mqtt/src/main/java/com/muyu/utils/ConversionUtil.java

355 lines
14 KiB
Java

package com.muyu.utils;
import com.muyu.mqtt.dao.MessageData;
import lombok.extern.log4j.Log4j2;
import java.nio.charset.StandardCharsets;
@Log4j2
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();
}
public static MessageData main (String args) {
// String str = "<?xml version=\"1.0\"?>\n" +
// "<monitorRoot type=\"param\"><synchronizeSyptom event=\"0\" initial=\"true\"><Action_ECG><Rhythm>Sinus</Rhythm><HR>80</HR><EMD>No Change</EMD><Conduct>0</Conduct></Action_ECG><Action_Osat value=\"94\" isRelativePercent=\"false\"/><Action_BP isRelativePercent=\"false\"><Shrink value=\"120\"/><Stretch value=\"80\"/></Action_BP><Action_Resp breathType=\"Normal\" value=\"14\" isRelativePercent=\"false\"/><Action_etCO2 value=\"34\" isRelativePercent=\"false\"/><Action_Temperature value=\"35.2\"/><Action_CVP value=\"6.0\"/><Action_PAPDia value=\"10\"/><Action_PAPSys value=\"25\"/><Action_WP value=\"9\"/></synchronizeSyptom></monitorRoot>";
// String strToSixteen = strToSixteen(str);
// System.out.println(str);
// System.out.println(str.length());
// System.out.println(strToSixteen);
// System.out.println(strToSixteen.replace(" ", "").length());
// String hexStr = "7E 48 58 49 54 48 58 55 51 52 32 54 55 43 30 58 33 55 31 37 31 37 35 38 39 32 35 37 35 35 31 31 31 36 2e 37 36 34 36 35 30 30 33 39 2e 35 37 34 38 38 30 30 32 37 30 2e 30 30 32 30 2e 33 35 30 30 30 30 30 30 32 37 36 30 30 30 38 30 30 30 30 32 32 30 38 30 30 30 30 30 44 31 30 30 30 39 2e 30 30 30 33 32 30 30 30 30 34 34 37 34 39 37 38 36 30 31 30 39 30 30 30 32 32 32 30 30 35 32 38 34 30 30 30 30 37 35 38 38 34 2e 33 32 30 30 30 30 31 33 30 30 30 30 31 31 34 30 30 30 30 36 39 39 30 30 30 34 30 30 30 33 30 30 30 35 30 30 30 30 30 35 30 30 30 30 30 32 33 30 30 30 30 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 11 7E";
String hexStringToString = hexStringToString(args);
System.out.println(args);
System.out.println(args.length());
System.out.println(hexStringToString);
System.out.println(hexStringToString.length());
//截取第一位和最后两位
String substring = hexStringToString.substring(1, hexStringToString.length() - 2);
log.error("substring:{}",substring.length());
//vin
String vin = substring.substring(0,17);
// log.error("length:{}",vin.length());
// log.error("vin:{}",vin);
//时间戳
String timestamp = substring.substring(17,30);
// log.error("length:{}",timestamp.length());
// log.error("timestamp:{}",timestamp);
// //经度
String longitude = substring.substring(30, 41);
// log.error("length:{}",longitude.length());
// log.error("longitude:{}",longitude);
// //维度
String latitude = substring.substring(41, 51);
// log.error("length:{}",latitude.length());
// log.error("latitude:{}",latitude);
// //车速
String speed = substring.substring(51, 57);
int length = speed.length();
// log.error("length:{}",length);
// log.info("speed:{}",speed);
//总里程
String mileage = substring.substring(57, 68);
// log.error("length:{}",mileage.length());
// log.error("mileage:{}",mileage);
//总电压
String dischargeVoltage = substring.substring(68, 74);
// log.error("length:{}",dischargeVoltage.length());
// log.error("dischargeVoltage:{}",dischargeVoltage);
//总电流
String dischargeCurrent = substring.substring(74, 79);
// log.error("length:{}",dischargeCurrent.length());
// log.error("dischargeCurrent:{}",dischargeCurrent);
//绝缘电阻
String insulationResistance = substring.substring(79, 88);
// log.error("length:{}",insulationResistance.length());
// log.error("insulationResistance:{}",insulationResistance);
// //档位
String gear = substring.substring(88, 89);
// log.error("length:{}",gear.length());
// log.error("gear:{}",gear);
//加速踏板行程值
String accelerationPedal = substring.substring(89, 91);
// log.error("length:{}",accelerationPedal.length());
// log.error("accelerationPedal:{}",accelerationPedal);
//制动踏板行程值
String brakePedal = substring.substring(91, 93);
// log.error("length:{}",brakePedal.length());
// log.error("brakePedal:{}",brakePedal);
//燃料消耗率
String fuelConsumption = substring.substring(93, 98);
// log.error("length:{}",fuelConsumption.length());
// log.error("fuelConsumption:{}",fuelConsumption);
////////////////
//电机控制器温度
String motorControllerTemperature = substring.substring(98, 104);
// log.error("length:{}",motorControllerTemperature.length());
// log.error("motorControllerTemperature:{}",motorControllerTemperature);
//电机转速
String motorSpeed = substring.substring(104, 109);
// log.error("length:{}",motorSpeed.length());
// log.error("motorSpeed:{}",motorSpeed);
//电机转矩
String motorTorque = substring.substring(109, 113);
// log.error("length:{}",motorTorque.length());
// log.error("motorTorque:{}",motorTorque);
//电机温度
String motorTemperature = substring.substring(113, 119);
// log.error("length:{}",motorTemperature.length());
// log.error("motorTemperature:{}",motorTemperature);
//电机电压
String motorVoltage = substring.substring(119, 124);
// log.error("length:{}",motorVoltage.length());
// log.error("motorVoltage:{}",motorVoltage);
//电机电流
String motorCurrent = substring.substring(124, 132);
// log.error("length:{}",motorCurrent.length());
// log.error("motorCurrent:{}",motorCurrent);
//动力电池剩余电量SOC
String powerBattery = substring.substring(132, 138);
// log.error("length:{}",powerBattery.length());
// log.error("powerBattery:{}",powerBattery);
//当前状态允许的最大反馈功率
String maxFeedbackPower = substring.substring(138, 144);
// log.error("length:{}",maxFeedbackPower.length());
// log.error("maxFeedbackPower:{}",maxFeedbackPower);
//当前状态允许最大放电功率
String maxDischargePower = substring.substring(144, 150);
// log.error("length:{}",maxDischargePower.length());
// log.error("maxDischargePower:{}",maxDischargePower);
//BMS自检计数器
String bmsSelfCheck = substring.substring(150, 152);
// log.error("length:{}",bmsSelfCheck.length());
// log.error("bmsSelfCheck:{}",bmsSelfCheck);
//动力电池充放电电流
String powerBatteryCurrent = substring.substring(152, 157);
// log.error("length:{}",powerBatteryCurrent.length());
// log.error("powerBatteryCurrent:{}",powerBatteryCurrent);
//动力电池负载端总电压V3
String powerBatteryV3 = substring.substring(157, 163);
// log.error("length:{}",powerBatteryV3.length());
// log.error("powerBatteryV3:{}",powerBatteryV3);
//单次最大电压
String maxVoltage = substring.substring(163, 167);
// log.error("length:{}",maxVoltage.length());
// log.error("maxVoltage:{}",maxVoltage);
//单体电池最低电压
String minVoltage = substring.substring(167, 171);
// log.error("length:{}",minVoltage.length());
// log.error("minVoltage:{}",minVoltage);
//单体电池最高温度
String maxTemperature = substring.substring(171, 177);
// log.error("length:{}",maxTemperature.length());
// log.error("maxTemperature:{}",maxTemperature);
//单体电池最低温度
String minTemperature = substring.substring(177, 183);
// log.error("length:{}",minTemperature.length());
// log.error("minTemperature:{}",minTemperature);
//动力电池可用容量
String availableCapacity = substring.substring(183, 189);
// log.error("length:{}",availableCapacity.length());
// log.error("availableCapacity:{}",availableCapacity);
//车辆状态
String vehicleStatus = substring.substring(189, 190);
// log.error("length:{}",vehicleStatus.length());
// log.error("vehicleStatus:{}",vehicleStatus);
//充电状态
String chargeStatus = substring.substring(190, 191);
// log.error("length:{}",chargeStatus.length());
// log.error("chargeStatus:{}",chargeStatus);
//运行状态
String runStatus = substring.substring(191, 192);
// log.error("length:{}",runStatus.length());
// log.error("runStatus:{}",runStatus);
//SOC
String soc = substring.substring(192, 193);
// log.error("length:{}",soc.length());
// log.error("soc:{}",soc);
//可充电储能装置工作状态
String chargeWorkStatus = substring.substring(193, 194);
// log.error("length:{}",chargeWorkStatus.length());
// log.error("chargeWorkStatus:{}",chargeWorkStatus);
//驱动电机状态
String driveMotorStatus = substring.substring(194, 195);
// log.error("length:{}",driveMotorStatus.length());
// log.error("driveMotorStatus:{}",driveMotorStatus);
//定位是否有效
String location = substring.substring(195, 196);
// log.error("length:{}",location.length());
// log.error("location:{}",location);
//EAS
String eas = substring.substring(196, 197);
// log.error("length:{}",eas.length());
// log.error("eas:{}",eas);
//PTC
String ptc = substring.substring(197, 198);
// log.error("length:{}",ptc.length());
// log.error("ptc:{}",ptc);
//EPS
String eps = substring.substring(198, 199);
// log.error("length:{}",eps.length());
// log.error("eps:{}",eps);
//ABS
String abs = substring.substring(199, 200);
// log.error("length:{}",abs.length());
// log.error("abs:{}",abs);
//MCU
String mcu = substring.substring(200, 201);
// log.error("length:{}",mcu.length());
// log.error("mcu:{}",mcu);
//动力电池加热状态
String powerBatteryHeating = substring.substring(201, 202);
// log.error("length:{}",powerBatteryHeating.length());
// log.error("powerBatteryHeating:{}",powerBatteryHeating);
//动力电池当前状态
String powerBatteryCurrentStatus = substring.substring(202, 203);
// log.error("length:{}",powerBatteryCurrentStatus.length());
// log.error("powerBatteryCurrentStatus:{}",powerBatteryCurrentStatus);
//动力电池保温状态
String powerBatteryHeat = substring.substring(203, 204);
// log.error("length:{}",powerBatteryHeat.length());
// log.error("powerBatteryHeat:{}",powerBatteryHeat);
//DCDC
String dcdc = substring.substring(204, 205);
// log.error("length:{}",dcdc.length());
// log.error("dcdc:{}",dcdc);
//CHG
String chg = substring.substring(205, 206);
// log.error("length:{}",chg.length());
// log.error("chg:{}",chg);
MessageData build = MessageData.builder()
.vin(vin)
.timestamp(timestamp)
.longitude(longitude)
.latitude(latitude)
.speed(speed)
.mileage(mileage)
.dischargeVoltage(dischargeVoltage)
.dischargeCurrent(dischargeCurrent)
.insulationResistance(insulationResistance)
.gear(gear)
.accelerationPedal(accelerationPedal)
.brakePedal(brakePedal)
.fuelConsumption(fuelConsumption)
.motorControllerTemperature(motorControllerTemperature)
.motorSpeed(motorSpeed)
.motorTorque(motorTorque)
.motorTemperature(motorTemperature)
.motorVoltage(motorVoltage)
.motorCurrent(motorCurrent)
.powerBattery(powerBattery)
.maxFeedbackPower(maxFeedbackPower)
.maxDischargePower(maxDischargePower)
.bmsSelfCheck(bmsSelfCheck)
.powerBatteryCurrent(powerBatteryCurrent)
.powerBatteryV3(powerBatteryV3)
.maxVoltage(maxVoltage)
.minVoltage(minVoltage)
.maxTemperature(maxTemperature)
.minTemperature(minTemperature)
.availableCapacity(availableCapacity)
.vehicleStatus(vehicleStatus)
.chargeStatus(chargeStatus)
.runStatus(runStatus)
.soc(soc)
.chargeWorkStatus(chargeWorkStatus)
.driveMotorStatus(driveMotorStatus)
.location(location)
.eas(eas)
.ptc(ptc)
.eps(eps)
.abs(abs)
.mcu(mcu)
.powerBatteryHeating(powerBatteryHeating)
.powerBatteryCurrentStatus(powerBatteryCurrentStatus)
.powerBatteryHeat(powerBatteryHeat)
.dcdc(dcdc)
.chg(chg)
.build();
log.error("报文解析:{}",build);
System.out.println(hexStringToString.length());
return build;
}
/**
* 16进制转换成为string类型字符串
* @param s
* @return
*/
public static String hexStringToString(String s) {
if (s == null || s.equals("")) {
return null;
}
s = s.replace(" ", "");
byte[] baKeyword = new byte[s.length() / 2];
for (int i = 0; i < baKeyword.length; i++) {
try {
baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
} catch (Exception e) {
e.printStackTrace();
}
}
try {
s = new String(baKeyword, StandardCharsets.UTF_8);
} catch (Exception e1) {
e1.printStackTrace();
}
return s;
}
}