除了遥测数据外,如果加入遥信数据、历史数据和录波数据,可以显著提高故障分类的准确性。我将更新Perl模块,整合这些额外数据源,以实现更精准的故障分类。

package FaultClassifier;

use strict;
use warnings;
use JSON;
use Data::Dumper;
use Time::Piece;
use List::Util qw(max min sum);
use POSIX qw(ceil floor);

# 导出函数
use Exporter qw(import);
our @EXPORT_OK = qw(classify_fault load_thresholds);

# 配置文件路径,可根据实际部署情况修改
my $CONFIG_FILE = '/path/to/fault_thresholds.json';
my $thresholds;

# 初始化时加载阈值配置
sub load_thresholds {
    my ($config_file) = @_ || $CONFIG_FILE;
    
    open my $fh, '<', $config_file or die "无法打开配置文件 $config_file: $!";
    local $/;
    my $config_data = <$fh>;
    close $fh;
    
    $thresholds = decode_json($config_data);
    return $thresholds;
}

# 主要故障分类函数
sub classify_fault {
    my ($data, $telemetry_data, $historical_data, $waveform_data) = @_;
    
    # 如果阈值未加载,则加载默认阈值
    $thresholds ||= load_thresholds();
    
    # 故障检测逻辑(按优先级顺序检查)
    
    # 1. 短路故障检测 - 使用遥测数据、录波数据增强判断
    my $short_circuit = check_short_circuit($data, $telemetry_data, $waveform_data);
    return $short_circuit if $short_circuit;
    
    # 2. 接地故障检测 - 使用遥测数据、录波数据增强判断
    my $ground_fault = check_ground_fault($data, $telemetry_data, $waveform_data);
    return $ground_fault if $ground_fault;
    
    # 3. 失压故障检测 - 使用历史数据增强判断
    my $voltage_loss = check_voltage_loss($data, $historical_data);
    return $voltage_loss if $voltage_loss;
    
    # 4. 过载故障检测 - 使用历史数据增强判断
    my $overload = check_overload($data, $historical_data);
    return $overload if $overload;
    
    # 5. 断路故障检测 - 使用遥信数据增强判断
    my $open_circuit = check_open_circuit($data, $telemetry_data);
    return $open_circuit if $open_circuit;
    
    # 没有检测到故障,返回正常状态
    return { status => "正常", code => "NORMAL", description => "设备运行正常" };
}

# 短路故障检测 - 增强版
sub check_short_circuit {
    my ($data, $telemetry_data, $waveform_data) = @_;
    
    # 基本短路判断逻辑
    my $basic_fault = check_basic_short_circuit($data);
    if ($basic_fault) {
        # 如果有录波数据,进行波形分析以提高准确性
        if ($waveform_data) {
            my $waveform_analysis = analyze_short_circuit_waveform($waveform_data);
            if ($waveform_analysis) {
                # 使用波形分析结果替换或补充基本判断
                if ($waveform_analysis->{subtype}) {
                    $basic_fault->{subtype} = $waveform_analysis->{subtype};
                    $basic_fault->{code} = $waveform_analysis->{code};
                }
                $basic_fault->{confidence} = $waveform_analysis->{confidence} || 0.95;
                $basic_fault->{waveform_analysis} = $waveform_analysis->{details} if $waveform_analysis->{details};
            }
        }
        
        # 如果有遥信数据,检查保护动作情况
        if ($telemetry_data && $telemetry_data->{protection_status}) {
            $basic_fault->{protection_action} = $telemetry_data->{protection_status};
            
            # 如果保护装置已动作,增加故障确定性
            if ($telemetry_data->{protection_status} eq "动作") {
                $basic_fault->{confidence} = 0.99;
            }
        }
    }
    
    return $basic_fault;
}

# 基本短路故障检测逻辑
sub check_basic_short_circuit {
    my ($data) = @_;
    
    # 三相短路:三相电流都非常高,且相间电压降低
    if ($data->{IA_Current} > $thresholds->{short_circuit}->{high_current_threshold} &&
        $data->{IB_Current} > $thresholds->{short_circuit}->{high_current_threshold} &&
        $data->{IC_Current} > $thresholds->{short_circuit}->{high_current_threshold} &&
        $data->{UAB_Voltage} < $thresholds->{short_circuit}->{low_voltage_threshold} &&
        $data->{UBC_Voltage} < $thresholds->{short_circuit}->{low_voltage_threshold} &&
        $data->{UCA_Voltage} < $thresholds->{short_circuit}->{low_voltage_threshold}) {
        return { status => "故障", category => "短路故障", subtype => "三相短路", code => "SC01", confidence => 0.9 };
    }
    
    # 两相短路:两相电流高,对应相间电压低
    if (($data->{IA_Current} > $thresholds->{short_circuit}->{high_current_threshold} &&
         $data->{IB_Current} > $thresholds->{short_circuit}->{high_current_threshold} &&
         $data->{UAB_Voltage} < $thresholds->{short_circuit}->{low_voltage_threshold}) ||
        ($data->{IB_Current} > $thresholds->{short_circuit}->{high_current_threshold} &&
         $data->{IC_Current} > $thresholds->{short_circuit}->{high_current_threshold} &&
         $data->{UBC_Voltage} < $thresholds->{short_circuit}->{low_voltage_threshold}) ||
        ($data->{IC_Current} > $thresholds->{short_circuit}->{high_current_threshold} &&
         $data->{IA_Current} > $thresholds->{short_circuit}->{high_current_threshold} &&
         $data->{UCA_Voltage} < $thresholds->{short_circuit}->{low_voltage_threshold})) {
        return { status => "故障", category => "短路故障", subtype => "两相短路", code => "SC02", confidence => 0.9 };
    }
    
    # 相间短路:任意两相之间的电压明显低于正常值
    if (($data->{UAB_Voltage} < $thresholds->{short_circuit}->{low_voltage_threshold}) ||
        ($data->{UBC_Voltage} < $thresholds->{short_circuit}->{low_voltage_threshold}) ||
        ($data->{UCA_Voltage} < $thresholds->{short_circuit}->{low_voltage_threshold})) {
        return { status => "故障", category => "短路故障", subtype => "相间短路", code => "SC03", confidence => 0.85 };
    }
    
    # 金属性短路:电流极高,电压几乎为零
    if (($data->{IA_Current} > $thresholds->{short_circuit}->{very_high_current_threshold} && $data->{UA_Voltage} < $thresholds->{short_circuit}->{very_low_voltage_threshold}) ||
        ($data->{IB_Current} > $thresholds->{short_circuit}->{very_high_current_threshold} && $data->{UB_Voltage} < $thresholds->{short_circuit}->{very_low_voltage_threshold}) ||
        ($data->{IC_Current} > $thresholds->{short_circuit}->{very_high_current_threshold} && $data->{UC_Voltage} < $thresholds->{short_circuit}->{very_low_voltage_threshold})) {
        return { status => "故障", category => "短路故障", subtype => "金属性短路", code => "SC04", confidence => 0.95 };
    }
    
    # 电弧性短路:电流和电压波动较大,但电压不为零
    if (($data->{IA_Current} > $thresholds->{short_circuit}->{high_current_threshold} && 
         $data->{UA_Voltage} < $thresholds->{short_circuit}->{low_voltage_threshold} && 
         $data->{UA_Voltage} > $thresholds->{short_circuit}->{very_low_voltage_threshold}) ||
        ($data->{IB_Current} > $thresholds->{short_circuit}->{high_current_threshold} && 
         $data->{UB_Voltage} < $thresholds->{short_circuit}->{low_voltage_threshold} && 
         $data->{UB_Voltage} > $thresholds->{short_circuit}->{very_low_voltage_threshold}) ||
        ($data->{IC_Current} > $thresholds->{short_circuit}->{high_current_threshold} && 
         $data->{UC_Voltage} < $thresholds->{short_circuit}->{low_voltage_threshold} && 
         $data->{UC_Voltage} > $thresholds->{short_circuit}->{very_low_voltage_threshold})) {
        return { status => "故障", category => "短路故障", subtype => "电弧性短路", code => "SC05", confidence => 0.85 };
    }
    
    return undef; # 没有短路故障
}

