|
| 1 | +import { get } from 'lit-translate'; |
| 2 | +import { newLogEvent } from '@compas-oscd/core'; |
| 3 | + |
| 4 | +export type IEC61850Edition = 'Edition_1' | 'Edition_2' | 'Edition_2_1'; |
| 5 | + |
| 6 | +export interface CodeComponentEntry { |
| 7 | + NSD: string; |
| 8 | + NSDOC: string; |
| 9 | +} |
| 10 | + |
| 11 | +export type EditionComponents = Partial<Record<string, CodeComponentEntry>>; |
| 12 | + |
| 13 | +interface CodeComponentsJson { |
| 14 | + IEC61850_code_components: Partial<Record<IEC61850Edition, EditionComponents>>; |
| 15 | +} |
| 16 | + |
| 17 | +const CODE_COMPONENTS_PATH = 'public/xml/IEC61850_code_components.json'; |
| 18 | + |
| 19 | +let cachedComponents: CodeComponentsJson | null = null; |
| 20 | + |
| 21 | +/** |
| 22 | + * Fetches and caches the IEC61850_code_components.json file. |
| 23 | + * |
| 24 | + * The file is served from `public/xml/`. At deploy time the private repo |
| 25 | + * overwrites the bundled baseline with the latest version — so this is always |
| 26 | + * a single fetch against one location. |
| 27 | + * |
| 28 | + * Dispatches an error log event on the given element if the file cannot be |
| 29 | + * fetched or parsed. |
| 30 | + * |
| 31 | + * @param element - Element used for dispatching error log events. |
| 32 | + * @returns The parsed JSON, or null on failure. |
| 33 | + */ |
| 34 | +export async function loadCodeComponentsJson( |
| 35 | + element: Element |
| 36 | +): Promise<CodeComponentsJson | null> { |
| 37 | + if (cachedComponents !== null) { |
| 38 | + return cachedComponents; |
| 39 | + } |
| 40 | + |
| 41 | + try { |
| 42 | + const response = await fetch(CODE_COMPONENTS_PATH); |
| 43 | + if (!response.ok) { |
| 44 | + dispatchCodeComponentsError( |
| 45 | + element, |
| 46 | + CODE_COMPONENTS_PATH, |
| 47 | + response.status |
| 48 | + ); |
| 49 | + return null; |
| 50 | + } |
| 51 | + const json = (await response.json()) as CodeComponentsJson; |
| 52 | + cachedComponents = json; |
| 53 | + return json; |
| 54 | + } catch (err) { |
| 55 | + console.warn('Failed to fetch IEC61850 code components JSON:', err); |
| 56 | + dispatchCodeComponentsError(element, CODE_COMPONENTS_PATH); |
| 57 | + return null; |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Returns the code component file entries (NSD + NSDOC filenames) for the |
| 63 | + * given IEC 61850 edition, or null if the JSON hasn't been loaded or the |
| 64 | + * edition is not yet populated. |
| 65 | + * |
| 66 | + * @param components - The parsed code components JSON. |
| 67 | + * @param edition - The edition key to look up. |
| 68 | + */ |
| 69 | +export function getEditionComponents( |
| 70 | + components: CodeComponentsJson, |
| 71 | + edition: IEC61850Edition |
| 72 | +): EditionComponents | null { |
| 73 | + const editionData = components.IEC61850_code_components[edition]; |
| 74 | + if (!editionData || Object.keys(editionData).length === 0) { |
| 75 | + return null; |
| 76 | + } |
| 77 | + return editionData; |
| 78 | +} |
| 79 | + |
| 80 | +function dispatchCodeComponentsError( |
| 81 | + element: Element, |
| 82 | + path: string, |
| 83 | + status?: number |
| 84 | +): void { |
| 85 | + const detail = get('compas.codeComponents.errorDetails', { |
| 86 | + path, |
| 87 | + status: status === undefined ? 'network error' : String(status), |
| 88 | + }); |
| 89 | + |
| 90 | + element.dispatchEvent( |
| 91 | + newLogEvent({ |
| 92 | + kind: 'error', |
| 93 | + title: get('compas.codeComponents.error'), |
| 94 | + message: detail, |
| 95 | + }) |
| 96 | + ); |
| 97 | +} |
0 commit comments