FrmMain.cs 14.3 KB
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AGVControl_Steel
{
    public partial class FrmMain : Form
    {
        private bool checkedChange;
        private BLL.Control control;

        public FrmMain()
        {
            InitializeComponent();
            control = new BLL.Control();
        }

        private void Control_AgvChanged(int agvIndex)
        {
            Invoke(new Action(() =>
            {
                DgvName.Rows[agvIndex].SetValues(Common.agvInfos[agvIndex].ToGridRow());
                tableLayoutPanel5.Controls["TxtAgvMission" + agvIndex].Text = Common.agvInfos[agvIndex].ToMissionState();
            }));
        }

        private void Control_AgvOnline(int agvIndex)
        {
            Invoke(new Action(() =>
            {
                DgvName.Rows[agvIndex].DefaultCellStyle.ForeColor = Common.agvInfos[agvIndex].IsOnline ? Color.Black : Color.Red;
                DgvName.Rows[agvIndex].SetValues(Common.agvInfos[agvIndex].ToGridRow());
                tableLayoutPanel5.Controls["TxtAgvMission" + agvIndex].Text = Common.agvInfos[agvIndex].ToMissionState();
            }));
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            string[] workshop = new string[Common.agvInfos.Count];
            for (int i = 0; i < Common.agvInfos.Count; i++)
                workshop[i] = Common.agvInfos[i].Workshop;
            Text += " (" + string.Join(" ", workshop) + ")";

            System.Reflection.Assembly assembly = System.Reflection.Assembly.GetEntryAssembly();
            LblVersion.Text = assembly.GetName().Version.ToString();

            checkedChange = true;
            ChkWorkTimeoutDel.Text = "回收钢板时删除" + Common.WorkTimeout + "min前的任务";
            ChkWorkTimeoutDel.Checked = Common.WorkTimeoutDel;
            ChkWorkAutoDel.Checked = Common.WorkAutoDel;
            checkedChange = false;

            Common.log.LogBox = TxtLog;
            Common.lstOldSteel = LstOldSteel;
            Common.lstNewSteel = LstNewSteel;
            Common.lstStorage = LstStorage;
            Common.lblStorageIO = LblStorageIO;
            BLL.SteelManage.OldSteelWorkLoad();
            BLL.SteelManage.NewSteelWorkLoad();
            BLL.SteelManage.StorageWorkLoad();

            BLL.RunMode.Init(this);
            BLL.WebService.Open(Common.appConfig.AppSettings.Settings["WebService"].Value);
            LblWeb.BackColor = BLL.WebService.IsOpen ? Color.Lime : Color.Red;
            LblWeb.Text = BLL.WebService.IsOpen ? "WebService Open" : "WebService Close";
            LstMission.Items.AddRange(Common.agvMissions.Keys.ToArray());

            List<string> missionHint = new List<string>();
            for (int i = 0; i < Common.agvLines.Count; i++)
                missionHint.AddRange(Common.agvLines[i].Lines);
            missionHint.Add(Common.STORAGE_ENTER_4C);
            missionHint.Add(Common.STORAGE_ENTER_4D);
            missionHint.Add(Common.STORAGE_LEAVE);
            LblMissionHint.Text = "提示:" + string.Join(",", missionHint);

            for (int i = 0; i < Common.agvInfos.Count; i++)
            {
                DgvName.Rows.Add(Common.agvInfos[i].ToGridRow());
                DgvName.Rows[i].DefaultCellStyle.ForeColor = Color.Red;
                DgvName.Rows[i].HeaderCell.Value = (i + 1).ToString();
                DgvName.Rows[i].Height = 30;
            }

            control.AgvChanged += Control_AgvChanged;
            control.AgvOnline += Control_AgvOnline;
            control.Start();
        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (BLL.RunMode.Closing(e))
            {
                control.Stop();
                BLL.WebService.Close();
            }
        }

        private void DgvName_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1) return;
            AgvInfo info = Common.agvInfos[e.RowIndex];

            if (e.ColumnIndex == DgvName.Columns.Count - 2)  //最后二列,自动/手动
            {
                info.IsAuto = !info.IsAuto;
                Common.log.Info("手动修改 " + info.Name + " 自动状态" + info.IsAuto);
                DgvName.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = info.IsAuto.ToString();
                Common.appConfig.AppSettings.Settings[info.Name].Value = info.IsAuto.ToString();
                Common.appConfig.Save();
                System.Configuration.ConfigurationManager.RefreshSection("appSettings");

                if (info.IsAuto && Common.WorkAutoDel)
                {
                    BLL.SteelManage.OldSteelWorkDelAll(info.Workshop);
                    BLL.SteelManage.NewSteelWorkDelAll(info.Workshop);
                    BLL.SteelManage.StorageWorkDelAll(info.Workshop);
                }
            }
            else if (e.ColumnIndex == DgvName.Columns.Count - 1)  //最后一列,清除任务
            {
                string text = "确定要清除 " + info.Name + " 的任务吗?";
                DialogResult dr = MessageBox.Show(text, "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                if (dr == DialogResult.Yes)
                {
                    Common.log.Info("手动清除 " + info.Name + " 任务");
                    info.CurrentJob = null;
                    Common.mir.Del_Mission(info.IP, info.Authorization);
                    info.IsAuto = false;
                    info.Place = "";
                    DgvName.Rows[e.RowIndex].Cells[e.ColumnIndex - 1].Value = info.IsAuto.ToString();
                    Common.appConfig.AppSettings.Settings[info.Name].Value = info.IsAuto.ToString();
                    Common.appConfig.Save();
                    System.Configuration.ConfigurationManager.RefreshSection("appSettings");
                }
            }
        }

        #region 手动添加小车任务
        private void BtnMissionAdd_Click(object sender, EventArgs e)
        {
            if (DgvName.SelectedCells.Count == 0) return;
            int idx = DgvName.SelectedCells[0].RowIndex;
            if (idx < 0) return;

            AgvInfo info = Common.agvInfos[idx];
            if (info.IsAuto)
            {
                MessageBox.Show(info.Name + " 自动状态,不允许手动发送任务");
                return;
            }

            if (LstMission.SelectedIndex == -1) return;
            if (Common.FLEET_SEND)
                Common.mir.Add_Mission_Fleet(info.FleetID, info.Authorization, LstMission.Text, out string id);
            else
                Common.mir.Add_Mission(info.IP, info.Authorization, LstMission.Text, out string id);
        }

        private void BtnMissionClear_Click(object sender, EventArgs e)
        {
            if (DgvName.SelectedCells.Count == 0) return;
            int idx = DgvName.SelectedCells[0].RowIndex;
            if (idx < 0) return;

            AgvInfo info = Common.agvInfos[DgvName.SelectedRows[0].Index];
            Common.mir.Del_Mission(info.IP, info.Authorization);
        }

        private void BtnMissionRun_Click(object sender, EventArgs e)
        {
            if (DgvName.SelectedCells.Count == 0) return;
            int idx = DgvName.SelectedCells[0].RowIndex;
            if (idx < 0) return;

            AgvInfo info = Common.agvInfos[idx];
            Common.mir.State_Ready(info.IP, info.Authorization);
        }

        private void BtnMissionPause_Click(object sender, EventArgs e)
        {
            if (DgvName.SelectedCells.Count == 0) return;
            int idx = DgvName.SelectedCells[0].RowIndex;
            if (idx < 0) return;

            AgvInfo info = Common.agvInfos[idx];
            Common.mir.State_Pause(info.IP, info.Authorization);
        }



        #endregion

        #region 手动修改钢网任务
        private void BtnOldSteelWorkAdd_Click(object sender, EventArgs e)
        {
            string text = "手动添加回收钢板任务";
            string msg = Microsoft.VisualBasic.Interaction.InputBox(text, "添加", "");
            if (string.IsNullOrWhiteSpace(msg)) return;
            Common.log.Info(text + " " + msg);
            BLL.SteelManage.OldSteelWorkAdd(msg);
        }

        private void BtnOldSteelWorkDel_Click(object sender, EventArgs e)
        {
            if (LstOldSteel.SelectedIndex == -1) return;
            string[] arr = LstOldSteel.Text.Split(',');
            string text = "手动删除回收钢板任务 " + arr[0];
            DialogResult dr = MessageBox.Show(text, "删除", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
            if (dr == DialogResult.Yes)
            {
                Common.log.Info(text);
                BLL.SteelManage.OldSteelWorkDel(arr[0]);
            }
        }

        private void BtnOldSteelWorkDelAll_Click(object sender, EventArgs e)
        {
            string text = "手动删除全部回收钢板任务";
            DialogResult dr = MessageBox.Show(text, "删除", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
            if (dr == DialogResult.Yes)
            {
                Common.log.Info(text);
                BLL.SteelManage.OldSteelWorkDelAll();
            }
        }

        private void BtnNewSteelWorkAdd_Click(object sender, EventArgs e)
        {
            string text = "手动添加送新钢板任务,from&place逗号分割";
            string msg = Microsoft.VisualBasic.Interaction.InputBox(text, "添加", "");
            if (string.IsNullOrWhiteSpace(msg)) return;
            string[] arr = msg.Split(',');
            Common.log.Info(text + " " + msg);
            BLL.SteelManage.NewSteelWorkAdd(arr[0], arr[1]);
        }

        private void BtnNewSteelWorkDel_Click(object sender, EventArgs e)
        {
            if (LstNewSteel.SelectedIndex == -1) return;
            string[] arr = LstNewSteel.Text.Split(',');
            string text = "手动删除【" + arr[0] + "," + arr[1] + "】送新钢板任务";
            DialogResult dr = MessageBox.Show(text, "删除", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
            if (dr == DialogResult.Yes)
            {
                Common.log.Info(text);
                BLL.SteelManage.NewSteelWorkDel(arr[0], arr[1]);
            }
        }

        private void BtnNewSteelWorkDelAll_Click(object sender, EventArgs e)
        {
            string text = "手动删除全部送新钢板任务";
            DialogResult dr = MessageBox.Show(text, "删除", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
            if (dr == DialogResult.Yes)
            {
                Common.log.Info(text);
                BLL.SteelManage.NewSteelWorkDelAll();
            }
        }

        private void BtnStorageWorkAdd_Click(object sender, EventArgs e)
        {
            string text = "手动添加仓库钢板任务";
            string msg = Microsoft.VisualBasic.Interaction.InputBox(text, "添加", "");
            if (string.IsNullOrWhiteSpace(msg)) return;
            Common.log.Info(text + " " + msg);
            BLL.SteelManage.StorageWorkAdd(msg);
        }

        private void BtnStorageWorkDel_Click(object sender, EventArgs e)
        {
            if (LstStorage.SelectedIndex == -1) return;
            string[] arr = LstStorage.Text.Split(',');
            string text = "手动删除【" + arr[0] + "】仓库钢板任务";
            DialogResult dr = MessageBox.Show(text, "删除", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
            if (dr == DialogResult.Yes)
            {
                Common.log.Info(text);
                BLL.SteelManage.StorageWorkDel(arr[0]);
            }
        }

        private void BtnStorageWorkDelAll_Click(object sender, EventArgs e)
        {
            string text = "手动删除全部仓库钢板任务";
            DialogResult dr = MessageBox.Show(text, "删除", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
            if (dr == DialogResult.Yes)
            {
                Common.log.Info(text);
                BLL.SteelManage.StorageWorkDelAll();
            }
        }

        private void ChkMissionDebug_CheckedChanged(object sender, EventArgs e)
        {
            BtnOldSteelWorkAdd.Enabled = ChkMissionDebug.Checked;
            BtnOldSteelWorkDel.Enabled = ChkMissionDebug.Checked;
            BtnOldSteelWorkDelAll.Enabled = ChkMissionDebug.Checked;
            BtnNewSteelWorkAdd.Enabled = ChkMissionDebug.Checked;
            BtnNewSteelWorkDel.Enabled = ChkMissionDebug.Checked;
            BtnNewSteelWorkDelAll.Enabled = ChkMissionDebug.Checked;
            BtnStorageWorkAdd.Enabled = ChkMissionDebug.Checked;
            BtnStorageWorkDel.Enabled = ChkMissionDebug.Checked;
            BtnStorageWorkDelAll.Enabled = ChkMissionDebug.Checked;
            ChkStorageIO.Enabled = ChkMissionDebug.Checked;
        }

        private void ChkStorageIO_CheckedChanged(object sender, EventArgs e)
        {
            Common.StorageDockAlway = ChkStorageIO.Checked;
        }


        #endregion

        private void BtnClearLog_Click(object sender, EventArgs e)
        {
            TxtLog.Text = "";
        }

        private void ChkWorkAutoDel_CheckedChanged(object sender, EventArgs e)
        {
            if (checkedChange) return;
            Common.WorkAutoDel = ChkWorkAutoDel.Checked;
            Common.appConfig.AppSettings.Settings["WorkAutoDel"].Value = Common.WorkAutoDel.ToString();
            Common.appConfig.Save();
            System.Configuration.ConfigurationManager.RefreshSection("appSettings");
            Common.log.Info("修改WorkAutoDel=" + Common.WorkAutoDel.ToString());
        }

        private void ChkWorkTimeoutDel_CheckedChanged(object sender, EventArgs e)
        {
            if (checkedChange) return;
            Common.WorkTimeoutDel = ChkWorkTimeoutDel.Checked;
            Common.appConfig.AppSettings.Settings["WorkTimeoutDel"].Value = Common.WorkTimeoutDel.ToString();
            Common.appConfig.Save();
            System.Configuration.ConfigurationManager.RefreshSection("appSettings");
            Common.log.Info("修改WorkTimeoutDel=" + Common.WorkTimeoutDel.ToString());
        }

        private void tabControl1_Click(object sender, EventArgs e)
        {
            ConfigHelper.AdvanceConfigForm.ShowEditDialog(this);
        }
    }
}