private static async Task SendTelemetrySendDeviceToCloud(IDeviceClient deviceClient, string messagebody) { var msg = new Message(Encoding.UTF8.GetBytes(messagebody)); msg.Properties.Add("message-type", "Fleet"); msg.Properties.Add("device-type", "Terminal"); msg.Properties.Add("message-class", "telemetry"); await deviceClient.SendEventAsync(msg); }
public async Task <string> ExecuteAsync(Dictionary <string, object> parameters) { var objectToSend = parameters["obj"]; var payload = JsonConvert.SerializeObject(objectToSend, JsonSerializerSettings); var message = new Message(Encoding.ASCII.GetBytes(payload)); await _deviceClient.SendEventAsync(message); return(payload); }
static async Task SendAlert(IDeviceClient deviceClient) { var messageBody = new { value = "ReceiptLowPaper", time = "9/1/2020 8:30:00 AM +01:00" }; var messageBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(messageBody)); var message = new Message(messageBytes); message.Properties.Add("device-type", "Terminal"); message.Properties.Add("message-type", "ReceiptLowPaper"); message.Properties.Add("message-class", "alert"); await deviceClient.SendEventAsync(message); }
private static async Task BroadcastDeviceRegistration(IDeviceClient deviceClient, string macAddress, IEnumerable <TimeSpan> retryIntervals, ILogger log) { var policy = Policy <bool> .HandleResult(false) .WaitAndRetryAsync( retryIntervals, (res, time) => log.Warning("BroadcastDeviceRegistration failed on attempt at {Interval} (Success={Result}).", time, res.Result)); var result = await policy.ExecuteAsync(async() => { try { log.Information("Sending device-registration message with mac address: {@macAddress}", macAddress); var message = new Message() .WithProperties( ("message-type", "device-registration")); if (!string.IsNullOrWhiteSpace(macAddress)) { message.Properties.Add("mac-address", macAddress); message.Properties.Add("device-details", "Fleet Service Engine is the gateway for transferring FSC telematry data to Fleed application."); await deviceClient.SendEventAsync(message, CancellationToken.None); log.Information("Device-registration message sent successfully."); return(true); } return(false); } catch (Exception ex) { log.Error(ex, "Failed to send device-registration message."); return(false); } }); if (result) { log.Information("Send device-registration - Success."); } else { log.Warning("Send device-registration - Fail."); } }
public async Task Start(CancellationToken cts) { var correlationId = Guid.NewGuid().ToString(); IDeviceClient deviceClient = null; var stopwatch = Stopwatch.StartNew(); try { deviceClient = CreateDeviceClient(); deviceClient.SetConnectionStatusChangesHandler(OnConnectionStatusChanged); deviceClient.OperationTimeoutInMilliseconds = deviceRunnerConfiguration.DeviceOperationTimeoutInMilliseconds; if (deviceRunnerConfiguration.DeviceRetryPolicy) { deviceClient.SetRetryPolicy(new ExponentialBackoff(10, TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(10), TimeSpan.FromMilliseconds(500))); } else { deviceClient.SetRetryPolicy(new NoRetry()); } await deviceClient.OpenAsync(); stopwatch.Stop(); Logger.Log($"{deviceId}: deviceClient created: {stopwatch.ElapsedMilliseconds}ms (retryPolicy={deviceRunnerConfiguration.DeviceRetryPolicy}, operationTimeoutInMilliseconds={deviceClient.OperationTimeoutInMilliseconds})"); } catch (Exception ex) { Logger.Log($"{deviceId}: deviceClient creation failed: {ex.Message}"); return; } var number = 0; var maxMsTwinUpdate = 0L; var numSlowTwin = 0; long maxMsC2D = 0; int numSlowC2D = 0; long maxMsSendEvent = 0; int numSlowSendEvent = 0; while (!cts.IsCancellationRequested) { number++; // Update Device Twin with increasing "Number" if (this.deviceRunnerConfiguration.UpdateTwin) { try { var reportedProperties = new TwinCollection(); reportedProperties["Number"] = $"{correlationId}-{number}"; stopwatch.Restart(); Logger.Log($"{deviceId}: begin twins update {number}."); await deviceClient.UpdateReportedPropertiesAsync(reportedProperties); stopwatch.Stop(); if (maxMsTwinUpdate < stopwatch.ElapsedMilliseconds) { maxMsTwinUpdate = stopwatch.ElapsedMilliseconds; } if (stopwatch.ElapsedMilliseconds > 4000) { numSlowTwin++; } Logger.Log($"{deviceId}: twins updated {number}: {stopwatch.ElapsedMilliseconds}ms. Max: {maxMsTwinUpdate}ms. Delays: {numSlowTwin}"); } catch (IotHubCommunicationException iotHubCommunicationException) { Logger.Log($"{deviceId}: iothub problem, waiting {deviceRunnerConfiguration.DelayAfterIotHubCommunicationError}ms. {iotHubCommunicationException.Message}"); await Task.Delay(deviceRunnerConfiguration.DelayAfterIotHubCommunicationError); } catch (Exception ex) { Logger.Log($"{deviceId}: failed to get update twin. {ex.Message}"); } } if (this.deviceRunnerConfiguration.SendEvent) { try { var message = new Message(Encoding.UTF8.GetBytes($"{correlationId}-{number}")); message.CorrelationId = correlationId; message.ContentType = "application/json"; stopwatch.Restart(); Logger.Log($"{deviceId}: begin send event {number}."); await deviceClient.SendEventAsync(message); stopwatch.Stop(); if (maxMsSendEvent < stopwatch.ElapsedMilliseconds) { maxMsSendEvent = stopwatch.ElapsedMilliseconds; } if (stopwatch.ElapsedMilliseconds > 4000) { numSlowSendEvent++; } Logger.Log($"{deviceId}: send event {number}: {stopwatch.ElapsedMilliseconds}ms. Max: {maxMsSendEvent}ms. Delays: {numSlowSendEvent}"); } catch (IotHubCommunicationException iotHubCommunicationException) { Logger.Log($"{deviceId}: iothub problem, waiting {deviceRunnerConfiguration.DelayAfterIotHubCommunicationError}ms. {iotHubCommunicationException.Message}"); await Task.Delay(deviceRunnerConfiguration.DelayAfterIotHubCommunicationError); } catch (Exception ex) { Logger.Log($"{deviceId}: failed to send event. {ex.Message}"); } } if (this.deviceRunnerConfiguration.CloudToDeviceMessage) { try { // Check Cloud-to-Device Message Logger.Log($"{deviceId}: checking c2d message"); stopwatch.Restart(); Message c2dMsg = await deviceClient.ReceiveAsync(TimeSpan.FromMilliseconds(500)); stopwatch.Stop(); if (maxMsC2D < stopwatch.ElapsedMilliseconds) { maxMsC2D = stopwatch.ElapsedMilliseconds; } if (stopwatch.ElapsedMilliseconds > 4000) { numSlowC2D++; } Logger.Log($"{deviceId}: done checking c2d message: {stopwatch.ElapsedMilliseconds}ms. Max: {maxMsC2D}ms. Delays: {numSlowC2D}"); if (c2dMsg != null) { var c2dMsgBody = c2dMsg.GetBodyAsText(); Logger.Log($"{deviceId}: c2d message received '{c2dMsgBody}'"); if (deviceRunnerConfiguration.CheckPendingCloudToDeviceMessage) { stopwatch.Restart(); var pendingMessage = await deviceClient.ReceiveAsync(TimeSpan.FromMilliseconds(200)); stopwatch.Stop(); if (maxMsC2D < stopwatch.ElapsedMilliseconds) { maxMsC2D = stopwatch.ElapsedMilliseconds; } if (stopwatch.ElapsedMilliseconds > 4000) { numSlowC2D++; } Logger.Log($"{deviceId}: done checking pending c2d message: {stopwatch.ElapsedMilliseconds}ms. Max: {maxMsC2D}ms. Delays: {numSlowC2D}"); if (pendingMessage != null) { var pendingMessageBody = pendingMessage.GetBodyAsText(); Logger.Log($"{deviceId}: abandoning c2d message '{pendingMessageBody}'"); await deviceClient.AbandonAsync(pendingMessage); Logger.Log($"{deviceId}: done abandoning c2d message '{pendingMessageBody}'"); } } if (deviceRunnerConfiguration.CompleteCloudToDeviceMessage) { Logger.Log($"{deviceId}: completing c2d message '{c2dMsgBody}'"); await deviceClient.CompleteAsync(c2dMsg); Logger.Log($"{deviceId}: done completing c2d message '{c2dMsgBody}'"); } } else { Logger.Log($"{deviceId}: no c2d message received"); } } catch (IotHubCommunicationException iotHubCommunicationException) { Logger.Log($"{deviceId}: iothub problem, waiting {deviceRunnerConfiguration.DelayAfterIotHubCommunicationError}ms. {iotHubCommunicationException.Message}"); await Task.Delay(deviceRunnerConfiguration.DelayAfterIotHubCommunicationError); } catch (Exception ex) { Logger.Log($"{deviceId}: failed to handle cloud to device message. {ex.Message}"); } } await Task.Delay(this.deviceRunnerConfiguration.DeviceLoopDelay); } }