|
| 1 | +import { LitElement, property } from 'lit-element'; |
| 2 | + |
| 3 | +import { stringify } from 'csv-stringify/browser/esm/sync'; |
| 4 | + |
| 5 | +import { compareNames } from '../foundation.js'; |
| 6 | + |
| 7 | +import { stripExtensionFromName } from '../compas/foundation.js'; |
| 8 | + |
| 9 | +import settings from '../../public/conf/export-ied-parameters.json'; |
| 10 | + |
| 11 | +function getDataElement(typeElement: Element, name: string): Element | null { |
| 12 | + if (typeElement.tagName === 'LNodeType') { |
| 13 | + return typeElement.querySelector(`:scope > DO[name="${name}"]`); |
| 14 | + } else if (typeElement.tagName === 'DOType') { |
| 15 | + return typeElement.querySelector( |
| 16 | + `:scope > SDO[name="${name}"], :scope > DA[name="${name}"]` |
| 17 | + ); |
| 18 | + } else { |
| 19 | + return typeElement.querySelector(`:scope > BDA[name="${name}"]`); |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +function getValue(element: Element, attributeName: string | undefined): string { |
| 24 | + if (attributeName) { |
| 25 | + return element.getAttribute(attributeName) ?? ''; |
| 26 | + } |
| 27 | + return element.textContent ?? ''; |
| 28 | +} |
| 29 | + |
| 30 | +function getSelector(selector: string, iedName: string) { |
| 31 | + return selector.replace('{{ iedName }}', iedName); |
| 32 | +} |
| 33 | + |
| 34 | +function getElements( |
| 35 | + iedElement: Element, |
| 36 | + selector: string | undefined, |
| 37 | + useOwnerDocument: boolean |
| 38 | +): Element[] { |
| 39 | + let elements: Element[] = [iedElement]; |
| 40 | + if (selector) { |
| 41 | + const iedName = iedElement.getAttribute('name') ?? ''; |
| 42 | + const substitutedSelector = getSelector(selector, iedName); |
| 43 | + if (useOwnerDocument) { |
| 44 | + elements = Array.from( |
| 45 | + iedElement.ownerDocument.querySelectorAll(substitutedSelector) |
| 46 | + ); |
| 47 | + } else { |
| 48 | + elements = Array.from(iedElement.querySelectorAll(substitutedSelector)); |
| 49 | + } |
| 50 | + } |
| 51 | + return elements; |
| 52 | +} |
| 53 | + |
| 54 | +export default class ExportIEDParametersPlugin extends LitElement { |
| 55 | + @property() doc!: XMLDocument; |
| 56 | + @property() docName!: string; |
| 57 | + |
| 58 | + private getTypeElement(lastElement: Element | null): Element | null { |
| 59 | + if (lastElement) { |
| 60 | + if (['DO', 'SDO'].includes(lastElement.tagName)) { |
| 61 | + const type = lastElement.getAttribute('type') ?? ''; |
| 62 | + return this.doc.querySelector(`DOType[id="${type}"]`); |
| 63 | + } else { |
| 64 | + const bType = lastElement.getAttribute('bType') ?? ''; |
| 65 | + if (bType === 'Struct') { |
| 66 | + const type = lastElement.getAttribute('type') ?? ''; |
| 67 | + return this.doc.querySelector(`DAType[id="${type}"]`); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + return null; |
| 72 | + } |
| 73 | + |
| 74 | + private getDataAttributeTemplateValue( |
| 75 | + element: Element, |
| 76 | + dataAttributePath: string[] |
| 77 | + ): string | null { |
| 78 | + // This is only useful if the element to start from is the LN(0) Element. |
| 79 | + if (['LN', 'LN0'].includes(element.tagName)) { |
| 80 | + // Search LNodeType Element that is linked to the LN(0) Element. |
| 81 | + const type = element.getAttribute('lnType'); |
| 82 | + let typeElement = this.doc.querySelector(`LNodeType[id="${type}"]`); |
| 83 | + let lastElement: Element | null = null; |
| 84 | + |
| 85 | + // Now start search through the Template section jumping between the type elements. |
| 86 | + dataAttributePath.forEach(name => { |
| 87 | + if (typeElement) { |
| 88 | + lastElement = getDataElement(typeElement, name); |
| 89 | + typeElement = this.getTypeElement(lastElement); |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | + if (lastElement) { |
| 94 | + const valElement = (<Element>lastElement).querySelector('Val'); |
| 95 | + return valElement?.textContent ?? null; |
| 96 | + } |
| 97 | + } |
| 98 | + return null; |
| 99 | + } |
| 100 | + |
| 101 | + private getDataAttributeInstanceValue( |
| 102 | + element: Element, |
| 103 | + dataAttributePath: string[] |
| 104 | + ): string | null { |
| 105 | + const daiSelector = dataAttributePath |
| 106 | + .slice() |
| 107 | + .reverse() |
| 108 | + .map((path, index) => { |
| 109 | + if (index === 0) { |
| 110 | + return `DAI[name="${path}"]`; |
| 111 | + } else if (index === dataAttributePath.length - 1) { |
| 112 | + return `DOI[name="${path}"]`; |
| 113 | + } |
| 114 | + return `SDI[name="${path}"]`; |
| 115 | + }) |
| 116 | + .reverse() |
| 117 | + .join(' > '); |
| 118 | + |
| 119 | + const daiValueElement = element.querySelector(daiSelector + ' Val'); |
| 120 | + if (daiValueElement) { |
| 121 | + return daiValueElement.textContent; |
| 122 | + } |
| 123 | + return null; |
| 124 | + } |
| 125 | + |
| 126 | + private getDataAttributeValue( |
| 127 | + element: Element, |
| 128 | + dataAttributePath: string[] |
| 129 | + ): string { |
| 130 | + let value = this.getDataAttributeInstanceValue(element, dataAttributePath); |
| 131 | + if (!value) { |
| 132 | + value = this.getDataAttributeTemplateValue(element, dataAttributePath); |
| 133 | + } |
| 134 | + return value ?? ''; |
| 135 | + } |
| 136 | + |
| 137 | + private content(): string[][] { |
| 138 | + return Array.from(this.doc.querySelectorAll(`IED`)) |
| 139 | + .sort(compareNames) |
| 140 | + .map(iedElement => { |
| 141 | + return settings.columns.map(value => { |
| 142 | + const elements = getElements( |
| 143 | + iedElement, |
| 144 | + value.selector, |
| 145 | + value.useOwnerDocument ?? false |
| 146 | + ); |
| 147 | + |
| 148 | + return elements |
| 149 | + .map(element => { |
| 150 | + if (value.dataAttributePath) { |
| 151 | + return this.getDataAttributeValue( |
| 152 | + element, |
| 153 | + value.dataAttributePath |
| 154 | + ); |
| 155 | + } |
| 156 | + return getValue(element, value.attributeName); |
| 157 | + }) |
| 158 | + .filter(value => value!) |
| 159 | + .join(' / '); |
| 160 | + }); |
| 161 | + }); |
| 162 | + } |
| 163 | + |
| 164 | + private columnHeaders(): string[] { |
| 165 | + return settings.columns.map(value => value.header); |
| 166 | + } |
| 167 | + |
| 168 | + async run(): Promise<void> { |
| 169 | + const content = stringify(this.content(), { |
| 170 | + header: true, |
| 171 | + columns: this.columnHeaders(), |
| 172 | + }); |
| 173 | + const blob = new Blob([content], { |
| 174 | + type: 'text/csv', |
| 175 | + }); |
| 176 | + |
| 177 | + const a = document.createElement('a'); |
| 178 | + a.download = stripExtensionFromName(this.docName) + '-ied-parameters.csv'; |
| 179 | + a.href = URL.createObjectURL(blob); |
| 180 | + a.dataset.downloadurl = ['text/csv', a.download, a.href].join(':'); |
| 181 | + a.style.display = 'none'; |
| 182 | + document.body.appendChild(a); |
| 183 | + a.click(); |
| 184 | + document.body.removeChild(a); |
| 185 | + setTimeout(function () { |
| 186 | + URL.revokeObjectURL(a.href); |
| 187 | + }, 5000); |
| 188 | + } |
| 189 | +} |
0 commit comments