basyx-typescript-sdk 1.0.1 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,726 @@
1
+ import { KeyTypes, ReferenceTypes, } from '@aas-core-works/aas-core3.0-typescript/types';
2
+ import { AasDiscoveryClient } from '../clients/AasDiscoveryClient';
3
+ import { AasRegistryClient } from '../clients/AasRegistryClient';
4
+ import { AasRepositoryClient } from '../clients/AasRepositoryClient';
5
+ import { Configuration } from '../generated';
6
+ import { base64Decode, base64Encode } from '../lib/base64Url';
7
+ import { AssetAdministrationShellDescriptor } from '../models/Descriptors';
8
+ import { extractEndpointHref } from '../utils/DescriptorUtils';
9
+ import { SubmodelService } from './SubmodelService';
10
+ /**
11
+ * AasService combines and orchestrates multiple clients and services, including:
12
+ * - AasRegistryClient
13
+ * - AasRepositoryClient
14
+ * - AasDiscoveryClient
15
+ * - SubmodelService
16
+ *
17
+ * It provides higher-level functionality for working with Asset Administration Shells
18
+ * by coordinating operations across these BaSyx components, demonstrating the multi-client pattern.
19
+ */
20
+ export class AasService {
21
+ registryClient;
22
+ repositoryClient;
23
+ discoveryClient;
24
+ registryConfig;
25
+ repositoryConfig;
26
+ discoveryConfig;
27
+ submodelService;
28
+ constructor(config) {
29
+ this.registryClient = new AasRegistryClient();
30
+ this.repositoryClient = new AasRepositoryClient();
31
+ this.discoveryClient = new AasDiscoveryClient();
32
+ this.registryConfig = config.registryConfig;
33
+ this.repositoryConfig = config.repositoryConfig;
34
+ this.discoveryConfig = config.discoveryConfig;
35
+ // Use separate submodel configs if provided, otherwise fall back to AAS configs
36
+ this.submodelService = new SubmodelService({
37
+ registryConfig: config.submodelRegistryConfig ?? config.registryConfig,
38
+ repositoryConfig: config.submodelRepositoryConfig ?? config.repositoryConfig,
39
+ conceptDescriptionRepositoryConfig: config.conceptDescriptionRepositoryConfig,
40
+ });
41
+ }
42
+ /**
43
+ * Retrieves a list of all Asset Administration Shells.
44
+ *
45
+ * This method first attempts to fetch AAS Descriptors from the Registry,
46
+ * then uses the descriptor endpoints to fetch the actual shells.
47
+ * If a registry is not configured or fails, it falls back to fetching
48
+ * AAS directly from the Repository.
49
+ *
50
+ * @param options Object containing:
51
+ * - preferRegistry?: Whether to prefer registry over repository (default: true)
52
+ * - limit?: Maximum number of elements to retrieve
53
+ * - cursor?: Pagination cursor
54
+ * - includeSubmodels?: Whether to fetch submodels for each shell (default: false)
55
+ * - includeConceptDescriptions?: Whether to fetch concept descriptions (default: false)
56
+ *
57
+ * @returns Either `{ success: true; data: { shells, source, submodels? } }` or `{ success: false; error: ... }`.
58
+ */
59
+ async getAasList(options) {
60
+ const preferRegistry = options?.preferRegistry ?? true;
61
+ const includeSubmodels = options?.includeSubmodels ?? false;
62
+ // Try registry first if configured and preferred
63
+ if (preferRegistry && this.registryConfig) {
64
+ const registryResult = await this.registryClient.getAllAssetAdministrationShellDescriptors({
65
+ configuration: this.registryConfig,
66
+ limit: options?.limit,
67
+ cursor: options?.cursor,
68
+ });
69
+ if (registryResult.success) {
70
+ // Fetch actual shells from descriptor endpoints
71
+ const shells = [];
72
+ for (const descriptor of registryResult.data.result) {
73
+ const endpoint = extractEndpointHref(descriptor, 'AAS-3.0');
74
+ if (endpoint && descriptor.id) {
75
+ // Extract base URL from endpoint (remove /shells/{id} part)
76
+ const baseUrl = endpoint.match(/^(https?:\/\/[^/]+(?::\d+)?)/)?.[1] || endpoint;
77
+ const config = new Configuration({ basePath: baseUrl });
78
+ const shellResult = await this.repositoryClient.getAssetAdministrationShellById({
79
+ configuration: config,
80
+ aasIdentifier: descriptor.id,
81
+ });
82
+ if (shellResult.success) {
83
+ shells.push(shellResult.data);
84
+ }
85
+ }
86
+ }
87
+ // Fetch submodels if requested
88
+ let submodelsMap;
89
+ if (includeSubmodels) {
90
+ submodelsMap = await this.fetchSubmodelsForShells(shells, options?.includeConceptDescriptions);
91
+ }
92
+ return {
93
+ success: true,
94
+ data: {
95
+ shells,
96
+ source: 'registry',
97
+ ...(submodelsMap && { submodels: submodelsMap }),
98
+ },
99
+ };
100
+ }
101
+ }
102
+ // Fall back to repository if registry failed or not configured
103
+ if (this.repositoryConfig) {
104
+ const repositoryResult = await this.repositoryClient.getAllAssetAdministrationShells({
105
+ configuration: this.repositoryConfig,
106
+ limit: options?.limit,
107
+ cursor: options?.cursor,
108
+ });
109
+ if (repositoryResult.success) {
110
+ const shells = repositoryResult.data.result;
111
+ // Fetch submodels if requested
112
+ let submodelsMap;
113
+ if (includeSubmodels) {
114
+ submodelsMap = await this.fetchSubmodelsForShells(shells, options?.includeConceptDescriptions);
115
+ }
116
+ return {
117
+ success: true,
118
+ data: {
119
+ shells,
120
+ source: 'repository',
121
+ ...(submodelsMap && { submodels: submodelsMap }),
122
+ },
123
+ };
124
+ }
125
+ return { success: false, error: repositoryResult.error };
126
+ }
127
+ return {
128
+ success: false,
129
+ error: {
130
+ errorType: 'ConfigurationError',
131
+ message: 'Neither registry nor repository configuration provided',
132
+ },
133
+ };
134
+ }
135
+ /**
136
+ * Retrieves an Asset Administration Shell by ID.
137
+ *
138
+ * This method first attempts to fetch the descriptor from the registry
139
+ * and use its endpoint to fetch the shell. If that fails, it falls back
140
+ * to fetching directly from the repository.
141
+ *
142
+ * @param options Object containing:
143
+ * - aasIdentifier: The AAS identifier
144
+ * - useRegistryEndpoint?: Whether to try registry endpoint first (default: true)
145
+ * - includeSubmodels?: Whether to fetch submodels for the shell (default: false)
146
+ * - includeConceptDescriptions?: Whether to fetch concept descriptions (default: false)
147
+ *
148
+ * @returns Either `{ success: true; data: { shell, descriptor?, submodels? } }` or `{ success: false; error: ... }`.
149
+ */
150
+ async getAasById(options) {
151
+ const { aasIdentifier, useRegistryEndpoint = true, includeSubmodels = false } = options;
152
+ // Try registry-based flow first
153
+ if (useRegistryEndpoint && this.registryConfig) {
154
+ const descriptorResult = await this.registryClient.getAssetAdministrationShellDescriptorById({
155
+ configuration: this.registryConfig,
156
+ aasIdentifier,
157
+ });
158
+ if (descriptorResult.success) {
159
+ const descriptor = descriptorResult.data;
160
+ const endpoint = extractEndpointHref(descriptor, 'AAS-3.0');
161
+ if (endpoint) {
162
+ // Extract base URL from endpoint (remove /shells/{id} part)
163
+ const baseUrl = endpoint.match(/^(https?:\/\/[^/]+(?::\d+)?)/)?.[1] || endpoint;
164
+ // Try to fetch from descriptor endpoint
165
+ const config = new Configuration({ basePath: baseUrl });
166
+ const shellResult = await this.repositoryClient.getAssetAdministrationShellById({
167
+ configuration: config,
168
+ aasIdentifier,
169
+ });
170
+ if (shellResult.success) {
171
+ const shell = shellResult.data;
172
+ // Fetch submodels if requested
173
+ let submodels;
174
+ if (includeSubmodels) {
175
+ const submodelsMap = await this.fetchSubmodelsForShells([shell], options.includeConceptDescriptions);
176
+ submodels = submodelsMap[shell.id] || [];
177
+ }
178
+ return {
179
+ success: true,
180
+ data: {
181
+ shell,
182
+ descriptor,
183
+ ...(submodels && { submodels }),
184
+ },
185
+ };
186
+ }
187
+ }
188
+ }
189
+ }
190
+ // Fall back to repository
191
+ if (!this.repositoryConfig) {
192
+ return {
193
+ success: false,
194
+ error: {
195
+ errorType: 'ConfigurationError',
196
+ message: 'No repository configuration available',
197
+ },
198
+ };
199
+ }
200
+ const shellResult = await this.repositoryClient.getAssetAdministrationShellById({
201
+ configuration: this.repositoryConfig,
202
+ aasIdentifier,
203
+ });
204
+ if (shellResult.success) {
205
+ const shell = shellResult.data;
206
+ // Fetch submodels if requested
207
+ let submodels;
208
+ if (includeSubmodels) {
209
+ const submodelsMap = await this.fetchSubmodelsForShells([shell], options.includeConceptDescriptions);
210
+ submodels = submodelsMap[shell.id] || [];
211
+ }
212
+ return {
213
+ success: true,
214
+ data: {
215
+ shell,
216
+ descriptor: undefined,
217
+ ...(submodels && { submodels }),
218
+ },
219
+ };
220
+ }
221
+ return { success: false, error: shellResult.error };
222
+ }
223
+ /**
224
+ * Gets the endpoint URL for an Asset Administration Shell by its identifier.
225
+ *
226
+ * If registry is configured and useRegistry is true, retrieves the endpoint
227
+ * from the descriptor's endpoint object. Otherwise, constructs the endpoint
228
+ * from the repository base path and the encoded AAS identifier.
229
+ *
230
+ * @param options Object containing:
231
+ * - aasIdentifier: The AAS identifier
232
+ * - useRegistry?: Whether to try getting endpoint from registry (default: true)
233
+ *
234
+ * @returns Either `{ success: true; data: endpointUrl }` or `{ success: false; error: ... }`.
235
+ */
236
+ async getAasEndpointById(options) {
237
+ const { aasIdentifier, useRegistry = true } = options;
238
+ // Try registry first if configured and requested
239
+ if (useRegistry && this.registryConfig) {
240
+ const descriptorResult = await this.registryClient.getAssetAdministrationShellDescriptorById({
241
+ configuration: this.registryConfig,
242
+ aasIdentifier,
243
+ });
244
+ if (descriptorResult.success) {
245
+ const endpoint = extractEndpointHref(descriptorResult.data, 'AAS-3.0');
246
+ if (endpoint) {
247
+ return { success: true, data: endpoint };
248
+ }
249
+ }
250
+ }
251
+ // Fall back to constructing from repository base
252
+ if (!this.repositoryConfig) {
253
+ return {
254
+ success: false,
255
+ error: {
256
+ errorType: 'ConfigurationError',
257
+ message: 'Repository configuration required',
258
+ },
259
+ };
260
+ }
261
+ const repositoryBasePath = this.repositoryConfig.basePath || 'http://localhost:8081';
262
+ const encodedId = base64Encode(aasIdentifier);
263
+ const endpoint = `${repositoryBasePath}/shells/${encodedId}`;
264
+ return { success: true, data: endpoint };
265
+ }
266
+ /**
267
+ * Retrieves an Asset Administration Shell by its endpoint URL.
268
+ *
269
+ * Extracts the base URL and base64url-encoded AAS identifier from the endpoint,
270
+ * decodes the identifier, and fetches the shell from the repository.
271
+ *
272
+ * @param options Object containing:
273
+ * - endpoint: The endpoint URL (format: http://host/shells/{base64EncodedId})
274
+ * - includeSubmodels?: Whether to fetch submodels for the shell (default: false)
275
+ * - includeConceptDescriptions?: Whether to fetch concept descriptions (default: false)
276
+ *
277
+ * @returns Either `{ success: true; data: { shell, submodels? } }` or `{ success: false; error: ... }`.
278
+ */
279
+ async getAasByEndpoint(options) {
280
+ const { endpoint, includeSubmodels = false } = options;
281
+ // Extract base URL and encoded ID from endpoint
282
+ // Expected format: http://localhost:8081/shells/base64EncodedId
283
+ const match = endpoint.match(/^(https?:\/\/[^/]+(?::\d+)?)\/shells\/([^/]+)$/);
284
+ if (!match) {
285
+ return {
286
+ success: false,
287
+ error: {
288
+ errorType: 'InvalidEndpoint',
289
+ message: 'Invalid endpoint format. Expected format: http://host/shells/{base64EncodedId}',
290
+ },
291
+ };
292
+ }
293
+ const [, baseUrl, encodedId] = match;
294
+ // Decode the ID
295
+ const aasIdentifier = base64Decode(encodedId);
296
+ // Create configuration for the endpoint
297
+ const config = new Configuration({ basePath: baseUrl });
298
+ // Fetch the shell
299
+ const shellResult = await this.repositoryClient.getAssetAdministrationShellById({
300
+ configuration: config,
301
+ aasIdentifier,
302
+ });
303
+ if (!shellResult.success) {
304
+ return { success: false, error: shellResult.error };
305
+ }
306
+ const shell = shellResult.data;
307
+ // Fetch submodels if requested
308
+ let submodels;
309
+ if (includeSubmodels) {
310
+ const submodelsMap = await this.fetchSubmodelsForShells([shell], options.includeConceptDescriptions);
311
+ submodels = submodelsMap[shell.id] || [];
312
+ }
313
+ return {
314
+ success: true,
315
+ data: {
316
+ shell,
317
+ ...(submodels && { submodels }),
318
+ },
319
+ };
320
+ }
321
+ /**
322
+ * Creates an Asset Administration Shell and optionally registers it in the registry.
323
+ *
324
+ * This method creates the AAS in the repository first. If registry is configured
325
+ * and registerInRegistry is true, it automatically creates a descriptor from the shell
326
+ * and registers it in the registry with the repository endpoint.
327
+ *
328
+ * @param options Object containing:
329
+ * - shell: The AAS to create
330
+ * - registerInRegistry?: Whether to register the descriptor in the registry (default: true)
331
+ *
332
+ * @returns Either `{ success: true; data: { shell, descriptor? } }` or `{ success: false; error: ... }`.
333
+ */
334
+ async createAas(options) {
335
+ const { shell, registerInRegistry = true } = options;
336
+ if (!this.repositoryConfig) {
337
+ return {
338
+ success: false,
339
+ error: {
340
+ errorType: 'ConfigurationError',
341
+ message: 'Repository configuration required',
342
+ },
343
+ };
344
+ }
345
+ // Create AAS in repository
346
+ const shellResult = await this.repositoryClient.postAssetAdministrationShell({
347
+ configuration: this.repositoryConfig,
348
+ assetAdministrationShell: shell,
349
+ });
350
+ if (!shellResult.success) {
351
+ return { success: false, error: shellResult.error };
352
+ }
353
+ // Register descriptor in registry if configured and requested
354
+ if (registerInRegistry && this.registryConfig) {
355
+ const descriptor = this.createDescriptorFromAas(shell);
356
+ const descriptorResult = await this.registryClient.postAssetAdministrationShellDescriptor({
357
+ configuration: this.registryConfig,
358
+ assetAdministrationShellDescriptor: descriptor,
359
+ });
360
+ if (!descriptorResult.success) {
361
+ // Note: Shell was created but descriptor registration failed
362
+ return { success: false, error: descriptorResult.error };
363
+ }
364
+ return {
365
+ success: true,
366
+ data: {
367
+ shell: shellResult.data,
368
+ descriptor: descriptorResult.data,
369
+ },
370
+ };
371
+ }
372
+ // Registry not configured or registration not requested
373
+ return {
374
+ success: true,
375
+ data: {
376
+ shell: shellResult.data,
377
+ descriptor: undefined,
378
+ },
379
+ };
380
+ }
381
+ /**
382
+ * Updates an Asset Administration Shell and optionally updates its descriptor in the registry.
383
+ *
384
+ * This method updates the AAS in the repository. If registry is configured
385
+ * and updateInRegistry is true, it automatically creates/updates the descriptor
386
+ * in the registry with the updated shell data.
387
+ *
388
+ * @param options Object containing:
389
+ * - shell: The updated AAS
390
+ * - updateInRegistry?: Whether to update the descriptor in the registry (default: true)
391
+ *
392
+ * @returns Either `{ success: true; data: { shell, descriptor? } }` or `{ success: false; error: ... }`.
393
+ */
394
+ async updateAas(options) {
395
+ const { shell, updateInRegistry = true } = options;
396
+ if (!this.repositoryConfig) {
397
+ return {
398
+ success: false,
399
+ error: {
400
+ errorType: 'ConfigurationError',
401
+ message: 'Repository configuration required',
402
+ },
403
+ };
404
+ }
405
+ // Update AAS in repository
406
+ const shellResult = await this.repositoryClient.putAssetAdministrationShellById({
407
+ configuration: this.repositoryConfig,
408
+ aasIdentifier: shell.id,
409
+ assetAdministrationShell: shell,
410
+ });
411
+ if (!shellResult.success) {
412
+ return { success: false, error: shellResult.error };
413
+ }
414
+ // Use the returned shell if available, otherwise use the input shell
415
+ const updatedShell = shellResult.data || shell;
416
+ // Update descriptor in registry if configured and requested
417
+ if (updateInRegistry && this.registryConfig) {
418
+ const descriptor = this.createDescriptorFromAas(shell);
419
+ const descriptorResult = await this.registryClient.putAssetAdministrationShellDescriptorById({
420
+ configuration: this.registryConfig,
421
+ aasIdentifier: shell.id,
422
+ assetAdministrationShellDescriptor: descriptor,
423
+ });
424
+ if (!descriptorResult.success) {
425
+ // Note: Shell was updated but descriptor update failed
426
+ return { success: false, error: descriptorResult.error };
427
+ }
428
+ // Use the returned descriptor if available, otherwise use the created descriptor
429
+ const updatedDescriptor = descriptorResult.data || descriptor;
430
+ return {
431
+ success: true,
432
+ data: {
433
+ shell: updatedShell,
434
+ descriptor: updatedDescriptor,
435
+ },
436
+ };
437
+ }
438
+ // Registry not configured or update not requested
439
+ return {
440
+ success: true,
441
+ data: {
442
+ shell: updatedShell,
443
+ descriptor: undefined,
444
+ },
445
+ };
446
+ }
447
+ /**
448
+ * Deletes an Asset Administration Shell and optionally removes it from the registry.
449
+ *
450
+ * This method removes the descriptor from the registry first (if requested),
451
+ * then deletes the AAS from the repository.
452
+ *
453
+ * @param options Object containing:
454
+ * - aasIdentifier: The AAS identifier to remove
455
+ * - deleteFromRegistry?: Whether to delete from registry (default: true)
456
+ *
457
+ * @returns Either `{ success: true; data: void }` or `{ success: false; error: ... }`.
458
+ */
459
+ async deleteAas(options) {
460
+ const { aasIdentifier, deleteFromRegistry = true } = options;
461
+ // Remove from registry first if configured and requested
462
+ if (deleteFromRegistry && this.registryConfig) {
463
+ const registryResult = await this.registryClient.deleteAssetAdministrationShellDescriptorById({
464
+ configuration: this.registryConfig,
465
+ aasIdentifier,
466
+ });
467
+ if (!registryResult.success) {
468
+ return { success: false, error: registryResult.error };
469
+ }
470
+ }
471
+ // Remove from repository
472
+ if (this.repositoryConfig) {
473
+ const repoResult = await this.repositoryClient.deleteAssetAdministrationShellById({
474
+ configuration: this.repositoryConfig,
475
+ aasIdentifier,
476
+ });
477
+ if (!repoResult.success) {
478
+ return { success: false, error: repoResult.error };
479
+ }
480
+ }
481
+ return { success: true, data: undefined };
482
+ }
483
+ /**
484
+ * Retrieves Asset Administration Shells by their asset identifiers.
485
+ *
486
+ * This method uses the discovery service to resolve asset IDs to AAS identifiers,
487
+ * then fetches the corresponding shells. If multiple AAS IDs are found for the given
488
+ * asset IDs, all matching shells are returned.
489
+ *
490
+ * @param options Object containing:
491
+ * - assetIds: Array of asset identifiers to search for
492
+ * - includeSubmodels?: Whether to fetch submodels for each shell (default: false)
493
+ * - includeConceptDescriptions?: Whether to fetch concept descriptions (default: false)
494
+ *
495
+ * @returns Either `{ success: true; data: { shells, aasIds } }` or `{ success: false; error: ... }`.
496
+ */
497
+ async getAasByAssetId(options) {
498
+ const { assetIds, includeSubmodels = false, includeConceptDescriptions = false } = options;
499
+ if (!this.discoveryConfig) {
500
+ return {
501
+ success: false,
502
+ error: {
503
+ errorType: 'ConfigurationError',
504
+ message: 'Discovery service configuration not provided',
505
+ },
506
+ };
507
+ }
508
+ // Use discovery service to resolve asset IDs to AAS identifiers
509
+ const discoveryResult = await this.discoveryClient.getAllAssetAdministrationShellIdsByAssetLink({
510
+ configuration: this.discoveryConfig,
511
+ assetIds,
512
+ });
513
+ if (!discoveryResult.success) {
514
+ return { success: false, error: discoveryResult.error };
515
+ }
516
+ const aasIds = discoveryResult.data.result;
517
+ if (aasIds.length === 0) {
518
+ return {
519
+ success: true,
520
+ data: {
521
+ shells: [],
522
+ aasIds: [],
523
+ },
524
+ };
525
+ }
526
+ // Fetch all AAS by their identifiers
527
+ const shellResults = await Promise.all(aasIds.map((aasId) => this.getAasById({
528
+ aasIdentifier: aasId,
529
+ includeSubmodels,
530
+ includeConceptDescriptions,
531
+ })));
532
+ // Collect successful results
533
+ const shells = [];
534
+ const errors = [];
535
+ shellResults.forEach((result, index) => {
536
+ if (result.success) {
537
+ shells.push(result.data.shell);
538
+ }
539
+ else {
540
+ errors.push({ aasId: aasIds[index], error: result.error });
541
+ }
542
+ });
543
+ // If some shells failed to fetch but others succeeded, still return success with available shells
544
+ if (shells.length > 0) {
545
+ return {
546
+ success: true,
547
+ data: {
548
+ shells,
549
+ aasIds,
550
+ },
551
+ };
552
+ }
553
+ // If all failed, return error
554
+ return {
555
+ success: false,
556
+ error: {
557
+ errorType: 'FetchError',
558
+ message: 'Failed to fetch any AAS',
559
+ details: errors,
560
+ },
561
+ };
562
+ }
563
+ /**
564
+ * Resolves a reference to retrieve the endpoints for the referenced AAS, Submodel, and SubmodelElement path.
565
+ *
566
+ * This method parses a Reference object and extracts the AAS ID, Submodel ID, and builds the path
567
+ * to the SubmodelElement if applicable. It supports ModelReferences with keys pointing to:
568
+ * - AssetAdministrationShell
569
+ * - Submodel
570
+ * - SubmodelElements (including nested elements in collections and lists)
571
+ *
572
+ * @param options Object containing:
573
+ * - reference: The Reference object to resolve
574
+ *
575
+ * @returns Either `{ success: true; data: { aasEndpoint?, submodelEndpoint?, submodelElementPath? } }` or `{ success: false; error: ... }`.
576
+ */
577
+ async resolveReference(options) {
578
+ const { reference } = options;
579
+ // Only support ModelReference type
580
+ if (reference.type !== ReferenceTypes.ModelReference) {
581
+ return {
582
+ success: false,
583
+ error: {
584
+ errorType: 'UnsupportedReferenceType',
585
+ message: `Reference type '${reference.type}' is not supported. Only 'ModelReference' is supported.`,
586
+ },
587
+ };
588
+ }
589
+ if (!reference.keys || reference.keys.length === 0) {
590
+ return {
591
+ success: false,
592
+ error: {
593
+ errorType: 'InvalidReference',
594
+ message: 'Reference must contain at least one key',
595
+ },
596
+ };
597
+ }
598
+ let aasEndpoint;
599
+ let submodelEndpoint;
600
+ let submodelElementPath;
601
+ let remainingKeys = [...reference.keys];
602
+ // Check if first key is AssetAdministrationShell
603
+ if (remainingKeys[0].type === KeyTypes.AssetAdministrationShell) {
604
+ const aasId = remainingKeys[0].value;
605
+ const aasEndpointResult = await this.getAasEndpointById({ aasIdentifier: aasId });
606
+ if (aasEndpointResult.success) {
607
+ aasEndpoint = aasEndpointResult.data;
608
+ }
609
+ remainingKeys = remainingKeys.slice(1);
610
+ // Check if next key is Submodel
611
+ if (remainingKeys.length > 0 && remainingKeys[0].type === KeyTypes.Submodel) {
612
+ const submodelId = remainingKeys[0].value;
613
+ const smEndpointResult = await this.submodelService.getSubmodelEndpointById({
614
+ submodelIdentifier: submodelId,
615
+ });
616
+ if (smEndpointResult.success) {
617
+ submodelEndpoint = smEndpointResult.data;
618
+ }
619
+ remainingKeys = remainingKeys.slice(1);
620
+ }
621
+ }
622
+ // Check if first key is Submodel (without AAS context)
623
+ else if (remainingKeys[0].type === KeyTypes.Submodel) {
624
+ const submodelId = remainingKeys[0].value;
625
+ const smEndpointResult = await this.submodelService.getSubmodelEndpointById({
626
+ submodelIdentifier: submodelId,
627
+ });
628
+ if (smEndpointResult.success) {
629
+ submodelEndpoint = smEndpointResult.data;
630
+ }
631
+ remainingKeys = remainingKeys.slice(1);
632
+ }
633
+ // Build submodel element path if we have a submodel endpoint and remaining keys
634
+ if (submodelEndpoint && remainingKeys.length > 0) {
635
+ submodelElementPath = `${submodelEndpoint}/submodel-elements/`;
636
+ for (let i = 0; i < remainingKeys.length; i++) {
637
+ const key = remainingKeys[i];
638
+ // For SubmodelElementList, we need to fetch the list and find the index
639
+ if (i > 0 && remainingKeys[i - 1].type === KeyTypes.SubmodelElementList) {
640
+ // Note: This would require fetching the list to determine the index
641
+ // For now, we'll just append the value as-is
642
+ // In a full implementation, you'd fetch the SME and find the index
643
+ submodelElementPath += `${encodeURIComponent('[' + key.value + ']')}`;
644
+ }
645
+ // For SubmodelElementCollection or other nested elements
646
+ else {
647
+ if (!submodelElementPath.endsWith('/submodel-elements/')) {
648
+ submodelElementPath += '.';
649
+ }
650
+ submodelElementPath += key.value;
651
+ }
652
+ }
653
+ }
654
+ return {
655
+ success: true,
656
+ data: {
657
+ aasEndpoint,
658
+ submodelEndpoint,
659
+ submodelElementPath,
660
+ },
661
+ };
662
+ }
663
+ /**
664
+ * Helper method to create an AAS descriptor from an AssetAdministrationShell.
665
+ * Populates descriptor fields from the shell including metadata and endpoint configuration.
666
+ *
667
+ * @param shell The AssetAdministrationShell to create descriptor from
668
+ * @returns AssetAdministrationShellDescriptor with all populated fields
669
+ */
670
+ createDescriptorFromAas(shell) {
671
+ // Create descriptor with all fields from the shell
672
+ const descriptor = new AssetAdministrationShellDescriptor(shell.id, shell.displayName ?? null, shell.description ?? null, shell.extensions ?? null, shell.administration ?? null, shell.idShort ?? null, shell.assetInformation?.assetKind ?? null, shell.assetInformation?.assetType ?? null, shell.assetInformation?.globalAssetId ?? null, shell.assetInformation?.specificAssetIds ?? null, null, // submodelDescriptors not included here
673
+ null // endpoints set below
674
+ );
675
+ // Set endpoint using repository base path
676
+ const repositoryBasePath = this.repositoryConfig.basePath || 'http://localhost:8081';
677
+ const encodedId = base64Encode(shell.id);
678
+ descriptor.endpoints = [
679
+ {
680
+ _interface: 'AAS-3.0',
681
+ protocolInformation: {
682
+ href: `${repositoryBasePath}/shells/${encodedId}`,
683
+ endpointProtocol: null,
684
+ endpointProtocolVersion: null,
685
+ subprotocol: null,
686
+ subprotocolBody: null,
687
+ subprotocolBodyEncoding: null,
688
+ securityAttributes: null,
689
+ },
690
+ },
691
+ ];
692
+ return descriptor;
693
+ }
694
+ /**
695
+ * Helper method to fetch submodels for a list of shells.
696
+ * Returns a map of shell IDs to their corresponding submodels.
697
+ *
698
+ * @param shells Array of Asset Administration Shells
699
+ * @param includeConceptDescriptions Whether to fetch concept descriptions for submodels
700
+ * @returns Record mapping shell IDs to arrays of Submodels
701
+ */
702
+ async fetchSubmodelsForShells(shells, includeConceptDescriptions) {
703
+ const submodelsMap = {};
704
+ await Promise.all(shells.map(async (shell) => {
705
+ const submodelRefs = shell.submodels || [];
706
+ const submodels = [];
707
+ for (const submodelRef of submodelRefs) {
708
+ // Extract submodel ID from reference
709
+ const submodelId = submodelRef.keys?.[0]?.value;
710
+ if (!submodelId)
711
+ continue;
712
+ // Fetch the submodel
713
+ const submodelResult = await this.submodelService.getSubmodelById({
714
+ submodelIdentifier: submodelId,
715
+ useRegistryEndpoint: false, // Use repository directly for consistency
716
+ includeConceptDescriptions,
717
+ });
718
+ if (submodelResult.success) {
719
+ submodels.push(submodelResult.data.submodel);
720
+ }
721
+ }
722
+ submodelsMap[shell.id] = submodels;
723
+ }));
724
+ return submodelsMap;
725
+ }
726
+ }