我开发了一个程序来监控路由器流量,当流量低于阈值时,我的 java 程序会触发警报。每 30 秒(定时器)我通过 snmp 从路由器获取一个流量值,并将每个值存储在数组中。我的问题是,当出现中断时,文本文件中显示的值为 +/- 0.0,文本文件颜色变为红色,但几分钟后文本字段的颜色变为绿色,文本文件的值仍然很低。你能帮我管理这种情况并让我的文本字段保持红色或黄色,直到流量恢复。下面是我的代码:
//sum of the array
public float sumArray(float arr[]){
int j;
float sum = 0;
for(j = 0; j < arr.length; j++){
sum = sum + arr[j];
}
return sum;
}
// method generating alert
public void blade1() {
//arr1 to made average of valCalOut = valFinalOut
arr1 = new float[3];
//arr2 calculate the threhsold it's the average of valCalOut
arr2 = new float[10];
// fill the arr1 with valCalOut
if ( i1 <= arr1.length ){
arr1[i1] = valCalOut;
i1 ++;
}else{
i1 =0;
arr1B1[i1] = valCalOut;
i1++;
}
//sum arr1 to get valFinalOut
if (index1 <= 3){
valFinalOut = sumArray(arr1) / index1 ;
index1++;
}else{
valFinalOut = sumArray(arr1) / 3 ;
}
// fill arr2 with valCalOut
if ( i2 < arr2.length ){
arr2[i2] = valCalOut;
i2 ++;
}else{
i2 =0;
arr2[i2] = valCalOut;
i2++;
}
// sum arr2 to calculate the threshold(is average of valCalOut)
if (index2 <= 10){
averageVal = sumArray(arr2) / index2 ;
index2++;
}else{
averageVal = sumArray(arr2) / 10;
}
threshold = averageVal - (averageVal * dropRatio);
txtout.setText(Float.toString(valFinalOut));
txtav.setText(Float.toString(averageVal));
txtth.setText(Float.toString(threshold));
if (valFinalOut < threshold ) {
if (txtout.getBackground() == Color.YELLOW) {
//btnreset pressed txtout turned yellow
stopAlarm();
} else {
btnreset.setEnabled(true);
txtout.setBackground(Color.RED);
btnreset.setEnabled(true);
playAlarm();
textArea.append(dateTime()+" : Traffic dropped, Bandwith : " + valFinalOut + " Mbps, Average : "+averageVal+ " Mbps, Threshold : " +threshold+ " Mbps, Please check ! \n ");
}
} else {
txtout.setBackground(Color.GREEN);
btnreset.setEnabled(false);
}
}