Ejemplo n.º 1
0
        private static async Task <bool> SendToSignalR(IAsyncCollector <SignalRMessage> signalRMessages, ILogger log, DateTimeOffset readAtUtc, double temperature, Weather weather)
        {
            try
            {
                await signalRMessages.AddAsync(
                    new SignalRMessage
                {
                    Target    = "notifyCurrentWeatherUpdated",
                    Arguments = new object[]
                    {
                        new
                        {
                            city          = City,
                            timestampWest = DateTimeOffsetHelper.ConvertToWest(readAtUtc),
                            temperature,
                            weatherId          = weather.id,
                            weatherDescription = weather.description,
                            weatherIcon        = weather.icon
                        }
                    }
                });

                return(true);
            }
            catch (Exception e)
            {
                log.LogError(e, "Error when sending current weather to signalr");
                return(false);
            }
        }
Ejemplo n.º 2
0
 public static TemperatureHumidityApiModel ToApiModel(this TemperatureHumidityReading businessModel)
 {
     return(new TemperatureHumidityApiModel
     {
         SensorId = businessModel.SensorId,
         TimestampWest = DateTimeOffsetHelper.ConvertToWest(businessModel.TimestampUtc),
         Temperature = businessModel.Temperature,
         Humidity = businessModel.Humidity
     });
 }
 public static CurrentWeatherApiModel ToApiModel(this CurrentWeatherReport businessModel)
 {
     return(new CurrentWeatherApiModel
     {
         City = businessModel.City,
         TimestampWest = DateTimeOffsetHelper.ConvertToWest(businessModel.TimestampUtc),
         WeatherId = businessModel.WeatherId,
         WeatherDescription = businessModel.WeatherDescription,
         WeatherIcon = businessModel.WeatherIcon,
         Temperature = businessModel.Temperature
     });
 }
 public static AggregatedTemperatureHumidityReadingsApiModel ToApiModel(this AggregatedTemperatureHumidityReadings businessModel)
 {
     return(new AggregatedTemperatureHumidityReadingsApiModel
     {
         SensorId = businessModel.SensorId,
         Count = businessModel.Count,
         FromWest = DateTimeOffsetHelper.ConvertToWest(businessModel.From),
         ToWest = DateTimeOffsetHelper.ConvertToWest(businessModel.To),
         AverageTemperature = businessModel.AverageTemperature,
         AverageHumidity = businessModel.AverageHumidity
     });
 }
Ejemplo n.º 5
0
        public static IActionResult Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req,
            [Table("TemperatureHumidity")] out TemperatureHumidityReading output,
            [SignalR(HubName = "climate")] IAsyncCollector <SignalRMessage> signalRMessages,
            ILogger log)
        {
            req.FixCorsHeaders();

            output = null;

            string requestBody;

            try
            {
                requestBody = new StreamReader(req.Body).ReadToEnd();
            }
            catch (Exception e)
            {
                log.LogWarning(e, "Could not read the body of the request");
                return(new BadRequestObjectResult("Could not read the body of the request"));
            }

            TemperatureHumidityInput input;

            try
            {
                input = JsonConvert.DeserializeObject <TemperatureHumidityInput>(requestBody);
            }
            catch (Exception e)
            {
                log.LogWarning(e, "The received json data ({Json}) could not be parsed", requestBody);
                return(new BadRequestObjectResult("The received json data could not be parsed"));
            }

            if (string.IsNullOrWhiteSpace(input.SensorId))
            {
                log.LogWarning("Sensor id missing");
                return(new BadRequestObjectResult("Sensor id missing"));
            }

            var readAtUtc = DateTimeOffset.UtcNow;

            output = new TemperatureHumidityReading
            {
                PartitionKey = input.SensorId,
                RowKey       = readAtUtc.ToRowKey(),
                ReadAtUtc    = readAtUtc,
                Temperature  = input.Temperature,
                Humidity     = input.Humidity
            };

            signalRMessages.AddAsync(
                new SignalRMessage
            {
                Target    = "notifyTemperatureHumidityUpdated",
                Arguments = new object[] { new
                                           {
                                               sensorId      = input.SensorId,
                                               timestampWest = DateTimeOffsetHelper.ConvertToWest(readAtUtc),
                                               temperature   = input.Temperature,
                                               humidity      = input.Humidity
                                           } }
            }).Wait();

            return(new OkResult());
        }