using MaterialTraceability.Business.Impl;
using MaterialTraceability.Common;
using MaterialTraceability.Entity.DAO;
using MaterialTraceability.Entity.DTO;
using MaterialTraceability.Entity.Enum;
using MaterialTraceability.Entity.UpLoad;
using MaterialTraceability.SqlSugar;
using MaterialTraceability.SqlSugar.ServiceImpl;
using System;
using System.Collections.Generic;
namespace MaterialTraceability.Business
{
///
/// 主业务逻辑,通过PLC事件触发
///
public class MainBusiness
{
///
/// 日志刷新
///
/// 日志内容
///
public delegate void LogRefresh(LogType logType,string massage);
public static event LogRefresh LogRefreshEvent;
///
/// 视图层数据刷新
///
///
public delegate void ViewModelRefresh(ViewModelDto viewModelDto);
public static event ViewModelRefresh ViewModelRefreshEvent;
///
/// PLC信号事件
///
///
///
public delegate void SignalReadInvoke(int status,int position);
///
/// RFID读写器业务
///
private EquipBusiness equipBusiness = new EquipBusiness();
//设备信息
private List proEquips = new List();
private ISignalReadFunction signalRead = null;
private PlcBusiness plcBusiness = new PlcBusiness();
private AppConfigDto appConfig = AppConfigDto.Instance;
public MainBusiness()
{
plcBusiness.SignalRefreshEvent += SignalRead_Invoke;
switch (appConfig.processId)
{
case "AB":
signalRead = new ABSignalReadBusiness();
break;
case "TB":
signalRead = new TBSignalReadBusiness();
break;
case "MQ_A":
signalRead = new MQSignalReadBusiness();
break;
case "LY_A":
signalRead = new LYSignalReadBusiness();
break;
default:
LogRefreshEvent?.Invoke(LogType.AlarmLog,"工序设置有误");
break;
}
//PLC初始化
this.PLCInit();
//初始化设备连接
this.EquipInit();
//更新设备状态
this.RefreshDeviceTime();
}
///
/// PLC连接初始化
///
private void PLCInit()
{
ViewModelDto viewModel = new ViewModelDto();
LogRefreshEvent?.Invoke(LogType.PlcLog, "PLC初始化");
if (appConfig.processId.Contains("MQ_A"))
{
plcBusiness.InitPlc("InovancePlc");
}
else
{
plcBusiness.InitPlc("OmronNJPlc");
}
if (plcBusiness.Connect())
{
LogRefreshEvent?.Invoke(LogType.PlcLog, "PLC连接成功");
viewModel.plcStatus = true;
switch (appConfig.processId)
{
case "TB":
TBSignalInit();
break;
case "LY_A":
LYSignalInit();
break;
case "MQ_A":
MqSignalInit();
break;
default:
break;
}
plcBusiness.LoopRead("");
}
else
{
LogRefreshEvent?.Invoke(LogType.PlcLog, "PLC连接失败");
LogRefreshEvent?.Invoke(LogType.AlarmLog, "PLC连接失败");
viewModel.plcStatus = false;
}
ViewModelRefreshEvent?.Invoke(viewModel);
System.Timers.Timer timer = new System.Timers.Timer(1000 * 10);
timer.Elapsed += new System.Timers.ElapsedEventHandler(RefreshPlcStatus);
timer.AutoReset = true;
timer.Enabled = true;
timer.Start();
}
///
/// RFID设备初始化
///
private void EquipInit()
{
ViewModelDto viewModel = new ViewModelDto();
//初始化读写器
LogRefreshEvent?.Invoke(LogType.RfidLog, "读写器初始化");
if (equipBusiness.InitEquipLsit())
{
LogRefreshEvent?.Invoke(LogType.RfidLog, "读写器设备初始化成功");
proEquips = equipBusiness.Equiplist;
List deviceInfos = new List();
proEquips.ForEach(x =>
{
deviceInfos.Add(new deviceInfo()
{
position = x.positionId,
status = x.IsConnect
});
});
viewModel.plcStatus = plcBusiness.IsConnected();
viewModel.deviceStatus = deviceInfos;
ViewModelRefreshEvent?.Invoke(viewModel);
}
else
{
LogRefreshEvent?.Invoke(LogType.RfidLog, "读写器初始化失败");
LogRefreshEvent?.Invoke(LogType.AlarmLog, "读写器初始化失败");
}
}
///
/// PLC信号逻辑处理
///
///
///
public void SignalRead_Invoke(int signal, int position)
{
try
{
switch (signal)
{
case 1:
signalRead.UpMaterialBegin(position);
break;
case 2:
signalRead.UpMaterialEnd(position);
break;
case 3:
signalRead.DownMaterialBegin(position);
break;
case 4:
signalRead.DownMaterialEnd(position);
break;
case 5:
signalRead.ForceDown(position);
break;
case 6:
signalRead.UpBegin(position);
break;
case 7:
signalRead.DownBegin(position);
break;
case 8:
signalRead.MachineStartUp();
break;
default:
break;
}
}catch(Exception ex)
{
LogHelper.Error("PLC信号逻辑处理异常", ex);
}
}
///
/// PLC状态刷新
///
///
///
private void RefreshPlcStatus(object source, System.Timers.ElapsedEventArgs e)
{
ViewModelDto viewModel = new ViewModelDto();
bool info = plcBusiness.IsConnected();
viewModel.plcStatus = info;
ViewModelRefreshEvent?.Invoke(viewModel);
if (!info)
{
LogRefreshEvent?.Invoke(LogType.PlcLog,"PLC断开,重新建立连接");
LogHelper.Info("PLC断开,重新建立连接");
bool result = plcBusiness.ReConnect();
if (result)
{
LogRefreshEvent?.Invoke(LogType.PlcLog, "PLC断开,重新连接成功");
LogHelper.Info("PLC断开,重新连接成功");
viewModel.plcStatus = result;
ViewModelRefreshEvent?.Invoke(viewModel);
}
else
{
LogRefreshEvent?.Invoke(LogType.AlarmLog, "PLC断开,重新连接失败");
LogHelper.Info("PLC断开,重新连接失败");
}
}
}
///
/// 涂布信号清除
///
public void TBSignalInit()
{
try
{
plcBusiness.writePlc(appConfig.TbAddress.A收卷轴涨紧, 0);
plcBusiness.writePlc(appConfig.TbAddress.A收卷轴完工, 0);
plcBusiness.writePlc(appConfig.TbAddress.B收卷轴涨紧, 0);
plcBusiness.writePlc(appConfig.TbAddress.B收卷轴完工, 0);
}catch(Exception ex)
{
LogHelper.Error("涂布信号清除", ex);
}
}
///
/// 冷压信号清除
///
public void LYSignalInit()
{
try
{
for(int i = 0; i <= 13; i++)
{
string plcAddress = "D600" + i;
Console.WriteLine("清除PLC信号:" + plcAddress);
LogHelper.Info("系统初始化清除PLC信号:"+plcAddress);
plcBusiness.writePlc(plcAddress, 0);
}
}catch(Exception ex)
{
LogHelper.Error("冷压信号清除", ex);
}
}
///
/// 模切信号清除
///
public void MqSignalInit()
{
plcBusiness.writePlc(appConfig.mqAddress.开机启动, 0);
LogHelper.Info("模切程序初始化开机启动信号复位,MW25678写0");
}
///
/// 定时向服务器写入当前时间,确保上位机在线
///
private void RefreshDeviceTime()
{
UpLoadBusiness loadBusiness = UpLoadBusiness.Instance;;
var updateTimer = new System.Timers.Timer(1000*60*3);
updateTimer.Elapsed += loadBusiness.RefreshDeviceTime;
updateTimer.Enabled = true;
}
}
}