# 分析短路故障的录波数据
sub analyze_short_circuit_waveform {
    my ($waveform_data) = @_;
    
    return undef unless $waveform_data && $waveform_data->{current_waveform} && $waveform_data->{voltage_waveform};
    
    my $result = {};
    my $current_waveform = $waveform_data->{current_waveform};
    my $voltage_waveform = $waveform_data->{voltage_waveform};
    
    # 检查电流波形的特征
    my $peak_current = max(@{$current_waveform->{A}}, @{$current_waveform->{B}}, @{$current_waveform->{C}});
    my $min_voltage = min(@{$voltage_waveform->{AB}}, @{$voltage_waveform->{BC}}, @{$voltage_waveform->{CA}});
    
    # 检查波形失真情况
    my $current_distortion = check_waveform_distortion($current_waveform);
    my $voltage_distortion = check_waveform_distortion($voltage_waveform);
    
    # 根据波形特征判断短路类型
    # 1. 电弧性短路:电流波形有明显失真,且有不规则波动
    if ($current_distortion > $thresholds->{waveform}->{high_distortion_threshold}) {
        $result = {
            subtype => "电弧性短路",
            code => "SC05",
            confidence => 0.95,
            details => {
                current_distortion => $current_distortion,
                voltage_distortion => $voltage_distortion,
                peak_current => $peak_current
            }
        };
    }
    # 2. 金属性短路:电流波形陡峭,电压波形接近零
    elsif ($peak_current > $thresholds->{waveform}->{very_high_current_peak} && 
           $min_voltage < $thresholds->{waveform}->{very_low_voltage_valley}) {
        $result = {
            subtype => "金属性短路",
            code => "SC04",
            confidence => 0.98,
            details => {
                peak_current => $peak_current,
                min_voltage => $min_voltage
            }
        };
    }
    # 3. 三相短路:三相电流波形均有明显升高
    elsif (max(@{$current_waveform->{A}}) > $thresholds->{waveform}->{high_current_peak} &&
           max(@{$current_waveform->{B}}) > $thresholds->{waveform}->{high_current_peak} &&
           max(@{$current_waveform->{C}}) > $thresholds->{waveform}->{high_current_peak}) {
        $result = {
            subtype => "三相短路",
            code => "SC01",
            confidence => 0.95,
            details => {
                peak_current_A => max(@{$current_waveform->{A}}),
                peak_current_B => max(@{$current_waveform->{B}}),
                peak_current_C => max(@{$current_waveform->{C}})
            }
        };
    }
    # 4. 两相短路:两相电流波形有明显升高
    elsif ((max(@{$current_waveform->{A}}) > $thresholds->{waveform}->{high_current_peak} && 
            max(@{$current_waveform->{B}}) > $thresholds->{waveform}->{high_current_peak}) ||
           (max(@{$current_waveform->{B}}) > $thresholds->{waveform}->{high_current_peak} && 
            max(@{$current_waveform->{C}}) > $thresholds->{waveform}->{high_current_peak}) ||
           (max(@{$current_waveform->{C}}) > $thresholds->{waveform}->{high_current_peak} && 
            max(@{$current_waveform->{A}}) > $thresholds->{waveform}->{high_current_peak})) {
        $result = {
            subtype => "两相短路",
            code => "SC02",
            confidence => 0.95,
            details => {
                peak_current_A => max(@{$current_waveform->{A}}),
                peak_current_B => max(@{$current_waveform->{B}}),
                peak_current_C => max(@{$current_waveform->{C}})
            }
        };
    }
    
    return $result;
}

# 检查波形失真情况
sub check_waveform_distortion {
    my ($waveform) = @_;
    
    # 计算波形失真度的简化算法
    # 实际应用中应使用更复杂的算法,如傅里叶变换计算谐波失真
    my $distortion = 0;
    
    foreach my $phase (keys %$waveform) {
        my @samples = @{$waveform->{$phase}};
        my $n = scalar(@samples);
        next unless $n > 10;
        
        # 计算标准差作为波形失真的简化指标
        my $mean = sum(@samples) / $n;
        my $variance = sum(map { ($_ - $mean) ** 2 } @samples) / $n;
        my $std_dev = sqrt($variance);
        
        # 归一化的标准差
        my $normalized_std_dev = $std_dev / $mean if $mean > 0;
        $distortion = $normalized_std_dev if $normalized_std_dev > $distortion;
    }
    
    return $distortion;
}

