Manual.cs 7.8 KB
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DeviceLibrary;
using Common;

namespace AGVControl
{
    public partial class Manual : Form
    {
        public Manual(Agv_Info agv)
        {
            InitializeComponent();
            Agv = agv;
            chkBxAuto.Checked = agv.IsUse;
            btnReSendTask.Enabled = agv.IsUse;
            btnAssignTask.Enabled = !agv.IsUse;
        }
        Agv_Info Agv;
        bool isInit = false;
        private void Manual_Load(object sender, EventArgs e)
        {
            lblInfo.Text = string.Format("[{0}]", Agv.IP);
            cmbBxMission.Items.AddRange(DeviceLibrary.Context.showNameMissionName.Values.ToArray());
            cmbBxMission.SelectedIndex = 0;
            InState.AddRange(new PictureBox[] { pictureBox1,pictureBox2,pictureBox3,pictureBox4});
            OutState.AddRange(new PictureBox[] { pictureBox5, pictureBox6, pictureBox7, pictureBox8 });
            timer1.Enabled = true;
            isInit = true;
        }

        private void chkBxAuto_CheckedChanged(object sender, EventArgs e)
        {
            if (!isInit) return;
            this.Invoke(new Action(() =>
            {

                btnReSendTask.Enabled = chkBxAuto.Checked;
                btnAssignTask.Enabled = !chkBxAuto.Checked;

                if (!chkBxAuto.Checked)//自动-》手动
                {
                    if (Agv.IsCon)
                    {
                        MiR_API.Del_Mission(Agv);
                        //添加Init任务
                        LogUtil.info(string.Format("{0} 运行模式改变:自动-》手动", Agv.Name));
                        Agv.AssignTask(SettingString.Init);
                    }

                    DeviceLibrary.Context.Standby.DelOccupyInfo(Agv.Name);
                    DeviceLibrary.Context.Charge.DelOccupyInfo(Agv.Name);

                }
                else
                {
                    //if (Agv.IsExistShelf)
                    //{
                    //    LogUtil.error(string.Format("{0} 车上有负载,无法开启自动模式,请先清空小车负载!", Agv.Name));
                    //    return;
                    //}
                   // if (MessageBox.Show(this, "是否继续上一次的任务?\r\n" + Agv.Msg, "提示", MessageBoxButtons.YesNo).Equals(DialogResult.No))
                    {
                        LogUtil.info(string.Format("{0} 运行模式改变:手动-》自动【不继续上一次任务:{1}】", Agv.Name, Agv.Msg));
                        Agv.CurJob = null;
                        if (Agv.Place.Equals(SettingString.Standby) || Agv.Place.Equals(SettingString.AutoCharge))
                            Agv.Place = "";
                        if (Agv.IsCon)
                        {
                            MiR_API.Del_Mission(Agv);
                        }
                        Agv.RFID = "";
                    }
                    //else
                    //{
                    //    if (Agv.IsCon)
                    //    {
                    //        Agv.Resend();
                    //        LogUtil.info(string.Format("{0} 运行模式改变:手动-》自动【继续上一次任务:{1}】", Agv.Name, Agv.Msg));
                    //    }

                    //}

                }
                Agv.IsUse = chkBxAuto.Checked;

            }));
        }

        private void btnReSendTask_Click(object sender, EventArgs e)
        {
            if (Agv.IsCon)
            {
                if (MessageBox.Show("确定对小车重发任务?", Agv.Name, MessageBoxButtons.YesNo) == DialogResult.No)
                    return;
                Task.Factory.StartNew(new Action(()=> {
                    MiR_API.Clear_Error(Agv);
                    System.Threading.Thread.Sleep(1000);
                    MiR_API.State_Ready(Agv);
                    Agv.Resend();
                    LogUtil.info(string.Format("{0} 手动重发任务", Agv.Name));
                }));

            }
        }

        private void btnReady_Click(object sender, EventArgs e)
        {
            if (Agv.IsCon)
            {
                MiR_API.State_Ready(Agv);
                LogUtil.info(string.Format("{0} 手动点击运行", Agv.Name));
            }

        }

        private void btnPause_Click(object sender, EventArgs e)
        {
            if (Agv.IsCon)
            {
                MiR_API.State_Pause(Agv);
                LogUtil.info(string.Format("{0} 手动点击暂停", Agv.Name));
            }
        }

        private void btnClearErr_Click(object sender, EventArgs e)
        {
            if (Agv.IsCon)
            {
                MiR_API.Clear_Error(Agv);
                LogUtil.info(string.Format("{0} 手动点击清除按钮", Agv.Name));
            }

        }

        private void btnAssignTask_Click(object sender, EventArgs e)
        {
            if (Agv.IsCon)
            {
                try
                {
                    var key = Context.showNameMissionName.Where(qq => qq.Value == cmbBxMission.SelectedItem.ToString()).Select(qq => qq.Key);
                    string taskName = key.ToList()[0];
                    if(!DeviceLibrary.Context.agvMission.Keys.Contains(taskName))
                    {
                        LogUtil.error("无选定任务: "+ taskName);
                        return;
                    }
                    if (taskName.Equals(SettingString.Enter) || taskName.Equals(SettingString.Leave))
                        if (MessageBox.Show("确定对小车发送进/出任务?", Agv.Name, MessageBoxButtons.YesNo) == DialogResult.No)
                            return;

                    Agv.AssignTask(taskName);
                    Agv.StateKanban.ShowInfo("目的地", "");
                    LogUtil.info(string.Format("{0} 手动发任务 {1}", Agv.Name, taskName));
                }
                catch(Exception ex)
                {
                    LogUtil.error("手动发任务 ",ex);
                }
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            Close();
            Dispose();
        }
        List<PictureBox> InState = new List<PictureBox>();
        List<PictureBox> OutState = new List<PictureBox>();
        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Invoke(new Action(() => {
                try
                {
                    if (Agv.IOInState != null && Agv.IOInState.Length == 4)
                    {
                        for (int i = 0; i < Agv.IOInState.Length; i++)
                        {
                            if (Agv.IOInState[i])
                            {
                                InState[i].Image = AGVDispatch.Properties.Resources.green1;
                            }
                            else
                            {
                                InState[i].Image = AGVDispatch.Properties.Resources.gray2;
                            }
                        }
                    }
                    if (Agv.IOOutState != null && Agv.IOOutState.Length == 4)
                    {
                        for (int i = 0; i < Agv.IOOutState.Length; i++)
                        {
                            if (Agv.IOOutState[i])
                            {
                                OutState[i].Image = AGVDispatch.Properties.Resources.green1;
                            }
                            else
                            {
                                OutState[i].Image = AGVDispatch.Properties.Resources.gray2;
                            }
                        }
                    }
                }
                catch { }
            }));
        }
    }
}