異常email 寄信
This commit is contained in:
parent
c2a84e7059
commit
7ab0656a1e
@ -19,6 +19,7 @@ namespace SolarPower.Models
|
||||
public string Reason { get; set; }
|
||||
public int UserId { get; set; }
|
||||
public int EmailType { get; set; }
|
||||
public int ExceptionId { get; set; }
|
||||
}
|
||||
|
||||
public class OperationPersonnel
|
||||
|
||||
@ -178,4 +178,24 @@ namespace SolarPower.Models
|
||||
public string TOTALTIME { get; set; }
|
||||
}
|
||||
|
||||
public class ExceptionEmailInfo
|
||||
{
|
||||
public int id { get; set; }//Id編號
|
||||
public string site_id { get; set; }//電站編號
|
||||
public string alarmClassName { get; set; }//異常類別
|
||||
public string errDevice { get; set; }//設備編號
|
||||
public string errValue { get; set; }//原始值
|
||||
public string dev_time { get; set; }//發生時間
|
||||
public string errMsg { get; set; }//錯誤原因
|
||||
public string PowerStationName { get; set; }//電站名稱
|
||||
public int PowerStationId { get; set; }//電站流水號
|
||||
}
|
||||
|
||||
public class UserPowerStationTo
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string Email { get; set; }
|
||||
public int Id { get; set; }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
80
SolarPower/Quartz/Jobs/ExceptionSchedule.cs
Normal file
80
SolarPower/Quartz/Jobs/ExceptionSchedule.cs
Normal file
@ -0,0 +1,80 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Quartz;
|
||||
using SolarPower.Models;
|
||||
using SolarPower.Repository.Interface;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
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;
|
||||
|
||||
public ExceptionSchedule(ILogger<ExceptionSchedule> logger, IOverviewRepository overviewRepository,INoticeScheduleRepository noticeScheduleRepository,IUserRepository userRepository)
|
||||
{
|
||||
this.logger = logger;
|
||||
this.overviewRepository = overviewRepository;
|
||||
this.noticeScheduleRepository = noticeScheduleRepository;
|
||||
this.userRepository = userRepository;
|
||||
}
|
||||
|
||||
public async Task Execute(IJobExecutionContext context)
|
||||
{
|
||||
try
|
||||
{
|
||||
var ExceptionList = await overviewRepository.GetEmailExceptionList();
|
||||
if(ExceptionList.Count > 0 )
|
||||
{
|
||||
foreach(var Exception in ExceptionList)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError("【{0}】{1}", nameof(logger), exception.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -631,5 +631,59 @@ namespace SolarPower.Repository.Implement
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<List<ExceptionEmailInfo>> GetEmailExceptionList()
|
||||
{
|
||||
List<ExceptionEmailInfo> exceptionEmailInfos = new List<ExceptionEmailInfo>();
|
||||
using (IDbConnection conn = this._databaseHelper.GetConnection())
|
||||
{
|
||||
try
|
||||
{
|
||||
var sql = @$"SELECT a.`*`,ns.Id FROM
|
||||
(
|
||||
select a.id, site_id, `timestamp`, FROM_UNIXTIME((`timestamp` / 1000), '%Y-%m-%d %H:%i:%s') dev_time , a.sourceState err_status, FROM_UNIXTIME( (a.normalTime / 1000), '%Y-%m-%d %H:%i:%s') normalTime,
|
||||
a.alarmClass, b.alarmClass as alarmClassName,ps.Name as PowerStationName,ps.Id as PowerStationId,
|
||||
errDevice, err_valueKind, errValue, FROM_UNIXTIME( (a.lastUpdate / 1000), '%Y-%m-%d %H:%i:%s') lastUpdate,
|
||||
case when c.errMsg_tw is null then d.errMsg_tw else c.errMsg_tw end errMsg
|
||||
from err_main a
|
||||
join alarmorion_orionalarmclass b on a.alarmclass = b.id
|
||||
left join ref_err_device c on trim(b.alarmClass) = c.deviceType
|
||||
left join ref_err_inv d on lower(b.alarmClass) = d.deviceType
|
||||
and case when lower(b.alarmClass) = 'inverter' and err_valuekind = 'e' then errvalue else '' end = d.errCode
|
||||
left join power_station ps on ps.Code = site_id
|
||||
left join operation_record pr on pr.ErrorCode = a.id
|
||||
) a
|
||||
LEFT JOIN
|
||||
notice_schedule ns ON ns.ExceptionId = a.id
|
||||
WHERE ns.Id IS NULL AND a.err_status = 1 AND a.PowerStationId IS NOT null";
|
||||
exceptionEmailInfos = (await conn.QueryAsync<ExceptionEmailInfo>(sql)).ToList();
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
throw exception;
|
||||
}
|
||||
};
|
||||
return exceptionEmailInfos;
|
||||
}
|
||||
|
||||
public async Task<List<UserPowerStationTo>> GetUserListWithPowerstation(int id)
|
||||
{
|
||||
List<UserPowerStationTo> result = new List<UserPowerStationTo>();
|
||||
using (IDbConnection conn = this._databaseHelper.GetConnection())
|
||||
{
|
||||
try
|
||||
{
|
||||
var sql = @$"SELECT u.Name,u.Email,u.Id FROM power_station_operation_personnel pss
|
||||
LEFT JOIN user u ON u.Id = pss.UserId
|
||||
WHERE pss.Deleted = 0 AND pss.EmailException = 1 AND pss.PowerStationId = {id} AND u.Deleted = 0";
|
||||
result = (await conn.QueryAsync<UserPowerStationTo>(sql)).ToList();
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
throw exception;
|
||||
}
|
||||
};
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -30,5 +30,7 @@ namespace SolarPower.Repository.Interface
|
||||
Task<List<InverterHistory>> GetListInverterYearByPowerStationId(int powerStationId, string year);
|
||||
Task<List<HistoryTable>> GethistoryTable(HistorySent post);
|
||||
Task<List<ExceptionDataTable>> GetExceptionTable2(ExceptionSent2 post);
|
||||
Task<List<ExceptionEmailInfo>> GetEmailExceptionList();
|
||||
Task<List<UserPowerStationTo>> GetUserListWithPowerstation(int id);
|
||||
}
|
||||
}
|
||||
|
||||
@ -132,6 +132,13 @@ namespace SolarPower
|
||||
);
|
||||
#endregion
|
||||
|
||||
#region 琩高钵盽穝糤<EFBFBD>Email
|
||||
services.AddSingleton<ExceptionSchedule>();
|
||||
services.AddSingleton(
|
||||
new JobSchedule(jobType: typeof(ExceptionSchedule), cronExpression: Configuration.GetValue<string>("BackgroundServiceCron:ExceptionSchedule"))
|
||||
);
|
||||
#endregion
|
||||
|
||||
services.AddHostedService<QuartzHostedService>();
|
||||
#endregion
|
||||
}
|
||||
|
||||
@ -26,7 +26,8 @@
|
||||
"CalcAvgPowerStationJob": "0 0 2 * * ?",
|
||||
"OperationScheduleJob": "0 0 2 * * ?",
|
||||
"CalcInverter15minJob": "0 2/15 * * * ?",
|
||||
"SendEmailJob": "0 15 2 * * ?"
|
||||
"SendEmailJob": "0 15 2 * * ?",
|
||||
"ExceptionSchedule": "0 0/5 * * * ?"
|
||||
},
|
||||
"SMTPConfig": {
|
||||
"Host": "smtp.gmail.com",
|
||||
|
||||
@ -27,7 +27,8 @@
|
||||
"CalcAvgPowerStationJob": "0 0 2 * * ?",
|
||||
"OperationScheduleJob": "0 0 2 * * ?",
|
||||
"CalcInverter15minJob": "0 2/15 * * * ?",
|
||||
"SendEmailJob": "0 15 2 * * ?"
|
||||
"SendEmailJob": "0 15 2 * * ?",
|
||||
"ExceptionSchedule": "0 0/5 * * * ?"
|
||||
},
|
||||
"SMTPConfig": {
|
||||
"Host": "smtp.gmail.com",
|
||||
|
||||
Loading…
Reference in New Issue
Block a user