11using Certify . Management ;
22using Certify . Models . Hub ;
33using Certify . Models . Reporting ;
4+ using Certify . Shared ;
45using Microsoft . AspNetCore . SignalR ;
56
67namespace Certify . Server . Hub . Api . SignalR . ManagementHub
@@ -123,15 +124,18 @@ public override Task OnDisconnectedAsync(Exception? exception)
123124 {
124125 var instanceId = _stateProvider . GetInstanceIdForConnection ( Context . ConnectionId ) ;
125126
126- _stateProvider . UpdateInstanceConnectionStatus ( instanceId , ConnectionStatus . Disconnected ) ;
127-
128- if ( exception != null )
129- {
130- _logger ? . LogError ( "InstanceManagementHub: Instance {instanceId} disconnected unexpectedly from instance management hub. {exp}" , instanceId , exception ) ;
131- }
132- else
127+ if ( instanceId != null )
133128 {
134- _logger ? . LogInformation ( "InstanceManagementHub: Instance {instanceId} disconnected from instance management hub, with no error." , instanceId ) ;
129+ _stateProvider . UpdateInstanceConnectionStatus ( instanceId , ConnectionStatus . Disconnected ) ;
130+
131+ if ( exception != null )
132+ {
133+ _logger ? . LogError ( "InstanceManagementHub: Instance {instanceId} disconnected unexpectedly from instance management hub. {exp}" , instanceId , exception ) ;
134+ }
135+ else
136+ {
137+ _logger ? . LogInformation ( "InstanceManagementHub: Instance {instanceId} disconnected from instance management hub, with no error." , instanceId ) ;
138+ }
135139 }
136140
137141 return base . OnDisconnectedAsync ( exception ) ;
@@ -199,19 +203,19 @@ private async Task ProcessInstanceCommandResult(InstanceCommandResult result, In
199203 // action this message from this instance
200204 _logger ? . LogDebug ( "[ProcessInstanceCommandResult] Received instance command result {instanceId} {cmdType}" , instanceId , cmd . CommandType ) ;
201205
202- if ( cmd . CommandType == ManagementHubCommands . GetManagedItems )
206+ if ( cmd . CommandType == ManagementHubCommands . GetManagedItems && result . Value != null )
203207 {
204208 // got items from an instance
205- var val = System . Text . Json . JsonSerializer . Deserialize < ManagedInstanceItems > ( result . Value ) ;
209+ var val = System . Text . Json . JsonSerializer . Deserialize < ManagedInstanceItems > ( result . Value , JsonOptions . DefaultJsonSerializerOptions ) ;
206210
207- _stateProvider . UpdateInstanceItemInfo ( instanceId , val . Items ) ;
211+ _stateProvider . UpdateInstanceItemInfo ( instanceId , val ! . Items ) ;
208212 }
209- else if ( cmd . CommandType == ManagementHubCommands . GetStatusSummary && result ? . Value != null )
213+ else if ( cmd . CommandType == ManagementHubCommands . GetStatusSummary && result . Value != null )
210214 {
211215 // got status summary
212- var val = System . Text . Json . JsonSerializer . Deserialize < StatusSummary > ( result . Value ) ;
216+ var val = System . Text . Json . JsonSerializer . Deserialize < StatusSummary > ( result . Value , JsonOptions . DefaultJsonSerializerOptions ) ;
213217
214- _stateProvider . UpdateInstanceStatusSummary ( instanceId , val ) ;
218+ _stateProvider . UpdateInstanceStatusSummary ( instanceId , val ! ) ;
215219 }
216220 else if ( result . IsCommandResponse )
217221 {
@@ -220,15 +224,15 @@ private async Task ProcessInstanceCommandResult(InstanceCommandResult result, In
220224 else
221225 {
222226 // item was not requested, queue for processing
223- if ( result . CommandType == ManagementHubCommands . NotificationUpdatedManagedItem )
227+ if ( result . CommandType == ManagementHubCommands . NotificationUpdatedManagedItem && result . Value != null )
224228 {
225- await _uiStatusHub . Clients . All . SendAsync ( Providers . StatusHubMessages . SendManagedCertificateUpdateMsg , System . Text . Json . JsonSerializer . Deserialize < Models . ManagedCertificate > ( result . Value ) ) ;
229+ await _uiStatusHub . Clients . All . SendAsync ( Providers . StatusHubMessages . SendManagedCertificateUpdateMsg , System . Text . Json . JsonSerializer . Deserialize < Models . ManagedCertificate > ( result . Value , JsonOptions . DefaultJsonSerializerOptions ) ) ;
226230 }
227- else if ( result . CommandType == ManagementHubCommands . NotificationManagedItemRequestProgress )
231+ else if ( result . CommandType == ManagementHubCommands . NotificationManagedItemRequestProgress && result . Value != null )
228232 {
229- await _uiStatusHub . Clients . All . SendAsync ( Providers . StatusHubMessages . SendProgressStateMsg , System . Text . Json . JsonSerializer . Deserialize < Models . RequestProgressState > ( result . Value ) ) ;
233+ await _uiStatusHub . Clients . All . SendAsync ( Providers . StatusHubMessages . SendProgressStateMsg , System . Text . Json . JsonSerializer . Deserialize < Models . RequestProgressState > ( result . Value , JsonOptions . DefaultJsonSerializerOptions ) ) ;
230234 }
231- else if ( result . CommandType == ManagementHubCommands . NotificationRemovedManagedItem )
235+ else if ( result . CommandType == ManagementHubCommands . NotificationRemovedManagedItem && result . Value != null )
232236 {
233237 // deleted :TODO
234238 await _uiStatusHub . Clients . All . SendAsync ( Providers . StatusHubMessages . SendMsg , $ "Deleted item { result . Value } ") ;
@@ -238,11 +242,10 @@ private async Task ProcessInstanceCommandResult(InstanceCommandResult result, In
238242
239243 private async Task ProcessInstanceInfoResult ( InstanceCommandResult result )
240244 {
241- var instanceInfo = System . Text . Json . JsonSerializer . Deserialize < ManagedInstanceInfo > ( result . Value ) ;
245+ var instanceInfo = result . Value == null ? null : System . Text . Json . JsonSerializer . Deserialize < ManagedInstanceInfo > ( result . Value , JsonOptions . DefaultJsonSerializerOptions ) ;
242246
243247 if ( instanceInfo != null )
244248 {
245-
246249 instanceInfo . LastReported = DateTimeOffset . UtcNow ;
247250 _stateProvider . UpdateInstanceConnectionInfo ( Context ? . ConnectionId ?? _localInstanceId , instanceInfo ) ;
248251
@@ -274,9 +277,13 @@ private async Task ProcessInstanceInfoResult(InstanceCommandResult result)
274277 }
275278 }
276279
280+ /// <summary>
281+ /// Receives a message from an instance and logs the message details.
282+ /// </summary>
283+ /// <param name="message">The message received from the instance.</param>
284+ /// <returns>A task that represents the asynchronous operation.</returns>
277285 public Task ReceiveInstanceMessage ( InstanceMessage message )
278286 {
279-
280287 var instanceId = _stateProvider . GetInstanceIdForConnection ( Context ? . ConnectionId ?? _localInstanceId ) ;
281288 if ( instanceId != null )
282289 {
0 commit comments