Skip to content

Commit

Permalink
fix: disabled endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
ralfaron committed Nov 26, 2024
1 parent 98046e6 commit e5e4196
Show file tree
Hide file tree
Showing 16 changed files with 100 additions and 69 deletions.
8 changes: 1 addition & 7 deletions projects/aas-core/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export type AASAbbreviation =
| 'SME'
| 'SML';

export type AASEndpointScheduleType = 'manual' | 'once' | 'every' | 'daily' | 'weekly';
export type AASEndpointScheduleType = 'disabled' | 'manual' | 'once' | 'every' | 'daily' | 'weekly';

/** The schedule type. */
export interface AASEndpointSchedule {
Expand All @@ -66,12 +66,6 @@ export type AASEndpoint = {
headers?: Record<string, string>;
};

/** Represents a server (AASX, OPC-UA) or file directory (AASX package files). */
export interface AASContainer extends AASEndpoint {
cursor?: string;
documents?: AASDocument[];
}

/** The unique identifier of an AAS. */
export interface AASDocumentId {
/** The identification of the Asset Administration Shell. */
Expand Down
11 changes: 1 addition & 10 deletions projects/aas-lib/src/test/assets/test-document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,7 @@
*
*****************************************************************************/

import { AASDocument, AASContainer, aas } from 'aas-core';

export function createContainer(url: string, documents: AASDocument[]): AASContainer {
return {
documents: documents,
url: url,
name: url,
type: 'FileSystem'
};
}
import { AASDocument, aas } from 'aas-core';

export function createDocument(name: string, endpoint = 'http://localhost/container1'): AASDocument {
const document: AASDocument = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ <h4 class="modal-title">
<div #collapse="ngbCollapse" [(ngbCollapse)]="isCollapsed">
<label class="form-lable" for="schedule-options" translate>AddEndpointForm.SCHEDULE</label>
<div class="border border-light-subtle rounded p-2 mb-2" id="schedule-options">
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="scheduleType" id="scheduleTypeDisabled" value="disabled"
[(ngModel)]="schedule">
<label class="form-check-label" for="scheduleTypeDisabled" translate>AddEndpointForm.DISABLED</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="scheduleType" id="scheduleTypeManual" value="manual"
[(ngModel)]="schedule">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,10 @@ export class AddEndpointFormComponent {
}

switch (this.schedule()) {
case 'disabled':
case 'manual':
endpoint.schedule = { type: 'manual' };
break;
case 'once':
endpoint.schedule = { type: 'once' };
endpoint.schedule = { type: this.schedule() };
break;
default:
endpoint.schedule = { type: 'every', values: [(this.hours() * 60 + this.minutes()) * 60000] };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class ExtrasEndpointService {

/**
* Returns all configured AAS endpoints.
* @returns An array of `AASContainer`.
* @returns An array of `AASEndpoint`.
**/
public getEndpoints(): Observable<AASEndpoint[]> {
return this.http.get<AASEndpoint[]>('/api/v1/endpoints');
Expand Down
2 changes: 1 addition & 1 deletion projects/aas-portal/src/app/start/start-api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class StartApiService {

/**
* Returns all configured AAS endpoints.
* @returns An array of `AASContainer`.
* @returns An array of `AASEndpoint`.
*/
public getEndpoints(): Observable<AASEndpoint[]> {
return this.http.get<AASEndpoint[]>('/api/v1/endpoints');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ <h4 class="modal-title">
@if (selectedItem().type === 'AAS_API') {
<label class="form-lable mt-2" for="schedule-options" translate>UpdateEndpointForm.SCHEDULE</label>
<div class="border border-light-subtle rounded p-2 mb-2" id="schedule-options">
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="scheduleType" id="scheduleTypeDisabled" value="disabled"
[(ngModel)]="schedule">
<label class="form-check-label" for="scheduleTypeDisabled" translate>UpdateEndpointForm.DISABLED</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="scheduleType" id="scheduleTypeManual" value="manual"
[(ngModel)]="schedule">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,10 @@ export class UpdateEndpointFormComponent {
endpoint.url = url.toString();

switch (this.schedule()) {
case 'disabled':
case 'manual':
endpoint.schedule = { type: 'manual' };
break;
case 'once':
endpoint.schedule = { type: 'once' };
endpoint.schedule = { type: this.schedule() };
break;
default:
endpoint.schedule = { type: 'every', values: [(this.hours() * 60 + this.minutes()) * 60000] };
Expand Down
3 changes: 3 additions & 0 deletions projects/aas-portal/src/assets/i18n/de-de.json
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@
"PLACEHOLDER_URL_WEBDAV": "http(s)://",
"ADVANCED_SETTINGS": "Erweiterte Einstellungen",
"SCHEDULE": "Endpunkt-Index aktualisieren",
"DISABLED": "deaktiviert",
"MANUAL": "manuell",
"ONCE": "einmalig",
"EVERY": "zyklisch",
Expand All @@ -210,6 +211,7 @@
"PLACEHOLDER_URL_OPCUA": "opc.tcp://",
"PLACEHOLDER_URL_WEBDAV": "http(s)://",
"SCHEDULE": "Endpunkt-Index aktualisieren",
"DISABLED": "deaktiviert",
"MANUAL": "manuell",
"ONCE": "einmalig",
"EVERY": "zyklisch",
Expand All @@ -230,6 +232,7 @@
"CONFIRM_RESET_CONFIGURATION": "Möchten Sie die AASNodePortal Standardkonfiguration wiederherstellen?",
"RESET": "Reset",
"CLOSE": "Schließen",
"disabled": "deaktiviert",
"manual": "manuell",
"once": "einmalig",
"every": "zyklisch"
Expand Down
3 changes: 3 additions & 0 deletions projects/aas-portal/src/assets/i18n/en-us.json
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@
"PLACEHOLDER_URL_WEBDAV": "http(s)://",
"ADVANCED_SETTINGS": "Advanced settings",
"SCHEDULE": "Update endpoint index",
"DISABLED": "disabled",
"MANUAL": "manual",
"ONCE": "once",
"EVERY": "cyclic",
Expand All @@ -211,6 +212,7 @@
"PLACEHOLDER_URL_OPCUA": "opc.tcp://",
"PLACEHOLDER_URL_WEBDAV": "http(s)://",
"SCHEDULE": "Update endpoint index",
"DISABLED": "disabled",
"MANUAL": "manual",
"ONCE": "once",
"EVERY": "cyclic",
Expand All @@ -231,6 +233,7 @@
"CONFIRM_RESET_CONFIGURATION": "Do you want to restore the default AASNodePortal configuration?",
"RESET": "Reset",
"CLOSE": "Close",
"disabled": "disabled",
"manual": "manual",
"once": "once",
"every": "cyclic"
Expand Down
11 changes: 1 addition & 10 deletions projects/aas-portal/src/test/assets/test-document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,7 @@
*
*****************************************************************************/

import { AASDocument, AASContainer, aas } from 'aas-core';

export function createContainer(url: string, documents: AASDocument[]): AASContainer {
return {
documents: documents,
url: url,
name: url,
type: 'AAS_API'
};
}
import { AASDocument, aas } from 'aas-core';

export function createDocument(name: string, endpoint= "http://localhost/container1"): AASDocument {
const document: AASDocument = {
Expand Down
4 changes: 2 additions & 2 deletions projects/aas-server/src/app/aas-index/aas-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export abstract class AASIndex {

public abstract getEndpoint(name: string): Promise<AASEndpoint>;

public abstract hasEndpoint(name: string): Promise<boolean>;
public abstract findEndpoint(name: string): Promise<AASEndpoint | undefined>;

public abstract addEndpoint(endpoint: AASEndpoint): Promise<void>;

Expand Down Expand Up @@ -75,7 +75,7 @@ export abstract class AASIndex {

public abstract remove(endpoint?: string, id?: string): Promise<boolean>;

public abstract clear(): Promise<void>;
public abstract clear(endpoint?: string): Promise<void>;

public abstract destroy(): Promise<void>;

Expand Down
32 changes: 22 additions & 10 deletions projects/aas-server/src/app/aas-index/lowdb/lowdb-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,16 @@ export class LowDbIndex extends AASIndex {
});
}

public override hasEndpoint(name: string): Promise<boolean> {
return new Promise<boolean>(resolve => {
resolve(this.db.data.endpoints.find(endpoint => endpoint.name === name) !== undefined);
public override findEndpoint(name: string): Promise<AASEndpoint | undefined> {
return new Promise<AASEndpoint | undefined>(resolve => {
resolve(this.db.data.endpoints.find(endpoint => endpoint.name === name));
});
}

public override async addEndpoint(endpoint: AASEndpoint): Promise<void> {
if (this.db.data.endpoints.some(item => item.name === endpoint.name)) {
throw new ApplicationError(
`An endpoint with the name "${name}" already exists.`,
`An endpoint with the name "${endpoint.name}" already exists.`,
ERRORS.RegistryAlreadyExists,
endpoint.name,
);
Expand All @@ -108,7 +108,7 @@ export class LowDbIndex extends AASIndex {
public override async updateEndpoint(endpoint: AASEndpoint): Promise<AASEndpoint> {
const index = this.db.data.endpoints.findIndex(item => item.name === endpoint.name);
if (index < 0) {
throw new Error(`An endpoint with the name ${name} does not exist.`);
throw new Error(`An endpoint with the name ${endpoint.name} does not exist.`);
}

const old = this.db.data.endpoints[index];
Expand Down Expand Up @@ -253,7 +253,9 @@ export class LowDbIndex extends AASIndex {
public override async remove(endpointName: string, id: string): Promise<boolean> {
const documents = this.db.data.documents;
const index = documents.findIndex(item => item.endpoint === endpointName && item.id === id);
if (index < 0) return false;
if (index < 0) {
return false;
}

const documentId = documents[index].uuid;
this.db.data.elements = this.db.data.elements.filter(element => element.uuid !== documentId);
Expand All @@ -263,10 +265,20 @@ export class LowDbIndex extends AASIndex {
return true;
}

public override async clear(): Promise<void> {
this.db.data.documents = [];
this.db.data.elements = [];
this.db.data.endpoints = [];
public override async clear(endpointName?: string): Promise<void> {
if (endpointName === undefined) {
this.db.data.documents = [];
this.db.data.elements = [];
this.db.data.endpoints = [];
} else {
const index = this.db.data.endpoints.findIndex(endpoint => endpoint.name === endpointName);
if (index < 0) {
return;
}

this.removeDocuments(endpointName);
}

await this.db.write();
}

Expand Down
30 changes: 24 additions & 6 deletions projects/aas-server/src/app/aas-index/mysql/mysql-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,14 @@ export class MySqlIndex extends AASIndex {
return this.toEndpoint(results[0]);
}

public override async hasEndpoint(name: string): Promise<boolean> {
public override async findEndpoint(name: string): Promise<AASEndpoint | undefined> {
const connection = await this.getConnection();
const [results] = await connection.query<MySqlEndpoint[]>('SELECT * FROM `endpoints` WHERE name = ?;', [name]);
return results.length > 0;
if (results.length === 0) {
return undefined;
}

return this.toEndpoint(results[0]);
}

public override async addEndpoint(endpoint: AASEndpoint): Promise<void> {
Expand Down Expand Up @@ -307,13 +311,27 @@ export class MySqlIndex extends AASIndex {
}
}

public override async clear(): Promise<void> {
public override async clear(endpointName?: string): Promise<void> {
const connection = await this.getConnection();
try {
await connection.beginTransaction();
await connection.query<ResultSetHeader>('DELETE FROM `elements`;');
await connection.query<ResultSetHeader>('DELETE FROM `documents`;');
await connection.query<ResultSetHeader>('DELETE FROM `endpoints`;');
if (endpointName === undefined) {
await connection.query<ResultSetHeader>('DELETE FROM `elements`;');
await connection.query<ResultSetHeader>('DELETE FROM `documents`;');
await connection.query<ResultSetHeader>('DELETE FROM `endpoints`;');
} else {
const documents = (
await connection.query<MySqlDocument[]>('SELECT * FROM `documents` WHERE endpoint = ?;', [
endpointName,
])
)[0];

await connection.query<ResultSetHeader>('DELETE FROM `documents` WHERE endpoint = ?;', [endpointName]);

for (const document of documents) {
await connection.query<ResultSetHeader>('DELETE FROM `elements` WHERE uuid = ?;', [document.uuid]);
}
}
await connection.commit();
} catch (error) {
await connection.rollback();
Expand Down
37 changes: 24 additions & 13 deletions projects/aas-server/src/app/aas-provider/aas-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ export class AASProvider {
} as AASServerMessage,
});

if (endpoint.schedule?.type === 'manual') {
const type = endpoint.schedule?.type;
if (type === 'manual' || type === 'disabled') {
return;
}

Expand All @@ -253,8 +254,12 @@ export class AASProvider {
task = this.taskHandler.createTask(endpoint.name, this, 'ScanEndpoint');
}

if (old.schedule?.type !== endpoint.schedule?.type) {
if (old.schedule?.type === 'manual') {
const oldType = old.schedule?.type;
const newType = endpoint.schedule?.type;
if (oldType !== newType) {
if (newType === 'disabled') {
await this.index.clear(endpoint.name);
} else if (oldType === 'manual') {
setTimeout(this.scanEndpoint, 0, task, endpoint);
}
}
Expand Down Expand Up @@ -534,7 +539,8 @@ export class AASProvider {
private startScan = async (): Promise<void> => {
try {
for (const endpoint of await this.index.getEndpoints()) {
if (endpoint.schedule?.type === 'manual') {
const type = endpoint.schedule?.type;
if (type === 'manual' || type === 'disabled') {
continue;
}

Expand Down Expand Up @@ -606,12 +612,13 @@ export class AASProvider {
return;
}

if ((await this.index.hasEndpoint(task.endpointName)) === true) {
const endpoint = await this.index.getEndpoint(task.endpointName);
const endpoint = await this.index.findEndpoint(task.endpointName);
if (endpoint !== undefined) {
task.state = 'idle';
task.end = Date.now();

if (endpoint.schedule?.type === 'once' || endpoint.schedule?.type === 'manual') {
const type = endpoint.schedule?.type;
if (type === 'once' || type === 'manual' || type === 'disabled') {
return;
}

Expand All @@ -631,7 +638,8 @@ export class AASProvider {

private async onUpdate(result: ScanEndpointResult): Promise<void> {
const document = result.document;
if ((await this.index.hasEndpoint(document.endpoint)) === false) {
const endpoint = await this.index.findEndpoint(document.endpoint);
if (endpoint === undefined || endpoint.schedule?.type === 'disabled') {
return;
}

Expand All @@ -644,18 +652,21 @@ export class AASProvider {
}

private async onAdded(result: ScanEndpointResult): Promise<void> {
if ((await this.index.hasEndpoint(result.document.endpoint)) === false) {
const document = result.document;
const endpoint = await this.index.findEndpoint(document.endpoint);
if (endpoint === undefined || endpoint.schedule?.type === 'disabled') {
return;
}

await this.index.add(result.document);
this.logger.info(`Added: AAS ${result.document.idShort} [${result.document.id}] in ${result.endpoint.url}`);
this.sendMessage({ type: 'Added', document: result.document });
await this.index.add(document);
this.logger.info(`Added: AAS ${document.idShort} [${document.id}] in ${endpoint.url}`);
this.sendMessage({ type: 'Added', document });
}

private async onRemoved(result: ScanEndpointResult): Promise<void> {
const document = result.document;
if ((await this.index.hasEndpoint(document.endpoint)) === false) {
const endpoint = await this.index.findEndpoint(document.endpoint);
if (endpoint === undefined || endpoint.schedule?.type === 'disabled') {
return;
}

Expand Down
Loading

0 comments on commit e5e4196

Please sign in to comment.