in DeviceBridge/Services/ConnectionManager.cs [416:464]
public async Task SendEventAsync(Logger logger, string deviceId, IDictionary<string, object> payload, CancellationToken cancellationToken, IDictionary<string, string> properties = null, string componentName = null, DateTime? creationTimeUtc = null)
{
logger.Info("Sending event for device {deviceId}", deviceId);
// This method expects a connection to have been previously established
if (!_clients.TryGetValue(deviceId, out DeviceClient client))
{
var e = new DeviceConnectionNotFoundException(deviceId);
logger.Error(e, "Tried to send event for device {deviceId} but an active connection was not found", deviceId);
throw e;
}
var data = JsonSerializer.Serialize(payload);
var eventMessage = new Message(Encoding.UTF8.GetBytes(data))
{
ContentEncoding = Encoding.UTF8.WebName,
ContentType = "application/json",
};
if (componentName != null)
{
eventMessage.ComponentName = componentName;
}
if (properties != null)
{
foreach (var property in properties)
{
eventMessage.Properties.Add(property.Key, property.Value);
}
}
if (creationTimeUtc.HasValue)
{
eventMessage.CreationTimeUtc = creationTimeUtc.Value;
}
try
{
await client.SendEventAsync(eventMessage, cancellationToken);
}
catch (Exception e)
{
throw TranslateSdkException(e, deviceId);
}
logger.Info("Event for device {deviceId} sent successfully", deviceId);
}