diff --git a/cloud-modules/cloud-modules-template/src/main/java/com/muyu/template/config/MqttConfigure.java b/cloud-modules/cloud-modules-template/src/main/java/com/muyu/template/config/MqttConfigure.java index 48bb64c..9802526 100644 --- a/cloud-modules/cloud-modules-template/src/main/java/com/muyu/template/config/MqttConfigure.java +++ b/cloud-modules/cloud-modules-template/src/main/java/com/muyu/template/config/MqttConfigure.java @@ -30,9 +30,7 @@ import java.util.Optional; import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.Stream; - /** - * * @author liuxinyue * @Package:com.muyu.mqtt.configure * @Project:cloud-server @@ -60,7 +58,6 @@ public class MqttConfigure { @PostConstruct public void MQTTMonitoring(){ - String topic = "car"; int qos = 2; String broker = "tcp://47.101.53.251:1883"; @@ -115,7 +112,8 @@ public class MqttConfigure { if (templateMessage.length() < 18) { throw new RuntimeException("The vehicle message is incorrect"); } - //将报文进行切割 + //将 templateMessage 按空格切割成多个字符串,得到一个字符串数组 hexArray。接着, + // 将每个十六进制字符串转换成十进制整数,然后再转换为字符,最后将所有字符拼接成一个完整的字符串 result。 String[] hexArray = templateMessage.split(" "); StringBuilder result = new StringBuilder(); for (String hex : hexArray) { diff --git a/cloud-modules/cloud-modules-template/src/main/java/com/muyu/template/test.java b/cloud-modules/cloud-modules-template/src/main/java/com/muyu/template/test.java index e7d8821..958969a 100644 --- a/cloud-modules/cloud-modules-template/src/main/java/com/muyu/template/test.java +++ b/cloud-modules/cloud-modules-template/src/main/java/com/muyu/template/test.java @@ -1,11 +1,6 @@ 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 com.alibaba.fastjson.JSONObject; 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; @@ -14,100 +9,46 @@ import java.util.concurrent.TimeUnit; /** * @author liuxinyue * @Package:com.muyu.template - * @name:test - * @Date:2024/10/4 9:42 + * @name:test2 + * @Date:2024/10/6 10:34 */ @Log4j2 public class test { - private static int DURATION_SECONDS = 60; + private static int DURATION_SECONDS = 5; private static List receivedStrings = new ArrayList<>(); - private static int elapsedSeconds = 0; private static ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); + private static int elapsedSeconds = 0; 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 carList = redisService.getCacheList("car"); - for (SysCar sysCar : carList) { - if(sysCar.getCarVin().equals("1HGCM82633A123456")){ - //获取到车的类型ID - carTypeId = sysCar.getCarTypeId(); + public static void main(String[] args) { + // 定义一个任务,每秒执行一次 + Runnable task = new Runnable() { + @Override + public void run() { + JSONObject stringFromSource = getStringFromSource(); // 模拟获取字符串 + receivedStrings.add(stringFromSource); + System.out.println("Received: " + stringFromSource); + // 清理超过的数据 + cleanUpOldStrings(); + // 检查超速条件 + checkForSpeeding(); } - } - - //查找车类型对应的策略 - List warnStrategyList = null; - //该车绑定的报文模版 - Long templateId=null; - //获取到车的类型之后 查找对应的策略 - List warnStrategy = redisService.getCacheList("warnStrategy"); - for (WarnStrategy strategy : warnStrategy) { - if(strategy.getCarTypeId().equals(carTypeId)){ - templateId=strategy.getTemplateId(); - warnStrategyList.add(strategy); - } - } - - //报文模版 根据templateId取出对应的模版 - List templateTypeList1=null; - List templateTypeList = redisService.getCacheList("templateTypeList"); - for (MessageTemplateType messageTemplateType : templateTypeList) { - if(messageTemplateType.getTemplateId().equals(templateId)){ - templateTypeList1.add(messageTemplateType); - } - } - - //获取策略对应的规则列表 - List warnRule = redisService.getCacheList("warnRule"); - //车辆对应的规则 - List 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 messageTemplateTypes=null; - for (String s : templateIdSplit) { - for (MessageTemplateType messageTemplateType : templateTypeList1) { - if(s.equals(messageTemplateType.getTemplateId())){ - messageTemplateTypes.add(messageTemplateType); - } - } - } - - JSONObject entries = messageParsing(message); - //将解析后的数据添加到List receivedStrings中 - receivedStrings.add(entries); - cleanUpOldStrings(); - checkForSpeeding(); + }; + // 每隔1秒执行一次任务 + scheduler.scheduleAtFixedRate(task, 0, 1, TimeUnit.SECONDS); } + // 模拟从某个源获取字符串的方法 + private static JSONObject getStringFromSource() { + JSONObject jsonObject = new JSONObject(); + jsonObject.put("message", "Hello World"); + jsonObject.put("time", System.currentTimeMillis()); + jsonObject.put("elapsed", elapsedSeconds); + return jsonObject; + } - // 清理超过指定秒数内的数据 + // 清理超过60秒的数据 private static void cleanUpOldStrings() { long currentTime = System.currentTimeMillis(); receivedStrings.removeIf(jsonObject -> @@ -118,11 +59,19 @@ public class test { // 检查是否有超速情况 private static void checkForSpeeding() { if (receivedStrings.size() < 2) return; // 如果数据不足,直接返回 + + JSONObject jsonObject = new JSONObject(); + jsonObject.put("message", "Hello World"); + jsonObject.put("time", System.currentTimeMillis()); + jsonObject.put("elapsed", 100); + for (int i = 0; i < receivedStrings.size() - 1; i++) { - JSONObject current = receivedStrings.get(i); - JSONObject next = receivedStrings.get(i + 1); + JSONObject current = receivedStrings.get(i); + JSONObject next = receivedStrings.get(i + 1); + Short currentElapsed = current.getShort(file); Short nextElapsed = next.getShort(file); + receivedStrings.add(jsonObject); // 检查条件,如果相差大于12,则记录错误 if (nextElapsed > currentElapsed + 12) { System.out.println("出错啦,出错啦,车子超速啦!!!"); @@ -131,73 +80,4 @@ public class test { } - - //解析报文的方法 - 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 carList = redisService.getCacheList("carList"); - for (SysCar sysCar : carList) { - if(sysCar.getCarVin().equals(carVin)){ - carByVin=sysCar; - } - } - log.info("车辆信息为:" + carByVin); - //对应车辆所对应的报文模版 - Integer templateId = carByVin.getTemplateId(); - List 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 templateTypeList1=null; - List 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; - } } diff --git a/cloud-modules/cloud-modules-template/src/main/java/com/muyu/template/test2.java b/cloud-modules/cloud-modules-template/src/main/java/com/muyu/template/test2.java deleted file mode 100644 index 32f1447..0000000 --- a/cloud-modules/cloud-modules-template/src/main/java/com/muyu/template/test2.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.muyu.template; -import com.alibaba.fastjson.JSONObject; -import lombok.extern.log4j.Log4j2; -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 - * @Package:com.muyu.template - * @name:test2 - * @Date:2024/10/6 10:34 - */ -@Log4j2 -public class test2 { - - private static final int DURATION_SECONDS = 5; - private static List receivedStrings = new ArrayList<>(); - private static ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); - private static int elapsedSeconds = 0; - private static String file="elapsed"; - - public static void main(String[] args) { - // 定义一个任务,每秒执行一次 - Runnable task = new Runnable() { - @Override - public void run() { - JSONObject stringFromSource = getStringFromSource(); // 模拟获取字符串 - receivedStrings.add(stringFromSource); - System.out.println("Received: " + stringFromSource); - // 清理超过的数据 - cleanUpOldStrings(); - // 检查超速条件 - checkForSpeeding(); - } - }; - // 每隔1秒执行一次任务 - scheduler.scheduleAtFixedRate(task, 0, 1, TimeUnit.SECONDS); - } - - - // 模拟从某个源获取字符串的方法 - private static JSONObject getStringFromSource() { - JSONObject jsonObject = new JSONObject(); - jsonObject.put("message", "Hello World"); - jsonObject.put("time", System.currentTimeMillis()); - jsonObject.put("elapsed", elapsedSeconds); - return jsonObject; - } - - - // 清理超过60秒的数据 - 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; // 如果数据不足,直接返回 - - JSONObject jsonObject = new JSONObject(); - jsonObject.put("message", "Hello World"); - jsonObject.put("time", System.currentTimeMillis()); - jsonObject.put("elapsed", 100); - - 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); - receivedStrings.add(jsonObject); - // 检查条件,如果相差大于12,则记录错误 - if (nextElapsed > currentElapsed + 12) { - System.out.println("出错啦,出错啦,车子超速啦!!!"); - } - } - } - - -}