# 接地故障检测 - 增强版
sub check_ground_fault {
    my ($data, $telemetry_data, $waveform_data) = @_;
    
    # 基本接地故障判断逻辑
    my $basic_fault = check_basic_ground_fault($data);
    if ($basic_fault) {
        # 如果有录波数据,进行波形分析以提高准确性
        if ($waveform_data) {
            my $waveform_analysis = analyze_ground_fault_waveform($waveform_data);
            if ($waveform_analysis) {
                # 使用波形分析结果替换或补充基本判断
                if ($waveform_analysis->{subtype}) {
                    $basic_fault->{subtype} = $waveform_analysis->{subtype};
                    $basic_fault->{code} = $waveform_analysis->{code};
                }
                $basic_fault->{confidence} = $waveform_analysis->{confidence} || 0.95;
                $basic_fault->{waveform_analysis} = $waveform_analysis->{details} if $waveform_analysis->{details};
            }
        }
        
        # 如果有遥信数据,检查接地保护装置状态
        if ($telemetry_data && $telemetry_data->{ground_protection_status}) {
            $basic_fault->{protection_action} = $telemetry_data->{ground_protection_status};
            
            # 如果接地保护装置已动作,增加故障确定性
            if ($telemetry_data->{ground_protection_status} eq "动作") {
                $basic_fault->{confidence} = 0.99;
            }
        }
    }
    
    return $basic_fault;
}

# 基本接地故障检测逻辑
sub check_basic_ground_fault {
    my ($data) = @_;
    
    # 零序电流是接地故障的主要指标
    my $i0 = $data->{I0_Current};
    
    # 单相接地:零序电流高,某一相电压低
    if ($i0 > $thresholds->{ground_fault}->{high_zero_sequence_current} &&
        ($data->{UA_Voltage} < $thresholds->{ground_fault}->{low_phase_voltage} ||
         $data->{UB_Voltage} < $thresholds->{ground_fault}->{low_phase_voltage} ||
         $data->{UC_Voltage} < $thresholds->{ground_fault}->{low_phase_voltage})) {
        return { status => "故障", category => "接地故障", subtype => "单相接地", code => "GF01", confidence => 0.9 };
    }
    
    # 低阻接地:零序电流明显高于阈值
    if ($i0 > $thresholds->{ground_fault}->{high_zero_sequence_current}) {
        return { status => "故障", category => "接地故障", subtype => "低阻接地", code => "GF02", confidence => 0.9 };
    }
    
    # 高阻接地:零序电流稍高于正常值
    if ($i0 > $thresholds->{ground_fault}->{medium_zero_sequence_current} &&
        $i0 <= $thresholds->{ground_fault}->{high_zero_sequence_current}) {
        return { status => "故障", category => "接地故障", subtype => "高阻接地", code => "GF03", confidence => 0.85 };
    }
    
    # 间歇性接地:零序电流在一定范围内波动(此处简化判断)
    if ($i0 > $thresholds->{ground_fault}->{low_zero_sequence_current} &&
        $i0 <= $thresholds->{ground_fault}->{medium_zero_sequence_current}) {
        return { status => "故障", category => "接地故障", subtype => "间歇性接地", code => "GF04", confidence => 0.7 };
    }
    
    # 中性点接地:零序电流略微升高,三相电压平衡但偏低
    if ($i0 > $thresholds->{ground_fault}->{very_low_zero_sequence_current} &&
        abs($data->{UA_Voltage} - $data->{UB_Voltage}) < $thresholds->{ground_fault}->{voltage_balance_threshold} &&
        abs($data->{UB_Voltage} - $data->{UC_Voltage}) < $thresholds->{ground_fault}->{voltage_balance_threshold} &&
        abs($data->{UC_Voltage} - $data->{UA_Voltage}) < $thresholds->{ground_fault}->{voltage_balance_threshold} &&
        $data->{UA_Voltage} < $thresholds->{ground_fault}->{neutral_grounding_voltage} &&
        $data->{UB_Voltage} < $thresholds->{ground_fault}->{neutral_grounding_voltage} &&
        $data->{UC_Voltage} < $thresholds->{ground_fault}->{neutral_grounding_voltage}) {
        return { status => "故障", category => "接地故障", subtype => "中性点接地", code => "GF05", confidence => 0.8 };
    }
    
    return undef; # 没有接地故障
}

# 分析接地故障的录波数据
sub analyze_ground_fault_waveform {
    my ($waveform_data) = @_;
    
    return undef unless $waveform_data && $waveform_data->{current_waveform} && $waveform_data->{voltage_waveform};
    
    my $result = {};
    my $current_waveform = $waveform_data->{current_waveform};
    my $voltage_waveform = $waveform_data->{voltage_waveform};
    my $zero_sequence_current = $waveform_data->{zero_sequence_current};
    
    return undef unless $zero_sequence_current;
    
    # 分析零序电流波形特征
    my $max_zero_current = max(@$zero_sequence_current);
    my $min_zero_current = min(@$zero_sequence_current);
    my $zero_current_range = $max_zero_current - $min_zero_current;
    
    # 分析相电压波形
    my $min_phase_voltage = min(
        min(@{$voltage_waveform->{A} || []}),
        min(@{$voltage_waveform->{B} || []}),
        min(@{$voltage_waveform->{C} || []})
    );
    
    # 检查零序电流波形的周期性变化(用于检测间歇性接地)
    my $is_intermittent = check_intermittent_pattern($zero_sequence_current);
    
    # 根据波形特征判断接地故障类型
    # 1. 间歇性接地:零序电流有明显的周期性变化
    if ($is_intermittent) {
        $result = {
            subtype => "间歇性接地",
            code => "GF04",
            confidence => 0.95,
            details => {
                max_zero_current => $max_zero_current,
                zero_current_range => $zero_current_range,
                is_intermittent => 1
            }
        };
    }
    # 2. 低阻接地:零序电流波形幅值大且稳定
    elsif ($max_zero_current > $thresholds->{waveform}->{high_zero_current_threshold} &&
           $zero_current_range < $thresholds->{waveform}->{stable_current_range}) {
        $result = {
            subtype => "低阻接地",
            code => "GF02",
            confidence => 0.95,
            details => {
                max_zero_current => $max_zero_current,
                zero_current_range => $zero_current_range
            }
        };
    }
    # 3. 高阻接地:零序电流波形幅值中等
    elsif ($max_zero_current > $thresholds->{waveform}->{medium_zero_current_threshold} &&
           $max_zero_current <= $thresholds->{waveform}->{high_zero_current_threshold}) {
        $result = {
            subtype => "高阻接地",
            code => "GF03",
            confidence => 0.9,
            details => {
                max_zero_current => $max_zero_current
            }
        };
    }
    # 4. 单相接地:零序电流高且某相电压降低明显
    elsif ($max_zero_current > $thresholds->{waveform}->{high_zero_current_threshold} &&
           $min_phase_voltage < $thresholds->{waveform}->{low_phase_voltage_threshold}) {
        $result = {
            subtype => "单相接地",
            code => "GF01",
            confidence => 0.95,
            details => {
                max_zero_current => $max_zero_current,
                min_phase_voltage => $min_phase_voltage
            }
        };
    }
    
    return $result;
}

