using Church.Net.Utility; using System; using System.Threading.Tasks; using WebAPI.Services.Interfaces; namespace WebAPI.Services.ScheduledTask { public class MorningPrayer : IScheduledTask { private readonly LineAutoBotService lineAutoBotService; private readonly ILoggingService loggingService; private DateTime? nextRunningTime = null; public MorningPrayer( LineAutoBotService lineAutoBotService, ILoggingService loggingService ) { this.lineAutoBotService = lineAutoBotService; this.loggingService = loggingService; this.SetNextRunningTime(); } public string Description => "Sent out Ark Morning Prayer"; public bool CheckTime(DateTime time) { if(nextRunningTime == null) { this.SetNextRunningTime(); return true; } return time >= nextRunningTime.Value; } public Task RunTask() { SetNextRunningTime(); return lineAutoBotService.PushCommandMessage(LineGroup.Ark, "#pray"); } private void SetNextRunningTime() { nextRunningTime = DateTimeHelper.Today().AddDays(1).AddHours(8); loggingService.Log($"Scheduled Task {this.Description}", nextRunningTime.Value); } } }