Skip to content

Commit

Permalink
Merge pull request #265 from com-pas/Fetch_NSDoc_Files_Locally
Browse files Browse the repository at this point in the history
Feat: CoMPAS-OpenSCD should fetch the NSDoc files locally
  • Loading branch information
juancho0202 authored Sep 12, 2023
2 parents 45c213f + 309d341 commit 5b4cbb9
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 10 deletions.
100 changes: 100 additions & 0 deletions src/compas-services/CompasNSDocFileService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { handleError, handleResponse } from './foundation.js';

interface NsDocFile {
filename: string;
id: string;
name: string;
}

// Temporary solution to map to the old logic
const nsDocfiles: NsDocFile[] = [
{
filename: 'IEC_61850-7-2_2007B3-en.nsdoc',
name: 'IEC 61850-7-2',
id: '87e5bed8-2f27-4006-8673-f9d00b0a5426',
},
{
filename: 'IEC_61850-7-3_2007B3-en.nsdoc',
name: 'IEC_61850-7-3',
id: '315b02ac-c4aa-4495-9b4f-f7175a75c315',
},
{
filename: 'IEC_61850-7-4_2007B3-en.nsdoc',
name: 'IEC 61850-7-4',
id: 'da1b2ca0-1263-4b10-9b16-2f148ae3a1f1',
},
{
filename: 'IEC_61850-8-1_2003A2-en.nsdoc',
name: 'IEC 61850-8-1',
id: '0c052ea7-a010-4ca8-b2c7-caa665cabc46',
},
];

const createElement = (
name: string,
textContent: string,
document: XMLDocument
): Element => {
const element: Element = document.createElement(name);
element.textContent = textContent;

return element;
};

/** TODO: Make this return JSON */
export function CompasNSDocFileService() {
return {
listNsdocFiles(): Promise<Document> {
const document: XMLDocument = new DOMParser().parseFromString(
'<NsdocListResponse></NsdocListResponse>',
'text/xml'
);

nsDocfiles.forEach(nsDocFile => {
const nsDocFileElement: Element = document.createElement('NsdocFile');

nsDocFileElement.appendChild(
createElement('Id', nsDocFile.id, document)
);
nsDocFileElement.appendChild(
createElement('NsdocId', nsDocFile.name, document)
);
nsDocFileElement.appendChild(
createElement('Checksum', nsDocFile.id, document)
);
nsDocFileElement.appendChild(
createElement('Filename', nsDocFile.filename, document)
);

document
.querySelector('NsdocListResponse')!
.appendChild(nsDocFileElement);
});

return Promise.resolve(document);
},

getNsdocFile(id: string): Promise<Document> {
const nsDocFile: NsDocFile = nsDocfiles.find(f => f.id === id)!;

if (!nsDocFile) {
return Promise.reject(`Unable to find nsDoc file with id ${id}`);
}
return fetch(`./public/nsdoc/${nsDocFile.filename}`)
.catch(handleError)
.then(handleResponse)
.then(res => {
const document: XMLDocument = new DOMParser().parseFromString(
'<NsdocResponse></NsdocResponse>',
'text/xml'
);

document
.querySelector('NsdocResponse')!
.appendChild(createElement('NsdocFile', res, document));

return document;
});
},
};
}
8 changes: 0 additions & 8 deletions src/compas-services/CompasValidatorService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,6 @@ export function CompasSclValidatorService() {
.then(parseXml);
},

listNsdocFiles(): Promise<Document> {
const svsUrl = getSclValidatorServiceUrl() + '/nsdoc/v1';
return fetch(svsUrl)
.catch(handleError)
.then(handleResponse)
.then(parseXml);
},

getNsdocFile(id: string): Promise<Document> {
const svsUrl = getSclValidatorServiceUrl() + '/nsdoc/v1/' + id;
return fetch(svsUrl)
Expand Down
5 changes: 3 additions & 2 deletions src/compas/CompasNsdoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { newLoadNsdocEvent } from '../Setting.js';

import { createLogEvent } from '../compas-services/foundation.js';
import { CompasSclValidatorService } from '../compas-services/CompasValidatorService.js';
import { CompasNSDocFileService } from '../compas-services/CompasNSDocFileService.js';

/**
* Load a single entry. Use the nsdocId to look in the Local Storage, if already loaded,
Expand Down Expand Up @@ -30,7 +31,7 @@ async function processNsdocFile(
checksumStored !== checksum
) {
console.info(`Loading NSDoc File '${nsdocId}' with ID '${id}'.`);
await CompasSclValidatorService()
await CompasNSDocFileService()
.getNsdocFile(id)
.then(document => {
const nsdocContent =
Expand All @@ -51,7 +52,7 @@ async function processNsdocFile(
* Load each item found using the function #processNsdocFile.
*/
export async function loadNsdocFiles(component: Element): Promise<void> {
await CompasSclValidatorService()
await CompasNSDocFileService()
.listNsdocFiles()
.then(response => {
Array.from(response.querySelectorAll('NsdocFile') ?? []).forEach(
Expand Down
36 changes: 36 additions & 0 deletions test/unit/compas-services/CompasNSDocFileService.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { expect } from '@open-wc/testing';

import { CompasNSDocFileService } from '../../../src/compas-services/CompasNSDocFileService.js';

describe('compas-nsdocfile-service', () => {
it('Should list all NSDoc files', async () => {
const res = await CompasNSDocFileService().listNsdocFiles();

const nsDocFiles: Element[] = Array.from(res.querySelectorAll('NsdocFile'));

expect(nsDocFiles.length).to.equal(4);
});

it('Should fail on invalid request', done => {
const id = '315b02ac-c4aa-4495-9b4f-f7175a75c315';
CompasNSDocFileService()
.getNsdocFile(id)
.then(() => done('Failed'))
.catch(err => {
expect(err.status).to.equal(404);
expect(err.type).to.equal('NotFoundError');
done();
});
});
it('Should fail on invalid id', done => {
const id = '1';

CompasNSDocFileService()
.getNsdocFile(id)
.then(() => done('Failed'))
.catch(err => {
expect(err).to.equal(`Unable to find nsDoc file with id ${id}`);
done();
});
});
});

0 comments on commit 5b4cbb9

Please sign in to comment.