protected async override Task<SensorTelemetryData> ReadDataAsync() { SensorTelemetryData measure = new SensorTelemetryData(); if (this.Units == PressureUnits.kPa || this.Units == PressureUnits.Pa) { PressureGetPascalResult pResults = await this.consumer.GetPascalAsync(); measure.Value = this.Units == PressureUnits.kPa ? pResults.Pascal / 1000 : pResults.Pascal; return measure; } else if (this.Units == PressureUnits.InchesOfMercury) { PressureGetInchesOfMercuryResult fResult = await this.consumer.GetInchesOfMercuryAsync(); measure.Value = fResult.InchesOfMercury; return measure; }else if (this.Units == PressureUnits.InchesOfMercury) { PressureGetMmOfMercuryResult fResult = await this.consumer.GetMmOfMercuryAsync(); measure.Value = fResult.MmOfMercury; return measure; } throw new ArgumentException(String.Format("Unknown Units {0} for Pressure", Units)); }
/// <summary> /// Send message to Azure Event Hub using HTTP/REST API /// </summary> /// <param name="message"></param> public async void sendMeasure(SensorTelemetryData measure) { bool hasMutex = false; try { hasMutex = mutex.WaitOne(1000); if (hasMutex) { if (this.deviceClient == null) InitIoTHubConnection(); measure = ApplySettingsToMeasure(measure); string message = measure.ToJson(); var msgBytes = new Message(Encoding.UTF8.GetBytes(message)); await this.deviceClient.SendEventAsync(msgBytes); Debug.WriteLine("Sent: {0}", message); } } catch(Exception ex) { Debug.WriteLine("Exception when sending message:" + ex.Message); } finally { if (hasMutex) { mutex.ReleaseMutex(); } } }
protected async override Task<SensorTelemetryData> ReadDataAsync() { HumidityGetRHResult RHResult = await this.consumer.GetRHAsync(); SensorTelemetryData measure = new SensorTelemetryData(); measure.UnitOfMeasure = "RH%"; measure.Value = RHResult.RH; return measure; }
private SensorTelemetryData ApplySettingsToMeasure(SensorTelemetryData measure) { measure.displayname = this.localSettings.DisplayName; measure.location = this.localSettings.Location; measure.organization = this.localSettings.Organization; return measure; }
/// <summary> /// Send message to Azure Event Hub using HTTP/REST API /// </summary> /// <param name="message"></param> public async void sendMeasure(SensorTelemetryData measure) { measure = ApplySettingsToMeasure(measure); string message = measure.ToJson(); if (this.EventHubConnectionInitialized) { try { HttpStringContent content = new HttpStringContent(message, Windows.Storage.Streams.UnicodeEncoding.Utf8, "application/json"); HttpResponseMessage postResult = await httpClient.PostAsync(uri, content); if (postResult.IsSuccessStatusCode) { Debug.WriteLine("Message Sent: {0}", content); } else { Debug.WriteLine("Failed sending message: {0}", postResult.ReasonPhrase); } } catch (Exception e) { Debug.WriteLine("Exception when sending message:" + e.Message); } } }