blob: ae5b03389d3bd979f931b5a6574d9cd305f9eb08 [file] [log] [blame]
Constantin Zieschefa612082020-04-03 09:54:56 +02001/*******************************************************************************
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*******************************************************************************/
11using BaSyx.AAS.Server.Http;
12using BaSyx.API.AssetAdministrationShell.Extensions;
13using BaSyx.API.Components;
14using BaSyx.Models.Connectivity;
15using BaSyx.Models.Core.AssetAdministrationShell.Generics;
16using BaSyx.Models.Export;
17using Microsoft.Extensions.DependencyInjection;
18using NLog;
Constantin Ziesche08215502020-09-21 19:08:32 +020019using System;
Constantin Zieschefa612082020-04-03 09:54:56 +020020using System.Collections.Generic;
21using System.IO;
Constantin Ziesche08215502020-09-21 19:08:32 +020022using System.IO.Packaging;
Constantin Zieschefa612082020-04-03 09:54:56 +020023using System.Linq;
24using System.Reflection;
25
26namespace 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 Ziesche08215502020-09-21 19:08:32 +020042 {
43 using (BaSyx.Models.Export.AASX aasx = new BaSyx.Models.Export.AASX(args[0], FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
Constantin Zieschefa612082020-04-03 09:54:56 +020044 {
45 AssetAdministrationShellEnvironment_V2_0 environment = aasx.GetEnvironment_V2_0();
Constantin Ziesche08215502020-09-21 19:08:32 +020046 if(environment == null)
Constantin Zieschefa612082020-04-03 09:54:56 +020047 {
Constantin Ziesche08215502020-09-21 19:08:32 +020048 logger.Error("Asset Administration Shell Environment cannot be obtained from AASX-Package " + args[0]);
Constantin Zieschefa612082020-04-03 09:54:56 +020049 return;
50 }
Constantin Ziesche08215502020-09-21 19:08:32 +020051
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 Zieschefa612082020-04-03 09:54:56 +020062 else
63 {
Constantin Ziesche08215502020-09-21 19:08:32 +020064 logger.Error("No Asset Administration Shells found AASX-Package " + args[0]);
65 return;
Constantin Zieschefa612082020-04-03 09:54:56 +020066 }
67 }
68 }
Constantin Ziesche08215502020-09-21 19:08:32 +020069 Console.Read();
Constantin Zieschefa612082020-04-03 09:54:56 +020070 logger.Info("Application shut down");
71 }
Constantin Ziesche08215502020-09-21 19:08:32 +020072
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 Zieschefa612082020-04-03 09:54:56 +0200159 }
160}