# 检查波形是否有间歇性模式
sub check_intermittent_pattern {
    my ($waveform) = @_;
    
    # 简化算法:寻找波形中的峰值数量
    # 实际应用中应使用更复杂的频谱分析
    my @samples = @$waveform;
    my $n = scalar(@samples);
    return 0 if $n < 20;
    
    my $threshold = 0.5 * (max(@samples) - min(@samples)) + min(@samples);
    my $peaks = 0;
    my $rising = 0;
    
    for (my $i = 1; $i < $n; $i++) {
        if (!$rising && $samples[$i] > $samples[$i-1] && $samples[$i] > $threshold) {
            $rising = 1;
        }
        elsif ($rising && $samples[$i] < $samples[$i-1] && $samples[$i-1] > $threshold) {
            $peaks++;
            $rising = 0;
        }
    }
    
    # 如果在波形中检测到多个峰值,则认为是间歇性的
    return $peaks >= $thresholds->{waveform}->{intermittent_peaks_threshold};
}

# 失压故障检测 - 增强版,使用历史数据
sub check_voltage_loss {
    my ($data, $historical_data) = @_;
    
    # 基本失压故障判断逻辑
    my $basic_fault = check_basic_voltage_loss($data);
    if ($basic_fault) {
        # 如果有历史数据,可以判断是暂时失压还是持续失压
        if ($historical_data && $historical_data->{voltage}) {
            my $history = analyze_voltage_history($historical_data->{voltage});
            
            # 更新失压故障的类型
            if ($history->{persistent_low_voltage}) {
                if ($basic_fault->{subtype} eq "全电压失压") {
                    $basic_fault->{description} = "持续全电压失压,已持续" . $history->{low_voltage_duration} . "分钟";
                } elsif ($basic_fault->{subtype} eq "部分失压") {
                    $basic_fault->{description} = "持续部分失压,已持续" . $history->{low_voltage_duration} . "分钟";
                }
                $basic_fault->{confidence} = 0.95;
            } else {
                if ($basic_fault->{subtype} eq "全电压失压") {
                    $basic_fault->{description} = "暂时全电压失压";
                } elsif ($basic_fault->{subtype} eq "部分失压") {
                    $basic_fault->{description} = "暂时部分失压";
                }
                $basic_fault->{confidence} = 0.85;
            }
            
            # 添加电压波动分析
            $basic_fault->{voltage_analysis} = {
                fluctuation_level => $history->{fluctuation_level},
                min_voltage => $history->{min_voltage},
                max_voltage => $history->{max_voltage},
                avg_voltage => $history->{avg_voltage}
            };
        }
    }
    
    return $basic_fault;
}

# 基本失压故障检测逻辑
sub check_basic_voltage_loss {
    my ($data) = @_;
    
    # 全电压失压:三相电压都极低
    if ($data->{UA_Voltage} < $thresholds->{voltage_loss}->{very_low_voltage} &&
        $data->{UB_Voltage} < $thresholds->{voltage_loss}->{very_low_voltage} &&
        $data->{UC_Voltage} < $thresholds->{voltage_loss}->{very_low_voltage}) {
        return { status => "故障", category => "失压故障", subtype => "全电压失压", code => "VL01", confidence => 0.9 };
    }
    
    # 部分失压:至少一相电压极低
    if ($data->{UA_Voltage} < $thresholds->{voltage_loss}->{very_low_voltage} ||
        $data->{UB_Voltage} < $thresholds->{voltage_loss}->{very_low_voltage} ||
        $data->{UC_Voltage} < $thresholds->{voltage_loss}->{very_low_voltage}) {
        return { status => "故障", category => "失压故障", subtype => "部分失压", code => "VL02", confidence => 0.9 };
    }
    
    # 电压骤降:三相电压都显著低于正常值但不为零
    if ($data->{UA_Voltage} < $thresholds->{voltage_loss}->{low_voltage} &&
        $data->{UB_Voltage} < $thresholds->{voltage_loss}->{low_voltage} &&
        $data->{UC_Voltage} < $thresholds->{voltage_loss}->{low_voltage}) {
        return { status => "故障", category => "失压故障", subtype => "电压骤降", code => "VL03", confidence => 0.85 };
    }
    
    # 欠电压:三相电压都低于正常值但高于警戒值
    if ($data->{UA_Voltage} < $thresholds->{voltage_loss}->{under_voltage} &&
        $data->{UB_Voltage} < $thresholds->{voltage_loss}->{under_voltage} &&
        $data->{UC_Voltage} < $thresholds->{voltage_loss}->{under_voltage}) {
        return { status => "故障", category => "失压故障", subtype => "欠电压", code => "VL04", confidence => 0.8 };
    }
    
    # 电压不平衡:三相电压差异超过阈值
    my $avg_voltage = ($data->{UA_Voltage} + $data->{UB_Voltage} + $data->{UC_Voltage}) / 3;
    my $max_deviation = $thresholds->{voltage_loss}->{imbalance_threshold} * $avg_voltage;
    
    if (abs($data->{UA_Voltage} - $avg_voltage) > $max_deviation ||
        abs($data->{UB_Voltage} - $avg_voltage) > $max_deviation ||
        abs($data->{UC_Voltage} - $avg_voltage) > $max_deviation) {
        return { status => "故障", category => "失压故障", subtype => "电压不平衡", code => "VL05", confidence => 0.85 };
    }
    
    return undef; # 没有失压故障
}

