using Admin.Core.Common;
using Admin.Core.IService;
using Admin.Core.IService.ISys;
using Admin.Core.Model;
using Admin.Core.PlcServer;
using Admin.Core.Service;
using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
///
/// 这里要注意下,命名空间和程序集是一样的,不然反射不到
///
namespace Admin.Core.Tasks
{
public class Job_Admin_Quartz : JobBase, IJob
{
private readonly IPmt_AlarmServices _pmtAlarmServices;
private readonly ILR_AlarmlogServices _lrAlarmlogServices;
public Job_Admin_Quartz(ISysTasksQzService SysTasksQzService, ISysJobLogService sysJobLogService,
IPmt_AlarmServices pmtAlarmServices, ILR_AlarmlogServices lrAlarmlogServices)
{
_SysTasksQzService = SysTasksQzService;
_sysJobLogService = sysJobLogService;
_pmtAlarmServices = pmtAlarmServices;
_lrAlarmlogServices = lrAlarmlogServices;
}
public async Task Execute(IJobExecutionContext context)
{
await ExecuteJob(context, async () => await Run(context));
}
public async Task Run(IJobExecutionContext context)
{
var plcList = PlcHelper.siemensList.SingleOrDefault(d => d.EquipName.Equals("小料PLC"));
if (plcList.plc.IsConnected)
{
List addAlarmList = new List();
List updateAlarmList = new List();
List list = await _pmtAlarmServices.QueryAsync();
foreach (var item in list)
{
if (plcList.plc.ReadBool(item.Alarm_Point))
{
var alarm = await _lrAlarmlogServices.FirstAsync(d => d.Alarm_ID == item.Alarm_ID);
if (alarm==null)
{
LR_Alarmlog lR_Alarmlog = new LR_Alarmlog();
lR_Alarmlog.Equip_Code = "01";
lR_Alarmlog.Alarm_ID = item.Alarm_ID;
lR_Alarmlog.Alarm_OccurTime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
lR_Alarmlog.Alarm_Status = 1;
lR_Alarmlog.Alarm_GUID = "01";
await _lrAlarmlogServices.AddAsync(lR_Alarmlog);
//addAlarmList.Add(lR_Alarmlog);
}
else
{
alarm.Alarm_ClearTime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
alarm.Alarm_Status = 1;
await _lrAlarmlogServices.UpdateAsync(alarm);
}
}
else
{
var alarm = await _lrAlarmlogServices.FirstAsync(d => d.Alarm_ID == item.Alarm_ID);
if (alarm == null)
{
LR_Alarmlog lR_Alarmlog = new LR_Alarmlog();
lR_Alarmlog.Equip_Code = "01";
lR_Alarmlog.Alarm_ID = item.Alarm_ID;
lR_Alarmlog.Alarm_OccurTime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
lR_Alarmlog.Alarm_Status = 0;
lR_Alarmlog.Alarm_GUID = "01";
await _lrAlarmlogServices.UpdateAsync(lR_Alarmlog);
}
else
{
alarm.Alarm_ClearTime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
alarm.Alarm_Status = 0;
await _lrAlarmlogServices.UpdateAsync(alarm);
}
}
}
}
}
}
}