@liruiyi962464
2025-07-17T10:23:20.000000Z
字数 4899
阅读 69
代码
public static void main(String[] args) throws IOException {
String text = "总用量:0.490m³,阀门状态:On,电压:3.7V,当前单价:0.00元/m³,余额:0.00元,结算周期用量:0.000m³/0.00元,rssi:-129,";
// 正则表达式模式,用于匹配阀门状态、余额和rssi字段
String pattern = "阀门状态:(.*?),.*?余额:(.*?)元,.*?rssi:(.*?),$";
// 创建 Pattern 对象
Pattern r = Pattern.compile(pattern);
// 创建 Matcher 对象
Matcher m = r.matcher(text);
if (m.find()) {
String valveStatus = m.group(1); // 第一个括号里的内容是阀门状态
String balance = m.group(2); // 第二个括号里的内容是余额
String rssi = m.group(3); // 第三个括号里的内容是rssi
System.out.println("阀门状态: " + valveStatus);
System.out.println("余额: " + balance);
System.out.println("rssi: " + rssi);
} else {
System.out.println("未找到匹配的内容");
}
}
public static void main(String[] args) {
// 示例数据
List<String> resList = Arrays.asList(
"PING 10.51.1.85 (10.51.1.85): 56 data bytes",
"64 bytes from 10.51.1.85: seq=0 ttl=123 time=165.331 ms",
"64 bytes from 10.51.1.85: seq=1 ttl=123 time=22.958 ms",
"64 bytes from 10.51.1.85: seq=2 ttl=123 time=22.418 ms",
"64 bytes from 10.51.1.85: seq=3 ttl=123 time=38.859 ms",
"64 bytes from 10.51.1.85: seq=4 ttl=123 time=46.738 ms",
"",
"--- 10.51.1.85 ping statistics ---",
"5 packets transmitted, 5 packets received, 0% packet loss",
"round-trip min/avg/max = 22.418/59.260/165.331 ms",
""
);
String s = "";
//获取最短时延,平均时延,最大时延
if (resList != null || resList.size() > 0) {
String line = resList.get(resList.size() - 2);
System.out.println(line);
String pattern = "min/avg/max = (\\d+\\.\\d+)/(\\d+\\.\\d+)/(\\d+\\.\\d+) ms";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(line);
if (m.find()) {
String min = calc(m.group(1));
String avg = calc(m.group(2));
String max = calc(m.group(3));
s = "ping次数为" + 5 + ",最小时延为" + min + "ms,最大时延为" + max + "ms,平均时延为" + avg + "ms";
System.err.println(s);
}else {
System.err.println("ping结果为空");
}
}
}
String line = resList.get(resList.size() - 3);
String pattern = "min/avg/max = (\\d+\\.\\d+)/(\\d+\\.\\d+)/(\\d+\\.\\d+) ms";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(line);
if (m.find()) {
String min = calc(m.group(1));
String avg = calc(m.group(2));
String max = calc(m.group(3));
s = "ping次数为" + validationParameter.getPingCount() + ",最小时延为" + min + "ms,最大时延为" + max + "ms,平均时延为" + avg + "ms";
}
private String attachResultHandle(Set<SysUser> userList,DeviceInfo deviceInfo,Object result, BusinessValidationParameter validationParameter, BusinessValidationDetails businessValidationDetails) {
//处理结果
String s = ""; //attach时延结果
Double successCount = 0.00; //attach时延不为error的次数
int m = 0; // attach次数
int totalDelay = 0; //attach时延总和
int minDelay = 0; //attach时延最小值
int maxDelay = 0; //attach时延最大值
int countDelayed = 0; //attach时延不为error的次数
int errorCount = 0; // attach时延为error的次数
String strResult = result.toString(); //attach时延结果
strResult = strResult.substring(1, strResult.length() - 2); //attach时延结果
List<String> resList = Arrays.asList(strResult.split(",")); //attach时延结果集合
if (resList != null && resList.size() > 0) {
System.out.println(resList);
for (int i = 0; i < Integer.parseInt(validationParameter.getAttachCount()); i++) {
m = i + 1; //attach次数
String delayStr = resList.get(i).trim(); // 去除可能的空格
if (!delayStr.equalsIgnoreCase("\"error\"")) {
try {
int delay = Integer.parseInt(delayStr);
successCount++;
totalDelay += delay;
countDelayed++;
// 更新最小和最大时延
if (delay < minDelay) {
minDelay = delay;
}
if (delay > maxDelay) {
maxDelay = delay;
}
} catch (NumberFormatException e) {
System.err.println("无效的数值格式: " + delayStr);
}
}else {
errorCount++;
}
}
// 计算平均时延
double avgDelay = (countDelayed == 0) ? 0 : (double) totalDelay / countDelayed;
// 构建包含统计信息的字符串
// 构建包含统计信息的字符串
if (errorCount != 0){
s = "attach次数为" + m + ",error次数为" + errorCount +",最小时延为" + minDelay + "ms,最大时延为" + maxDelay + "ms,平均时延为" + String.format("%.2f", avgDelay) + "ms";
}else {
s = "attach次数为" + m + ",最小时延为" + minDelay + "ms,最大时延为" + maxDelay + "ms,平均时延为" + String.format("%.2f", avgDelay) + "ms";
}
//计算成功率
Double successRate = successCount / Integer.parseInt(validationParameter.getAttachCount());
successRate = successRate * 100;
businessValidationDetails.setAttachSuccessRate(successRate.toString());
//判断成功率是否正常
if (Integer.parseInt(validationParameter.getAttachSuccessRate()) > successRate) {
//成功率异常
// 告警且推送
//创建告警记录对象
String alarmContent = deviceInfo.getName() + "设备的attach业务验证成功率为" + successRate + "%," + "低于设定的标准值";
AlarmRecord alarmRecord = new AlarmRecord();
alarmRecord.setAlarmType("6");
alarmRecord.setAlarmTime(getCurrentDay());
alarmRecord.setDeviceId(deviceInfo.getId());
alarmRecord.setAlarmContent(alarmContent);
alarmRecordService.save(alarmRecord);
//推送到系统公告中
sendSysAnnouncement(userList, "attach业务验证", alarmContent);
businessValidationDetails.setSuccessState("0");
} else {
//成功率正常
businessValidationDetails.setSuccessState("1");
}
//判断延时是否正常
//计算平均延时
double avgDelayed = countDelayed / successCount;
if (avgDelayed > Double.parseDouble(validationParameter.getAttachDelayed())) {
//延时异常
//告警且推送
String alarmContent = deviceInfo.getName() + "设备的attach业务验证平均时延为" + avgDelayed + "ms,低于设定的标准值";
AlarmRecord alarmRecord = new AlarmRecord();
alarmRecord.setAlarmType("6");
alarmRecord.setAlarmTime(getCurrentDay());
alarmRecord.setDeviceId(deviceInfo.getId());
alarmRecord.setAlarmContent(alarmContent);
alarmRecordService.save(alarmRecord);
//推送到系统公告中
sendSysAnnouncement(userList, "attach业务验证", alarmContent);
businessValidationDetails.setTimeDelayState("0");
} else {
//延时正常
businessValidationDetails.setTimeDelayState("1");
}
}
return s;
}**粗体文本**