cloud-car/cloud-modules/cloud-modules-template/src/main/java/com/muyu/template/test.java

204 lines
8.4 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.muyu.template;
import cn.hutool.json.JSONObject;
import com.alibaba.fastjson2.JSON;
import com.muyu.common.domain.*;
import com.muyu.common.redis.service.RedisService;
import lombok.extern.log4j.Log4j2;
import org.springframework.data.redis.core.RedisTemplate;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
* @author liuxinyue
* @Packagecom.muyu.template
* @nametest
* @Date2024/10/4 9:42
*/
@Log4j2
public class test {
private static int DURATION_SECONDS = 60;
private static List<JSONObject> receivedStrings = new ArrayList<>();
private static int elapsedSeconds = 0;
private static ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
private static String file="elapsed";
@Resource
private RedisTemplate redisTemplate;
@Resource
private RedisService redisService;
public void main(String[] args) {
String message="7E 36 33 59 43 5a 44 59 36 33 33 36 43 38 48 34 43 41 31 37 32 37 36 36 38 35 39 37 37 38 39 31 32 31 2e 34 37 34 30 30 30 30 33 31 2e 32 33 30 30 30 31 30 31 38 2e 30 30 30 31 38 38 31 2e 37 39 30 30 30 30 35 36 31 30 30 30 33 31 30 30 30 36 38 39 39 30 30 30 30 30 50 31 30 39 30 39 2e 35 30 30 32 34 30 30 30 30 31 33 32 36 35 37 33 39 30 36 37 30 30 30 30 32 31 31 30 30 31 33 32 35 30 30 30 30 36 37 30 30 30 2e 32 32 30 30 30 30 37 35 30 30 30 30 31 33 31 30 30 30 30 35 37 30 30 30 30 33 30 30 30 34 30 30 30 37 39 30 30 30 30 32 30 30 30 30 30 31 37 30 30 30 30 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 55 7E";
//车类型
Long carTypeId=null;
//查找车对应的类型
List<SysCar> carList = redisService.getCacheList("car");
for (SysCar sysCar : carList) {
if(sysCar.getCarVin().equals("1HGCM82633A123456")){
//获取到车的类型ID
carTypeId = sysCar.getCarTypeId();
}
}
//查找车类型对应的策略
List<WarnStrategy> warnStrategyList = null;
//该车绑定的报文模版
Long templateId=null;
//获取到车的类型之后 查找对应的策略
List<WarnStrategy> warnStrategy = redisService.getCacheList("warnStrategy");
for (WarnStrategy strategy : warnStrategy) {
if(strategy.getCarTypeId().equals(carTypeId)){
templateId=strategy.getTemplateId();
warnStrategyList.add(strategy);
}
}
//报文模版 根据templateId取出对应的模版
List<MessageTemplateType> templateTypeList1=null;
List<MessageTemplateType> templateTypeList = redisService.getCacheList("templateTypeList");
for (MessageTemplateType messageTemplateType : templateTypeList) {
if(messageTemplateType.getTemplateId().equals(templateId)){
templateTypeList1.add(messageTemplateType);
}
}
//获取策略对应的规则列表
List<WarnRule> warnRule = redisService.getCacheList("warnRule");
//车辆对应的规则
List<WarnRule> warnRuleList = null;
for (WarnRule rule : warnRule) {
for (WarnStrategy strategy : warnStrategyList) {
if(rule.getStrategyId()==strategy.getId()){
warnRuleList.add(rule);
}
}
}
String templateIds=null;
for (WarnRule rule : warnRuleList) {
//滑窗时间
Long slideTime = rule.getSlideTime();
if(slideTime!=null){
DURATION_SECONDS= Math.toIntExact(slideTime);
}
templateIds+=","+rule.getMsgTypeId();
}
String[] templateIdSplit = templateIds.split(",");
//最终获取到的报文模版 使用他进行比较
List<MessageTemplateType> messageTemplateTypes=null;
for (String s : templateIdSplit) {
for (MessageTemplateType messageTemplateType : templateTypeList1) {
if(s.equals(messageTemplateType.getTemplateId())){
messageTemplateTypes.add(messageTemplateType);
}
}
}
JSONObject entries = messageParsing(message);
//将解析后的数据添加到List<JSONObject> receivedStrings中
receivedStrings.add(entries);
cleanUpOldStrings();
checkForSpeeding();
}
// 清理超过指定秒数内的数据
private static void cleanUpOldStrings() {
long currentTime = System.currentTimeMillis();
receivedStrings.removeIf(jsonObject ->
currentTime - jsonObject.getLong("time") > TimeUnit.SECONDS.toMillis(DURATION_SECONDS)
);
}
// 检查是否有超速情况
private static void checkForSpeeding() {
if (receivedStrings.size() < 2) return; // 如果数据不足,直接返回
for (int i = 0; i < receivedStrings.size() - 1; i++) {
JSONObject current = receivedStrings.get(i);
JSONObject next = receivedStrings.get(i + 1);
Short currentElapsed = current.getShort(file);
Short nextElapsed = next.getShort(file);
// 检查条件如果相差大于12则记录错误
if (nextElapsed > currentElapsed + 12) {
System.out.println("出错啦,出错啦,车子超速啦!!!");
}
}
}
//解析报文的方法
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 = null;
List<SysCar> carList = redisService.getCacheList("carList");
for (SysCar sysCar : carList) {
if(sysCar.getCarVin().equals(carVin)){
carByVin=sysCar;
}
}
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=null;
List<MessageTemplateType> templateTypeList2 = redisService.getCacheList("templateTypeList");
for (MessageTemplateType messageTemplateType : templateTypeList2) {
if(messageTemplateType.getTemplateId()==templateId){
templateTypeList1.add(messageTemplateType);
}
}
templateTypeList = templateTypeList1;
templateTypeList.forEach(
templateType ->
redisTemplate.opsForList().rightPush(
redisKey, 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));
}
log.info("解析后的报文是:" + jsonObject);
return jsonObject;
}
}