# 分析电压历史数据
sub analyze_voltage_history {
    my ($voltage_history) = @_;
    
    my $result = {
        persistent_low_voltage => 0,
        low_voltage_duration => 0,
        fluctuation_level => 0,
        min_voltage => 1000,  # 初始设置为一个很大的值
        max_voltage => 0,
        avg_voltage => 0
    };
    
    return $result unless $voltage_history && ref($voltage_history) eq 'ARRAY' && @$voltage_history > 0;
    
    my $low_voltage_count = 0;
    my $total_points = scalar(@$voltage_history);
    my $sum_voltage = 0;
    my @all_voltages;
    
    foreach my $point (@$voltage_history) {
        my $avg = ($point->{UA_Voltage} + $point->{UB_Voltage} + $point->{UC_Voltage}) / 3;
        push @all_voltages, $avg;
        $sum_voltage += $avg;
        
        if ($avg < $thresholds->{voltage_loss}->{low_voltage}) {
            $low_voltage_count++;
        }
        
        $result->{min_voltage} = $avg if $avg < $result->{min_voltage};
        $result->{max_voltage} = $avg if $avg > $result->{max_voltage};
    }
    
    $result->{avg_voltage} = $sum_voltage / $total_points;
    
    # 计算电压波动水平(标准差/平均值)
    if ($total_points > 1) {
        my $variance = sum(map { ($_ - $result->{avg_voltage}) ** 2 } @all_voltages) / $total_points;
        my $std_dev = sqrt($variance);
        $result->{fluctuation_level} = $std_dev / $result->{avg_voltage};
    }
    
    # 如果大部分历史点都是低电压,则认为是持续失压
    if ($low_voltage_count / $total_points > $thresholds->{historical}->{persistent_ratio}) {
        $result->{persistent_low_voltage} = 1;
        $result->{low_voltage_duration} = int($total_points * $thresholds->{historical}->{time_interval});
    }
    
    return $result;
}

# 过载故障检测 - 增强版,使用历史数据
sub check_overload {
    my ($data, $historical_data) = @_;
    
    # 基本过载故障判断逻辑
    my $basic_fault = check_basic_overload($data);
    if ($basic_fault) {
        # 如果有历史数据,可以判断是短时过载还是持续过载
        if ($historical_data && $historical_data->{power}) {
            my $history = analyze_power_history($historical_data->{power});
            
            # 更新过载故障的类型
            if ($history->{persistent_overload}) {
                if ($basic_fault->{subtype} eq "短时过载") {
                    $basic_fault->{subtype} = "持续过载";
                    $basic_fault->{code} = "OL01";
                }
                $basic_fault->{description} = "持续过载,已持续" . $history->{overload_duration} . "分钟";
                $basic_fault->{confidence} = 0.95;
            } else {
                if ($basic_fault->{subtype} eq "持续过载") {
                    $basic_fault->{subtype} = "短时过载";
                    $basic_fault->{code} = "OL02";
                }
                $basic_fault->{description} = "短时过载";
                $basic_fault->{confidence} = 0.85;
            }
            
            # 添加功率波动分析
            $basic_fault->{power_analysis} = {
                fluctuation_level => $history->{fluctuation_level},
                peak_power => $history->{peak_power},
                avg_power => $history->{avg_power}
            };
        }
    }
    
    return $basic_fault;
}

# 基本过载故障检测逻辑
sub check_basic_overload {
    my ($data) = @_;
    
    # 持续过载:总功率超过阈值
    if ($data->{ApparentPower_Total} > $thresholds->{overload}->{total_power_threshold}) {
        return { status => "故障", category => "过载故障", subtype => "持续过载", code => "OL01", confidence => 0.9 };
    }
    
    # 短时过载:总功率略超过阈值
    if ($data->{ApparentPower_Total} > $thresholds->{overload}->{short_overload_threshold} &&
        $data->{ApparentPower_Total} <= $thresholds->{overload}->{total_power_threshold}) {
        return { status => "故障", category => "过载故障", subtype => "短时过载", code => "OL02", confidence => 0.8 };
    }
    
    # 变压器过载:三相电流都高,但电压正常
    if ($data->{IA_Current} > $thresholds->{overload}->{phase_current_threshold} &&
        $data->{IB_Current} > $thresholds->{overload}->{phase_current_threshold} &&
        $data->{IC_Current} > $thresholds->{overload}->{phase_current_threshold} &&
        $data->{UA_Voltage} >= $thresholds->{voltage_loss}->{normal_voltage_min} &&
        $data->{UB_Voltage} >= $thresholds->{voltage_loss}->{normal_voltage_min} &&
        $data->{UC_Voltage} >= $thresholds->{voltage_loss}->{normal_voltage_min}) {
        return { status => "故障", category => "过载故障", subtype => "变压器过载", code => "OL03", confidence => 0.85 };
    }
    
    # 线路过载:三相电流都高,且电压略有下降
    if ($data->{IA_Current} > $thresholds->{overload}->{phase_current_threshold} &&
        $data->{IB_Current} > $thresholds->{overload}->{phase_current_threshold} &&
        $data->{IC_Current} > $thresholds->{overload}->{phase_current_threshold} &&
        $data->{UA_Voltage} < $thresholds->{voltage_loss}->{normal_voltage_min} &&
        $data->{UB_Voltage} < $thresholds->{voltage_loss}->{normal_voltage_min} &&
        $data->{UC_Voltage} < $thresholds->{voltage_loss}->{normal_voltage_min}) {
        return { status => "故障", category => "过载故障", subtype => "线路过载", code => "OL04", confidence => 0.85 };
    }
    
    # 开关设备过载:任一相电流极高,导致功率因数降低
    my $power_factor = $data->{ActivePower_Total} / $data->{ApparentPower_Total} if $data->{ApparentPower_Total} > 0;
    
    if (($data->{IA_Current} > $thresholds->{overload}->{very_high_current_threshold} ||
         $data->{IB_Current} > $thresholds->{overload}->{very_high_current_threshold} ||
         $data->{IC_Current} > $thresholds->{overload}->{very_high_current_threshold}) &&
        $power_factor < $thresholds->{overload}->{low_power_factor}) {
        return { status => "故障", category => "过载故障", subtype => "开关设备过载", code => "OL05", confidence => 0.85 };
    }
    
    return undef; # 没有过载故障
}

