Пример #1
0
        public static DeviceValuesUpdated MapDeviceValuesUpdated(ThermometerValuesUpdated @event)
        {
            var lastValuesJson = new LastValuesJson
            {
                LastCommunicationDate = @event.CommunicationDate.Value,
                Sensors = new List <SensorJson>
                {
                    new SensorJson
                    {
                        SensorId     = Guid.NewGuid().ToString(),
                        Descriptions = new List <DescriptionJson>
                        {
                            new DescriptionJson
                            {
                                Code  = "en",
                                Value = "Temperature"
                            }
                        },
                        SensorType = new SensorTypeJson
                        {
                            SensorTypeId = "c1af5cd2-392d-425c-b1c8-d947ca1910e0",
                            Description  = "Value"
                        },
                        DataType           = "string",
                        SensorValue        = @event.Temperature.Value.ToString("F"),
                        SensorKey          = "Temperature",
                        UnitOfMeasure      = "°C",
                        ShowInGrid         = false,
                        EnableFilterInGrid = false
                    }
                }
            };

            return(new DeviceValuesUpdated(@event.DeviceId, lastValuesJson, @event.Who, @event.When));
        }
        public static async Task <string> Run([EventHubTrigger("globalazurethermometer", Connection = "EventHubConnection")]
                                              EventData[] events, ILogger log)
        {
            log.LogInformation("Start listen to Thermometer eventhub");

            var exceptions = new List <Exception>();

            foreach (EventData eventData in events)
            {
                if (eventData.Body == null || eventData.Body.Array == null)
                {
                    continue;
                }

                try
                {
                    var messageBody = Encoding.UTF8.GetString(eventData.Body.Array, eventData.Body.Offset,
                                                              eventData.Body.Count);

                    log.LogInformation("Deserialize eventData Body");
                    var thermometerValuesUpdated = JsonConvert.DeserializeObject <ThermometerValuesUpdated>(messageBody);

                    var celsiusTemperature =
                        TemperatureConverter.FromFahrenheitToCelsius(thermometerValuesUpdated.Temperature);

                    thermometerValuesUpdated = new ThermometerValuesUpdated(thermometerValuesUpdated.DeviceId,
                                                                            thermometerValuesUpdated.EventId,
                                                                            thermometerValuesUpdated.DeviceName,
                                                                            celsiusTemperature,
                                                                            new UnitOfMeasurement("°C"),
                                                                            thermometerValuesUpdated.CommunicationDate,
                                                                            thermometerValuesUpdated.Who, thermometerValuesUpdated.When);

                    return(JsonConvert.SerializeObject(thermometerValuesUpdated));
                }
                catch (Exception ex)
                {
                    exceptions.Add(ex);
                    log.LogError(ex.Message);
                }
            }

            if (exceptions.Count > 1)
            {
                throw new AggregateException(exceptions);
            }

            if (exceptions.Count == 1)
            {
                throw exceptions.Single();
            }

            return(string.Empty);
        }