|
| 1 | +import { get } from "lit-translate"; |
| 2 | + |
| 3 | +export const PROTOCOL_104_PRIVATE = 'IEC_60870_5_104'; |
| 4 | + |
| 5 | +export interface Signal104 { |
| 6 | + name: string | null; |
| 7 | + signalNumber: string | null; |
| 8 | + isMonitorSignal: boolean; |
| 9 | + ioa: string | null; |
| 10 | + ti: string | null; |
| 11 | +} |
| 12 | + |
| 13 | +interface ExtractSignal104Result { |
| 14 | + signal: Signal104 | null; |
| 15 | + error?: string; |
| 16 | +} |
| 17 | + |
| 18 | +enum SignalType { |
| 19 | + Monitor, |
| 20 | + Control, |
| 21 | + Unknown |
| 22 | +} |
| 23 | + |
| 24 | +const private104Selector = `Private[type="${PROTOCOL_104_PRIVATE}"]`; |
| 25 | + |
| 26 | +export function extractAllSignal104Data(doc: XMLDocument): { signals: Signal104[], errors: string[] } { |
| 27 | + const signals: Signal104[] = []; |
| 28 | + const errors: string[] = []; |
| 29 | + const address104Elements = doc.querySelectorAll(`${private104Selector} > Address`); |
| 30 | + |
| 31 | + address104Elements.forEach((addressElement) => { |
| 32 | + const signal104Result = extractSignal104Data(addressElement, doc); |
| 33 | + |
| 34 | + if (signal104Result.error) { |
| 35 | + errors.push(signal104Result.error); |
| 36 | + } else { |
| 37 | + signals.push(signal104Result.signal!); |
| 38 | + } |
| 39 | + }); |
| 40 | + |
| 41 | + return { signals, errors }; |
| 42 | +} |
| 43 | + |
| 44 | +function extractSignal104Data(addressElement: Element, doc: XMLDocument): ExtractSignal104Result { |
| 45 | + const ti = addressElement.getAttribute('ti'); |
| 46 | + const ioa = addressElement.getAttribute('ioa'); |
| 47 | + |
| 48 | + // By convention the last four digits of the ioa are the signalnumber, see https://github.com/com-pas/compas-open-scd/issues/334 |
| 49 | + if (ti === null || ioa === null || ioa.length < 4) { |
| 50 | + return { signal: null, error: get('protocol104.export.errors.tiOrIoaInvalid', { ti: ti ?? '', ioa: ioa ?? '' }) }; |
| 51 | + } |
| 52 | + const { signalNumber, bayName } = splitIoa(ioa); |
| 53 | + |
| 54 | + const signalType = getSignalType(ti); |
| 55 | + if (signalType === SignalType.Unknown) { |
| 56 | + return { signal: null, error: get('protocol104.export.errors.unknownSignalType', { ti: ti ?? '', ioa: ioa ?? '' }) }; |
| 57 | + } |
| 58 | + const isMonitorSignal = signalType === SignalType.Monitor; |
| 59 | + |
| 60 | + addressElement.parentElement; |
| 61 | + const parentDOI = addressElement.closest('DOI'); |
| 62 | + |
| 63 | + if (!parentDOI) { |
| 64 | + return { signal: null, error: get('protocol104.export.errors.noDoi', { ioa: ioa ?? '' }) }; |
| 65 | + } |
| 66 | + |
| 67 | + const doiDesc = parentDOI.getAttribute('desc'); |
| 68 | + |
| 69 | + const parentBayQuery = `:root > Substation > VoltageLevel > Bay[name="${bayName}"]`; |
| 70 | + const parentBay = doc.querySelector(parentBayQuery); |
| 71 | + |
| 72 | + if (!parentBay) { |
| 73 | + return { signal: null, error: get('protocol104.export.errors.noBay', { bayName, ioa: ioa ?? '' }) }; |
| 74 | + } |
| 75 | + |
| 76 | + const parentVoltageLevel = parentBay.closest('VoltageLevel'); |
| 77 | + |
| 78 | + if (!parentVoltageLevel) { |
| 79 | + return { signal: null, error: get('protocol104.export.errors.noVoltageLevel', { bayName, ioa: ioa ?? '' }) }; |
| 80 | + } |
| 81 | + |
| 82 | + const voltageLevelName = parentVoltageLevel.getAttribute('name'); |
| 83 | + const parentSubstation = parentVoltageLevel.closest('Substation'); |
| 84 | + |
| 85 | + if (!parentSubstation) { |
| 86 | + return { signal: null, error: get('protocol104.export.errors.noSubstation', { voltageLevelName: voltageLevelName ?? '', ioa: ioa ?? '' }) }; |
| 87 | + } |
| 88 | + |
| 89 | + const substationName = parentSubstation.getAttribute('name'); |
| 90 | + |
| 91 | + const name = `${substationName}${voltageLevelName}${bayName}${doiDesc}`; |
| 92 | + |
| 93 | + return { |
| 94 | + signal: { |
| 95 | + name, |
| 96 | + signalNumber, |
| 97 | + isMonitorSignal, |
| 98 | + ti, |
| 99 | + ioa |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +// For signal classification details see https://github.com/com-pas/compas-open-scd/issues/334 |
| 105 | +function getSignalType(tiString: string): SignalType { |
| 106 | + const ti = parseInt(tiString); |
| 107 | + |
| 108 | + if (isNaN(ti)) { |
| 109 | + return SignalType.Unknown; |
| 110 | + } |
| 111 | + |
| 112 | + if ((ti >= 1 && ti <= 21) || (ti >= 30 && ti <= 40)) { |
| 113 | + return SignalType.Monitor; |
| 114 | + } else if ((ti >= 45 && ti <= 51) || (ti >= 58 && ti <= 64)) { |
| 115 | + return SignalType.Control; |
| 116 | + } else { |
| 117 | + return SignalType.Unknown; |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +// By Alliander convention the last four digits of the ioa are the signalnumber and the rest is the bay number |
| 122 | +// And every bay name consists of "V" + bay number |
| 123 | +function splitIoa(ioa: string): { signalNumber: string, bayName: string } { |
| 124 | + const signalNumber = ioa.slice(-4); |
| 125 | + const bayName = `V${ioa.slice(0, -4)}`; |
| 126 | + |
| 127 | + return { signalNumber, bayName }; |
| 128 | +} |
0 commit comments