fix():修改协议解析方法
parent
c79a39f54e
commit
008efdde25
|
@ -25,126 +25,129 @@ import java.util.List;
|
||||||
@Log4j2
|
@Log4j2
|
||||||
@Component
|
@Component
|
||||||
public class MqttConfigure {
|
public class MqttConfigure {
|
||||||
@Autowired
|
// @Autowired
|
||||||
private RedisService redisService;
|
// private RedisService redisService;
|
||||||
|
//
|
||||||
|
// @Resource
|
||||||
|
// private SysCarService sysCarService;
|
||||||
|
//
|
||||||
|
// @Resource
|
||||||
|
// private SysCarServiceImpl service;
|
||||||
|
//
|
||||||
|
// @Resource
|
||||||
|
// private MessageTemplateTypeService messageTemplateTypeService;
|
||||||
|
//
|
||||||
|
// @Autowired
|
||||||
|
// private RedisTemplate redisTemplate;
|
||||||
|
//
|
||||||
|
// @PostConstruct
|
||||||
|
// public void MQTTMonitoring(){
|
||||||
|
//
|
||||||
|
// String topic = "vehicle";
|
||||||
|
// int qos = 2;
|
||||||
|
// String broker = "tcp://47.101.53.251:1883";
|
||||||
|
// String clientId = "测试mqtt";
|
||||||
|
// try {
|
||||||
|
// MqttClient sampleClient = new MqttClient(broker, clientId);
|
||||||
|
// MqttConnectOptions connOpts = new MqttConnectOptions();
|
||||||
|
// //是否清空session
|
||||||
|
// connOpts.setCleanSession(true);
|
||||||
|
// log.info("Connecting to broker: " + broker);
|
||||||
|
// //连接
|
||||||
|
// sampleClient.connect(connOpts);
|
||||||
|
// sampleClient.subscribe(topic,qos);
|
||||||
|
// sampleClient.setCallback(new MqttCallback() {
|
||||||
|
// //连接丢失(报错)
|
||||||
|
// @Override
|
||||||
|
// public void connectionLost(Throwable throwable) {
|
||||||
|
// log.error("error:"+throwable.getMessage());
|
||||||
|
// }
|
||||||
|
// //消息已经接收到
|
||||||
|
// @Override
|
||||||
|
// public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
|
||||||
|
// // 将MQTT消息转换为字符串
|
||||||
|
// String messageContent = new String(mqttMessage.getPayload());
|
||||||
|
// // 解析JSON字符串
|
||||||
|
// JSONObject jsonObject = new JSONObject(messageContent);
|
||||||
|
// // 从JSON对象中获取"msg"字段的值
|
||||||
|
// String msgValue = jsonObject.getStr("msg");
|
||||||
|
// messageParsing(msgValue);
|
||||||
|
// log.info("接收到的值为:"+msgValue);
|
||||||
|
// }
|
||||||
|
// //交付完成
|
||||||
|
// @Override
|
||||||
|
// public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
|
||||||
|
//
|
||||||
|
// }
|
||||||
|
// });
|
||||||
|
// } catch(MqttException me) {
|
||||||
|
// System.out.println("reason "+me.getReasonCode());
|
||||||
|
// System.out.println("msg "+me.getMessage());
|
||||||
|
// System.out.println("loc "+me.getLocalizedMessage());
|
||||||
|
// System.out.println("cause "+me.getCause());
|
||||||
|
// System.out.println("excep "+me);
|
||||||
|
// me.printStackTrace();
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public JSONObject messageParsing(String templateMessage) {
|
||||||
|
// //给一个JSON对象
|
||||||
|
// JSONObject jsonObject = new JSONObject();
|
||||||
|
// //先截取出VIN码 然后根据VIN码查询这个车属于什么类型
|
||||||
|
// if (templateMessage.length() < 18) {
|
||||||
|
// throw new RuntimeException("The vehicle message is incorrect");
|
||||||
|
// }
|
||||||
|
// //将报文进行切割
|
||||||
|
// String[] hexArray = templateMessage.split(" ");
|
||||||
|
// StringBuilder result = new StringBuilder();
|
||||||
|
// for (String hex : hexArray) {
|
||||||
|
// int decimal = Integer.parseInt(hex, 16);
|
||||||
|
// result.append((char) decimal);
|
||||||
|
// }
|
||||||
|
// //取出VIN码
|
||||||
|
// String carVin = result.substring(0, 18 - 1);
|
||||||
|
// log.info("carVin码为:" + carVin);
|
||||||
|
// //根据VIN码获取车辆信息
|
||||||
|
// SysCar carByVin = service.findCarByVin(carVin);
|
||||||
|
// log.info("车辆信息为:" + carByVin);
|
||||||
|
// //对应车辆所对应的报文模版
|
||||||
|
// Integer templateId = carByVin.getTemplateId();
|
||||||
|
// List<MessageTemplateType> templateTypeList;
|
||||||
|
// //key
|
||||||
|
// String redisKey = "messageTemplateType" + templateId;
|
||||||
|
// //key存在
|
||||||
|
// if (redisTemplate.hasKey(redisKey)) {
|
||||||
|
//
|
||||||
|
// List list = redisTemplate.opsForList().range(redisKey, 0, -1);
|
||||||
|
//
|
||||||
|
// templateTypeList = list.stream().map(o -> JSON.parseObject(o.toString(), MessageTemplateType.class))
|
||||||
|
// .toList();
|
||||||
|
// } else {
|
||||||
|
// List<MessageTemplateType> templateTypeList1 = messageTemplateTypeService.findTemplateById(templateId);
|
||||||
|
// templateTypeList = templateTypeList1;
|
||||||
|
// templateTypeList.forEach(
|
||||||
|
// templateType ->
|
||||||
|
// redisTemplate.opsForList().rightPush(
|
||||||
|
// redisKey, com.alibaba.fastjson.JSON.toJSONString(templateType)
|
||||||
|
// )
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
// //将模版里面有的配置进行循环
|
||||||
|
// for (MessageTemplateType messageTemplateType : templateTypeList) {
|
||||||
|
// //开始位置
|
||||||
|
// Integer startIndex = messageTemplateType.getStartIndex() - 1;
|
||||||
|
// //结束位置
|
||||||
|
// Integer endIndex = messageTemplateType.getEndIndex();
|
||||||
|
// //将每个解析后的字段都存入到JSON对象中
|
||||||
|
// jsonObject.put(messageTemplateType.getMessageField(), result.substring(startIndex, endIndex));
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// System.out.println("哈哈哈红红火火恍恍惚惚");
|
||||||
|
// log.info("解析后的报文是:" + jsonObject);
|
||||||
|
//
|
||||||
|
// return jsonObject;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
|
||||||
@Resource
|
|
||||||
private SysCarService sysCarService;
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private SysCarServiceImpl service;
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private MessageTemplateTypeService messageTemplateTypeService;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private RedisTemplate redisTemplate;
|
|
||||||
|
|
||||||
@PostConstruct
|
|
||||||
public void MQTTMonitoring(){
|
|
||||||
|
|
||||||
String topic = "vehicle";
|
|
||||||
int qos = 2;
|
|
||||||
String broker = "tcp://47.101.53.251:1883";
|
|
||||||
String clientId = "测试mqtt";
|
|
||||||
try {
|
|
||||||
MqttClient sampleClient = new MqttClient(broker, clientId);
|
|
||||||
MqttConnectOptions connOpts = new MqttConnectOptions();
|
|
||||||
//是否清空session
|
|
||||||
connOpts.setCleanSession(true);
|
|
||||||
log.info("Connecting to broker: " + broker);
|
|
||||||
//连接
|
|
||||||
sampleClient.connect(connOpts);
|
|
||||||
sampleClient.subscribe(topic,qos);
|
|
||||||
sampleClient.setCallback(new MqttCallback() {
|
|
||||||
//连接丢失(报错)
|
|
||||||
@Override
|
|
||||||
public void connectionLost(Throwable throwable) {
|
|
||||||
log.error("error:"+throwable.getMessage());
|
|
||||||
}
|
|
||||||
//消息已经接收到
|
|
||||||
@Override
|
|
||||||
public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
|
|
||||||
// 将MQTT消息转换为字符串
|
|
||||||
String messageContent = new String(mqttMessage.getPayload());
|
|
||||||
// 解析JSON字符串
|
|
||||||
JSONObject jsonObject = new JSONObject(messageContent);
|
|
||||||
// 从JSON对象中获取"msg"字段的值
|
|
||||||
String msgValue = jsonObject.getStr("msg");
|
|
||||||
messageParsing(msgValue);
|
|
||||||
log.info("接收到的值为:"+msgValue);
|
|
||||||
}
|
|
||||||
//交付完成
|
|
||||||
@Override
|
|
||||||
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
|
|
||||||
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} catch(MqttException me) {
|
|
||||||
System.out.println("reason "+me.getReasonCode());
|
|
||||||
System.out.println("msg "+me.getMessage());
|
|
||||||
System.out.println("loc "+me.getLocalizedMessage());
|
|
||||||
System.out.println("cause "+me.getCause());
|
|
||||||
System.out.println("excep "+me);
|
|
||||||
me.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public JSONObject messageParsing(String templateMessage) {
|
|
||||||
//给一个JSON对象
|
|
||||||
JSONObject jsonObject = new JSONObject();
|
|
||||||
//先截取出VIN码 然后根据VIN码查询这个车属于什么类型
|
|
||||||
if (templateMessage.length() < 18) {
|
|
||||||
throw new RuntimeException("The vehicle message is incorrect");
|
|
||||||
}
|
|
||||||
//将报文进行切割
|
|
||||||
String[] hexArray = templateMessage.split(" ");
|
|
||||||
StringBuilder result = new StringBuilder();
|
|
||||||
for (String hex : hexArray) {
|
|
||||||
int decimal = Integer.parseInt(hex, 16);
|
|
||||||
result.append((char) decimal);
|
|
||||||
}
|
|
||||||
//取出VIN码
|
|
||||||
String carVin = result.substring(0, 18 - 1);
|
|
||||||
log.info("carVin码为:" + carVin);
|
|
||||||
//根据VIN码获取车辆信息
|
|
||||||
SysCar carByVin = service.findCarByVin(carVin);
|
|
||||||
log.info("车辆信息为:" + carByVin);
|
|
||||||
//对应车辆所对应的报文模版
|
|
||||||
Integer templateId = carByVin.getTemplateId();
|
|
||||||
List<MessageTemplateType> templateTypeList;
|
|
||||||
//key
|
|
||||||
String redisKey = "messageTemplateType" + templateId;
|
|
||||||
//key存在
|
|
||||||
if (redisTemplate.hasKey(redisKey)) {
|
|
||||||
|
|
||||||
List list = redisTemplate.opsForList().range(redisKey, 0, -1);
|
|
||||||
|
|
||||||
templateTypeList = list.stream().map(o -> JSON.parseObject(o.toString(), MessageTemplateType.class))
|
|
||||||
.toList();
|
|
||||||
} else {
|
|
||||||
List<MessageTemplateType> templateTypeList1 = messageTemplateTypeService.findTemplateById(templateId);
|
|
||||||
templateTypeList = templateTypeList1;
|
|
||||||
templateTypeList.forEach(
|
|
||||||
templateType ->
|
|
||||||
redisTemplate.opsForList().rightPush(
|
|
||||||
redisKey, com.alibaba.fastjson.JSON.toJSONString(templateType)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
//将模版里面有的配置进行循环
|
|
||||||
for (MessageTemplateType messageTemplateType : templateTypeList) {
|
|
||||||
//开始位置
|
|
||||||
Integer startIndex = messageTemplateType.getStartIndex() - 1;
|
|
||||||
//结束位置
|
|
||||||
Integer endIndex = messageTemplateType.getEndIndex();
|
|
||||||
//将每个解析后的字段都存入到JSON对象中
|
|
||||||
jsonObject.put(messageTemplateType.getMessageField(), result.substring(startIndex, endIndex));
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.println("哈哈哈红红火火恍恍惚惚");
|
|
||||||
log.info("解析后的报文是:" + jsonObject);
|
|
||||||
|
|
||||||
return jsonObject;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package com.muyu.server.controller;
|
||||||
|
|
||||||
import com.muyu.common.core.domain.Result;
|
import com.muyu.common.core.domain.Result;
|
||||||
import com.muyu.common.domain.CarType;
|
import com.muyu.common.domain.CarType;
|
||||||
|
import com.muyu.common.redis.service.RedisService;
|
||||||
import com.muyu.server.service.CarTypeService;
|
import com.muyu.server.service.CarTypeService;
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
|
@ -10,6 +11,8 @@ import lombok.extern.log4j.Log4j2;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 车辆类型管理
|
* 车辆类型管理
|
||||||
* @author liuxingue
|
* @author liuxingue
|
||||||
|
@ -29,6 +32,9 @@ public class CarTypeController {
|
||||||
@Autowired
|
@Autowired
|
||||||
private CarTypeService carTypeService;
|
private CarTypeService carTypeService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RedisService redisService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 车辆类型列表
|
* 车辆类型列表
|
||||||
* @return
|
* @return
|
||||||
|
@ -36,6 +42,8 @@ public class CarTypeController {
|
||||||
@GetMapping("/selectCarTypeList")
|
@GetMapping("/selectCarTypeList")
|
||||||
@Operation(summary = "车辆类型列表",description = "车辆类型列表")
|
@Operation(summary = "车辆类型列表",description = "车辆类型列表")
|
||||||
public Result selectCarTypeList(){
|
public Result selectCarTypeList(){
|
||||||
|
List<Object> car = redisService.getCacheList("car");
|
||||||
|
log.info("取出来的集合是:"+car);
|
||||||
return Result.success(carTypeService.selectCarTypeList());
|
return Result.success(carTypeService.selectCarTypeList());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue