Skip to content

Commit 3385dba

Browse files
Feat: CoMPAS-OpenSCD should fetch the NSDoc files locally from public/nsdoc (This is a mountable docker volume)
Signed-off-by: Pascal Wilbrink <[email protected]>
1 parent 45c213f commit 3385dba

File tree

4 files changed

+141
-10
lines changed

4 files changed

+141
-10
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import { handleError, handleResponse } from './foundation.js';
2+
3+
interface NsDocFile {
4+
filename: string;
5+
id: string;
6+
name: string;
7+
}
8+
9+
// Temporary solution to map to the old logic
10+
const nsDocfiles: NsDocFile[] = [
11+
{
12+
filename: 'IEC_61850-7-2_2007B3-en.nsdoc',
13+
name: 'IEC 61850-7-2',
14+
id: '87e5bed8-2f27-4006-8673-f9d00b0a5426',
15+
},
16+
{
17+
filename: 'IEC_61850-7-3_2007B3-en.nsdoc',
18+
name: 'IEC_61850-7-3',
19+
id: '315b02ac-c4aa-4495-9b4f-f7175a75c315',
20+
},
21+
{
22+
filename: 'IEC_61850-7-4_2007B3-en.nsdoc',
23+
name: 'IEC 61850-7-4',
24+
id: 'da1b2ca0-1263-4b10-9b16-2f148ae3a1f1',
25+
},
26+
{
27+
filename: 'IEC_61850-8-1_2003A2-en.nsdoc',
28+
name: 'IEC 61850-8-1',
29+
id: '0c052ea7-a010-4ca8-b2c7-caa665cabc46',
30+
},
31+
];
32+
33+
const createElement = (
34+
name: string,
35+
textContent: string,
36+
document: XMLDocument
37+
): Element => {
38+
const element: Element = document.createElement(name);
39+
element.textContent = textContent;
40+
41+
return element;
42+
};
43+
44+
/** TODO: Make this return JSON */
45+
export function CompasNSDocFileService() {
46+
return {
47+
listNsdocFiles(): Promise<Document> {
48+
const document: XMLDocument = new DOMParser().parseFromString(
49+
'<NsdocListResponse></NsdocListResponse>',
50+
'text/xml'
51+
);
52+
53+
nsDocfiles.forEach(nsDocFile => {
54+
const nsDocFileElement: Element = document.createElement('NsdocFile');
55+
56+
nsDocFileElement.appendChild(
57+
createElement('Id', nsDocFile.id, document)
58+
);
59+
nsDocFileElement.appendChild(
60+
createElement('NsdocId', nsDocFile.name, document)
61+
);
62+
nsDocFileElement.appendChild(
63+
createElement('Checksum', nsDocFile.id, document)
64+
);
65+
nsDocFileElement.appendChild(
66+
createElement('Filename', nsDocFile.filename, document)
67+
);
68+
69+
document
70+
.querySelector('NsdocListResponse')!
71+
.appendChild(nsDocFileElement);
72+
});
73+
74+
return Promise.resolve(document);
75+
},
76+
77+
getNsdocFile(id: string): Promise<Document> {
78+
const nsDocFile: NsDocFile = nsDocfiles.find(f => f.id === id)!;
79+
80+
if (!nsDocFile) {
81+
return Promise.reject(`Unable to find nsDoc file with id ${id}`);
82+
}
83+
return fetch(`./public/nsdoc/${nsDocFile.filename}`)
84+
.catch(handleError)
85+
.then(handleResponse)
86+
.then(res => {
87+
const document: XMLDocument = new DOMParser().parseFromString(
88+
'<NsdocResponse></NsdocResponse>',
89+
'text/xml'
90+
);
91+
92+
document
93+
.querySelector('NsdocResponse')!
94+
.appendChild(createElement('NsdocFile', res, document));
95+
96+
return document;
97+
});
98+
},
99+
};
100+
}

src/compas-services/CompasValidatorService.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,6 @@ export function CompasSclValidatorService() {
6060
.then(parseXml);
6161
},
6262

63-
listNsdocFiles(): Promise<Document> {
64-
const svsUrl = getSclValidatorServiceUrl() + '/nsdoc/v1';
65-
return fetch(svsUrl)
66-
.catch(handleError)
67-
.then(handleResponse)
68-
.then(parseXml);
69-
},
70-
7163
getNsdocFile(id: string): Promise<Document> {
7264
const svsUrl = getSclValidatorServiceUrl() + '/nsdoc/v1/' + id;
7365
return fetch(svsUrl)

src/compas/CompasNsdoc.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { newLoadNsdocEvent } from '../Setting.js';
22

33
import { createLogEvent } from '../compas-services/foundation.js';
44
import { CompasSclValidatorService } from '../compas-services/CompasValidatorService.js';
5+
import { CompasNSDocFileService } from '../compas-services/CompasNSDocFileService.js';
56

67
/**
78
* Load a single entry. Use the nsdocId to look in the Local Storage, if already loaded,
@@ -30,9 +31,10 @@ async function processNsdocFile(
3031
checksumStored !== checksum
3132
) {
3233
console.info(`Loading NSDoc File '${nsdocId}' with ID '${id}'.`);
33-
await CompasSclValidatorService()
34+
await CompasNSDocFileService()
3435
.getNsdocFile(id)
3536
.then(document => {
37+
console.log('document: ', document);
3638
const nsdocContent =
3739
document.querySelectorAll('NsdocFile').item(0).textContent ?? '';
3840
component.dispatchEvent(newLoadNsdocEvent(nsdocContent, filename));
@@ -51,7 +53,7 @@ async function processNsdocFile(
5153
* Load each item found using the function #processNsdocFile.
5254
*/
5355
export async function loadNsdocFiles(component: Element): Promise<void> {
54-
await CompasSclValidatorService()
56+
await CompasNSDocFileService()
5557
.listNsdocFiles()
5658
.then(response => {
5759
Array.from(response.querySelectorAll('NsdocFile') ?? []).forEach(
@@ -67,6 +69,7 @@ export async function loadNsdocFiles(component: Element): Promise<void> {
6769
);
6870
})
6971
.catch(reason => {
72+
console.log('reason: ', reason);
7073
createLogEvent(component, reason);
7174
});
7275
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { expect } from '@open-wc/testing';
2+
3+
import { CompasNSDocFileService } from '../../../src/compas-services/CompasNSDocFileService.js';
4+
5+
describe('compas-nsdocfile-service', () => {
6+
it('Should list all NSDoc files', async () => {
7+
const res = await CompasNSDocFileService().listNsdocFiles();
8+
9+
const nsDocFiles: Element[] = Array.from(res.querySelectorAll('NsdocFile'));
10+
11+
expect(nsDocFiles.length).to.equal(4);
12+
});
13+
14+
it('Should fail on invalid request', done => {
15+
const id = '315b02ac-c4aa-4495-9b4f-f7175a75c315';
16+
CompasNSDocFileService()
17+
.getNsdocFile(id)
18+
.then(() => done('Failed'))
19+
.catch(err => {
20+
expect(err.status).to.equal(404);
21+
expect(err.type).to.equal('NotFoundError');
22+
done();
23+
});
24+
});
25+
it('Should fail on invalid id', done => {
26+
const id = '1';
27+
28+
CompasNSDocFileService()
29+
.getNsdocFile(id)
30+
.then(() => done('Failed'))
31+
.catch(err => {
32+
expect(err).to.equal(`Unable to find nsDoc file with id ${id}`);
33+
done();
34+
});
35+
});
36+
});

0 commit comments

Comments
 (0)