Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify models for IoTHub support hence support trimming #565

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 19 additions & 33 deletions samples/AzureIotHub/AzureIotEventsConsumer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,49 +19,35 @@ public AzureIotEventsConsumer(ILogger<AzureIotEventsConsumer> logger)
public Task ConsumeAsync(EventContext<MyIotHubEvent> context, CancellationToken cancellationToken)
{
var evt = context.Event;
var source = evt.Source;
if (source == IotHubEventMessageSource.Telemetry)
if (evt.IsTelemetry)
{
var telemetry = evt.Telemetry!;
var telemetry = evt.GetTelemetry<MyIotHubTelemetry>();
var deviceId = context.GetIotHubDeviceId();
var enqueued = context.GetIotHubEnqueuedTime();

logger.LogInformation("Received {Source} from {DeviceId}\r\nEnqueued: {EnqueuedTime}\r\nTimestamped: {Timestamp}\r\nTelemetry:{Telemetry}",
source,
logger.LogInformation("Received Telemetry from {DeviceId}\r\nEnqueued: {EnqueuedTime}\r\nTimestamped: {Timestamp}\r\nTelemetry:{Telemetry}",
deviceId,
enqueued,
telemetry.Timestamp,
JsonSerializer.Serialize(telemetry, serializerOptions));
}
else if (source == IotHubEventMessageSource.TwinChangeEvents)
else
{
var @tce = evt.TwinEvent!;
logger.LogInformation("TwinChange event received of type '{Type}' from '{DeviceId}{ModuleId}' in '{HubName}'.\r\nEvent:{Event}",
tce.Type,
tce.DeviceId,
tce.ModuleId,
tce.HubName,
JsonSerializer.Serialize(tce.Event, serializerOptions));
}
else if (source == IotHubEventMessageSource.DeviceLifecycleEvents)
{
var lce = evt.LifecycleEvent!;
logger.LogInformation("Device Lifecycle event received of type '{Type}' from '{DeviceId}{ModuleId}' in '{HubName}'.\r\nEvent:{Event}",
lce.Type,
lce.DeviceId,
lce.ModuleId,
lce.HubName,
JsonSerializer.Serialize(lce.Event, serializerOptions));
}
else if (source == IotHubEventMessageSource.DeviceConnectionStateEvents)
{
var cse = evt.ConnectionStateEvent!;
logger.LogInformation("Device connection state event received of type '{Type}' from '{DeviceId}{ModuleId}' in '{HubName}'.\r\nEvent:{Event}",
cse.Type,
cse.DeviceId,
cse.ModuleId,
cse.HubName,
JsonSerializer.Serialize(cse.Event, serializerOptions));
var prefix = evt.Source switch
{
IotHubEventMessageSource.TwinChangeEvents => "TwinChange",
IotHubEventMessageSource.DeviceLifecycleEvents => "Device Lifecycle",
IotHubEventMessageSource.DeviceConnectionStateEvents => "Device connection state",
_ => throw new InvalidOperationException($"Unknown event source '{evt.Source}'."),
};
var ope = evt.Event;
logger.LogInformation("{Prefix} event received of type '{Type}' from '{DeviceId}{ModuleId}' in '{HubName}'.\r\nEvent:{Event}",
prefix,
ope.Type,
ope.DeviceId,
ope.ModuleId,
ope.HubName,
JsonSerializer.Serialize(ope.Payload, serializerOptions));
}

return Task.CompletedTask;
Expand Down
10 changes: 1 addition & 9 deletions samples/AzureIotHub/MyIotHubEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,7 @@

namespace AzureIotHub;

public record MyIotHubEvent : IotHubEvent<MyIotHubTelemetry>
{
public MyIotHubEvent(IotHubEventMessageSource source,
MyIotHubTelemetry? telemetry,
IotHubOperationalEvent<IotHubDeviceTwinChangeEvent>? twinEvent,
IotHubOperationalEvent<IotHubDeviceLifecycleEvent>? lifecycleEvent,
IotHubOperationalEvent<IotHubDeviceConnectionStateEvent>? connectionStateEvent)
: base(source, telemetry, twinEvent, lifecycleEvent, connectionStateEvent) { }
}
public record MyIotHubEvent : IotHubEvent { }

public class MyIotHubTelemetry
{
Expand Down
10 changes: 1 addition & 9 deletions samples/MultipleDifferentTransports/VehicleTelemetryEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,7 @@

namespace MultipleDifferentTransports;

internal record VehicleTelemetryEvent : IotHubEvent<VehicleTelemetry>
{
public VehicleTelemetryEvent(IotHubEventMessageSource source,
VehicleTelemetry? telemetry,
IotHubOperationalEvent<IotHubDeviceTwinChangeEvent>? twinEvent,
IotHubOperationalEvent<IotHubDeviceLifecycleEvent>? lifecycleEvent,
IotHubOperationalEvent<IotHubDeviceConnectionStateEvent>? connectionStateEvent)
: base(source, telemetry, twinEvent, lifecycleEvent, connectionStateEvent) { }
}
internal record VehicleTelemetryEvent : IotHubEvent { }

internal class VehicleTelemetry
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ public VehicleTelemetryEventsConsumer(ILogger<VehicleTelemetryEventsConsumer> lo
public async Task ConsumeAsync(EventContext<VehicleTelemetryEvent> context, CancellationToken cancellationToken)
{
var evt = context.Event;
var source = evt.Source;
if (source != IotHubEventMessageSource.Telemetry) return;
if (!evt.IsTelemetry) return;

var telemetry = evt.Telemetry!;
var telemetry = evt.GetTelemetry<VehicleTelemetry>();
var action = telemetry.Action;
if (action is not "door-status-changed")
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,11 @@ public static EventRegistration ConfigureAsIotHubEvent(this EventRegistration re
}

/// <summary>
/// Use the serializer that supports <see cref="IotHubEvent{TDeviceTelemetry, TDeviceTwinChange, TDeviceLifecycle}"/>.
/// Use the serializer that supports <see cref="IotHubEvent"/>.
/// </summary>
/// <param name="registration">The <see cref="EventRegistration"/> to configure.</param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
[RequiresDynamicCode(MessageStrings.JsonSerializationRequiresDynamicCodeMessage)]
[RequiresUnreferencedCode(MessageStrings.JsonSerializationUnreferencedCodeMessage)]
public static EventRegistration UseIotHubEventSerializer(this EventRegistration registration)
{
if (registration is null) throw new ArgumentNullException(nameof(registration));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,17 @@ public static bool TryGetPropertyValue<T>(this EventData data, string key, [NotN
{
return data.TryGetPropertyValue<T>(key, out var value) ? value : default;
}

/// <summary>
/// Gets the required property value that is associated with the specified key from
/// <see cref="EventData.SystemProperties"/> or <see cref="EventData.Properties"/>.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="data">The <see cref="EventData"/> instance to use.</param>
/// <param name="key">The key to locate.</param>
/// <returns></returns>
public static T GetRequiredPropertyValue<T>(this EventData data, string key) where T : IConvertible
{
return data.GetPropertyValue<T>(key) ?? throw new InvalidOperationException($"The property '{key}' could not be found.");
}
}
Loading