async Task HandleDesiredPropertiesUpdate(Core.IMessage desiredPropertiesUpdate) { try { TwinCollection twinCollection = this.twinCollectionMessageConverter.FromMessage(desiredPropertiesUpdate); using (await this.configLock.LockAsync()) { Option <EdgeHubConfig> edgeHubConfig = await this.lastDesiredProperties .Map(e => this.PatchDesiredProperties(e, twinCollection)) .GetOrElse(() => this.GetConfigInternal()); await edgeHubConfig.ForEachAsync( async config => { if (this.configUpdateCallback != null) { await this.configUpdateCallback(config); } }); } } catch (Exception ex) { Events.ErrorHandlingDesiredPropertiesUpdate(ex); } }
// This method updates local state and should be called only after acquiring edgeHubConfigLock async Task <Option <EdgeHubConfig> > GetConfigInternal() { Option <EdgeHubConfig> edgeHubConfig; try { Core.IMessage message = await this.twinManager.GetTwinAsync(this.id); Twin twin = this.twinMessageConverter.FromMessage(message); this.lastDesiredProperties = Option.Some(twin.Properties.Desired); try { var desiredProperties = JsonConvert.DeserializeObject <EdgeHubDesiredProperties>(twin.Properties.Desired.ToJson()); edgeHubConfig = Option.Some(this.configParser.GetEdgeHubConfig(desiredProperties)); await this.UpdateReportedProperties(twin.Properties.Desired.Version, new LastDesiredStatus(200, string.Empty)); Events.GetConfigSuccess(); } catch (Exception ex) { await this.UpdateReportedProperties(twin.Properties.Desired.Version, new LastDesiredStatus(400, $"Error while parsing desired properties - {ex.Message}")); throw; } } catch (Exception ex) { edgeHubConfig = Option.None <EdgeHubConfig>(); Events.ErrorGettingEdgeHubConfig(ex); } return(edgeHubConfig); }
public Task OnDesiredPropertyUpdates(IMessage desiredProperties) { desiredProperties.SystemProperties[SystemProperties.OutboundUri] = Constants.OutboundUriTwinDesiredPropertyUpdate; this.channel.Handle(this.messageConverter.FromMessage(desiredProperties)); Events.SentDesiredPropertyUpdate(this.Identity); return(Task.FromResult(true)); }
public Task SendTwinUpdate(IMessage twin) { twin.SystemProperties[SystemProperties.LockToken] = Constants.TwinLockToken; twin.SystemProperties[SystemProperties.OutboundUri] = Constants.OutboundUriTwinEndpoint; IProtocolGatewayMessage pgMessage = this.messageConverter.FromMessage(twin); this.channel.Handle(pgMessage); return(Task.CompletedTask); }
public Task SendC2DMessageAsync(IMessage message) { message.SystemProperties[TemplateParameters.DeviceIdTemplateParam] = this.Identity.Id; message.SystemProperties[SystemProperties.OutboundUri] = Constants.OutboundUriC2D; IProtocolGatewayMessage pgMessage = this.messageConverter.FromMessage(message); this.channel.Handle(pgMessage); Events.SendMessage(this.Identity); return(Task.FromResult(true)); }
// This method updates local state and should be called only after acquiring edgeHubConfigLock async Task <Option <EdgeHubConfig> > GetConfigInternal() { Option <EdgeHubConfig> edgeHubConfig; try { Core.IMessage message = await this.twinManager.GetTwinAsync(this.id); Twin twin = this.twinMessageConverter.FromMessage(message); TwinCollection desiredProperties = twin.Properties.Desired; Events.LogDesiredPropertiesAfterFullTwin(desiredProperties); if (!this.CheckIfManifestSigningIsEnabled(desiredProperties)) { Events.ManifestSigningIsEnabled(); } else { Events.ManifestSigningIsNotEnabled(); if (this.ExtractHubTwinAndVerify(desiredProperties)) { Events.VerifyTwinSignatureSuceeded(); } else { Events.VerifyTwinSignatureFailed(); return(Option.None <EdgeHubConfig>()); } } this.lastDesiredProperties = Option.Some(desiredProperties); try { edgeHubConfig = Option.Some(this.configParser.GetEdgeHubConfig(twin.Properties.Desired.ToJson())); await this.UpdateReportedProperties(twin.Properties.Desired.Version, new LastDesiredStatus(200, string.Empty)); Events.GetConfigSuccess(); } catch (Exception ex) { await this.UpdateReportedProperties(twin.Properties.Desired.Version, new LastDesiredStatus(400, $"Error while parsing desired properties - {ex.Message}")); throw; } } catch (Exception ex) { edgeHubConfig = Option.None <EdgeHubConfig>(); Events.ErrorGettingEdgeHubConfig(ex); } return(edgeHubConfig); }
Task UpdateReportedProperties(long desiredVersion, LastDesiredStatus desiredStatus) { try { var edgeHubReportedProperties = new ReportedProperties(this.versionInfo, desiredVersion, desiredStatus); var twinCollection = new TwinCollection(JsonConvert.SerializeObject(edgeHubReportedProperties)); Core.IMessage reportedPropertiesMessage = this.twinCollectionMessageConverter.ToMessage(twinCollection); return(this.twinManager.UpdateReportedPropertiesAsync(this.id, reportedPropertiesMessage)); } catch (Exception ex) { Events.ErrorUpdatingLastDesiredStatus(ex); return(Task.CompletedTask); } }
public Task SendMessageAsync(IMessage message, string input) { bool result = false; if (this.Identity is IModuleIdentity moduleIdentity) { message.SystemProperties[TemplateParameters.DeviceIdTemplateParam] = moduleIdentity.DeviceId; message.SystemProperties[Constants.ModuleIdTemplateParameter] = moduleIdentity.ModuleId; message.SystemProperties[SystemProperties.InputName] = input; message.SystemProperties[SystemProperties.OutboundUri] = Constants.OutboundUriModuleEndpoint; IProtocolGatewayMessage pgMessage = this.messageConverter.FromMessage(message); this.channel.Handle(pgMessage); result = true; } return(Task.FromResult(result)); }
async Task HandleDesiredPropertiesUpdate(Core.IMessage desiredPropertiesUpdate) { try { TwinCollection patch = this.twinCollectionMessageConverter.FromMessage(desiredPropertiesUpdate); using (await this.configLock.LockAsync()) { Option <EdgeHubConfig> edgeHubConfig = await this.lastDesiredProperties .Map(baseline => this.PatchDesiredProperties(baseline, patch)) .GetOrElse(() => this.GetConfigInternal()); edgeHubConfig.ForEach( config => { this.ConfigUpdated?.Invoke(this, config); }); } } catch (Exception ex) { Events.ErrorHandlingDesiredPropertiesUpdate(ex); } }
public Commands.ICommand UpdateState(Core.IMessage command) { throw new NotImplementedException(); }