FIC_Solar/SolarPower/Quartz/Jobs/ExceptionSchedule.cs
2021-09-17 16:57:16 +08:00

123 lines
5.5 KiB
C#

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<ExceptionSchedule> logger;
private readonly IOverviewRepository overviewRepository;
private readonly INoticeScheduleRepository noticeScheduleRepository;
private readonly IUserRepository userRepository;
private readonly IPowerStationRepository powerStationRepository;
public ExceptionSchedule(ILogger<ExceptionSchedule> logger, IOverviewRepository overviewRepository,INoticeScheduleRepository noticeScheduleRepository,IUserRepository userRepository,IPowerStationRepository powerStationRepository)
{
this.logger = logger;
this.overviewRepository = overviewRepository;
this.noticeScheduleRepository = noticeScheduleRepository;
this.userRepository = userRepository;
this.powerStationRepository = powerStationRepository;
}
public async Task Execute(IJobExecutionContext context)
{
try
{
var ExceptionList = await overviewRepository.GetEmailExceptionList();
if(ExceptionList.Count > 0 )
{
var ExceptionListex = ExceptionList.Where(x => x.normalTime == "1970-01-01 08:00:00" && DateTime.Now.Subtract(DateTime.Parse(x.dev_time)).Hours >= 4).ToList();
foreach (var Exception in ExceptionListex)
{
var UserListWithPowerstation = await overviewRepository.GetUserListWithPowerstation(Exception.PowerStationId);
foreach (var user in UserListWithPowerstation)
{
var Content = $"電站名稱:{Exception.PowerStationName}" + "<br>" +
$"設備編號:{Exception.errDevice}" + "<br>" +
$"異常ID編號:{Exception.id}" + "<br>" +
$"異常類別:{Exception.alarmClassName}" + "<br>" +
$"設備訊息:{Exception.errMsg}" + "<br>" +
$"發生時間:{Exception.dev_time}" ;
NoticeSchedule DaySchedule = new NoticeSchedule()
{
UserId = user.Id,
EmailType = 4,
RecipientEmail = user.Email,
Subject = "異常通知",
Content = Content,
RecipientName = user.Name,
Type = 1,
ExceptionId = Exception.id
};
List<string> properties = new List<string>()
{
"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)
{
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;
}
}
}
}