# 分析功率历史数据
sub analyze_power_history {
    my ($power_history) = @_;
    
    my $result = {
        persistent_overload => 0,
        overload_duration => 0,
        fluctuation_level => 0,
        peak_power => 0,
        avg_power => 0
    };
    
    return $result unless $power_history && ref($power_history) eq 'ARRAY' && @$power_history > 0;
    
    my $overload_count = 0;
    my $total_points = scalar(@$power_history);
    my $sum_power = 0;
    my @all_powers;
    
    foreach my $point (@$power_history) {
        my $power = $point->{ApparentPower_Total};
        push @all_powers, $power;
        $sum_power += $power;
        
        if ($power > $thresholds->{overload}->{short_overload_threshold}) {
            $overload_count++;
        }
        
        $result->{peak_power} = $power if $power > $result->{peak_power};
    }
    
    $result->{avg_power} = $sum_power / $total_points;
    
    # 计算功率波动水平(标准差/平均值)
    if ($total_points > 1) {
        my $variance = sum(map { ($_ - $result->{avg_power}) ** 2 } @all_powers) / $total_points;
        my $std_dev = sqrt($variance);
        $result->{fluctuation_level} = $std_dev / $result->{avg_power};
    }
    
    # 如果大部分历史点都是过载,则认为是持续过载
    if ($overload_count / $total_points > $thresholds->{historical}->{persistent_ratio}) {
        $result->{persistent_overload} = 1;
        $result->{overload_duration} = int($total_points * $thresholds->{historical}->{time_interval});
    }
    
    return $result;
}

# 断路故障检测 - 增强版,使用遥信数据
sub check_open_circuit {
    my ($data, $telemetry_data) = @_;
    
    # 基本断路故障判断逻辑
    my $basic_fault = check_basic_open_circuit($data);
    if ($basic_fault) {
        # 如果有遥信数据,可以更准确判断断路设备类型
        if ($telemetry_data) {
            # 检查开关状态信息
            if ($telemetry_data->{switch_status}) {
                if ($telemetry_data->{switch_status} eq "拒动" || 
                    $telemetry_data->{switch_status} eq "操作失败") {
                    $basic_fault->{subtype} = "开关拒动";
                    $basic_fault->{code} = "OC03";
                    $basic_fault->{confidence} = 0.98;
                    $basic_fault->{device_info} = $telemetry_data->{device_name} || "未知开关设备";
                }
            }
            
            # 检查熔断器状态
            if ($telemetry_data->{fuse_status} && $telemetry_data->{fuse_status} eq "熔断") {
                $basic_fault->{subtype} = "熔断器熔断";
                $basic_fault->{code} = "OC04";
                $basic_fault->{confidence} = 0.98;
                $basic_fault->{device_info} = $telemetry_data->{fuse_name} || "未知熔断器";
            }
            
            # 检查线路状态
            if ($telemetry_data->{line_status} && $telemetry_data->{line_status} eq "断线告警") {
                $basic_fault->{subtype} = "导线断线";
                $basic_fault->{code} = "OC01";
                $basic_fault->{confidence} = 0.98;
                $basic_fault->{device_info} = $telemetry_data->{line_name} || "未知线路";
            }
            
            # 检查接头状态
            if ($telemetry_data->{connector_status} && $telemetry_data->{connector_status} eq "接头松动") {
                $basic_fault->{subtype} = "接头断开";
                $basic_fault->{code} = "OC02";
                $basic_fault->{confidence} = 0.95;
                $basic_fault->{device_info} = $telemetry_data->{connector_name} || "未知接头";
            }
        }
    }
    
    return $basic_fault;
}

# 基本断路故障检测逻辑
sub check_basic_open_circuit {
    my ($data) = @_;
    
    # 导线断线:某相电流为零,电压正常或稍高
    if (($data->{IA_Current} < $thresholds->{open_circuit}->{zero_current_threshold} && 
         $data->{UA_Voltage} >= $thresholds->{voltage_loss}->{normal_voltage_min}) ||
        ($data->{IB_Current} < $thresholds->{open_circuit}->{zero_current_threshold} && 
         $data->{UB_Voltage} >= $thresholds->{voltage_loss}->{normal_voltage_min}) ||
        ($data->{IC_Current} < $thresholds->{open_circuit}->{zero_current_threshold} && 
         $data->{UC_Voltage} >= $thresholds->{voltage_loss}->{normal_voltage_min})) {
        return { status => "故障", category => "断路故障", subtype => "导线断线", code => "OC01", confidence => 0.85 };
    }
    
    # 接头断开:某相电流极低,电压略有波动
    if (($data->{IA_Current} < $thresholds->{open_circuit}->{very_low_current_threshold} && 
         $data->{IA_Current} >= $thresholds->{open_circuit}->{zero_current_threshold}) ||
        ($data->{IB_Current} < $thresholds->{open_circuit}->{very_low_current_threshold} && 
         $data->{IB_Current} >= $thresholds->{open_circuit}->{zero_current_threshold}) ||
        ($data->{IC_Current} < $thresholds->{open_circuit}->{very_low_current_threshold} && 
         $data->{IC_Current} >= $thresholds->{open_circuit}->{zero_current_threshold})) {
        return { status => "故障", category => "断路故障", subtype => "接头断开", code => "OC02", confidence => 0.8 };
    }
    
    # 开关拒动:某一相或多相电流突然为零,但电压正常
    if (($data->{IA_Current} < $thresholds->{open_circuit}->{zero_current_threshold} ||
         $data->{IB_Current} < $thresholds->{open_circuit}->{zero_current_threshold} ||
         $data->{IC_Current} < $thresholds->{open_circuit}->{zero_current_threshold}) &&
        $data->{UAB_Voltage} >= $thresholds->{voltage_loss}->{normal_voltage_min} &&
        $data->{UBC_Voltage} >= $thresholds->{voltage_loss}->{normal_voltage_min} &&
        $data->{UCA_Voltage} >= $thresholds->{voltage_loss}->{normal_voltage_min}) {
        return { status => "故障", category => "断路故障", subtype => "开关拒动", code => "OC03", confidence => 0.8 };
    }
    
    # 熔断器熔断:某相电流为零,对应相电压为零或极低
    if (($data->{IA_Current} < $thresholds->{open_circuit}->{zero_current_threshold} && 
         $data->{UA_Voltage} < $thresholds->{open_circuit}->{fuse_voltage_threshold}) ||
        ($data->{IB_Current} < $thresholds->{open_circuit}->{zero_current_threshold} && 
         $data->{UB_Voltage} < $thresholds->{open_circuit}->{fuse_voltage_threshold}) ||
        ($data->{IC_Current} < $thresholds->{open_circuit}->{zero_current_threshold} && 
         $data->{UC_Voltage} < $thresholds->{open_circuit}->{fuse_voltage_threshold})) {
        return { status => "故障", category => "断路故障", subtype => "熔断器熔断", code => "OC04", confidence => 0.85 };
    }
    
    # 机械性断路:三相电流都为零,三相电压正常或稍高
    if ($data->{IA_Current} < $thresholds->{open_circuit}->{zero_current_threshold} &&
        $data->{IB_Current} < $thresholds->{open_circuit}->{zero_current_threshold} &&
        $data->{IC_Current} < $thresholds->{open_circuit}->{zero_current_threshold} &&
        $data->{UA_Voltage} >= $thresholds->{voltage_loss}->{normal_voltage_min} &&
        $data->{UB_Voltage} >= $thresholds->{voltage_loss}->{normal_voltage_min} &&
        $data->{UC_Voltage} >= $thresholds->{voltage_loss}->{normal_voltage_min}) {
        return { status => "故障", category => "断路故障", subtype => "机械性断路", code => "OC05", confidence => 0.9 };
    }
    
    return undef; # 没有断路故障
}

