コード例 #1
0
ファイル: DiscoveryExtensions.cs プロジェクト: i-Asset/basyx
 private static void DiscoveryServer_ServiceInstanceShutdown(object sender, ServiceInstanceEventArgs e)
 {
     try
     {
         if (assetAdministrationShellRegistry != null && e.TxtRecords?.Count > 0)
         {
             string aasIdKeyValue = e.TxtRecords.FirstOrDefault(t => t.StartsWith(ASSETADMINISTRATIONSHELL_ID + KEY_VALUE_SEPERATOR));
             if (!string.IsNullOrEmpty(aasIdKeyValue))
             {
                 string[] splittedItem = aasIdKeyValue.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
                 if (splittedItem != null && splittedItem.Length == 2)
                 {
                     if (splittedItem[0] == ASSETADMINISTRATIONSHELL_ID)
                     {
                         var deletedResult = assetAdministrationShellRegistry.DeleteAssetAdministrationShell(splittedItem[1]);
                         if (deletedResult.Success)
                         {
                             deletedResult.LogResult(logger, LogLevel.Info, "Successfully deregistered AAS from registry");
                         }
                         else
                         {
                             deletedResult.LogResult(logger, LogLevel.Error, "Could not unregister AAS from registry");
                         }
                     }
                 }
             }
         }
     }
     catch (Exception exc)
     {
         logger.Error(exc, "Error service instance shutdown");
     }
 }
コード例 #2
0
        private ServiceInstanceEventArgs GetServiceInstanceEventArgs(MessageEventArgs e)
        {
            var servers    = e.Message.AdditionalRecords.OfType <SRVRecord>();
            var addresses  = e.Message.AdditionalRecords.OfType <AddressRecord>();
            var txtRecords = e.Message.AdditionalRecords.OfType <TXTRecord>()?.SelectMany(s => s.Strings);

            if (servers?.Count() > 0 && addresses.Count() > 0)
            {
                foreach (var server in servers)
                {
                    ServiceInstanceEventArgs args = new ServiceInstanceEventArgs();
                    if (txtRecords?.Count() > 0)
                    {
                        args.TxtRecords.AddRange(txtRecords);
                    }

                    logger.Info($"host '{server.Target}' for '{server.Name}' at port '{server.Port}'");
                    var serverAddresses = addresses.Where(w => w.Name == server.Target);
                    if (serverAddresses?.Count() > 0)
                    {
                        foreach (var serverAddress in serverAddresses)
                        {
                            logger.Info($"host '{serverAddress.Name}' at {serverAddress.Address}");
                            args.Servers.Add(new Server()
                            {
                                Name    = server.Name.ToString(),
                                Target  = server.Target.ToString(),
                                Port    = server.Port,
                                Address = serverAddress.Address
                            });
                        }
                        return(args);
                    }
                }
            }
            return(null);
        }
コード例 #3
0
ファイル: DiscoveryExtensions.cs プロジェクト: i-Asset/basyx
        private static async void DiscoveryServer_ServiceInstanceDiscovered(object sender, ServiceInstanceEventArgs e)
        {
            try
            {
                IAssetAdministrationShellDescriptor aasDescriptor = null;
                foreach (var server in e.Servers)
                {
                    bool pingable = await BaSyx.Utils.Network.NetworkUtils.PingHostAsync(server.Address.ToString());

                    if (pingable)
                    {
                        string uri = string.Empty;
                        if (server.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                        {
                            uri = "http://" + server.Address.ToString() + ":" + server.Port + "/aas";
                        }
                        else if (server.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
                        {
                            uri = "http://[" + server.Address.ToString() + "]:" + server.Port + "/aas";
                        }
                        else
                        {
                            continue;
                        }

                        Uri aasEndpoint = new Uri(uri);
                        AssetAdministrationShellHttpClient            client             = new AssetAdministrationShellHttpClient(aasEndpoint);
                        IResult <IAssetAdministrationShellDescriptor> retrieveDescriptor = client.RetrieveAssetAdministrationShellDescriptor();
                        if (retrieveDescriptor.Success && retrieveDescriptor.Entity != null)
                        {
                            retrieveDescriptor.LogResult(logger, LogLevel.Info, "Successfully retrieved AAS descriptor");
                            if (aasDescriptor == null)
                            {
                                aasDescriptor = retrieveDescriptor.Entity;
                                aasDescriptor.SetEndpoints(new List <IEndpoint>()
                                {
                                    new HttpEndpoint(uri)
                                });

                                foreach (var submodelDescriptor in retrieveDescriptor.Entity.SubmodelDescriptors)
                                {
                                    List <IEndpoint> submodelEndpoints = new List <IEndpoint>();
                                    foreach (var submodelEndpoint in submodelDescriptor.Endpoints)
                                    {
                                        if (submodelEndpoint.Address.Contains(server.Address.ToString()))
                                        {
                                            submodelEndpoints.Add(submodelEndpoint);
                                        }
                                    }
                                    aasDescriptor.SubmodelDescriptors[submodelDescriptor.IdShort].SetEndpoints(submodelEndpoints);
                                }
                            }
                            else
                            {
                                aasDescriptor.AddEndpoints(new List <IEndpoint>()
                                {
                                    new HttpEndpoint(uri)
                                });

                                foreach (var submodelDescriptor in retrieveDescriptor.Entity.SubmodelDescriptors)
                                {
                                    List <IEndpoint> submodelEndpoints = new List <IEndpoint>();
                                    foreach (var submodelEndpoint in submodelDescriptor.Endpoints)
                                    {
                                        if (submodelEndpoint.Address.Contains(server.Address.ToString()))
                                        {
                                            if (aasDescriptor.SubmodelDescriptors[submodelDescriptor.IdShort].Endpoints.FirstOrDefault(f => f.Address == submodelEndpoint.Address) == null)
                                            {
                                                submodelEndpoints.Add(submodelEndpoint);
                                            }
                                        }
                                    }
                                    aasDescriptor.SubmodelDescriptors[submodelDescriptor.IdShort].AddEndpoints(submodelEndpoints);
                                }
                            }
                        }
                        else
                        {
                            retrieveDescriptor.LogResult(logger, LogLevel.Info, "Could not retrieve AAS descriptor");
                        }
                    }
                }
                if (aasDescriptor != null)
                {
                    var registeredResult = assetAdministrationShellRegistry.CreateAssetAdministrationShell(aasDescriptor);
                    if (registeredResult.Success)
                    {
                        registeredResult.LogResult(logger, LogLevel.Info, "Successfully registered AAS at registry");
                    }
                    else
                    {
                        registeredResult.LogResult(logger, LogLevel.Error, "Could not register AAS at registry");
                    }
                }
            }
            catch (Exception exc)
            {
                logger.Error(exc, "Error accessing discovered service instance");
            }
        }