public static void AddNewPhasor(this TableOperations <Phasor> phasorTable, Phasor phasor) =>
 phasorTable.AddNewRecord(phasor);
        private void SaveDevicePhasors(IConfigurationCell cell, Device device, TableOperations <Measurement> measurementTable, ScanParameters scanParams)
        {
            bool phaseMatchExact(string phaseLabel, string[] phaseMatches) =>
            phaseMatches.Any(match => phaseLabel.Equals(match, StringComparison.Ordinal));

            bool phaseEndsWith(string phaseLabel, string[] phaseMatches, bool ignoreCase) =>
            phaseMatches.Any(match => phaseLabel.EndsWith(match, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal));

            bool phaseStartsWith(string phaseLabel, string[] phaseMatches, bool ignoreCase) =>
            phaseMatches.Any(match => phaseLabel.StartsWith(match, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal));

            bool phaseContains(string phaseLabel, string[] phaseMatches, bool ignoreCase) =>
            phaseMatches.Any(match => phaseLabel.IndexOf(match, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal) > -1);

            bool phaseMatchHighConfidence(string phaseLabel, string[] containsMatches, string[] endsWithMatches)
            {
                if (phaseEndsWith(phaseLabel, containsMatches, true))
                {
                    return(true);
                }

                if (phaseStartsWith(phaseLabel, containsMatches, true))
                {
                    return(true);
                }

                foreach (string match in containsMatches.Concat(endsWithMatches))
                {
                    string[] variations = { $" {match}", $"_{match}", $"-{match}", $".{match}" };

                    if (phaseEndsWith(phaseLabel, variations, false))
                    {
                        return(true);
                    }
                }

                foreach (string match in containsMatches)
                {
                    string[] variations = { $" {match} ", $"_{match}_", $"-{match}-", $"-{match}_", $"_{match}-", $".{match}." };

                    if (phaseContains(phaseLabel, variations, false))
                    {
                        return(true);
                    }
                }

                return(false);
            }

            char guessPhase(char phase, string phasorLabel)
            {
                if (phaseMatchExact(phasorLabel, new[] { "V1PM", "I1PM" }) || phaseMatchHighConfidence(phasorLabel, new[] { "V1", "VP", "I1", "IP", "VSEQ1", "ISEQ1" }, new[] { "POS", "V1PM", "I1PM", "PS", "PSV", "PSI" }) || phaseEndsWith(phasorLabel, new[] { "+SV", "+SI", "+V", "+I" }, true))
                {
                    return('+');
                }

                if (phaseMatchExact(phasorLabel, new[] { "V0PM", "I0PM", "VZPM", "IZPM" }) || phaseMatchHighConfidence(phasorLabel, new[] { "V0", "I0", "VSEQ0", "ISEQ0" }, new[] { "ZERO", "ZPV", "ZPI", "VSPM", "V0PM", "I0PM", "VZPM", "IZPM", "ZS", "ZSV", "ZSI" }) || phaseEndsWith(phasorLabel, new[] { "0SV", "0SI" }, true))
                {
                    return('0');
                }

                if (phaseMatchExact(phasorLabel, new[] { "VAPM", "IAPM" }) || phaseMatchHighConfidence(phasorLabel, new[] { "VA", "IA" }, new[] { "APV", "API", "VAPM", "IAPM", "AV", "AI" }))
                {
                    return('A');
                }

                if (phaseMatchExact(phasorLabel, new[] { "VBPM", "IBPM" }) || phaseMatchHighConfidence(phasorLabel, new[] { "VB", "IB" }, new[] { "BPV", "BPI", "VBPM", "IBPM", "BV", "BI" }))
                {
                    return('B');
                }

                if (phaseMatchExact(phasorLabel, new[] { "VCPM", "ICPM" }) || phaseMatchHighConfidence(phasorLabel, new[] { "VC", "IC" }, new[] { "CPV", "CPI", "VCPM", "ICPM", "CV", "CI" }))
                {
                    return('C');
                }

                if (phaseMatchExact(phasorLabel, new[] { "VNPM", "INPM" }) || phaseMatchHighConfidence(phasorLabel, new[] { "VN", "IN" }, new[] { "NEUT", "NPV", "NPI", "VNPM", "INPM", "NV", "NI" }))
                {
                    return('N');
                }

                if (phaseMatchExact(phasorLabel, new[] { "V2PM", "I2PM" }) || phaseMatchHighConfidence(phasorLabel, new[] { "V2", "I2", "VSEQ2", "ISEQ2" }, new[] { "NEG", "-SV", "-SI", "V2PM", "I2PM", "NS", "NSV", "NSI" }))
                {
                    return('-');
                }

                return(phase);
            }

            int guessBaseKV(int baseKV, string phasorLabel, string deviceLabel)
            {
                // Check phasor label before device
                foreach (string voltageLevel in s_commonVoltageLevels)
                {
                    if (phasorLabel.IndexOf(voltageLevel, StringComparison.Ordinal) > -1)
                    {
                        return(int.Parse(voltageLevel));
                    }
                }

                foreach (string voltageLevel in s_commonVoltageLevels)
                {
                    if (deviceLabel.IndexOf(voltageLevel, StringComparison.Ordinal) > -1)
                    {
                        return(int.Parse(voltageLevel));
                    }
                }

                return(baseKV);
            }

            AdoDataConnection        connection  = scanParams.Connection;
            TableOperations <Phasor> phasorTable = new(connection);

            // Get phasor signal types
            SignalType iphmSignalType = m_phasorSignalTypes["IPHM"];
            SignalType iphaSignalType = m_phasorSignalTypes["IPHA"];
            SignalType vphmSignalType = m_phasorSignalTypes["VPHM"];
            SignalType vphaSignalType = m_phasorSignalTypes["VPHA"];

            Phasor[] phasors = phasorTable.QueryPhasorsForDevice(device.ID).ToArray();

            bool dropAndAdd = phasors.Length != cell.PhasorDefinitions.Count;

            if (!dropAndAdd)
            {
                // Also do add operation if phasor source index records are not sequential
                if (phasors.Where((phasor, index) => phasor.SourceIndex != index + 1).Any())
                {
                    dropAndAdd = true;
                }
            }

            if (dropAndAdd)
            {
                if (cell.PhasorDefinitions.Count > 0)
                {
                    connection.DeletePhasorsForDevice(device.ID);
                }

                foreach (IPhasorDefinition phasorDefinition in cell.PhasorDefinitions)
                {
                    bool isVoltage = phasorDefinition.PhasorType == PhasorType.Voltage;

                    Phasor phasor = phasorTable.NewPhasor();
                    phasor.DeviceID            = device.ID;
                    phasor.Label               = phasorDefinition.Label;
                    phasor.Type                = isVoltage ? 'V' : 'I';
                    phasor.Phase               = guessPhase('+', phasor.Label);
                    phasor.BaseKV              = guessBaseKV(500, phasor.Label, string.IsNullOrWhiteSpace(device.Name) ? device.Acronym ?? "" : device.Name);
                    phasor.DestinationPhasorID = null;
                    phasor.SourceIndex         = phasorDefinition.Index + 1;

                    phasorTable.AddNewPhasor(phasor);
                    SavePhasorMeasurement(isVoltage ? vphmSignalType : iphmSignalType, device, phasorDefinition, phasor.Phase, phasor.SourceIndex, phasor.BaseKV, measurementTable, scanParams);
                    SavePhasorMeasurement(isVoltage ? vphaSignalType : iphaSignalType, device, phasorDefinition, phasor.Phase, phasor.SourceIndex, phasor.BaseKV, measurementTable, scanParams);
                }
            }
            else
            {
                foreach (IPhasorDefinition phasorDefinition in cell.PhasorDefinitions)
                {
                    bool isVoltage = phasorDefinition.PhasorType == PhasorType.Voltage;

                    Phasor phasor = phasorTable.QueryPhasorForDevice(device.ID, phasorDefinition.Index + 1);
                    phasor.DeviceID = device.ID;
                    phasor.Label    = phasorDefinition.Label;
                    phasor.Type     = isVoltage ? 'V' : 'I';

                    phasorTable.AddNewPhasor(phasor);
                    SavePhasorMeasurement(isVoltage ? vphmSignalType : iphmSignalType, device, phasorDefinition, phasor.Phase, phasor.SourceIndex, phasor.BaseKV, measurementTable, scanParams);
                    SavePhasorMeasurement(isVoltage ? vphaSignalType : iphaSignalType, device, phasorDefinition, phasor.Phase, phasor.SourceIndex, phasor.BaseKV, measurementTable, scanParams);
                }
            }
        }