# 创建默认阈值配置文件
sub create_default_thresholds {
    my $default_thresholds = {
        # 短路故障阈值
        short_circuit => {
            high_current_threshold => 150.0,       # 高电流阈值,用于判断短路
            very_high_current_threshold => 300.0,  # 极高电流阈值,用于判断金属性短路
            low_voltage_threshold => 190.0,        # 低电压阈值,用于判断短路
            very_low_voltage_threshold => 50.0,    # 极低电压阈值,用于判断金属性短路
        },
        
        # 接地故障阈值
        ground_fault => {
            very_low_zero_sequence_current => 2.0,    # 极低零序电流阈值
            low_zero_sequence_current => 5.0,         # 低零序电流阈值
            medium_zero_sequence_current => 10.0,     # 中等零序电流阈值
            high_zero_sequence_current => 20.0,       # 高零序电流阈值
            low_phase_voltage => 180.0,               # 低相电压阈值
            voltage_balance_threshold => 10.0,        # 电压平衡阈值
            neutral_grounding_voltage => 220.0,       # 中性点接地电压阈值
        },
        
        # 失压故障阈值
        voltage_loss => {
            very_low_voltage => 50.0,           # 极低电压阈值
            low_voltage => 170.0,               # 低电压阈值
            under_voltage => 195.0,             # 欠电压阈值
            normal_voltage_min => 210.0,        # 正常电压最小值
            normal_voltage_max => 240.0,        # 正常电压最大值
            imbalance_threshold => 0.1,         # 电压不平衡阈值(偏差百分比)
        },
        
        # 过载故障阈值
        overload => {
            total_power_threshold => 1000.0,           # 总功率阈值(持续过载)
            short_overload_threshold => 800.0,         # 短时过载阈值
            phase_current_threshold => 100.0,          # 相电流阈值
            very_high_current_threshold => 200.0,      # 极高电流阈值
            low_power_factor => 0.85,                  # 低功率因数阈值
        },
        
        # 断路故障阈值
        open_circuit => {
            zero_current_threshold => 0.5,             # 零电流阈值
            very_low_current_threshold => 5.0,         # 极低电流阈值
            fuse_voltage_threshold => 30.0,            # 熔断器熔断电压阈值
        },
        
        # 波形分析阈值
        waveform => {
            high_distortion_threshold => 0.3,           # 高失真度阈值
            high_current_peak => 200.0,                 # 高电流峰值阈值
            very_high_current_peak => 400.0,            # 极高电流峰值阈值
            very_low_voltage_valley => 30.0,            # 极低电压谷值阈值
            high_zero_current_threshold => 25.0,        # 高零序电流阈值
            medium_zero_current_threshold => 12.0,      # 中等零序电流阈值
            low_phase_voltage_threshold => 150.0,       # 低相电压阈值
            stable_current_range => 10.0,               # 稳定电流范围阈值
            intermittent_peaks_threshold => 3,          # 间歇性峰值数量阈值
        },
        
        # 历史数据分析阈值
        historical => {
            persistent_ratio => 0.7,                   # 持续故障比例阈值
            time_interval => 5,                        # 时间间隔(分钟)
        }
    };
    
    return $default_thresholds;
}

# 创建默认配置文件
sub write_default_config {
    my ($file_path) = @_;
    
    my $default_thresholds = create_default_thresholds();
    my $json = encode_json($default_thresholds);
    
    open my $fh, '>', $file_path or die "无法创建配置文件 $file_path: $!";
    print $fh $json;
    close $fh;
    
    print "已创建默认配置文件: $file_path\n";
}

# 如果指定配置文件不存在,则创建默认配置
sub init_config {
    my ($config_file) = @_;
    
    unless (-e $config_file) {
        write_default_config($config_file);
    }
}

1; # 模块必须返回真值

更新后的配置文件(fault_thresholds.json):

{
    "short_circuit": {
        "high_current_threshold": 150.0,
        "very_high_current_threshold": 300.0,
        "low_voltage_threshold": 190.0,
        "very_low_voltage_threshold": 50.0
    },
    "ground_fault": {
        "very_low_zero_sequence_current": 2.0,
        "low_zero_sequence_current": 5.0,
        "medium_zero_sequence_current": 10.0,
        "high_zero_sequence_current": 20.0,
        "low_phase_voltage": 180.0,
        "voltage_balance_threshold": 10.0,
        "neutral_grounding_voltage": 220.0
    },
    "voltage_loss": {
        "very_low_voltage": 50.0,
        "low_voltage": 170.0,
        "under_voltage": 195.0,
        "normal_voltage_min": 210.0,
        "normal_voltage_max": 240.0,
        "imbalance_threshold": 0.1
    },
    "overload": {
        "total_power_threshold": 1000.0,
        "short_overload_threshold": 800.0,
        "phase_current_threshold": 100.0,
        "very_high_current_threshold": 200.0,
        "low_power_factor": 0.85
    },
    "open_circuit": {
        "zero_current_threshold": 0.5,
        "very_low_current_threshold": 5.0,
        "fuse_voltage_threshold": 30.0
    },
    "waveform": {
        "high_distortion_threshold": 0.3,
        "high_current_peak": 200.0,
        "very_high_current_peak": 400.0,
        "very_low_voltage_valley": 30.0,
        "high_zero_current_threshold": 25.0,
        "medium_zero_current_threshold": 12.0,
        "low_phase_voltage_threshold": 150.0,
        "stable_current_range": 10.0,
        "intermittent_peaks_threshold": 3
    },
    "historical": {
        "persistent_ratio": 0.7,
        "time_interval": 5
    }
}

