Merge branch 'dev.server' into dev
# Conflicts: # cloud-modules/cloud-modules-enterprise/cloud-modules-enterprise-server/src/main/java/com/muyu/enterprise/controller/VehicleController.java # cloud-modules/cloud-modules-protocol-parsing/src/main/java/com/muyu/cloud/protocol/parsing/test/ParsingTest.javadev.redis
commit
afdcc48a42
|
@ -6,10 +6,12 @@ import com.muyu.domain.MessageValue;
|
|||
import com.muyu.domain.req.MessageValueReq;
|
||||
import com.muyu.domain.resp.MessageValueListResp;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 报文模版缓存
|
||||
*/
|
||||
public class AllMessageValueCacheService extends CacheAbsBacis<String, MessageValueListResp> {
|
||||
public class AllMessageValueCacheService extends CacheAbsBacis<String, List<MessageValueListResp>> {
|
||||
|
||||
|
||||
@Override
|
||||
|
|
|
@ -48,9 +48,9 @@ public class MessageValueController {
|
|||
@Operation(summary = "报文数据列表", description = "根据报文类别, 报文模版筛选报文数据")
|
||||
public Result<List<MessageValueListResp>> findAll(@RequestBody MessageValueReq messageValueReq) {
|
||||
List<MessageValueListResp> list = messageValueService.findAll(messageValueReq);
|
||||
for (MessageValueListResp messageValueListResp : list) {
|
||||
allMessageValueCacheService.put(String.valueOf(messageValueListResp.getMessageTemplateId()), messageValueListResp);
|
||||
}
|
||||
// for (MessageValueListResp messageValueListResp : list) {
|
||||
// allMessageValueCacheService.put(String.valueOf(messageValueListResp.getMessageTemplateId()), (List<MessageValueListResp>) messageValueListResp);
|
||||
// }
|
||||
return Result.success(list);
|
||||
}
|
||||
|
||||
|
@ -102,6 +102,7 @@ public class MessageValueController {
|
|||
@Operation(summary = "根据报文模版id查询报文数据", description = "根据报文模版id查询报文数据")
|
||||
public Result<List<MessageValueListResp>> findByTemplateId(@PathVariable("templateId") Long templateId) {
|
||||
List<MessageValueListResp> list = messageValueService.findByTemplateId(templateId);
|
||||
allMessageValueCacheService.put(String.valueOf(templateId), list);
|
||||
return Result.success(list);
|
||||
}
|
||||
|
||||
|
|
|
@ -48,6 +48,13 @@ public class VehicleController extends BaseController {
|
|||
//添加车辆缓存
|
||||
@Autowired
|
||||
private VehicleCacheService vehicleCacheService;
|
||||
//车辆信息
|
||||
@Autowired
|
||||
private AllVehicleCacheService allVehicleCacheService;
|
||||
|
||||
@Autowired
|
||||
private RedisTemplate redisTemplate;
|
||||
|
||||
|
||||
/**
|
||||
* 查询车辆管理列表
|
||||
|
@ -59,11 +66,10 @@ public class VehicleController extends BaseController {
|
|||
public Result<TableDataInfo<VehicleManageResp>> getVehicleList(@RequestBody VehicleManageReq vehicleManageReq) {
|
||||
startPage();
|
||||
List<VehicleManageResp> list = vehicleService.getVehicleList(vehicleManageReq);
|
||||
//将车辆信息存到Redis
|
||||
List<Vehicle> vehicleList = vehicleService.list();
|
||||
vehicleList.forEach(vehicle -> {
|
||||
vehicleCacheService.put(vehicle.getVehicleVin(), vehicle);
|
||||
});
|
||||
// 将车辆信息存到Redis
|
||||
for (VehicleManageResp resp : list) {
|
||||
allVehicleCacheService.put(resp.getVehicleVin(), resp);
|
||||
}
|
||||
return getDataTable(list);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package com.muyu.enterprise.mapper;
|
||||
|
||||
import com.github.yulichang.base.MPJBaseMapper;
|
||||
import com.muyu.domain.FenceGroup;
|
||||
import com.muyu.domain.Vehicle;
|
||||
import com.muyu.domain.req.BoundFenceGroupReq;
|
||||
import com.muyu.domain.req.VehicleManageReq;
|
||||
|
@ -49,4 +50,8 @@ public interface VehicleMapper extends MPJBaseMapper<Vehicle> {
|
|||
})
|
||||
void boundFenceGroup(BoundFenceGroupReq boundFenceGroupReq);
|
||||
|
||||
@Select("SELECT * FROM bound_middle bm LEFT JOIN vehicle v ON bm.vehicle_id=v.vehicle_id WHERE bm.fence_group_id = #{fenceGroupId}")
|
||||
List<FenceGroup> showBoundFenceGroup(@Param("fenceGroupId") Long fenceGroupId);
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -30,11 +30,6 @@ public interface ElectService extends IService<Fence> {
|
|||
*/
|
||||
List<Fence> mapShow(@Param("fenceId") Long fenceId);
|
||||
|
||||
// /**
|
||||
// * 根据id查询车辆
|
||||
// */
|
||||
// Fence boundFence(@Param("fenceId") Long fenceId);
|
||||
|
||||
/**
|
||||
* 查询电子围栏(终版)
|
||||
*/
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package com.muyu.enterprise.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.muyu.domain.FenceGroup;
|
||||
import com.muyu.domain.Vehicle;
|
||||
import com.muyu.domain.req.BoundFenceGroupReq;
|
||||
import com.muyu.domain.req.VehicleManageReq;
|
||||
|
@ -39,4 +40,11 @@ public interface VehicleService extends IService<Vehicle> {
|
|||
*/
|
||||
void boundFenceGroup(BoundFenceGroupReq boundFenceGroupReq);
|
||||
|
||||
/**
|
||||
* 查询绑定围栏组信息
|
||||
* @param fenceGroupId
|
||||
* @return
|
||||
*/
|
||||
List<FenceGroup> showBoundFenceGroup(@Param("fenceGroupId") Long fenceGroupId);
|
||||
|
||||
}
|
||||
|
|
|
@ -75,45 +75,4 @@ public class MessageValueServiceImpl
|
|||
// allMessageValueCacheService.put(String.valueOf(templateId), collect);
|
||||
return collect;
|
||||
}
|
||||
|
||||
|
||||
// @Override
|
||||
// public JSONObject analysis(String testStr) {
|
||||
//
|
||||
// if (testStr.length() < 18){
|
||||
// throw new RuntimeException("报文格式不正确");
|
||||
// }
|
||||
// //根据空格切割数据
|
||||
// String[] hexArray = testStr.split(" ");
|
||||
// StringBuilder result = new StringBuilder();
|
||||
// for (String hex : hexArray) {
|
||||
// int decimal = Integer.parseInt(hex, 16);
|
||||
// result.append((char) decimal);
|
||||
// }
|
||||
// log.info(result);
|
||||
// //取出车辆VIN码
|
||||
// String vehicleVin = result.substring(1, 18);
|
||||
// log.info("车辆VIN:" + vehicleVin);
|
||||
// //根据车辆VIN码, 查找到报文模版id
|
||||
// Long templateId = messageValueMapper.getTemplateId(vehicleVin);
|
||||
// log.info("模版id:" + templateId);
|
||||
// //获取到报文模版的列表
|
||||
// List<MessageValueListResp> templateList = messageValueMapper.getTemplateList(templateId);
|
||||
// //存储报文模板解析后数据
|
||||
// JSONObject jsonObject = new JSONObject();
|
||||
// for (MessageValueListResp messageValue : templateList) {
|
||||
// //起始位下标
|
||||
// Integer startIndex = messageValue.getMessageStartIndex() - 1;
|
||||
// //截止位下标
|
||||
// Integer endIndex = messageValue.getMessageEndIndex();
|
||||
// //根据报文模版截取数据
|
||||
// String value = result.substring(startIndex, endIndex);
|
||||
// //存入数据
|
||||
// jsonObject.put(messageValue.getMessageLabel(), value);
|
||||
// }
|
||||
// for (Map.Entry<String, Object> stringObjectEntry : jsonObject) {
|
||||
// log.info(stringObjectEntry.getKey() + ":" + stringObjectEntry.getValue());
|
||||
// }
|
||||
// return jsonObject;
|
||||
// }
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ package com.muyu.enterprise.service.impl;
|
|||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.github.yulichang.wrapper.MPJLambdaWrapper;
|
||||
import com.muyu.common.core.utils.StringUtils;
|
||||
import com.muyu.domain.FenceGroup;
|
||||
import com.muyu.domain.Vehicle;
|
||||
import com.muyu.domain.VehicleType;
|
||||
import com.muyu.domain.req.BoundFenceGroupReq;
|
||||
|
@ -14,6 +15,7 @@ import com.muyu.enterprise.service.VehicleService;
|
|||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
|
@ -76,5 +78,11 @@ public class VehicleServiceImpl
|
|||
vehicleMapper.boundFenceGroup(boundFenceGroupReq);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<FenceGroup> showBoundFenceGroup(Long fenceGroupId) {
|
||||
List<FenceGroup> fenceGroups = vehicleMapper.showBoundFenceGroup(fenceGroupId);
|
||||
return fenceGroups;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,155 @@
|
|||
package com.muyu.cloud.protocol.parsing.test;
|
||||
|
||||
import cn.hutool.json.JSONObject;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.muyu.cloud.protocol.parsing.feign.RemoteServiceClient;
|
||||
import com.muyu.common.core.domain.Result;
|
||||
import com.muyu.domain.resp.MessageValueListResp;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.apache.kafka.clients.producer.KafkaProducer;
|
||||
import org.apache.kafka.clients.producer.ProducerRecord;
|
||||
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: LiDongJia
|
||||
* @Package: com.muyu.cloud.protocol.parsing.service.impl
|
||||
* @Project: 2112-car-cloud-server
|
||||
* @name: ParsingServiceImpl
|
||||
* @Date: 2024/9/28 14:31
|
||||
* @Description: 协议解析实现层
|
||||
*/
|
||||
@Log4j2
|
||||
@Component
|
||||
public class ParsingTest {
|
||||
|
||||
@Resource
|
||||
private RedisTemplate<String, Object> redisTemplate;
|
||||
|
||||
@Autowired
|
||||
private RemoteServiceClient remoteServiceClient;
|
||||
|
||||
@Resource
|
||||
private KafkaProducer<String, String> kafkaProducer;
|
||||
|
||||
/**
|
||||
* 协议解析
|
||||
*/
|
||||
@PostConstruct
|
||||
public void mqttClient() {
|
||||
String topic = "vehicle";
|
||||
String broker = "tcp://111.231.50.146:1883";
|
||||
String clientId = "JavaSample";
|
||||
|
||||
try {
|
||||
// 第三个参数为空,默认持久化策略
|
||||
MqttClient sampleClient = new MqttClient(broker, clientId);
|
||||
MqttConnectOptions connOpts = new MqttConnectOptions();
|
||||
connOpts.setCleanSession(true);
|
||||
System.out.println("Connecting to broker: " + broker);
|
||||
sampleClient.connect(connOpts);
|
||||
sampleClient.subscribe(topic, 0);
|
||||
sampleClient.setCallback(new MqttCallback() {
|
||||
// 连接丢失
|
||||
@Override
|
||||
public void connectionLost(Throwable throwable) {
|
||||
|
||||
}
|
||||
|
||||
// 连接成功
|
||||
@Override
|
||||
public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
|
||||
System.out.println(new String(mqttMessage.getPayload()));
|
||||
JSONObject entries = this.protocolParsing(new String(mqttMessage.getPayload()));
|
||||
|
||||
ProducerRecord<String, String> producerRecord = new ProducerRecord<>("zeshi",
|
||||
entries.toString() );
|
||||
kafkaProducer.send(producerRecord);
|
||||
System.out.println(entries);
|
||||
}
|
||||
|
||||
/**
|
||||
* 协议解析
|
||||
* @param messageStr
|
||||
* @return
|
||||
*/
|
||||
public JSONObject protocolParsing(String messageStr) {
|
||||
//根据空格切割数据
|
||||
String[] hexArray = messageStr.split(" ");
|
||||
StringBuilder result = new StringBuilder();
|
||||
//遍历十六进制数据转换为字符
|
||||
for (String hex : hexArray) {
|
||||
int decimal = Integer.parseInt(hex, 16);
|
||||
result.append((char) decimal);
|
||||
}
|
||||
//取出车辆VIN码
|
||||
String vehicleVin = result.substring(1, 18);
|
||||
log.info("车辆VIN码: " + vehicleVin);
|
||||
//根据车辆VIN码查询报文模板ID
|
||||
Result<Long> byVehicleVin = remoteServiceClient.findByVehicleVin(vehicleVin);
|
||||
Long templateId = byVehicleVin.getData();
|
||||
List<MessageValueListResp> templateList;
|
||||
//从redis缓存中获取报文模板数据
|
||||
try {
|
||||
String redisKey = "messageTemplate" + templateId;
|
||||
if (redisTemplate.hasKey(redisKey)) {
|
||||
List<Object> list = redisTemplate.opsForList().range(redisKey, 0, -1);
|
||||
templateList = list.stream()
|
||||
.map(obj -> JSON.parseObject(obj.toString(), MessageValueListResp.class))
|
||||
.toList();
|
||||
log.info("Redis缓存查询成功");
|
||||
} else {
|
||||
Result<List<MessageValueListResp>> byTemplateId = remoteServiceClient.findByTemplateId(templateId);
|
||||
templateList = byTemplateId.getData();
|
||||
templateList.forEach(
|
||||
listResp ->
|
||||
redisTemplate.opsForList().rightPush(
|
||||
redisKey, JSON.toJSONString(listResp)
|
||||
)
|
||||
);
|
||||
log.info("数据库查询成功");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("获取报文模板失败");
|
||||
}
|
||||
//判断报文模板列表不为空
|
||||
if (templateList.isEmpty()) {
|
||||
throw new RuntimeException("报文模版为空");
|
||||
}
|
||||
//存储报文模版解析后的数据
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
for (MessageValueListResp messageValue : templateList) {
|
||||
//起始位下标
|
||||
Integer startIndex = messageValue.getMessageStartIndex() - 1;
|
||||
//结束位下标
|
||||
Integer endIndex = messageValue.getMessageEndIndex();
|
||||
//根据报文模版截取数据
|
||||
String value = result.substring(startIndex, endIndex);
|
||||
//存入数据
|
||||
jsonObject.put(messageValue.getMessageLabel(), value);
|
||||
}
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
// 接收信息
|
||||
@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();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue