Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [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.AAS.Server.Http; |
| 12 | using BaSyx.API.AssetAdministrationShell.Extensions; |
| 13 | using BaSyx.API.Components; |
| 14 | using BaSyx.Models.Connectivity; |
| 15 | using BaSyx.Models.Core.AssetAdministrationShell.Generics; |
| 16 | using BaSyx.Models.Export; |
| 17 | using Microsoft.Extensions.DependencyInjection; |
| 18 | using NLog; |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame^] | 19 | using System; |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 20 | using System.Collections.Generic; |
| 21 | using System.IO; |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame^] | 22 | using System.IO.Packaging; |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 23 | using System.Linq; |
| 24 | using System.Reflection; |
| 25 | |
| 26 | namespace BaSyx.AASX.Server.Http |
| 27 | { |
| 28 | class Program |
| 29 | { |
| 30 | private static readonly ILogger logger = LogManager.GetCurrentClassLogger(); |
| 31 | |
| 32 | static void Main(string[] args) |
| 33 | { |
| 34 | logger.Info("Starting AASX Hoster..."); |
| 35 | |
| 36 | if(args?.Length == 0) |
| 37 | { |
| 38 | logger.Error("No arguments provided --> Application is shutting down..."); |
| 39 | return; |
| 40 | } |
| 41 | if (!string.IsNullOrEmpty(args[0]) && File.Exists(args[0])) |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame^] | 42 | { |
| 43 | using (BaSyx.Models.Export.AASX aasx = new BaSyx.Models.Export.AASX(args[0], FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 44 | { |
| 45 | AssetAdministrationShellEnvironment_V2_0 environment = aasx.GetEnvironment_V2_0(); |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame^] | 46 | if(environment == null) |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 47 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame^] | 48 | logger.Error("Asset Administration Shell Environment cannot be obtained from AASX-Package " + args[0]); |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 49 | return; |
| 50 | } |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame^] | 51 | |
| 52 | logger.Info("AASX-Package successfully loaded"); |
| 53 | |
| 54 | if (environment.AssetAdministrationShells.Count == 1) |
| 55 | { |
| 56 | LoadSingleAssetAdministrationShellServer(environment.AssetAdministrationShells.ElementAt(0), aasx.SupplementaryFiles, args); |
| 57 | } |
| 58 | else if (environment.AssetAdministrationShells.Count > 1) |
| 59 | { |
| 60 | LoadMultiAssetAdministrationShellServer(environment.AssetAdministrationShells, aasx.SupplementaryFiles, args); |
| 61 | } |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 62 | else |
| 63 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame^] | 64 | logger.Error("No Asset Administration Shells found AASX-Package " + args[0]); |
| 65 | return; |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | } |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame^] | 69 | Console.Read(); |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 70 | logger.Info("Application shut down"); |
| 71 | } |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame^] | 72 | |
| 73 | private static void LoadMultiAssetAdministrationShellServer(List<IAssetAdministrationShell> assetAdministrationShells, List<PackagePart> supplementaryFiles, string[] args) |
| 74 | { |
| 75 | MultiAssetAdministrationShellHttpServer multiServer = new MultiAssetAdministrationShellHttpServer(); |
| 76 | AssetAdministrationShellRepositoryServiceProvider repositoryService = new AssetAdministrationShellRepositoryServiceProvider(); |
| 77 | |
| 78 | foreach (var shell in assetAdministrationShells) |
| 79 | { |
| 80 | var aasServiceProvider = shell.CreateServiceProvider(true); |
| 81 | repositoryService.RegisterAssetAdministrationShellServiceProvider(shell.IdShort, aasServiceProvider); |
| 82 | } |
| 83 | |
| 84 | List<HttpEndpoint> endpoints; |
| 85 | if (args.Length == 2 && !string.IsNullOrEmpty(args[1])) |
| 86 | { |
| 87 | logger.Info("Using " + args[1] + " as host url"); |
| 88 | endpoints = new List<HttpEndpoint>() { new HttpEndpoint(args[1]) }; |
| 89 | } |
| 90 | else |
| 91 | { |
| 92 | endpoints = multiServer.Settings.ServerConfig.Hosting.Urls.ConvertAll(c => |
| 93 | { |
| 94 | string address = c.Replace("+", "127.0.0.1"); |
| 95 | logger.Info("Using " + address + " as host url"); |
| 96 | return new HttpEndpoint(address); |
| 97 | }); |
| 98 | |
| 99 | } |
| 100 | |
| 101 | repositoryService.UseDefaultEndpointRegistration(endpoints); |
| 102 | multiServer.SetServiceProvider(repositoryService); |
| 103 | |
| 104 | foreach (var file in supplementaryFiles) |
| 105 | { |
| 106 | using (Stream stream = file.GetStream()) |
| 107 | { |
| 108 | logger.Info("Providing content on server: " + file.Uri); |
| 109 | multiServer.ProvideContent(file.Uri, stream); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | multiServer.RunAsync(); |
| 114 | } |
| 115 | |
| 116 | private static void LoadSingleAssetAdministrationShellServer(IAssetAdministrationShell assetAdministrationShell, List<PackagePart> supplementaryFiles, string[] args) |
| 117 | { |
| 118 | AssetAdministrationShellHttpServer server = new AssetAdministrationShellHttpServer(); |
| 119 | IAssetAdministrationShellServiceProvider service = assetAdministrationShell.CreateServiceProvider(true); |
| 120 | |
| 121 | List<HttpEndpoint> endpoints; |
| 122 | if (args.Length == 2 && !string.IsNullOrEmpty(args[1])) |
| 123 | { |
| 124 | logger.Info("Using " + args[1] + " as host url"); |
| 125 | endpoints = new List<HttpEndpoint>() { new HttpEndpoint(args[1]) }; |
| 126 | } |
| 127 | else |
| 128 | { |
| 129 | endpoints = server.Settings.ServerConfig.Hosting.Urls.ConvertAll(c => |
| 130 | { |
| 131 | string address = c.Replace("+", "127.0.0.1"); |
| 132 | logger.Info("Using " + address + " as host url"); |
| 133 | return new HttpEndpoint(address); |
| 134 | }); |
| 135 | |
| 136 | } |
| 137 | service.UseDefaultEndpointRegistration(endpoints); |
| 138 | server.SetServiceProvider(service); |
| 139 | |
| 140 | server.ConfigureServices(services => |
| 141 | { |
| 142 | Assembly controllerAssembly = Assembly.Load("BaSyx.API.Http.Controllers.AASX"); |
| 143 | services.AddMvc() |
| 144 | .AddApplicationPart(controllerAssembly) |
| 145 | .AddControllersAsServices(); |
| 146 | }); |
| 147 | |
| 148 | foreach (var file in supplementaryFiles) |
| 149 | { |
| 150 | using (Stream stream = file.GetStream()) |
| 151 | { |
| 152 | logger.Info("Providing content on server: " + file.Uri); |
| 153 | server.ProvideContent(file.Uri, stream); |
| 154 | } |
| 155 | } |
| 156 | logger.Info("Server is starting up..."); |
| 157 | server.RunAsync(); |
| 158 | } |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 159 | } |
| 160 | } |