protected override async Task ExecuteAsync(CancellationToken stoppingToken) { this.files = Directory.GetFiles(this.configuration["azaan:source"], "*.opus"); for (int i = 0; i < this.files.Length; i++) { this.files[i] = $"http://home.cpsharp.net/{Path.GetFileName(this.files[i])}"; } Random r = new Random(); AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException; this.logger.LogInformation("Starting the caster: {time}", DateTimeOffset.Now); this.casterSet.Subscribe(); while (!stoppingToken.IsCancellationRequested) { this.logger.LogInformation("New daily cycle."); AzaanTimes times = await this.timeService.GetBroadcastTimes(); this.logger.LogInformation(times.ToString()); Queue <DateTime> q = times.AsQueue(); while (q.Any() && q.Peek() < DateTime.Now) { DateTime item = q.Dequeue(); this.logger.LogInformation($"service spin up at {DateTime.Now}, discarding {item}"); } if (q.Any()) { this.logger.LogInformation($"Entering actionable loop with {q.Count}, next broadcast at {q.Peek()}"); } while (q.Any()) { if (q.Peek() < DateTime.Now) { int chosen = r.Next(files.Length - 1); DateTime actionable = q.Dequeue(); this.logger.LogInformation($"Broadcasting {actionable}"); await this.Broadcast(this.files[chosen]); } await Task.Delay(int.Parse(this.configuration["azaan:delay"]), stoppingToken); } TimeSpan sleepTime = DateTime.Today.AddDays(1) - DateTime.Now; this.logger.LogInformation($"Finished daily routine. Sleeping till midnight {sleepTime}. Goodbye."); await Task.Delay(sleepTime, stoppingToken); } }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException; this.logger.LogInformation("Starting the caster: {time}", DateTimeOffset.Now); casterSet.Subscribe(); while (!stoppingToken.IsCancellationRequested) { this.logger.LogInformation("New daily cycle."); AzaanTimes times = await GetBroadcastTimes(); this.logger.LogInformation(times.ToString()); Queue <DateTime> q = times.AsQueue(); while (q.Peek() < DateTime.Now) { DateTime item = q.Dequeue(); this.logger.LogInformation($"service spin up at {DateTime.Now}, discarding {item}"); } this.logger.LogInformation($"Entering actionable loop with {q.Count}, next broadcast at {q.Peek()}"); while (q.Any()) { if (q.Peek() < DateTime.Now) { DateTime actionable = q.Dequeue(); this.logger.LogInformation($"Broadcasting {actionable}"); await this.Broadcast(); } await Task.Delay(int.Parse(this.configuration["azaan:delay"]), stoppingToken); } TimeSpan sleepTime = DateTime.Today.AddDays(1) - DateTime.Now; this.logger.LogInformation($"Finished daily routine. Sleeping till midnight {sleepTime}. Goodbye."); await Task.Delay(sleepTime, stoppingToken); } }
public async Task <AzaanTimes> GetBroadcastTimes() { HttpClient c = new HttpClient(); c.DefaultRequestHeaders.Accept.Clear(); c.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); c.DefaultRequestHeaders.Add("User-Agent", "AzaanService 2.0"); string url = this.url; Stream result = await c.GetStreamAsync(url); JsonDocument jd = JsonDocument.Parse(result); AzaanTimes r = new AzaanTimes(); //foreach (JsonProperty jsonElement in jd.RootElement.GetProperty("data").EnumerateObject().First().Value) JsonElement jsonElement = jd.RootElement.GetProperty("data").EnumerateObject().FirstOrDefault().Value; string dateFor = $"{DateTime.Today.Year}-{DateTime.Today.Month}-{DateTime.Today.Day}"; foreach (JsonProperty jsonProperty in jsonElement.EnumerateObject()) { this.logger.LogInformation($"{jsonProperty.Name}: {jsonProperty.Value}"); string propertyName = jsonProperty.Name.ToLower(); string val = jsonProperty.Value.GetString(); switch (propertyName) { case "date_for": dateFor = val; break; case "sunrise": //fajr r.Fajr = AzaanTimeConverter.CustomParse(dateFor, val); break; case "dhuhr": r.Dhuhr = AzaanTimeConverter.CustomParse(dateFor, val); break; case "asr": r.Asr = AzaanTimeConverter.CustomParse(dateFor, val); break; case "maghrib": r.Magrib = AzaanTimeConverter.CustomParse(dateFor, val); break; case "isha": r.Isha = AzaanTimeConverter.CustomParse(dateFor, val); break; case "shurooq": //r.Shurooq = AzaanTimeConverter.CustomParse(dateFor, val); break; } } if (r.IsFilled()) { return(r); } this.logger.LogWarning($"Something's wrong: {jd.RootElement.ToString()}"); this.logger.LogTrace(r.ToString()); return(r); }
private async Task <AzaanTimes> GetBroadcastTimes() { HttpClient c = new HttpClient(); c.DefaultRequestHeaders.Accept.Clear(); c.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); c.DefaultRequestHeaders.Add("User-Agent", "AzaanService 2.0"); string url = $"{this.configuration["azaan:apitarget"]}{this.configuration["azaan:apikey"]}"; Task <Stream> result = c.GetStreamAsync(url); JsonDocument jd = JsonDocument.Parse(await result); AzaanTimes r = new AzaanTimes(); foreach (JsonElement jsonElement in jd.RootElement.GetProperty("items").EnumerateArray()) { string dateFor = $"{DateTime.Today.Year}-{DateTime.Today.Month}-{DateTime.Today.Day}"; foreach (JsonProperty jsonProperty in jsonElement.EnumerateObject()) { this.logger.LogInformation($"{jsonProperty.Name}: {jsonProperty.Value}"); string propertyName = jsonProperty.Name; string val = jsonProperty.Value.GetString(); switch (propertyName) { case "date_for": dateFor = val; break; case "fajr": r.Fajr = AzaanTimeConverter.CustomParse(dateFor, val); break; case "dhuhr": r.Dhuhr = AzaanTimeConverter.CustomParse(dateFor, val); break; case "asr": r.Asr = AzaanTimeConverter.CustomParse(dateFor, val); break; case "maghrib": r.Magrib = AzaanTimeConverter.CustomParse(dateFor, val); break; case "isha": r.Isha = AzaanTimeConverter.CustomParse(dateFor, val); break; case "shurooq": r.Shurooq = AzaanTimeConverter.CustomParse(dateFor, val); break; } } } if (!r.IsFilled()) { throw new InvalidOperationException($"Something's wrong: {jd.ToString()}"); } return(r); }