private async Task ListenerAction(CancellationToken token) { this._weatherService.ApiKey = this._attribute.ApiKey; var cityData = new CityInfo(); double?lastTemperature = null; while (!token.IsCancellationRequested) { this._logger.LogDebug($"[{this.GetType().Name} ({this._instanceId.ToSmallString()})] - GetCityInfoAsync for {this._attribute.CityName} start"); try { cityData = await this._weatherService.GetCityInfoAsync(this._attribute.CityName); } catch (Exception) { cityData = null; } this._logger.LogDebug($"[{this.GetType().Name} ({this._instanceId.ToSmallString()})] - GetCityInfoAsync for {this._attribute.CityName} finish"); if (!lastTemperature.HasValue || (cityData != null && Math.Abs(cityData.Temperature - lastTemperature.Value) > this._attribute.TemperatureThreshold)) { this._logger.LogDebug($"[{this.GetType().Name} ({this._instanceId.ToSmallString()})] - Function firing: lastTemperature={lastTemperature}, currentTemperature={cityData.Temperature} for {cityData.CityCode}"); var weatherPayload = new WeatherPayload() { CityName = this._attribute.CityName, CurrentTemperature = cityData.Temperature, Timestamp = cityData.Timestamp, LastTemperature = lastTemperature }; await _executor.TryExecuteAsync(new TriggeredFunctionData() { TriggerValue = weatherPayload }, token); lastTemperature = cityData.Temperature; } await Task.Delay(TimeSpan.FromSeconds(this._attribute.SecondsBetweenCheck), token); } }
private async Task ListenerAction(CancellationToken token) { this._weatherService.ApiKey = this._attribute.ApiKey; var cityData = new CityInfo(); double?lastTemperature = null; while (!token.IsCancellationRequested) { try { cityData = await this._weatherService.GetCityInfoAsync(this._attribute.CityName); } catch (Exception) { cityData = null; } if (!lastTemperature.HasValue || (cityData != null && Math.Abs(cityData.Temperature - lastTemperature.Value) > this._attribute.TemperatureThreshold)) { var weatherPayload = new WeatherPayload() { CityName = this._attribute.CityName, CurrentTemperature = cityData.Temperature, Timestamp = cityData.Timestamp, LastTemperature = lastTemperature }; await _executor.TryExecuteAsync(new TriggeredFunctionData() { TriggerValue = weatherPayload }, token); lastTemperature = cityData.Temperature; } await Task.Delay(TimeSpan.FromSeconds(30), token); } }