fix():完善数据预警方法
parent
d5d17321ff
commit
79f9309c0f
|
@ -13,7 +13,6 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|||
@EnableMyFeignClients
|
||||
@SpringBootApplication
|
||||
public class CloudTemplateApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(CloudTemplateApplication.class, args);
|
||||
}
|
||||
|
|
|
@ -10,10 +10,8 @@ import com.muyu.common.domain.resp.SysCarVo;
|
|||
import com.muyu.common.domain.resp.WarnRuleResp;
|
||||
import com.muyu.common.domain.resp.WarnStrategyResp;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Executors;
|
||||
|
@ -21,7 +19,6 @@ import java.util.concurrent.ScheduledExecutorService;
|
|||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
/**
|
||||
* @author liuxinyue
|
||||
* @Package:com.muyu.template
|
||||
|
@ -35,6 +32,12 @@ public class test {
|
|||
private static ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
|
||||
private static int elapsedSeconds = 0;
|
||||
private static String file="elapsed";
|
||||
private static List<MessageTemplateType> messageTemplateTypes=null;
|
||||
private static Long msgTypeId=null;
|
||||
//滑窗时间
|
||||
private static Long slideTime=null;
|
||||
//增长率
|
||||
private static Long slideFrequency=null;
|
||||
//预警策略
|
||||
@Resource
|
||||
private WarnStrategyCacheService warnStrategyCacheService;
|
||||
|
@ -83,36 +86,18 @@ public class test {
|
|||
}
|
||||
}
|
||||
//报文模版
|
||||
List<MessageTemplateType> messageTemplateTypes = messageTemplateTypeCacheService.get(messageTemplateTypeCacheService.keyPre());
|
||||
Long msgTypeId=null;
|
||||
//滑窗时间
|
||||
Long slideTime=null;
|
||||
//增长率
|
||||
Long slideFrequency=null;
|
||||
messageTemplateTypes = messageTemplateTypeCacheService.get(messageTemplateTypeCacheService.keyPre());
|
||||
for (WarnRuleResp ruleResp : warnRuleResp) {
|
||||
//每一个规则他绑定了报文模版里面对应的一个配置 比如:电池,或者车速
|
||||
msgTypeId = ruleResp.getMsgTypeId();
|
||||
slideTime = ruleResp.getSlideTime();
|
||||
//将规则中对应的滑窗时间赋值为DURATION_SECONDS
|
||||
DURATION_SECONDS = Math.toIntExact(ruleResp.getSlideTime());
|
||||
slideFrequency = ruleResp.getSlideFrequency();
|
||||
}
|
||||
for (JSONObject receivedString : receivedStrings) {
|
||||
for (MessageTemplateType messageTemplateType : messageTemplateTypes) {
|
||||
if(messageTemplateType.getMessageTemplateTypeId().equals(msgTypeId)){
|
||||
//例如: 车速 {"车速":127}
|
||||
Long str = Long.valueOf(receivedString.getStr(messageTemplateType.getMessageField()));
|
||||
if(str+slideFrequency>slideFrequency){
|
||||
log.info("出错啦,出错啦,您的"+messageTemplateType.getMessageField()+"不正常,请检查!!!");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// 定义一个任务,每秒执行一次
|
||||
Runnable task = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
JSONObject stringFromSource = getStringFromSource(); // 模拟获取字符串
|
||||
receivedStrings.add(stringFromSource);
|
||||
System.out.println("Received: " + stringFromSource);
|
||||
// 清理超过的数据
|
||||
cleanUpOldStrings();
|
||||
// 检查超速条件
|
||||
|
@ -123,15 +108,6 @@ public class test {
|
|||
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();
|
||||
|
@ -139,23 +115,21 @@ public class test {
|
|||
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++) {
|
||||
for (int i = 0; i < receivedStrings.size(); 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("出错啦,出错啦,车子超速啦!!!");
|
||||
for (MessageTemplateType messageTemplateType : messageTemplateTypes) {
|
||||
if(messageTemplateType.getMessageTemplateTypeId().equals(msgTypeId)){
|
||||
Short currentElapsed = current.getShort(messageTemplateType.getMessageField());
|
||||
Short nextElapsed = next.getShort(messageTemplateType.getMessageField());
|
||||
if (nextElapsed > currentElapsed + slideFrequency) {
|
||||
log.info("出错啦,出错啦,您的"+messageTemplateType.getMessageField()+"不正常,请检查!!!");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue