|
|
using Admin.Core.Common;
|
|
|
using Admin.Core.IService;
|
|
|
using Admin.Core.Model;
|
|
|
using Admin.Core.PlcServer;
|
|
|
using log4net;
|
|
|
using System.Timers;
|
|
|
|
|
|
namespace Admin.Core.RunPlc
|
|
|
{
|
|
|
/// <summary>
|
|
|
/// 扫码器初始化
|
|
|
/// </summary>
|
|
|
public class RunPlcService : IRunPlcService
|
|
|
{
|
|
|
|
|
|
private readonly IPmt_AlarmServices _pmtAlarmServices;
|
|
|
private readonly ILR_AlarmlogServices _lrAlarmlogServices;
|
|
|
private static readonly log4net.ILog log = LogManager.GetLogger(typeof(RunPlcService));
|
|
|
bool flag = true;
|
|
|
|
|
|
#region 构造函数
|
|
|
public RunPlcService(IPmt_AlarmServices pmtAlarmServices, ILR_AlarmlogServices lrAlarmlogServices)
|
|
|
{
|
|
|
_pmtAlarmServices = pmtAlarmServices;
|
|
|
_lrAlarmlogServices = lrAlarmlogServices;
|
|
|
StartSiemensPlcServer();//西门子
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
#region 心跳
|
|
|
public Task StartPlcAsync()
|
|
|
{
|
|
|
System.Timers.Timer timer = new System.Timers.Timer(5000);//创建定时器,设置间隔时间为5000毫秒;
|
|
|
timer.Elapsed += new System.Timers.ElapsedEventHandler(ExecSiemensHeartTask);
|
|
|
timer.AutoReset = true;//设置是执行一次(false)还是一直执行(true);
|
|
|
timer.Enabled = true;//需要调用 timer.Start()或者timer.Enabled = true来启动它,
|
|
|
timer.Start();//timer.Start()的内部原理还是设置timer.Enabled = true;
|
|
|
return Task.CompletedTask;
|
|
|
}
|
|
|
private static void StartSiemensPlcServer()
|
|
|
{
|
|
|
var allPlcServices = Appsettings.app<PlcModel>("PLCServer").ToList();
|
|
|
for (int i = 0; i < allPlcServices.Count; i++)
|
|
|
{
|
|
|
PlcModel model = new PlcModel();
|
|
|
model.Id = allPlcServices[i].Id;
|
|
|
model.EquipName = allPlcServices[i].EquipName;
|
|
|
model.IP = allPlcServices[i].IP;
|
|
|
model.Port = allPlcServices[i].Port;
|
|
|
model.plc = new SiemensPlc(allPlcServices[i].IP, allPlcServices[i].Port);
|
|
|
PlcHelper.siemensList.Add(model);
|
|
|
}
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
#region 心跳检测
|
|
|
private async void ExecSiemensHeartTask(object? sender, ElapsedEventArgs e)
|
|
|
{
|
|
|
var plcList = PlcHelper.siemensList.Where(d => d.EquipName.Equals("小料PLC")).ToList();
|
|
|
foreach (var item in plcList)
|
|
|
{
|
|
|
if (await item.plc.Read("DB104.DBW0"))
|
|
|
{
|
|
|
item.IsConnect = true;
|
|
|
item.plc.IsConnected = true;
|
|
|
//Console.WriteLine(item.melsecPlc.ReadBool("M100"));
|
|
|
//Console.WriteLine($"{item.EquipName}:PLC连接成功!");
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
if (item.plc.IsConnected == false)
|
|
|
{
|
|
|
item.IsConnect = false;
|
|
|
//Console.WriteLine($"{item.EquipName}:PLC连接失败!");
|
|
|
System.GC.Collect();
|
|
|
bool r = item.plc.Connect(item.IP, item.Port);
|
|
|
if (r)
|
|
|
{
|
|
|
item.plc.IsConnected = r;
|
|
|
item.IsConnect = r;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
item.plc.IsConnected = r;
|
|
|
item.plc.DisConnect();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
public Task StartAlarmAsync()
|
|
|
{
|
|
|
//System.Timers.Timer timer = new System.Timers.Timer(1000);//创建定时器,设置间隔时间为1000毫秒;
|
|
|
//timer.Elapsed += new System.Timers.ElapsedEventHandler(StartAlarm); //到达时间的时候执行事件;
|
|
|
//timer.AutoReset = true;//设置是执行一次(false)还是一直执行(true);
|
|
|
//timer.Enabled = true;//需要调用 timer.Start()或者timer.Enabled = true来启动它,
|
|
|
//timer.Start();//timer.Start()的内部原理还是设置timer.Enabled = true;
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async void StartAlarm(object source, System.Timers.ElapsedEventArgs e)
|
|
|
{
|
|
|
if (flag)
|
|
|
{
|
|
|
flag = false;
|
|
|
try
|
|
|
{
|
|
|
var plcList = PlcHelper.siemensList.SingleOrDefault(d => d.EquipName.Equals("小料PLC"));
|
|
|
if (plcList.plc.IsConnected)
|
|
|
{
|
|
|
List<LR_Alarmlog> addAlarmList = new List<LR_Alarmlog>();
|
|
|
List<LR_Alarmlog> updateAlarmList = new List<LR_Alarmlog>();
|
|
|
List<Pmt_Alarm> list = _pmtAlarmServices.QueryAsync().Result;
|
|
|
Console.WriteLine("=======================开始================================");
|
|
|
|
|
|
int i= 0;
|
|
|
do
|
|
|
{
|
|
|
Console.WriteLine(i);
|
|
|
await ReadAlarm(plcList, list[i]);
|
|
|
i++;
|
|
|
} while (list.Count >= i);
|
|
|
|
|
|
Console.WriteLine("=======================结束================================");
|
|
|
}
|
|
|
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
log.Error(ex.Message);
|
|
|
}
|
|
|
finally { flag = true; }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private async Task ReadAlarm(PlcModel plcList, Pmt_Alarm item)
|
|
|
{
|
|
|
|
|
|
bool result = await plcList.plc.ReadBoolAsync(item.Alarm_Point);
|
|
|
|
|
|
Console.WriteLine(item.Alarm_Point + ":" + result);
|
|
|
if (result)
|
|
|
{
|
|
|
var alarm =await _lrAlarmlogServices.FirstAsync(d => d.Alarm_ID == item.Alarm_ID && d.Alarm_Status == 1);
|
|
|
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";
|
|
|
|
|
|
_ = _lrAlarmlogServices.AddAsync(lR_Alarmlog).Result;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
//alarm.Alarm_ClearTime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
|
|
|
//alarm.Alarm_Status = 1;
|
|
|
//_= _lrAlarmlogServices.UpdateAsync(alarm).Result;
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
var alarm = _lrAlarmlogServices.FirstAsync(d => d.Alarm_ID == item.Alarm_ID && d.Alarm_Status == 1).Result;
|
|
|
if (alarm != null)
|
|
|
{
|
|
|
alarm.Alarm_ClearTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
|
|
alarm.Alarm_Status = 0;
|
|
|
_ = _lrAlarmlogServices.UpdateAsync(alarm).Result;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
}
|