-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathfoundation.ts
277 lines (253 loc) · 8.89 KB
/
foundation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import { getSclSchemaVersion } from '@openscd/open-scd/src/foundation.js';
import { serviceTypes } from '../foundation.js';
function dataAttributeSpecification(
anyLn: Element,
doName: string,
daName: string
): { cdc: string | null; bType: string | null } {
const doc = anyLn.ownerDocument;
const lNodeType = doc.querySelector(
`LNodeType[id="${anyLn.getAttribute('lnType')}"]`
);
const doNames = doName.split('.');
let leaf: Element | null | undefined = lNodeType;
for (const doName of doNames) {
const dO: Element | null | undefined = leaf?.querySelector(
`DO[name="${doName}"], SDO[name="${doName}"]`
);
leaf = doc.querySelector(`DOType[id="${dO?.getAttribute('type')}"]`);
}
if (!leaf || !leaf.getAttribute('cdc')) return { cdc: null, bType: null };
const cdc = leaf.getAttribute('cdc')!;
const daNames = daName.split('.');
for (const daName of daNames) {
const dA: Element | null | undefined = leaf?.querySelector(
`DA[name="${daName}"], BDA[name="${daName}"]`
);
leaf =
daNames.indexOf(daName) < daNames.length - 1
? doc.querySelector(`DAType[id="${dA?.getAttribute('type')}"]`)
: dA;
}
if (!leaf || !leaf.getAttribute('bType')) return { cdc, bType: null };
const bType = leaf.getAttribute('bType')!;
return { bType, cdc };
}
/**
* @param fcda - Data attribute reference in a data set
* @returns Data objects `CDC` and data attributes `bType`
*/
export function fcdaSpecification(fcda: Element): {
cdc: string | null;
bType: string | null;
} {
const [doName, daName] = ['doName', 'daName'].map(attr =>
fcda.getAttribute(attr)
);
if (!doName || !daName) return { cdc: null, bType: null };
const ied = fcda.closest('IED');
const anyLn = Array.from(
ied?.querySelectorAll(
`LDevice[inst="${fcda.getAttribute(
'ldInst'
)}"] > LN, LDevice[inst="${fcda.getAttribute('inst')}"] LN0`
) ?? []
).find(anyLn => {
return (
(anyLn.getAttribute('prefix') ?? '') ===
(fcda.getAttribute('prefix') ?? '') &&
(anyLn.getAttribute('lnClass') ?? '') ===
(fcda.getAttribute('lnClass') ?? '') &&
(anyLn.getAttribute('inst') ?? '') === (fcda.getAttribute('lnInst') ?? '')
);
});
if (!anyLn) return { cdc: null, bType: null };
return dataAttributeSpecification(anyLn, doName, daName);
}
/**
* Edition 2 and later SCL files allow to restrict subscription on
* later binding type inputs (`ExtRef` elements) based on a `CDC` and
* basic type `bType`.
* @param extRef - A later binding type input in the sink IED
* @returns data objects `CDC` and data attribute basic type `bType` or `null`
*/
export function inputRestriction(extRef: Element): {
cdc: string | null;
bType: string | null;
} {
const [pLN, pDO, pDA] = ['pLN', 'pDO', 'pDA'].map(attr =>
extRef.getAttribute(attr)
);
if (!pLN || !pDO || !pDA) return { cdc: null, bType: null };
const anyLns = Array.from(
extRef
.closest('IED')
?.querySelectorAll(`LN[lnClass="${pLN}"],LN0[lnClass="${pLN}"]`) ?? []
);
for (const anyLn of anyLns) {
const dataSpec = dataAttributeSpecification(anyLn, pDO, pDA);
if (dataSpec.cdc !== null && dataSpec.bType !== null) return dataSpec;
}
return { cdc: null, bType: null };
}
/**
* Simple function to check if the attribute of the Left Side has the same value as the attribute of the Right Element.
*
* @param leftElement - The Left Element to check against.
* @param rightElement - The Right Element to check.
* @param attributeName - The name of the attribute to check.
*/
export function sameAttributeValue(
leftElement: Element | undefined,
rightElement: Element | undefined,
attributeName: string
): boolean {
return (
(leftElement?.getAttribute(attributeName) ?? '') ===
(rightElement?.getAttribute(attributeName) ?? '')
);
}
/**
* Simple function to check if the attribute of the Left Side has the same value as the attribute of the Right Element.
*
* @param leftElement - The Left Element to check against.
* @param leftAttributeName - The name of the attribute (left) to check against.
* @param rightElement - The Right Element to check.
* @param rightAttributeName - The name of the attribute (right) to check.
*/
export function sameAttributeValueDiffName(
leftElement: Element | undefined,
leftAttributeName: string,
rightElement: Element | undefined,
rightAttributeName: string
): boolean {
return (
(leftElement?.getAttribute(leftAttributeName) ?? '') ===
(rightElement?.getAttribute(rightAttributeName) ?? '')
);
}
/**
* If needed check version specific attributes against FCDA Element.
*
* @param controlTag - Indicates which type of control element.
* @param controlElement - The Control Element to check against.
* @param extRefElement - The Ext Ref Element to check.
*/
function checkEditionSpecificRequirements(
controlTag: 'SampledValueControl' | 'GSEControl',
controlElement: Element | undefined,
extRefElement: Element
): boolean {
// For 2003 Edition no extra check needed.
if (getSclSchemaVersion(extRefElement.ownerDocument) === '2003') {
return true;
}
const lDeviceElement = controlElement?.closest('LDevice') ?? undefined;
const lnElement = controlElement?.closest('LN0') ?? undefined;
// If ExtRef is missing 'srcLNClass', it defaults to 'LLN0' as specified in the standard
const extRefIsMissingSrcLNClass = !extRefElement.hasAttribute('srcLNClass');
const isLnClassLLN0 = lnElement?.getAttribute('lnClass') === 'LLN0';
const canIgnoreSrcLNClass = isLnClassLLN0 && extRefIsMissingSrcLNClass;
// For the 2007B and 2007B4 Edition we need to check some extra attributes.
return (
(extRefElement.getAttribute('serviceType') ?? '') ===
serviceTypes[controlTag] &&
sameAttributeValueDiffName(
extRefElement,
'srcLDInst',
lDeviceElement,
'inst'
) &&
sameAttributeValueDiffName(
extRefElement,
'scrPrefix',
lnElement,
'prefix'
) &&
(canIgnoreSrcLNClass || sameAttributeValueDiffName(
extRefElement,
'srcLNClass',
lnElement,
'lnClass'
)) &&
sameAttributeValueDiffName(extRefElement, 'srcLNInst', lnElement, 'inst') &&
sameAttributeValueDiffName(
extRefElement,
'srcCBName',
controlElement,
'name'
)
);
}
/**
* Check if specific attributes from the ExtRef Element are the same as the ones from the FCDA Element
* and also if the IED Name is the same. If that is the case this ExtRef subscribes to the selected FCDA
* Element.
*
* @param controlTag - Indicates which type of control element.
* @param controlElement - The Control Element to check against.
* @param fcdaElement - The FCDA Element to check against.
* @param extRefElement - The Ext Ref Element to check.
*/
export function isSubscribedTo(
controlTag: 'SampledValueControl' | 'GSEControl',
controlElement: Element | undefined,
fcdaElement: Element | undefined,
extRefElement: Element
): boolean {
return (
extRefElement.getAttribute('iedName') ===
fcdaElement?.closest('IED')?.getAttribute('name') &&
sameAttributeValue(fcdaElement, extRefElement, 'ldInst') &&
sameAttributeValue(fcdaElement, extRefElement, 'prefix') &&
sameAttributeValue(fcdaElement, extRefElement, 'lnClass') &&
sameAttributeValue(fcdaElement, extRefElement, 'lnInst') &&
sameAttributeValue(fcdaElement, extRefElement, 'doName') &&
sameAttributeValue(fcdaElement, extRefElement, 'daName') &&
checkEditionSpecificRequirements(controlTag, controlElement, extRefElement)
);
}
/**
* Check if the ExtRef is already subscribed to a FCDA Element.
*
* @param extRefElement - The Ext Ref Element to check.
*/
export function isSubscribed(extRefElement: Element): boolean {
return (
extRefElement.hasAttribute('iedName') &&
extRefElement.hasAttribute('ldInst') &&
extRefElement.hasAttribute('prefix') &&
extRefElement.hasAttribute('lnClass') &&
extRefElement.hasAttribute('lnInst') &&
extRefElement.hasAttribute('doName') &&
extRefElement.hasAttribute('daName')
);
}
export function getExtRefElements(
rootElement: Element,
fcdaElement: Element | undefined,
includeLaterBinding: boolean
): Element[] {
return Array.from(rootElement.querySelectorAll('ExtRef'))
.filter(
element =>
(includeLaterBinding && element.hasAttribute('intAddr')) ||
(!includeLaterBinding && !element.hasAttribute('intAddr'))
)
.filter(element => element.closest('IED') !== fcdaElement?.closest('IED'));
}
export function getSubscribedExtRefElements(
rootElement: Element,
controlTag: 'SampledValueControl' | 'GSEControl',
fcdaElement: Element | undefined,
controlElement: Element | undefined,
includeLaterBinding: boolean
): Element[] {
return getExtRefElements(
rootElement,
fcdaElement,
includeLaterBinding
).filter(extRefElement =>
isSubscribedTo(controlTag, controlElement, fcdaElement, extRefElement)
);
}