protected virtual void OnDailyInfo() // sends out event { var ctx = new HamsterDBContext(); var historyLog = ctx.HistoryLogs.ToList(); var hamsterlist = ctx.Hamsters.ToList(); DailyReportEventArgs dailyReport = new DailyReportEventArgs(hamsterlist, historyLog, ticks, currentDate); SendDailyInfo?.Invoke(this, dailyReport); }
} // function to contruct to add all hamster to cages #endregion #region API AREA protected void SendMail() // sends out event { var ctx = new HamsterDBContext(); var historyLog = ctx.HistoryLogs.ToList(); var hamsterlist = ctx.Hamsters.ToList(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < hamsterlist.Count; i++) { var query = ctx.Owners //Owner Object .Where(o => o.OwnerId == hamsterlist[i].OwnerId) .First(); string hamster = $"Hamster ID:{hamsterlist[i].HamsterId} \nName: {hamsterlist[i].Name} \nTime Waited To WorkOut: {hamsterlist[i].TimeWaited}\nOwner: {query.FirstName +" "+ query.LastName}\nContact Info\nPhone: {query.Phone}\nEmail: {query.Email}\n"; sb.Append(hamster); sb.Append("\n "); } DailyReportEventArgs dailyReport = new DailyReportEventArgs(hamsterlist, historyLog, ticks, currentDate); SmtpClient Client = new SmtpClient(); { Client.Host = "smtp.gmail.com"; //mail host Client.Port = 587; //port used for mail Client.EnableSsl = true; //enable connections Client.DeliveryMethod = SmtpDeliveryMethod.Network; // email sends trough a SMTP server Client.UseDefaultCredentials = false; Client.Credentials = new NetworkCredential() //to check mail password etc { UserName = "******", Password = "******" }; MailAddress fromEmail = new MailAddress("*****@*****.**"); MailMessage mailMessage = new MailMessage() { From = fromEmail, Subject = $"DailyReport {currentDate}", Body = sb.ToString() }; mailMessage.To.Add(fromEmail); // can add multiply sends Client.Send(mailMessage); try { Client.Send(mailMessage); } catch (Exception ex) { Console.WriteLine(ex); } } }
public async void ShowDailyLog(object sender, DailyReportEventArgs e) { await Task.Run(() => { Console.Clear(); int centerPos = 30; Console.SetCursorPosition(centerPos, 5); Console.Write($"Daily Report: {e.Date}"); Console.SetCursorPosition(15, 7); Console.ForegroundColor = ConsoleColor.DarkYellow; Console.Write("ID"); Console.SetCursorPosition(25, 7); Console.Write("Hamster Name"); Console.SetCursorPosition(45, 7); Console.Write("Time Waited For Work-out"); Console.SetCursorPosition(75, 7); Console.Write("Total Workouts"); Console.ForegroundColor = ConsoleColor.White; for (int i = 0; i < e.Hamsters.Count; i++) { Console.SetCursorPosition(15, 8 + i); Console.Write(e.Hamsters[i].HamsterId); Console.SetCursorPosition(25, 8 + i); Console.Write(e.Hamsters[i].Name); Console.SetCursorPosition(45, 8 + i); Console.Write(e.Hamsters[i].TimeWaited.ToString()); var query = e.Logs .Where(l => l.Timestamp.Day == e.Date.Day) .Where(z => z.ActivityId == 1) .Where(y => y.HamsterId == e.Hamsters[i].HamsterId) .Count(); Console.SetCursorPosition(75, 8 + i); Console.Write(query); } }); }
public virtual void OnSendningDailyInfo(object sender, DailyReportEventArgs e) { Timer_.Change(Timeout.Infinite, Timeout.Infinite); // stops the ticker Timer_.Change(5000, tickerSpeed); }