Skip to content

Commit

Permalink
PCF demo with Basyx
Browse files Browse the repository at this point in the history
  • Loading branch information
aorzelskiGH committed Jan 18, 2024
1 parent 986afb1 commit 3c25af6
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 8 deletions.
18 changes: 16 additions & 2 deletions src/AasxServerBlazor/Pages/Pcf2.razor
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
@using System.Drawing;
@using System.Globalization;
@using static AasxServer.AasxTask;
@inject NavigationManager NavMan
@implements IDisposable

@{
Expand Down Expand Up @@ -283,12 +284,25 @@
string iframePath = getIframePath();
link = getAasLink(@node.aas);
// JUIJUI
// var registry = IO.Swagger.Registry.Controllers.RegistryAndDiscoveryInterfaceApiController.getRegistry;
var registry = AasRegistryService.GetRegistryList();
if (registry.Count != 0)
{
link += "&registryURL=" + registry[0];
// check for buffered shell descriptors from getregistry() for basyx
var aasDescriptors = AasRegistryService.GetAasDescriptorsForSubmodelView();

if (aasDescriptors != null && aasDescriptors.Count != 0)
{
string registryURL = NavMan.Uri;
registryURL = registryURL.Substring(0, registryURL.Length - "/pcf".Length);
Console.WriteLine("registryURL " + registryURL);
link += "&registryURL=" + registryURL;
}
else
{
link += "&registryURL=" + registry[0];
}
}

// JUIJUI
Expand Down
7 changes: 4 additions & 3 deletions src/AasxServerBlazor/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
},
"AasxServerBlazor": {
"commandName": "Project",
"commandLineArgs": "--save-temp -1 --no-security --aasx-in-memory 1000 --data-path \"C:\\Development\\basyxV2\" --edit --external-blazor http://localhost:5001",
"commandLineArgs": "--no-security --aasx-in-memory 1000 --data-path \"C:\\Development\\eis-view\" --edit --external-blazor http://localhost:5001",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"AASREGISTRY": "http://localhost:5002",
"AASREGISTRY": "https://basyx-aasreg.h2894164.stratoserver.net/api/v3.0",
"IFRAMEPATH": "https://dpp40-2-v2.industrialdigitaltwin.org/dashboard/submodelViewV3.html",
"AASREGISTRY2": "https://pcf-v3-registry.h2894164.stratoserver.net"
"AASREGISTRY2": "https://pcf-v3-registry.h2894164.stratoserver.net",
"SUBMODELREGISTRY": "https://basyx-subreg.h2894164.stratoserver.net/api/v3.0"
},
"applicationUrl": "http://localhost:5001",
"jsWebView2Debugging": true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public virtual IActionResult GetAllAssetAdministrationShellDescriptors([FromQuer
List<AssetAdministrationShellDescriptor> aasDescriptors;
if (!Program.withDb)
{
// from memory
// from AAS memory
aasDescriptors = _aasRegistryService.GetAllAssetAdministrationShellDescriptors(assetKind, assetList);
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public interface IRegistryInitializerService
void CreateAssetAdministrationShellDescriptor(AssetAdministrationShellDescriptor newAasDesc, DateTime timestamp, bool initial = false);
void CreateMultipleAssetAdministrationShellDescriptor(List<AssetAdministrationShellDescriptor> body, DateTime timestamp);
ISubmodel GetAasRegistry();

List<AssetAdministrationShellDescriptor> GetAasDescriptorsForSubmodelView();
List<string> GetRegistryList();
void InitRegistry(List<AasxCredentialsEntry> cList, DateTime timestamp, bool initAgain = false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ internal static AssetAdministrationShellDescriptor AssetAdministrationShellDescr
return new AssetAdministrationShellDescriptor(administrativeInformation, assetKind, assetType, endpoints, globalAssetId, idShort, id, specificAssetIds, submodelDescriptors);
}

private static SubmodelDescriptor SubmodelDescriptorFrom(JsonNode node, out Reporting.Error error)
internal static SubmodelDescriptor SubmodelDescriptorFrom(JsonNode node, out Reporting.Error error)
{
error = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,21 @@ public static AssetAdministrationShellDescriptor AssetAdministrationShellDescrip
?? throw new System.InvalidOperationException(
"Unexpected output null when error is null");
}

public static SubmodelDescriptor SubmodelDescriptorFrom(JsonNode node)
{
SubmodelDescriptor? result = DescriptorDeserializeImplementation.SubmodelDescriptorFrom(
node,
out Reporting.Error? error);
if (error != null)
{
throw new Jsonization.Exception(
Reporting.GenerateJsonPath(error.PathSegments),
error.Cause);
}
return result
?? throw new System.InvalidOperationException(
"Unexpected output null when error is null");
}
}
}
25 changes: 25 additions & 0 deletions src/IO.Swagger.Registry.Lib.V3/Services/AasRegistryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Linq;
using System.Text.Json;
using System.Text.Json.Nodes;
using static AasxServer.Program;

namespace IO.Swagger.Registry.Lib.V3.Services
{
Expand Down Expand Up @@ -91,6 +92,30 @@ public List<AssetAdministrationShellDescriptor> GetAllAssetAdministrationShellDe
if (aasIdentifier != null && assetList != null)
return result;

// Check for stored combined basyx list for descriptors by getRegistry()
var aasDescriptors = _registryInitializerService.GetAasDescriptorsForSubmodelView();

if (aasDescriptors != null && aasDescriptors.Count != 0)
{
foreach (var ad in aasDescriptors)
{
bool found = false;
if (aasIdentifier == null && assetList.IsNullOrEmpty())
found = true;
if (aasIdentifier != null)
{
if (aasIdentifier.Equals(ad.Id))
{
found = true;
}
}
if (found)
result.Add(ad);
}

return result;
}

var aasRegistry = _registryInitializerService.GetAasRegistry();
if (aasRegistry != null)
{
Expand Down
120 changes: 119 additions & 1 deletion src/IO.Swagger.Registry.Lib.V3/Services/RegistryInitializerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using System.Text;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using static AasxServer.Program;

namespace IO.Swagger.Registry.Lib.V3.Services
{
Expand All @@ -35,6 +36,7 @@ public class RegistryInitializerService : IRegistryInitializerService
static List<string> postRegistry = new List<string>();
static List<string> federatedElemensSemanticId = new List<string>();
static int submodelRegistryCount = 0;
static List<AssetAdministrationShellDescriptor> aasDescriptorsForSubmodelView = new List<AssetAdministrationShellDescriptor>();

public List<string> GetRegistryList()
{
Expand All @@ -44,6 +46,10 @@ public ISubmodel GetAasRegistry()
{
return aasRegistry;
}
public List<AssetAdministrationShellDescriptor> GetAasDescriptorsForSubmodelView()
{
return aasDescriptorsForSubmodelView;
}

public static X509Certificate2 certificate = null;

Expand Down Expand Up @@ -185,6 +191,86 @@ public void InitRegistry(List<AasxCredentialsEntry> cList, DateTime timestamp, b
}
if (getRegistry.Count != 0)
{
var submodelDescriptors = new List<SubmodelDescriptor>();
string submodelRegistryUrl = System.Environment.GetEnvironmentVariable("SUBMODELREGISTRY");
if (submodelRegistryUrl != null)
{
string json = null;
string accessToken = null;

submodelRegistryUrl = submodelRegistryUrl.Replace("\r", "");
submodelRegistryUrl = submodelRegistryUrl.Replace("\n", "");

// basyx with Submodel Registry: read submodel descriptors
string requestPath = submodelRegistryUrl + "/submodel-descriptors";

var handler = new HttpClientHandler();

if (!requestPath.Contains("localhost"))
{
if (AasxServer.AasxTask.proxy != null)
handler.Proxy = AasxServer.AasxTask.proxy;
else
handler.DefaultProxyCredentials = CredentialCache.DefaultCredentials;
}

var client = new HttpClient(handler);
client.Timeout = TimeSpan.FromSeconds(10);
if (accessToken != null)
client.SetBearerToken(accessToken);

bool error = false;
HttpResponseMessage response = new HttpResponseMessage();
try
{
Console.WriteLine("GET " + requestPath);
var task = Task.Run(async () =>
{
response = await client.GetAsync(requestPath);
});
task.Wait();
json = response.Content.ReadAsStringAsync().Result;
// TODO (jtikekar, 2023-09-04): check this call flow
// aasDescriptors = JsonConvert.DeserializeObject<List<AssetAdministrationShellDescriptor>>(json);
if (!string.IsNullOrEmpty(json))
{
MemoryStream mStrm = new MemoryStream(Encoding.UTF8.GetBytes(json));
JsonNode node = System.Text.Json.JsonSerializer.DeserializeAsync<JsonNode>(mStrm).Result;
if (node is JsonObject jo)
{
if (jo.ContainsKey("result"))
{
node = (JsonNode)jo["result"];
if (node is JsonArray a)
{
foreach (JsonNode n in a)
{
if (n != null)
submodelDescriptors.Add(DescriptorDeserializer.SubmodelDescriptorFrom(n));
}
}
}
}
}
error = !response.IsSuccessStatusCode;
}
catch
{
error = true;
}
if (error)
{
string r = "ERROR GET; " + response.StatusCode.ToString();
r += " ; " + requestPath;
if (response.Content != null)
r += " ; " + response.Content.ReadAsStringAsync().Result;
Console.WriteLine(r);
}
else // OK
{
}
}

foreach (var greg in getRegistry)
{
List<AssetAdministrationShellDescriptor> aasDescriptors = new List<AssetAdministrationShellDescriptor>();
Expand Down Expand Up @@ -236,7 +322,39 @@ public void InitRegistry(List<AasxCredentialsEntry> cList, DateTime timestamp, b
foreach (JsonNode n in a)
{
if (n != null)
aasDescriptors.Add(DescriptorDeserializer.AssetAdministrationShellDescriptorFrom(n));
{
var ad = DescriptorDeserializer.AssetAdministrationShellDescriptorFrom(n);
aasDescriptors.Add(ad);
if (ad.SubmodelDescriptors == null)
ad.SubmodelDescriptors = new List<SubmodelDescriptor>();
if (ad.SubmodelDescriptors.Count == 0)
{
requestPath = ad.Endpoints[0].ProtocolInformation.Href;
Console.WriteLine("GET " + requestPath);
task = Task.Run(async () =>
{
response = await client.GetAsync(requestPath);
});
task.Wait();
json = response.Content.ReadAsStringAsync().Result;
mStrm = new MemoryStream(Encoding.UTF8.GetBytes(json));
node = System.Text.Json.JsonSerializer.DeserializeAsync<JsonNode>(mStrm).Result;
var aas = Jsonization.Deserialize.AssetAdministrationShellFrom(node);

var ids = new List<string>();
foreach (var s in aas.Submodels)
{
var id = s.Keys[0].Value;
ids.Add(id);
}
foreach (var sd in submodelDescriptors)
{
if (ids.Contains(sd.Id))
ad.SubmodelDescriptors.Add(sd);
}
aasDescriptorsForSubmodelView.Add(ad);
}
}
}
}
}
Expand Down

0 comments on commit 3c25af6

Please sign in to comment.