Merge remote-tracking branch 'refs/remotes/origin/dev.template' into dev
commit
708214b86e
|
@ -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,17 @@
|
|||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-common-kafka</artifactId>
|
||||
<version>3.6.3</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.muyu.server</groupId>
|
||||
<artifactId>saas-server</artifactId>
|
||||
<version>3.6.3</version>
|
||||
</dependency>
|
||||
|
||||
<!-- SpringCloud Alibaba Nacos -->
|
||||
<dependency>
|
||||
|
@ -77,7 +88,6 @@
|
|||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-common-xxl</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
|
|
|
@ -1,19 +1,15 @@
|
|||
package com.muyu.template;
|
||||
|
||||
import com.muyu.common.security.annotation.EnableCustomConfig;
|
||||
import com.muyu.common.security.annotation.EnableMyFeignClients;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* @author liuxinyue
|
||||
* @Package:com.muyu.template
|
||||
* @name:CloudTemplateApplication
|
||||
* @Date:2024/9/30 10:36
|
||||
*/
|
||||
|
||||
@EnableCustomConfig
|
||||
//@EnableCustomSwagger2
|
||||
@EnableMyFeignClients
|
||||
@SpringBootApplication
|
||||
public class CloudTemplateApplication {
|
||||
|
|
|
@ -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,150 @@
|
|||
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.impl.SysCarServiceImpl;
|
||||
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 javax.annotation.Resource;
|
||||
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;
|
||||
|
||||
@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;
|
||||
}
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
package com.muyu.template.service;
|
||||
|
||||
/**
|
||||
* @author liuxinyue
|
||||
* @Package:com.muyu.template.service
|
||||
* @name:TemplateService
|
||||
* @Date:2024/9/30 10:57
|
||||
*/
|
||||
public interface TemplateService {
|
||||
}
|
|
@ -1,19 +0,0 @@
|
|||
package com.muyu.template.service.impl;
|
||||
|
||||
import com.muyu.template.service.TemplateService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* @author liuxinyue
|
||||
* @Package:com.muyu.template.service.impl
|
||||
* @name:TemplateServiceImpl
|
||||
* @Date:2024/9/30 10:57
|
||||
*/
|
||||
@Service
|
||||
public class TemplateServiceImpl implements TemplateService {
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -1,9 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.muyu.server.mapper.CarTypeMapper">
|
||||
|
||||
|
||||
|
||||
</mapper>
|
|
@ -1,67 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<!--
|
||||
1.在mybats的开发中namespace有特殊的意思,一定要是对应接口的全限定名
|
||||
通过namespace可以简历mapper.xml和接口之间的关系(名字不重要,位置不重要)
|
||||
-->
|
||||
<mapper namespace="com.muyu.server.mapper.EnterpriseDao">
|
||||
|
||||
<!--查询企业信息-->
|
||||
<select id="selectEnterprise" resultType="HashMap" parameterType="Map">
|
||||
select *
|
||||
from tb_enterprise
|
||||
<where>
|
||||
<if test="enterpriseName != null">
|
||||
and enterprise_name like concat('%',#{enterpriseName},'%')
|
||||
</if>
|
||||
</where>
|
||||
limit #{start},#{length}
|
||||
</select>
|
||||
|
||||
<!--查询企业记录总数-->
|
||||
<select id="selectEnterpriseCount" resultType="Long">
|
||||
select count(enterprise_id)
|
||||
from tb_enterprise
|
||||
</select>
|
||||
|
||||
<!--新增企业信息-->
|
||||
<insert id="insert" parameterType="com.muyu.server.mapper.EnterpriseDao">
|
||||
insert into tb_enterprise
|
||||
set enterprise_name = #{enterpriseName},
|
||||
enterprise_car_count = #{enterpriseCarCount},
|
||||
enterprise_fence_count = #{enterpriseFenceCount}
|
||||
</insert>
|
||||
|
||||
|
||||
|
||||
<!--根据编号查询企业信息-->
|
||||
<select id="searchById" resultType="java.util.HashMap">
|
||||
select enterprise_id,enterprise_name,enterprise_car_count,enterprise_fence_count
|
||||
from tb_enterprise
|
||||
where enterprise_id = #{enterpriseId}
|
||||
</select>
|
||||
<!--修改企业信息-->
|
||||
<update id="updateEnterprise" parameterType="com.muyu.common.domain.Enterprise">
|
||||
update tb_enterprise
|
||||
set enterprise_name = #{enterpriseName},
|
||||
enterprise_car_count = #{enterpriseCarCount},
|
||||
enterprise_fence_count = #{enterpriseFenceCount}
|
||||
where enterprise_id = #{enterpriseId} and enterprise_id != 0
|
||||
</update>
|
||||
|
||||
|
||||
|
||||
<!--删除企业信息-->
|
||||
<delete id="deleteByIds">
|
||||
delete from tb_enterprise
|
||||
where enterprise_id in
|
||||
<foreach collection="ids" open="(" separator="," close=")" item="one">
|
||||
#{one}
|
||||
</foreach>
|
||||
</delete>
|
||||
|
||||
|
||||
|
||||
</mapper>
|
|
@ -1,83 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.muyu.server.mapper.SysCarMapper">
|
||||
<insert id="addSysCar">
|
||||
INSERT INTO `four`.`sys_car`
|
||||
( `car_vin`, `car_type_id`, `state`, `car_motor_manufacturer`, `car_motor_model`,
|
||||
`car_battery_manufacturer`, `car_battery_model`, `strategy_id`,`group_id`, `create_by`, `create_time`, `update_by`, `update_time`, `remark`,)
|
||||
VALUES (#{carVin}, #{carTypeId}, '1', #{carMotorManufacturer}, #{carMotorModel},
|
||||
#{carBatteryManufacturer}, #{carBatteryModel}, #{strategyId},#{groupId},#{createBy}, #{createTime}, #{updateBy}, #{updateTime}, #{remark})
|
||||
</insert>
|
||||
<update id="updSysCarById">
|
||||
UPDATE `four`.`sys_car`
|
||||
SET `car_vin` = #{carVin},
|
||||
`car_type_id` = #{carTypeId},
|
||||
`state` = #{state},
|
||||
`car_motor_manufacturer` = #{carMotorManufacturer},
|
||||
`car_motor_model` = #{carMotorModel},
|
||||
`car_battery_manufacturer` = #{carBatteryManufacturer},
|
||||
`car_battery_model` = #{carBatteryModel},
|
||||
`strategy_id` = #{strategyId},
|
||||
`group_id`=#{groupId}
|
||||
`create_by` = #{createBy},
|
||||
`create_time` = #{createTime},
|
||||
`update_by` = #{updateBy},
|
||||
`update_time` = #{updateTime},
|
||||
`remark` = #{remark} WHERE `id` = #{id}
|
||||
</update>
|
||||
<select id="selectSysCarVoList" resultType="com.muyu.common.domain.resp.SysCarVo">
|
||||
SELECT * ,car_type.type_name,warn_strategy.strategy_name,electronic_fence_group.group_name
|
||||
FROM `sys_car`
|
||||
LEFT JOIN car_type ON sys_car.car_type_id=car_type.id
|
||||
LEFT JOIN warn_strategy ON sys_car.strategy_id=warn_strategy.id
|
||||
LEFT JOIN electronic_fence_group ON sys_car.group_id=electronic_fence_group.id
|
||||
<where>
|
||||
<if test="carVin!=null and carVin!=''">
|
||||
sys_car.car_vin=#{carVin}
|
||||
</if>
|
||||
<if test="state!=null and state!=''">
|
||||
and sys_car.state=#{state}
|
||||
</if>
|
||||
<if test="carMotorManufacturer!=null and carMotorManufacturer!=''">
|
||||
and sys_car.car_motor_manufacturer=#{carMotorManufacturer}
|
||||
</if>
|
||||
<if test="carBatteryManufacturer!=null and carBatteryManufacturer!=''">
|
||||
and sys_car.car_battery_manufacturer=#{carBatteryManufacturer}
|
||||
</if>
|
||||
<if test="carMotorModel!=null and carMotorModel!=''">
|
||||
and sys_car.car_motor_model=#{carMotorModel}
|
||||
</if>
|
||||
<if test="carBatteryModel!=null and carBatteryModel!=''">
|
||||
and sys_car.car_battery_model=#{carBatteryModel}
|
||||
</if>
|
||||
</where>
|
||||
</select>
|
||||
|
||||
|
||||
<select id="selectSysCarVoById" resultType="com.muyu.common.domain.resp.SysCarVo">
|
||||
SELECT * ,car_type.type_name,warn_strategy.strategy_name,electronic_fence_group.group_name
|
||||
FROM `sys_car`
|
||||
LEFT JOIN car_type ON sys_car.car_type_id=car_type.id
|
||||
LEFT JOIN warn_strategy ON sys_car.strategy_id=warn_strategy.id
|
||||
LEFT JOIN electronic_fence_group ON sys_car.group_id=electronic_fence_group.id
|
||||
where sys_car.id=#{id}
|
||||
</select>
|
||||
<select id="findFenceByCarVin" resultType="com.muyu.common.domain.resp.SysCarFaultLogVo">
|
||||
SELECT
|
||||
sys_car_fault_log.*,
|
||||
sys_car_fault.*
|
||||
FROM
|
||||
sys_car_fault_log
|
||||
LEFT JOIN sys_car_fault ON sys_car_fault_log.sys_car_fault_id = sys_car_fault.id
|
||||
WHERE
|
||||
sys_car_fault_log.vin = #{carVin};
|
||||
</select>
|
||||
<select id="findCarByVin" resultType="com.muyu.common.domain.SysCar">
|
||||
select * from sys_car where car_vin=#{carVin}
|
||||
</select>
|
||||
<select id="selectByCarVin" resultType="com.muyu.common.domain.SysCar">
|
||||
select * from sys_car where car_cin=#{carVin}
|
||||
</select>
|
||||
</mapper>
|
|
@ -1,10 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.muyu.server.mapper.TemplateNeedMapper">
|
||||
|
||||
<select id="selectByTemplateId" resultType="com.muyu.common.domain.MessageTemplateType">
|
||||
SELECT * FROM `message_template_type` WHERE template_id=#{templateId}
|
||||
</select>
|
||||
</mapper>
|
|
@ -1,31 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.muyu.server.mapper.WarnLogsMapper">
|
||||
<insert id="addWarnLog">
|
||||
INSERT INTO warn_logs VALUES
|
||||
<foreach collection="list" item="warnLogs" index="index" separator=",">
|
||||
(#{warnLogs.id})
|
||||
</foreach>
|
||||
</insert>
|
||||
<select id="selectWarnLogsList" resultType="com.muyu.common.domain.resp.WarnLogsResp">
|
||||
SELECT
|
||||
*,
|
||||
warn_rule.rule_name
|
||||
FROM
|
||||
warn_logs
|
||||
LEFT JOIN warn_rule
|
||||
ON warn_logs.warn_rule_id = warn_rule.id
|
||||
</select>
|
||||
<select id="selectWarnLogs" resultType="com.muyu.common.domain.resp.WarnLogsResp">
|
||||
SELECT
|
||||
*,
|
||||
warn_rule.rule_name
|
||||
FROM
|
||||
warn_logs
|
||||
LEFT JOIN warn_rule
|
||||
ON warn_logs.warn_rule_id = warn_rule.id
|
||||
where warn_rule.id=#{id}
|
||||
</select>
|
||||
</mapper>
|
|
@ -1,23 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
|
||||
<mapper namespace="com.muyu.server.mapper.WarnRuleMapper">
|
||||
|
||||
<select id="selectListByStrategyId" resultType="com.muyu.common.domain.resp.WarnRuleResp">
|
||||
SELECT * ,warn_strategy.strategy_name,
|
||||
message_template_type.message_field
|
||||
FROM `warn_rule`
|
||||
LEFT JOIN warn_strategy ON warn_rule.strategy_id=warn_strategy.id
|
||||
LEFT JOIN message_template_type ON warn_rule.msg_type_id=message_template_type.message_template_type_id
|
||||
WHERE warn_rule.strategy_id=#{strategyId}
|
||||
</select>
|
||||
<select id="selectWarnRuleRespList" resultType="com.muyu.common.domain.resp.WarnRuleResp">
|
||||
SELECT * ,warn_strategy.strategy_name,
|
||||
message_template_type.message_field
|
||||
FROM `warn_rule`
|
||||
LEFT JOIN warn_strategy ON warn_rule.strategy_id=warn_strategy.id
|
||||
LEFT JOIN message_template_type ON warn_rule.msg_type_id=message_template_type.message_template_type_id
|
||||
</select>
|
||||
</mapper>
|
|
@ -1,35 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
|
||||
<mapper namespace="com.muyu.server.mapper.WarnStrategyMapper">
|
||||
<select id="selectWarnStrategyList" resultType="com.muyu.common.domain.resp.WarnStrategyResp">
|
||||
SELECT *,
|
||||
car_type.type_name,
|
||||
t_template.template_name
|
||||
FROM `warn_strategy`
|
||||
LEFT JOIN car_type ON warn_strategy.car_type_id=car_type.id
|
||||
LEFT JOIN t_template ON warn_strategy.template_id=t_template.template_id
|
||||
<where>
|
||||
<if test="carTypeId!=null and carTypeId!=''">
|
||||
car_type.id=#{carTypeId}
|
||||
</if>
|
||||
<if test="strategyName!=null and strategyName!=''">
|
||||
and warn_strategy.strategy_name=#{strategyName}
|
||||
</if>
|
||||
<if test="templateId!=null and templateId!=''">
|
||||
and t_template.template_id=#{templateId}
|
||||
</if>
|
||||
</where>
|
||||
</select>
|
||||
<select id="selectListByCarType" resultType="com.muyu.common.domain.resp.WarnStrategyResp">
|
||||
SELECT *,
|
||||
car_type.type_name,
|
||||
t_template.template_name
|
||||
FROM `warn_strategy`
|
||||
LEFT JOIN car_type ON warn_strategy.car_type_id=car_type.id
|
||||
LEFT JOIN t_template ON warn_strategy.template_id=t_template.template_id
|
||||
where warn_strategy.car_type_id=#{carTypeId}
|
||||
</select>
|
||||
</mapper>
|
|
@ -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