using Bro.Common.Factory;
|
using Bro.Common.Helper;
|
using Bro.Common.PubSub;
|
using Bro.UI.Model.Winform;
|
using System;
|
using System.Collections.Generic;
|
using System.Drawing;
|
using System.Linq;
|
using System.Threading;
|
using System.Threading.Tasks;
|
using System.Windows.Forms;
|
using WeifenLuo.WinFormsUI.Docking;
|
using static Bro.Common.Helper.EnumHelper;
|
|
namespace Bro.UI.Config.MenuForms
|
{
|
[DockOption(DockState.DockLeft, 200, 0)]
|
[MenuNode("Operation", "操作", 2, "View1", true)]
|
public partial class FrmOperation : MenuFrmBase
|
{
|
static bool _isFirstLoad = true;
|
public FrmOperation()
|
{
|
InitializeComponent();
|
PubSubCenter.GetInstance().RemoveSubscribers(PubSubCenterMessageType.UpdateProductionCodes.ToString());
|
PubSubCenter.GetInstance().Subscribe(PubSubCenterMessageType.UpdateProductionCodes.ToString(), OnUpdateProductionCodes);
|
|
LoadProcessCode();
|
LoadProductionCode();
|
|
//Task.Run(() =>
|
//{
|
// Thread.Sleep(1000);
|
|
// if ((!plProcess.Visible) && (!plProduct.Visible) && _isFirstLoad)
|
// {
|
// LoadProcess();
|
// _isFirstLoad = false;
|
// }
|
//});
|
//if ((!plProcess.Visible) && (!plProduct.Visible) && _isFirstLoad)
|
//{
|
// LoadProcess();
|
// _isFirstLoad = false;
|
//}
|
}
|
|
#region Load Codes
|
string _processCode = "";
|
string _productionCode = "";
|
|
private object OnUpdateProductionCodes(ISubscriber arg1, object arg2, object arg3)
|
{
|
LoadProductionCode();
|
return null;
|
}
|
|
private void LoadProcessCode()
|
{
|
var systemProcessCodes = ProcessFactory.GetProcessCodes();
|
var avaiableProcessCodes = SettingHelper.GetProcessCodes();
|
|
List<string> pCodes = new List<string>();
|
|
if (avaiableProcessCodes.Count > 0)
|
{
|
pCodes = avaiableProcessCodes.Intersect(systemProcessCodes).ToList();
|
}
|
else
|
{
|
pCodes = systemProcessCodes;
|
}
|
|
if (pCodes.Count == 1 && pCodes[0] == "")
|
{
|
plProcess.Visible = false;
|
_processCode = pCodes[0];
|
return;
|
}
|
else
|
{
|
pCodes.Remove("");
|
}
|
|
if (pCodes.Count > 1)
|
{
|
plProcess.Visible = true;
|
|
cboProcessCode.SelectedIndexChanged -= cboProcessCode_SelectedIndexChanged;
|
UIHelper.SetCombo(cboProcessCode, pCodes, "", "");
|
cboProcessCode.SelectedIndexChanged += cboProcessCode_SelectedIndexChanged;
|
cboProcessCode_SelectedIndexChanged(null, null);
|
}
|
else if (pCodes.Count == 1)
|
{
|
plProcess.Visible = false;
|
_processCode = pCodes[0];
|
}
|
else
|
{
|
throw new ProcessException("未能获取流程代码");
|
}
|
}
|
|
private void LoadProductionCode()
|
{
|
var pCodes = SettingHelper.GetProductionCodes();
|
|
if (pCodes.Count > 1)
|
{
|
plProduct.Visible = true;
|
cboProductionCode.SelectedIndexChanged -= cboProductionCode_SelectedIndexChanged;
|
UIHelper.SetCombo(cboProductionCode, pCodes, "", "");
|
cboProductionCode.SelectedIndexChanged += cboProductionCode_SelectedIndexChanged;
|
cboProductionCode_SelectedIndexChanged(null, null);
|
}
|
else
|
{
|
plProduct.Visible = false;
|
}
|
}
|
|
private void cboProcessCode_SelectedIndexChanged(object sender, System.EventArgs e)
|
{
|
_processCode = cboProcessCode.Text;
|
}
|
|
private void cboProductionCode_SelectedIndexChanged(object sender, System.EventArgs e)
|
{
|
_productionCode = cboProductionCode.Text;
|
}
|
#endregion
|
|
private void FrmOperation_Load(object sender, System.EventArgs e)
|
{
|
if ((!plProcess.Visible) && (!plProduct.Visible) && _isFirstLoad)
|
{
|
LoadProcess();
|
_isFirstLoad = false;
|
}
|
|
btnStart.Enabled = true;
|
}
|
|
private void btnLoad_Click(object sender, System.EventArgs e)
|
{
|
btnStart.Enabled = true;
|
|
LogAsync(DateTime.Now, "载入流程");
|
LoadProcess();
|
}
|
|
bool isStart = true;
|
private async void btnStart_Click(object sender, System.EventArgs e)
|
{
|
if (Process == null)
|
{
|
MessageBox.Show("无法操作空流程");
|
return;
|
}
|
|
btnStart.Enabled = false;
|
|
//Task.Run(() =>
|
//{
|
try
|
{
|
//if (Process.ProcessState != EnumHelper.DeviceState.DSOpen)
|
if (isStart)
|
{
|
await ProcessOperation(true);
|
}
|
else
|
{
|
await ProcessOperation(false);
|
}
|
|
isStart = !isStart;
|
}
|
catch (Exception ex)
|
{
|
MessageBox.Show(ex.GetExceptionMessage());
|
}
|
finally
|
{
|
//this.BeginInvoke(new Action(() => btnStart.Enabled = true));
|
btnStart.Enabled = true;
|
}
|
//});
|
}
|
|
string _currentProcssCode = "";
|
string _currentProductionCode = "";
|
private void LoadProcess()
|
{
|
if (_processCode != _currentProcssCode || _productionCode != _currentProductionCode || Process == null)
|
{
|
Process = ProcessFactory.CreateStationProcess(_processCode, _productionCode, out string msg);
|
|
if (!string.IsNullOrWhiteSpace(msg))
|
{
|
Process = null;
|
LogAsync(DateTime.Now, "Process创建失败", $"{msg}");
|
return;
|
}
|
|
//Process.OnProcessStateChanged += Process_OnProcessStateChanged;
|
Process.OnLog += Process_OnLog;
|
|
_currentProcssCode = _processCode;
|
_currentProductionCode = _productionCode;
|
}
|
|
Process.InitialProcess("");
|
|
OnUploadProcess?.Invoke(Id, Process);
|
}
|
|
private void Process_OnLog(DateTime dt, string prefix, string msg)
|
{
|
LogAsync(dt, prefix, msg);
|
}
|
|
//private void Process_OnProcessStateChanged(EnumHelper.DeviceState state)
|
//{
|
// try
|
// {
|
// if (InvokeRequired)
|
// {
|
// this.Invoke(new Action<EnumHelper.DeviceState>(Process_OnProcessStateChanged), state);
|
// }
|
// else
|
// {
|
// try
|
// {
|
// btnStart.Enabled = true;
|
|
// switch (state)
|
// {
|
// case EnumHelper.DeviceState.DSOpen:
|
// btnStart.Text = " 停 止";
|
// btnStart.ImageIndex = 1;
|
// btnStart.BackColor = Color.FromArgb(0x7f, Color.LimeGreen);
|
|
// btnLoad.Enabled = false;
|
// break;
|
// case EnumHelper.DeviceState.DSClose:
|
// btnStart.Text = " 启 动";
|
// btnStart.ImageIndex = 0;
|
// btnStart.BackColor = SystemColors.Control;
|
|
// btnLoad.Enabled = true;
|
// break;
|
// default:
|
// break;
|
// }
|
|
// this.Refresh();
|
// }
|
// catch (Exception ex)
|
// {
|
// }
|
// }
|
// }
|
// catch (Exception ex)
|
// {
|
// }
|
//}
|
|
private async Task ProcessOperation(bool isStart)
|
{
|
if (isStart)
|
{
|
await Task.Run(() => Process.Open());
|
|
//this.BeginInvoke(new Action(() =>
|
//{
|
btnStart.Text = " 停 止";
|
btnStart.ImageIndex = 1;
|
btnStart.BackColor = Color.FromArgb(0x7f, Color.LimeGreen);
|
|
btnLoad.Enabled = false;
|
//}));
|
|
}
|
else
|
{
|
await Task.Run(() => Process.Close());
|
|
//this.BeginInvoke(new Action(() =>
|
//{
|
btnStart.Text = " 启 动";
|
btnStart.ImageIndex = 0;
|
btnStart.BackColor = SystemColors.Control;
|
|
btnLoad.Enabled = true;
|
//}));
|
}
|
}
|
}
|
}
|