示例#1
0
        public Task Persist(string stationId, string sensorId, SensorDayAverageState state)
        {
            if (string.IsNullOrWhiteSpace(stationId))
            {
                throw new ArgumentOutOfRangeException(nameof(stationId), "StationId no especificado");
            }
            if (string.IsNullOrWhiteSpace(sensorId))
            {
                throw new ArgumentOutOfRangeException(nameof(sensorId), "SensorId no especificado");
            }
            var tableName   = GetValidTableName(stationId, sensorId);
            var tableClient = account.CreateCloudTableClient();
            var table       = tableClient.GetTableReference(tableName);

            table.CreateIfNotExists();
            var day         = state.Day;
            var partition   = $"{day.Year:D4}";
            var rowKey      = $"{day.DayOfYear:D3}";
            var tableEntity = new DynamicTableEntity(partition, rowKey)
            {
                Properties = new Dictionary <string, EntityProperty>
                {
                    [nameof(SensorDayAverageState.Day)]     = new EntityProperty(state.Day),
                    [nameof(SensorDayAverageState.Average)] = new EntityProperty(state.Average),
                }
            };
            var operation = TableOperation.InsertOrReplace(tableEntity);

            return(table.ExecuteAsync(operation));
        }
        public async Task ProcessMessage([QueueTrigger("stationsensordaystatechanged")] StationSensorDayStateChanged message)
        {
            if (message == null)
            {
                return;
            }
            var stationId       = message.StationId;
            var sensorId        = message.SensorId;
            var day             = message.Day;
            var dayStates       = hourlyStatePersister.DayStates(stationId, sensorId, day);
            var dayStatesActive = from s in dayStates where s.Active select s;
            var dayAverage      = new SensorDayAverageState
            {
                Day     = day,
                Average = sensorId.ToLowerInvariant() == "rain" ? dayStatesActive.Sum(x => x.State) : dayStatesActive.Average(x => x.State)
            };
            await dailyStatePersister.Persist(stationId, sensorId, dayAverage);

            logger.LogTrace(() => $"Actualizado promedio diario para '{sensorId}' de '{stationId}' del {day}");

            await sensorActivityPersister.Persist(stationId, sensorId, day.Year);

            logger.LogTrace(() => $"Registrado '{sensorId}' de '{stationId}' en {day.Year}");
        }