You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

314 lines
10 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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
{
/// <summary>
/// 主业务逻辑通过PLC事件触发
/// </summary>
public class MainBusiness
{
/// <summary>
/// 日志刷新
/// </summary>
/// <param name="type">日志内容</param>
/// <param name="massage"></param>
public delegate void LogRefresh(LogType logType,string massage);
public static event LogRefresh LogRefreshEvent;
/// <summary>
/// 视图层数据刷新
/// </summary>
/// <param name="viewModelDto"></param>
public delegate void ViewModelRefresh(ViewModelDto viewModelDto);
public static event ViewModelRefresh ViewModelRefreshEvent;
/// <summary>
/// PLC信号事件
/// </summary>
/// <param name="status"></param>
/// <param name="position"></param>
public delegate void SignalReadInvoke(int status,int position);
/// <summary>
/// RFID读写器业务
/// </summary>
private EquipBusiness equipBusiness = new EquipBusiness();
//设备信息
private List<ProEquip> proEquips = new List<ProEquip>();
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();
}
/// <summary>
/// PLC连接初始化
/// </summary>
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();
}
/// <summary>
/// RFID设备初始化
/// </summary>
private void EquipInit()
{
ViewModelDto viewModel = new ViewModelDto();
//初始化读写器
LogRefreshEvent?.Invoke(LogType.RfidLog, "读写器初始化");
if (equipBusiness.InitEquipLsit())
{
LogRefreshEvent?.Invoke(LogType.RfidLog, "读写器设备初始化成功");
proEquips = equipBusiness.Equiplist;
List<deviceInfo> deviceInfos = new List<deviceInfo>();
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, "读写器初始化失败");
}
}
/// <summary>
/// PLC信号逻辑处理
/// </summary>
/// <param name="signal"></param>
/// <param name="position"></param>
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);
}
}
/// <summary>
/// PLC状态刷新
/// </summary>
/// <param name="source"></param>
/// <param name="e"></param>
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断开重新连接失败");
}
}
}
/// <summary>
/// 涂布信号清除
/// </summary>
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);
}
}
/// <summary>
/// 冷压信号清除
/// </summary>
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);
}
}
/// <summary>
/// 模切信号清除
/// </summary>
public void MqSignalInit()
{
plcBusiness.writePlc(appConfig.mqAddress., 0);
LogHelper.Info("模切程序初始化开机启动信号复位MW25678写0");
}
/// <summary>
/// 定时向服务器写入当前时间,确保上位机在线
/// </summary>
private void RefreshDeviceTime()
{
UpLoadBusiness loadBusiness = UpLoadBusiness.Instance;;
var updateTimer = new System.Timers.Timer(1000*60*3);
updateTimer.Elapsed += loadBusiness.RefreshDeviceTime;
updateTimer.Enabled = true;
}
}
}