using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using Newtonsoft.Json.Linq; using Quartz; using SolarPower.Models; using SolarPower.Repository.Interface; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Threading; using System.Threading.Tasks; namespace SolarPower.Quartz.Jobs { [DisallowConcurrentExecution] public class ExceptionSchedule : IJob { private readonly ILogger logger; private readonly IOverviewRepository overviewRepository; private readonly INoticeScheduleRepository noticeScheduleRepository; private readonly IUserRepository userRepository; private readonly IPowerStationRepository powerStationRepository; private readonly IConfiguration Configuration; public ExceptionSchedule(ILogger logger, IOverviewRepository overviewRepository,INoticeScheduleRepository noticeScheduleRepository,IUserRepository userRepository,IPowerStationRepository powerStationRepository, IConfiguration Configuration) { this.logger = logger; this.overviewRepository = overviewRepository; this.noticeScheduleRepository = noticeScheduleRepository; this.userRepository = userRepository; this.powerStationRepository = powerStationRepository; this.Configuration = Configuration; } public async Task Execute(IJobExecutionContext context) { try { var ExceptionTimes = this.Configuration.GetValue("ExceptionTimes"); var ExceptionTimes_Priority1 = this.Configuration.GetValue("ExceptionTimes_Priority1"); var ExceptionTimes_Priority2 = this.Configuration.GetValue("ExceptionTimes_Priority2"); var ExceptionTimes_Priority3 = this.Configuration.GetValue("ExceptionTimes_Priority3"); var ExceptionList = await overviewRepository.GetEmailExceptionList(); if(ExceptionList.Count > 0 ) { var ExceptionListex = ExceptionList.Where(x => x.sourceState == 1 && x.priority == 1 && (DateTime.Now.Subtract(DateTime.Parse(x.dev_time)).TotalSeconds / 60) >= ExceptionTimes_Priority1).ToList(); var ExceptionListex2 = ExceptionList.Where(x => x.sourceState == 1 && x.priority == 2 && (DateTime.Now.Subtract(DateTime.Parse(x.dev_time)).TotalSeconds / 60) >= ExceptionTimes_Priority2).ToList(); var ExceptionListex3 = ExceptionList.Where(x => x.sourceState == 1 && x.priority == 3 && (DateTime.Now.Subtract(DateTime.Parse(x.dev_time)).TotalSeconds / 60) >= ExceptionTimes_Priority3).ToList(); ExceptionListex.AddRange(ExceptionListex2); ExceptionListex.AddRange(ExceptionListex3); foreach (var Exception in ExceptionListex) { var UserListWithPowerstation = await overviewRepository.GetUserListWithPowerstation(Exception.PowerStationId); var Content = $"電站名稱:{Exception.PowerStationName}" + "
" + $"設備編號:{Exception.errDevice}" + "
" + $"異常ID編號:{Exception.id}" + "
" + $"異常類別:{Exception.alarmClassName}" + "
" + $"設備訊息:{Exception.errMsg}" + "
"; if(Exception.errMsgT == "d") { Content += $"當前數值:{Exception.errValue}" + "
"; } Content += $"發生時間:{Exception.dev_time}"; foreach (var user in UserListWithPowerstation) { NoticeSchedule DaySchedule = new NoticeSchedule() { UserId = user.Id, EmailType = 4, RecipientEmail = user.Email, Subject = "異常通知", Content = Content, RecipientName = user.Name, Type = 1, ExceptionId = Exception.id }; List properties = new List() { "UserId", "EmailType", "RecipientEmail", "Subject", "Content", "RecipientName", "Type", "ExceptionId" }; await noticeScheduleRepository.AddOneAsync(DaySchedule, properties); } var powerstation = await powerStationRepository.GetOneAsync(Exception.PowerStationId); if (powerstation.line_token != null) { Content = " %0D%0A " + Content.Replace("
", " %0D%0A "); CallLineToken(Content, powerstation.line_token); } } } } catch (Exception exception) { logger.LogError("【{0}】{1}", nameof(logger), exception.Message); } } public string CallLineToken(string message, string LineToken) { try { HttpWebRequest Postrequest = (HttpWebRequest)WebRequest.Create("https://notify-api.line.me/api/notify?message=" + message); Postrequest.Method = "POST"; Postrequest.Headers.Add("Authorization", "Bearer " + LineToken); Postrequest.PreAuthenticate = true; HttpWebResponse response = (HttpWebResponse)Postrequest.GetResponse(); var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd(); var final = JObject.Parse(responseString); var get = final["status"].ToString(); if (get != "200") { logger.LogError("CallLineToken發送Line接收失敗"); //Logger.LogError("【" + controllerName + "/" + actionName + "】" + "CallLineToken發送Line接收失敗"); } return get; } catch (Exception ex) { logger.LogError("【{0}】CallLineToken: {1}", nameof(logger), ex.Message); //Logger.LogError("【" + controllerName + "/" + actionName + "】" + "CallLineToken:" + ex); throw ex; } } } }