fix():修改协议解析方法

dev.operation
Number7 2024-10-02 10:03:36 +08:00
parent a13ba8199a
commit c79a39f54e
12 changed files with 17 additions and 404 deletions

View File

@ -19,7 +19,14 @@
<dependency>
<groupId>com.muyu</groupId>
<artifactId>cloud-common-saas</artifactId>
<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 -->
@ -81,13 +88,6 @@
<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>
<properties>

View File

@ -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
* @Packagecom.muyu.template
* @nameCloudTemplateApplication
* @Date2024/9/30 10:36
*/
@EnableCustomConfig
//@EnableCustomSwagger2
@EnableMyFeignClients
@SpringBootApplication
public class CloudTemplateApplication {

View File

@ -1,5 +1,4 @@
package com.muyu.template.config;
import cn.hutool.json.JSONObject;
import com.alibaba.fastjson2.JSON;
import com.muyu.common.domain.MessageTemplateType;
@ -7,18 +6,16 @@ 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 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
* @Packagecom.muyu.mqtt.configure
* @Projectcloud-server
@ -31,18 +28,21 @@ public class MqttConfigure {
@Autowired
private RedisService redisService;
@Autowired
@Resource
private SysCarService sysCarService;
@Autowired
@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";
@ -108,16 +108,13 @@ public class MqttConfigure {
String carVin = result.substring(0, 18 - 1);
log.info("carVin码为:" + carVin);
//根据VIN码获取车辆信息
SysCar carByVin = sysCarService.findCarByVin(carVin);
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)) {
@ -125,7 +122,6 @@ public class MqttConfigure {
templateTypeList = list.stream().map(o -> JSON.parseObject(o.toString(), MessageTemplateType.class))
.toList();
} else {
List<MessageTemplateType> templateTypeList1 = messageTemplateTypeService.findTemplateById(templateId);
templateTypeList = templateTypeList1;
@ -150,7 +146,5 @@ public class MqttConfigure {
log.info("解析后的报文是:" + jsonObject);
return jsonObject;
}
}

View File

@ -1,16 +0,0 @@
package com.muyu.template.service;
import java.sql.SQLException;
import java.util.concurrent.ExecutionException;
/**
* @author liuxinyue
* @Packagecom.muyu.template.service
* @nameTemplateService
* @Date2024/9/30 10:57
*/
public interface TemplateService {
void messageParsing(String templateMessage) ;
}

View File

@ -1,103 +0,0 @@
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
* @Packagecom.muyu.template.service.impl
* @nameTemplateServiceImpl
* @Date2024/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);
}
}

View File

@ -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>

View File

@ -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>

View File

@ -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>

View File

@ -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>

View File

@ -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>

View File

@ -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>

View File

@ -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>