fix():修改协议解析方法
parent
e8bc92c658
commit
a13ba8199a
|
@ -23,6 +23,7 @@
|
|||
<module>cloud-common-saas</module>
|
||||
<module>cloud-common-swagger</module>
|
||||
<module>cloud-common-cache</module>
|
||||
<module>cloud-common-kafka</module>
|
||||
</modules>
|
||||
|
||||
<artifactId>cloud-common</artifactId>
|
||||
|
|
|
@ -17,6 +17,10 @@
|
|||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-common-saas</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- SpringCloud Alibaba Nacos -->
|
||||
<dependency>
|
||||
|
@ -77,6 +81,12 @@
|
|||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-common-xxl</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.muyu.server</groupId>
|
||||
<artifactId>saas-server</artifactId>
|
||||
<version>3.6.3</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package com.muyu.server.config;
|
||||
package com.muyu.template.config;
|
||||
|
||||
import org.apache.kafka.clients.consumer.KafkaConsumer;
|
||||
import org.apache.kafka.common.serialization.Deserializer;
|
|
@ -1,4 +1,4 @@
|
|||
package com.muyu.server.config;
|
||||
package com.muyu.template.config;
|
||||
|
||||
import org.apache.kafka.clients.producer.KafkaProducer;
|
||||
import org.apache.kafka.common.serialization.Serializer;
|
|
@ -0,0 +1,156 @@
|
|||
package com.muyu.template.config;
|
||||
|
||||
import cn.hutool.json.JSONObject;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.muyu.common.domain.MessageTemplateType;
|
||||
import com.muyu.common.domain.SysCar;
|
||||
import com.muyu.common.redis.service.RedisService;
|
||||
import com.muyu.server.service.MessageTemplateTypeService;
|
||||
import com.muyu.server.service.SysCarService;
|
||||
import com.muyu.server.service.TemplateService;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.eclipse.paho.client.mqttv3.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author liuxinyue
|
||||
* @Package:com.muyu.mqtt.configure
|
||||
* @Project:cloud-server
|
||||
* @name:MqttConfigure
|
||||
* @Date:2024/9/28 16:10
|
||||
*/
|
||||
@Log4j2
|
||||
@Component
|
||||
public class MqttConfigure {
|
||||
@Autowired
|
||||
private RedisService redisService;
|
||||
|
||||
@Autowired
|
||||
private SysCarService sysCarService;
|
||||
|
||||
@Autowired
|
||||
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 = sysCarService.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;
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -1,5 +1,8 @@
|
|||
package com.muyu.template.service;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
/**
|
||||
* @author liuxinyue
|
||||
* @Package:com.muyu.template.service
|
||||
|
@ -7,4 +10,7 @@ package com.muyu.template.service;
|
|||
* @Date:2024/9/30 10:57
|
||||
*/
|
||||
public interface TemplateService {
|
||||
|
||||
void messageParsing(String templateMessage) ;
|
||||
|
||||
}
|
||||
|
|
|
@ -1,19 +1,103 @@
|
|||
package com.muyu.template.service.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.muyu.common.domain.MessageTemplateType;
|
||||
import com.muyu.common.domain.SysCar;
|
||||
import com.muyu.common.redis.service.RedisService;
|
||||
import com.muyu.server.service.MessageTemplateTypeService;
|
||||
import com.muyu.server.service.SysCarService;
|
||||
import com.muyu.template.service.TemplateService;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
/**
|
||||
* @author liuxinyue
|
||||
* @Package:com.muyu.template.service.impl
|
||||
* @name:TemplateServiceImpl
|
||||
* @Date:2024/9/30 10:57
|
||||
*/
|
||||
@Log4j2
|
||||
@Service
|
||||
public class TemplateServiceImpl implements TemplateService {
|
||||
|
||||
@Autowired
|
||||
private RedisService redisService;
|
||||
|
||||
@Autowired
|
||||
private SysCarService sysCarService;
|
||||
|
||||
@Autowired
|
||||
private MessageTemplateTypeService messageTemplateTypeService;
|
||||
|
||||
@Autowired
|
||||
private RedisTemplate redisTemplate;
|
||||
|
||||
@Override
|
||||
public void 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 = sysCarService.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);
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,75 +0,0 @@
|
|||
package com.muyu.server.config;
|
||||
|
||||
import cn.hutool.json.JSONObject;
|
||||
import com.muyu.server.service.TemplateService;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.eclipse.paho.client.mqttv3.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author liuxinyue
|
||||
* @Package:com.muyu.mqtt.configure
|
||||
* @Project:cloud-server
|
||||
* @name:MqttConfigure
|
||||
* @Date:2024/9/28 16:10
|
||||
*/
|
||||
@Log4j2
|
||||
@Component
|
||||
public class MqttConfigure {
|
||||
|
||||
@Autowired
|
||||
private TemplateService templateService;
|
||||
|
||||
@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");
|
||||
templateService.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();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -45,17 +45,7 @@ public class TemplateController {
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* 解析报文
|
||||
* @param templateMessage
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/messageParsing")
|
||||
@Operation(summary = "报文解析",description = "报文解析")
|
||||
public Result messageParsing(@RequestParam("templateMessage") String templateMessage) throws SQLException, IoTDBConnectionException, ClassNotFoundException, StatementExecutionException, ExecutionException, InterruptedException {
|
||||
templateService.messageParsing(templateMessage);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 报文模版添加
|
||||
|
|
|
@ -19,7 +19,7 @@ import java.util.concurrent.ExecutionException;
|
|||
public interface TemplateService extends IService<Template> {
|
||||
|
||||
|
||||
void messageParsing(String templateMessage) throws SQLException, IoTDBConnectionException, ClassNotFoundException, StatementExecutionException, ExecutionException, InterruptedException;
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -35,79 +35,7 @@ public class TemplateServiceImpl extends ServiceImpl<TemplateMapper, Template> i
|
|||
@Autowired
|
||||
private static TemplateMapper templateMapper;
|
||||
|
||||
@Autowired
|
||||
private SysCarService sysCarService;
|
||||
|
||||
@Autowired
|
||||
private MessageTemplateTypeService messageTemplateTypeService;
|
||||
|
||||
@Autowired
|
||||
private RedisTemplate redisTemplate;
|
||||
|
||||
|
||||
@Override
|
||||
public void messageParsing(String templateMessage) throws SQLException, IoTDBConnectionException, ClassNotFoundException, StatementExecutionException, ExecutionException, InterruptedException {
|
||||
|
||||
//给一个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 = sysCarService.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);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue