private async Task SendStatus(WaterfallStepContext stepContext, CancellationToken cancellationToken) { SmokerStatus status = await _smoker.GetStatus(); string modeText = (status.Mode == SmokerMode.Hold.ToString()) ? $"{status.Mode} {status.SetPoint}°F" : status.Mode; string grillText = (status.Temps.GrillTemp == -1) ? "Unplugged" : $"{status.Temps.GrillTemp}°F"; string probe1Text = (status.Temps.Probe1Temp == -1) ? "Unplugged" : $"{status.Temps.Probe1Temp}°F"; string probe2Text = (status.Temps.Probe2Temp == -1) ? "Unplugged" : $"{status.Temps.Probe2Temp}°F"; string probe3Text = (status.Temps.Probe3Temp == -1) ? "Unplugged" : $"{status.Temps.Probe3Temp}°F"; string probe4Text = (status.Temps.Probe4Temp == -1) ? "Unplugged" : $"{status.Temps.Probe4Temp}°F"; var statusText = $"**Mode:** {modeText}\n"; statusText += $"**Grill:** {grillText}\n"; statusText += $"**Probe1:** {probe1Text}\n"; statusText += $"**Probe2:** {probe1Text}\n"; statusText += $"**Probe3:** {probe1Text}\n"; statusText += $"**Probe4:** {probe1Text}\n"; await stepContext.Context.SendActivityAsync(MessageFactory.Text(statusText), cancellationToken); }
/// <summary> /// Module behavior: /// Sends data periodically (with default frequency of 5 seconds). /// </summary> static async Task SendEvents(ModuleClient moduleClient, CancellationToken cancellationToken) { // Read ModuleId from env string moduleId = Environment.GetEnvironmentVariable("IOTEDGE_MODULEID"); int count = 1; string url = "http://localhost:5000/api/status"; string json; while (!cancellationToken.IsCancellationRequested) { var correlationId = Guid.NewGuid().ToString(); Log.Information($"New status message - CorrelationId={correlationId}"); using (HttpResponseMessage response = _httpClient.GetAsync(url).Result) { using (HttpContent content = response.Content) { json = content.ReadAsStringAsync().Result; } } // Log.Information($"Device sending Event/Telemetry to IoT Hub..."); SmokerStatus status = JsonConvert.DeserializeObject <SmokerStatus>(await _httpClient.GetStringAsync("http://localhost:5000/api/status")); if (!string.IsNullOrEmpty(SessionID)) { status.SessionId = SessionID; } status.SmokerId = deviceId; status.Type = "status"; json = JsonConvert.SerializeObject(status); //Log.Information($"Device sending Event/Telemetry to IoT Hub| SmokerStaus.SmokerId = {status.SmokerId}, SmokerStaus.Type = {status.Type} || {json}"); Message eventMessage = new Message(Encoding.UTF8.GetBytes(json)); eventMessage.ContentType = "application/json"; eventMessage.ContentEncoding = "UTF-8"; eventMessage.Properties.Add("correlationId", correlationId); eventMessage.Properties.Add("sequenceNumber", count.ToString()); eventMessage.Properties.Add("SessionId", SessionID); try { await moduleClient.SendEventAsync("output1", eventMessage); //telemetry.TrackEvent("81-Heartbeat-Sent-MessageForwarder", telemetryProperties); //Log.Information("Smoker Status message sent"); Log.Information($"Telemetry sent | SmokerStaus.SmokerId = {status.SmokerId}, SmokerStaus.Type = {status.Type} || {json}"); } catch (Exception e) { Log.Error(e, "Error during message sending to Edge Hub"); //telemetry.TrackEvent("85-ErrorHeartbeatMessageNotSentToEdgeHub", telemetryProperties); } count++; await Task.Delay(telemetryInterval); } }
static async Task Main(string[] args) { Console.WriteLine("Timestamp,Setpoint,Grill,Probe"); HttpClient _client = new HttpClient(); while (true) { SmokerStatus status = JsonConvert.DeserializeObject <SmokerStatus>(await _client.GetStringAsync("http://localhost:5000/api/status")); Console.WriteLine($"{status.CurrentTime},{status.SetPoint},{status.Temps.GrillTemp},{status.Temps.ProbeTemp}"); await Task.Delay(TimeSpan.FromSeconds(5)); } }
private async Task SendEventAsync() { while (true) //TODO: use cancellation context { if (s_connectionStatus == ConnectionStatus.Connected) { _logger.LogInformation($"Device sending Event/Telemetry to IoT Hub..."); SmokerStatus status = JsonConvert.DeserializeObject <SmokerStatus>(await _httpClient.GetStringAsync("http://localhost:5000/api/status")); status.SmokerId = _deviceId; status.PartitionKey = $"{status.SmokerId}-{DateTime.UtcNow:yyyy-MM}"; _logger.LogInformation($"{status.PartitionKey},{status.CurrentTime},{status.SmokerId},{status.ttl},{status.SetPoint},{status.Temps.GrillTemp},{status.Temps.Probe1Temp},{status.Temps.Probe2Temp},{status.Temps.Probe3Temp},{status.Temps.Probe4Temp}"); string json = JsonConvert.SerializeObject(status); _logger.LogInformation($"Sending {json}"); Message eventMessage = new Message(Encoding.UTF8.GetBytes(json)); while (true) { try { await _deviceClient.SendEventAsync(eventMessage).ConfigureAwait(false); _logger.LogInformation($"Sent eventMessage"); eventMessage.Dispose(); break; } catch (IotHubException ex) when(ex.IsTransient) { // Inspect the exception to figure out if operation should be retried, or if user-input is required. _logger.LogError($"An IotHubException was caught, but will try to recover and retry: {ex}"); } catch (Exception ex) when(ExceptionHelper.IsNetworkExceptionChain(ex)) { _logger.LogError($"A network related exception was caught, but will try to recover and retry: {ex}"); } catch (Exception ex) { _logger.LogError($"Unexpected error {ex}"); } // wait and retry await Task.Delay(s_sleepDuration); } //wait between event message / telemetry message send await Task.Delay(TelemetryInterval); } } }
static async Task PrintStatus() { HttpResponseMessage result = await InfernoApiRequest(Endpoint.status); SmokerStatus status = JsonConvert.DeserializeObject <SmokerStatus>(await result.Content.ReadAsStringAsync()); Console.WriteLine($"Mode: {status.Mode}"); Console.WriteLine($"Setpoint: {status.SetPoint}"); Console.WriteLine(); Console.WriteLine($"Grill temp: {status.Temps.GrillTemp}*F"); Console.WriteLine($"Probe temp: " + ((status.Temps.ProbeTemp > 0) ? status.Temps.ProbeTemp.ToString() + "*F" : "Unplugged")); Console.WriteLine(); Console.WriteLine($"Auger on: {status.AugerOn}"); Console.WriteLine($"Igniter on: {status.IgniterOn}"); Console.WriteLine($"Blower on: {status.BlowerOn}"); Console.WriteLine(); Console.WriteLine($"Fire healthy: {status.FireHealthy}"); }