Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2020 Robert Bosch GmbH |
| 3 | * Author: Constantin Ziesche (constantin.ziesche@bosch.com) |
| 4 | * |
| 5 | * This program and the accompanying materials are made available under the |
| 6 | * terms of the Eclipse Public License 2.0 which is available at |
| 7 | * http://www.eclipse.org/legal/epl-2.0 |
| 8 | * |
| 9 | * SPDX-License-Identifier: EPL-2.0 |
| 10 | *******************************************************************************/ |
| 11 | using BaSyx.Models.Core.AssetAdministrationShell.Generics; |
| 12 | using BaSyx.Utils.ResultHandling; |
| 13 | using System.Collections.Generic; |
| 14 | using System.Linq; |
| 15 | using BaSyx.API.AssetAdministrationShell.Extensions; |
| 16 | using BaSyx.Models.Connectivity.Descriptors; |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 17 | |
| 18 | namespace BaSyx.API.Components |
| 19 | { |
| 20 | public abstract class AssetAdministrationShellServiceProvider : IAssetAdministrationShellServiceProvider, ISubmodelServiceProviderRegistry |
| 21 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 22 | private IAssetAdministrationShell _assetAdministrationShell; |
| 23 | |
| 24 | /// <summary> |
| 25 | /// Stores the Asset Administration Shell built by the BuildAssetAdministationShell() function |
| 26 | /// </summary> |
| 27 | public virtual IAssetAdministrationShell AssetAdministrationShell |
| 28 | { |
| 29 | get |
| 30 | { |
| 31 | if (_assetAdministrationShell == null) |
| 32 | { |
| 33 | IAssetAdministrationShell assetAdministrationShell = BuildAssetAdministrationShell(); |
| 34 | BindTo(assetAdministrationShell); |
| 35 | } |
Constantin Ziesche | eb74d64 | 2020-11-04 17:57:12 +0100 | [diff] [blame^] | 36 | return GetBinding(); |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 37 | } |
| 38 | } |
| 39 | /// <summary> |
| 40 | /// Custom function to build the Asset Administration Shell to be provided by the ServiceProvider. |
| 41 | /// Within this function you can import data (e.g. from AASX-packages, databases, etc.) to build your Asset Administration Shell. |
| 42 | /// </summary> |
| 43 | /// <returns>The built Asset Administration Shell</returns> |
| 44 | public abstract IAssetAdministrationShell BuildAssetAdministrationShell(); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 45 | |
| 46 | private IAssetAdministrationShellDescriptor _serviceDescriptor; |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 47 | |
| 48 | /// <summary> |
| 49 | /// The Asset Administration Shell Descriptor containing the information to register the Service Provider at the next higher instance (e.g. AssetAdministrationShellRepository, AssetAdministrationShellRegistry) |
| 50 | /// </summary> |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 51 | public IAssetAdministrationShellDescriptor ServiceDescriptor |
| 52 | { |
| 53 | get |
| 54 | { |
| 55 | if (_serviceDescriptor == null) |
| 56 | _serviceDescriptor = new AssetAdministrationShellDescriptor(AssetAdministrationShell, null); |
| 57 | |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 58 | if (_serviceDescriptor.SubmodelDescriptors?.Count() == 0 && SubmodelServiceProviders != null) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 59 | foreach (var submodelServiceProvider in SubmodelServiceProviders) |
| 60 | { |
| 61 | if (submodelServiceProvider.Value?.ServiceDescriptor != null) |
| 62 | _serviceDescriptor.SubmodelDescriptors.Create(submodelServiceProvider.Value.ServiceDescriptor); |
| 63 | } |
| 64 | |
| 65 | return _serviceDescriptor; |
| 66 | } |
| 67 | private set |
| 68 | { |
| 69 | _serviceDescriptor = value; |
| 70 | } |
| 71 | } |
| 72 | public ISubmodelServiceProviderRegistry SubmodelRegistry => this; |
| 73 | private Dictionary<string, ISubmodelServiceProvider> SubmodelServiceProviders { get; } = new Dictionary<string, ISubmodelServiceProvider>(); |
| 74 | |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 75 | /// <summary> |
| 76 | /// Base implementation for IAssetAdministrationShellServiceProvider |
| 77 | /// </summary> |
| 78 | protected AssetAdministrationShellServiceProvider() |
| 79 | { } |
| 80 | |
| 81 | protected AssetAdministrationShellServiceProvider(IAssetAdministrationShellDescriptor assetAdministrationShellDescriptor) : this() |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 82 | { |
| 83 | ServiceDescriptor = assetAdministrationShellDescriptor; |
| 84 | } |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 85 | |
| 86 | protected AssetAdministrationShellServiceProvider(IAssetAdministrationShell assetAdministrationShell) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 87 | { |
| 88 | BindTo(assetAdministrationShell); |
| 89 | } |
| 90 | |
Constantin Ziesche | e837f99 | 2020-08-19 12:04:32 +0200 | [diff] [blame] | 91 | public virtual void BindTo(IAssetAdministrationShell element) |
| 92 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 93 | _assetAdministrationShell = element; |
| 94 | ServiceDescriptor = ServiceDescriptor ?? new AssetAdministrationShellDescriptor(_assetAdministrationShell, null); |
Constantin Ziesche | e837f99 | 2020-08-19 12:04:32 +0200 | [diff] [blame] | 95 | } |
| 96 | public virtual IAssetAdministrationShell GetBinding() |
| 97 | { |
Constantin Ziesche | eb74d64 | 2020-11-04 17:57:12 +0100 | [diff] [blame^] | 98 | IAssetAdministrationShell shell = _assetAdministrationShell; |
| 99 | |
| 100 | foreach (var submodelServiceProvider in SubmodelServiceProviders) |
| 101 | { |
| 102 | ISubmodel submodel = submodelServiceProvider.Value.GetBinding(); |
| 103 | shell.Submodels.CreateOrUpdate(submodel.IdShort, submodel); |
| 104 | } |
| 105 | return shell; |
Constantin Ziesche | e837f99 | 2020-08-19 12:04:32 +0200 | [diff] [blame] | 106 | } |
| 107 | |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 108 | public virtual void UseDefaultSubmodelServiceProvider() |
| 109 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 110 | foreach (var submodel in AssetAdministrationShell.Submodels.Values) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 111 | { |
| 112 | var submodelServiceProvider = submodel.CreateServiceProvider(); |
| 113 | RegisterSubmodelServiceProvider(submodel.IdShort, submodelServiceProvider); |
| 114 | } |
| 115 | } |
| 116 | |
Constantin Ziesche | e837f99 | 2020-08-19 12:04:32 +0200 | [diff] [blame] | 117 | public virtual IResult<IEnumerable<ISubmodelServiceProvider>> GetSubmodelServiceProviders() |
| 118 | { |
| 119 | if (SubmodelServiceProviders.Values == null) |
| 120 | return new Result<IEnumerable<ISubmodelServiceProvider>>(false, new NotFoundMessage("Submodel Service Providers")); |
| 121 | |
| 122 | return new Result<IEnumerable<ISubmodelServiceProvider>>(true, SubmodelServiceProviders.Values?.ToList()); |
| 123 | } |
| 124 | |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 125 | public virtual IResult<ISubmodelDescriptor> RegisterSubmodelServiceProvider(string submodelId, ISubmodelServiceProvider submodelServiceProvider) |
| 126 | { |
| 127 | if (SubmodelServiceProviders.ContainsKey(submodelId)) |
| 128 | SubmodelServiceProviders[submodelId] = submodelServiceProvider; |
| 129 | else |
| 130 | SubmodelServiceProviders.Add(submodelId, submodelServiceProvider); |
| 131 | |
| 132 | return new Result<ISubmodelDescriptor>(true, submodelServiceProvider.ServiceDescriptor); |
| 133 | } |
| 134 | public virtual IResult<ISubmodelServiceProvider> GetSubmodelServiceProvider(string submodelId) |
| 135 | { |
| 136 | if (SubmodelServiceProviders.TryGetValue(submodelId, out ISubmodelServiceProvider submodelServiceProvider)) |
| 137 | return new Result<ISubmodelServiceProvider>(true, submodelServiceProvider); |
| 138 | else |
Constantin Ziesche | eb74d64 | 2020-11-04 17:57:12 +0100 | [diff] [blame^] | 139 | return new Result<ISubmodelServiceProvider>(false, new NotFoundMessage(submodelId)); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | public virtual IResult UnregisterSubmodelServiceProvider(string submodelId) |
| 143 | { |
Constantin Ziesche | 7b6d479 | 2020-08-18 17:15:11 +0200 | [diff] [blame] | 144 | if (SubmodelServiceProviders.ContainsKey(submodelId)) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 145 | { |
| 146 | SubmodelServiceProviders.Remove(submodelId); |
Constantin Ziesche | eb74d64 | 2020-11-04 17:57:12 +0100 | [diff] [blame^] | 147 | return new Result(true); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 148 | } |
| 149 | else |
Constantin Ziesche | eb74d64 | 2020-11-04 17:57:12 +0100 | [diff] [blame^] | 150 | return new Result(false, new NotFoundMessage(submodelId)); |
Constantin Ziesche | e837f99 | 2020-08-19 12:04:32 +0200 | [diff] [blame] | 151 | } |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 152 | } |
| 153 | } |