3333using System . Text . Json . Nodes ;
3434using System . Threading . Tasks ;
3535
36- namespace IO . Swagger . Registry . Lib . V3 . Services ;
37-
36+ namespace IO . Swagger . Registry . Lib . V3 . Services ;
37+
38+ using System . Drawing . Printing ;
3839using System . Globalization ;
40+ using System . Security . Policy ;
3941using AasxServerDB ;
4042using AdminShellNS ;
4143
@@ -48,6 +50,7 @@ public class RegistryInitializerService : IRegistryInitializerService
4850 private static int initiallyEmpty ;
4951 private static List < string > getRegistry = [ ] ;
5052 private static List < string > postRegistry = [ ] ;
53+ private static List < string > postSubmodelRegistry = [ ] ;
5154 private static List < string ? > federatedElemensSemanticId = [ ] ;
5255 private static List < AssetAdministrationShellDescriptor > aasDescriptorsForSubmodelView = [ ] ;
5356
@@ -155,6 +158,19 @@ public async Task InitRegistry(List<AasxCredentialsEntry> cList, DateTime timest
155158 }
156159 }
157160
161+ if ( p . IdShort ? . ToLower ( CultureInfo . InvariantCulture ) == "postsubmodelregistry" )
162+ {
163+ if ( p . Value != null )
164+ {
165+ var registryURL = TranslateURL ( p . Value ) ;
166+ Console . WriteLine ( "POST to Submodel Registry: " + registryURL ) ;
167+ if ( registryURL != "" )
168+ {
169+ postSubmodelRegistry . Add ( registryURL ) ;
170+ }
171+ }
172+ }
173+
158174 if ( p . IdShort ? . ToLower ( CultureInfo . InvariantCulture ) == "getregistry" )
159175 {
160176 if ( p . Value != null )
@@ -864,74 +880,111 @@ void AddAasToRegistry(AdminShellPackageEnv? env, DateTime timestamp, AssetAdmini
864880 if ( pr == "this" )
865881 {
866882 continue ;
867- }
868-
869- var watch = System . Diagnostics . Stopwatch . StartNew ( ) ;
870- string ? accessToken = null ;
871- var requestPath = pr ;
872- if ( ! requestPath . Contains ( "?" , StringComparison . InvariantCulture ) )
873- {
874- //requestPath = requestPath + "/registry/shell-descriptors";
875- requestPath += "/shell-descriptors" ;
876- }
877-
878- //string json = JsonConvert.SerializeObject(ad);
879- var json = DescriptorSerializer . ToJsonObject ( ad ) ? . ToJsonString ( ) ;
880-
881- var handler = new HttpClientHandler { ServerCertificateCustomValidationCallback = delegate { return true ; } } ;
882-
883- if ( ! requestPath . Contains ( "localhost" ) )
884- {
885- if ( AasxTask . proxy != null )
886- {
887- handler . Proxy = AasxTask . proxy ;
888- }
889- else
890- {
891- handler . DefaultProxyCredentials = CredentialCache . DefaultCredentials ;
892- }
893- }
894-
895- var client = new HttpClient ( handler ) ;
896- if ( accessToken != null )
897- client . SetBearerToken ( accessToken ) ;
898- client . Timeout = TimeSpan . FromSeconds ( 20 ) ;
899-
900- if ( json != "" )
901- {
902- var error = false ;
903- var response = new HttpResponseMessage ( ) ;
904- try
905- {
906- Console . WriteLine ( $ "POST { requestPath } ") ;
907- if ( json != null )
908- {
909- var content = new StringContent ( json , Encoding . UTF8 , "application/json" ) ;
910- var task = Task . Run ( async ( ) =>
911- {
912- response = await client . PostAsync (
913- requestPath , content ) ;
914- } ) ;
915- task . Wait ( ) ;
916- }
917-
918- error = ! response . IsSuccessStatusCode ;
919- }
920- catch
921- {
922- error = true ;
923- }
924-
925- if ( error )
926- {
927- var r = $ "ERROR POST; { response . StatusCode } ; { requestPath } ; { response . Content } ";
928- Console . WriteLine ( r ) ;
929- }
930- }
931-
932- watch . Stop ( ) ;
933- Console . WriteLine ( $ "{ watch . ElapsedMilliseconds } ms") ;
883+ }
884+
885+ transmitDescriptor ( ad , null , pr ) ;
886+
887+ if ( ad . SubmodelDescriptors != null && ad . SubmodelDescriptors . Count != 0 )
888+ {
889+ foreach ( var sd in ad . SubmodelDescriptors )
890+ {
891+ foreach ( var psr in postSubmodelRegistry )
892+ {
893+ if ( psr == "this" )
894+ {
895+ continue ;
896+ }
897+
898+ transmitDescriptor ( null , sd , psr ) ;
899+ }
900+ }
901+ }
934902 }
903+ }
904+
905+ private void transmitDescriptor ( AssetAdministrationShellDescriptor ? ad , SubmodelDescriptor ? sd , string requestPath )
906+ {
907+ var watch = System . Diagnostics . Stopwatch . StartNew ( ) ;
908+ string ? accessToken = null ;
909+
910+ var json = "" ;
911+ if ( ad != null )
912+ {
913+ if ( ! requestPath . Contains ( "?" , StringComparison . InvariantCulture ) )
914+ {
915+ requestPath += "/shell-descriptors" ;
916+ }
917+ json = DescriptorSerializer . ToJsonObject ( ad ) ? . ToJsonString ( ) ;
918+ }
919+ else
920+ {
921+ if ( sd != null )
922+ {
923+ if ( ! requestPath . Contains ( "?" , StringComparison . InvariantCulture ) )
924+ {
925+ requestPath += "/submodel-descriptors" ;
926+ }
927+ json = DescriptorSerializer . ToJsonObject ( sd ) ? . ToJsonString ( ) ;
928+ }
929+ }
930+ if ( json == "" )
931+ {
932+ return ;
933+ }
934+
935+ var handler = new HttpClientHandler { ServerCertificateCustomValidationCallback = delegate { return true ; } } ;
936+
937+ if ( ! requestPath . Contains ( "localhost" ) )
938+ {
939+ if ( AasxTask . proxy != null )
940+ {
941+ handler . Proxy = AasxTask . proxy ;
942+ }
943+ else
944+ {
945+ handler . DefaultProxyCredentials = CredentialCache . DefaultCredentials ;
946+ }
947+ }
948+
949+ var client = new HttpClient ( handler ) ;
950+ if ( accessToken != null )
951+ client . SetBearerToken ( accessToken ) ;
952+ client . Timeout = TimeSpan . FromSeconds ( 20 ) ;
953+
954+ if ( json != "" )
955+ {
956+ var error = false ;
957+ var response = new HttpResponseMessage ( ) ;
958+ try
959+ {
960+ Console . WriteLine ( $ "POST { requestPath } ") ;
961+ if ( json != null )
962+ {
963+ var content = new StringContent ( json , Encoding . UTF8 , "application/json" ) ;
964+ var task = Task . Run ( async ( ) =>
965+ {
966+ response = await client . PostAsync (
967+ requestPath , content ) ;
968+ } ) ;
969+ task . Wait ( ) ;
970+ }
971+
972+ error = ! response . IsSuccessStatusCode ;
973+ }
974+ catch
975+ {
976+ error = true ;
977+ }
978+
979+ if ( error )
980+ {
981+ var r = $ "ERROR POST; { response . StatusCode } ; { requestPath } ; { response . Content } ";
982+ Console . WriteLine ( r ) ;
983+ }
984+ }
985+
986+ watch . Stop ( ) ;
987+ Console . WriteLine ( $ "{ watch . ElapsedMilliseconds } ms") ;
935988 }
936989
937990 public void CreateAssetAdministrationShellDescriptor ( AssetAdministrationShellDescriptor newAasDesc , DateTime timestamp , bool initial = false )
0 commit comments