使用示例(不需要包含在模块中):

#!/usr/bin/perl

use strict;
use warnings;
use FaultClassifier qw(classify_fault load_thresholds);
use JSON;

# 加载阈值
load_thresholds('/path/to/fault_thresholds.json');

# 示例实时数据(两相短路故障)
my $current_data = {
    UAB_Voltage => 150.5,  # 低于正常值
    UBC_Voltage => 381.2,
    UCA_Voltage => 379.8,
    UA_Voltage => 120.3,   # A相电压低
    UB_Voltage => 150.8,   # B相电压低
    UC_Voltage => 221.2,
    
    IA_Current => 250.2,   # A相电流高
    IB_Current => 240.8,   # B相电流高
    IC_Current => 51.1,
    I0_Current => 5.2,
    
    ActivePower_Total => 500.5,
    ActivePower_A => 200.7,
    ActivePower_B => 250.5,
    ActivePower_C => 49.3,
    ReactivePower_Total => 200.2,
    ReactivePower_A => 60.8,
    ReactivePower_B => 110.5,
    ReactivePower_C => 28.9,
    ApparentPower_Total => 530.8,
    ApparentPower_A => 209.1,
    ApparentPower_B => 273.2,
    ApparentPower_C => 48.5
};

# 示例遥信数据
my $telemetry_data = {
    protection_status => "动作",  # 保护装置已动作
    switch_status => "正常",
    fuse_status => "正常",
    line_status => "正常",
    device_name => "10kV配网开关柜#3",
    line_name => "配电线路#2501"
};

# 示例历史数据
my $historical_data = {
    voltage => [
        # 过去1小时的电压数据,每5分钟一个点
        {
            timestamp => "2025-02-10 08:00:00",
            UA_Voltage => 220.5,
            UB_Voltage => 221.2,
            UC_Voltage => 219.8
        },
        {
            timestamp => "2025-02-10 08:05:00",
            UA_Voltage => 219.3,
            UB_Voltage => 220.1,
            UC_Voltage => 220.5
        },
        # ...更多历史点
    ],
    power => [
        # 过去1小时的功率数据
        {
            timestamp => "2025-02-10 08:00:00",
            ApparentPower_Total => 300.5
        },
        {
            timestamp => "2025-02-10 08:05:00",
            ApparentPower_Total => 310.2
        },
        # ...更多历史点
    ]
};

# 示例录波数据
my $waveform_data = {
    timestamp => "2025-02-10 09:00:00",
    # 电流波形数据(简化为采样点数组)
    current_waveform => {
        A => [10, 50, 120, 250, 320, 250, 120, 50, 10, -50, -120, -250, -320, -250, -120, -50, 10],
        B => [15, 55, 125, 240, 310, 240, 125, 55, 15, -55, -125, -240, -310, -240, -125, -55, 15],
        C => [5, 25, 45, 51, 45, 25, 5, -25, -45, -51, -45, -25, -5, 25, 45, 51, 45]
    },
    # 电压波形数据
    voltage_waveform => {
        A => [50, 80, 100, 120, 100, 80, 50, 0, -50, -80, -100, -120, -100, -80, -50, 0, 50],
        B => [60, 90, 110, 150, 110, 90, 60, 0, -60, -90, -110, -150, -110, -90, -60, 0, 60],
        C => [180, 200, 210, 220, 210, 200, 180, 0, -180, -200, -210, -220, -210, -200, -180, 0, 180],
        AB => [120, 150, 140, 150, 140, 150, 120, 0, -120, -150, -140, -150, -140, -150, -120, 0, 120],
        BC => [230, 280, 320, 380, 320, 280, 230, 0, -230, -280, -320, -380, -320, -280, -230, 0, 230],
        CA => [230, 280, 320, 380, 320, 280, 230, 0, -230, -280, -320, -380, -320, -280, -230, 0, 230]
    },
    # 零序电流波形
    zero_sequence_current => [1, 2, 4, 5, 4, 2, 1, 0, -1, -2, -4, -5, -4, -2, -1, 0, 1]
};

# 故障分类
my $fault_result = classify_fault($current_data, $telemetry_data, $historical_data, $waveform_data);

# 输出结果
print "故障分类结果:\n";
print encode_json($fault_result);

增强功能解释:

  1. 遥信数据的使用

    • 结合开关状态、熔断器状态、线路状态等遥信信息,实现了更精确的故障类型判断
    • 可以识别具体的故障设备,如"10kV配网开关柜#3"
    • 根据保护装置的动作情况,增加故障判断的可信度
  2. 历史数据的使用

    • 可以区分短时故障和持续性故障
    • 通过电压波动趋势分析,更准确地判断失压类型
    • 通过功率趋势分析,区分短时过载和持续过载
    • 提供故障持续时间的估计
  3. 录波数据的使用

    • 通过分析电流电压波形特征,区分不同类型的短路故障
    • 特别是区分金属性短路和电弧性短路
    • 提高接地故障分类的准确性,特别是间歇性接地故障
    • 计算波形失真度,作为故障特征的重要依据
  4. 可信度评估

    • 每个故障诊断结果都包含可信度评分
    • 多种数据源共同佐证的故障类型,可信度更高
    • 有助于后续决策和故障处理的优先级排序

这个增强版的Perl模块可以大幅提高故障分类的准确性,特别是在复杂的电网故障情况下。通过综合多种数据源,可以更准确地判断故障类型,并提供有价值的故障详情,帮助运维人员更快速、更有针对性地处理故障。