FrmRobot.cs 6.3 KB
using Common;
using DeviceLib.BLL;
using DeviceLib.Model.AGV;
using System;
using System.Text;
using UIControl.Forms;

namespace AGVDispatch
{
    public partial class FrmRobot : FrmBase
    {
        public FrmRobot()
        {
            InitializeComponent();
            loadRobot();
            loadMission();
            timer1.Start();
        }
        void loadRobot()
        {
            listBox1.DataSource = RobotManager.GetAllRobots();
            listBox1.DisplayMember = "name";        
        }
        void loadMission(int group_id=1)
        {
            listBox2.DataSource = MissionManager.GetMissions(group_id);
            listBox2.DisplayMember = "alias";
        }
        Robot robot;
        Mission mission;
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex == -1) return;
            //robot = RobotManager.GetRobot((int)listBox1.SelectedValue);
            robot = (Robot)listBox1.SelectedValue;
        }

        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox2.SelectedIndex == -1) return;
            //mission = MissionManager.GetMission(listBox2.SelectedValue.ToString());
            mission = (Mission)listBox2.SelectedValue;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                lblSelectedInfo.Text = $"【{robot?.name ?? ""}】【{robot?.ip ?? ""}】" +
                        $"【{robot?.mission_state?.mission?.name ?? ""}】【{robot?.mission_state?.mission?.alias ?? ""}】" +
                        $"【{robot?.mission_state?.run_id ?? -1}】";
                if (robot != null && robot.io_modules != null)
                {
                    StringBuilder sb = new StringBuilder();
                    foreach (var item in robot.io_modules)
                    {
                        sb.AppendLine(JsonHelper.SerializeObject(item));
                    }
                    lblIO.Text = $"{sb.ToString()}";
                }
            }
            catch { }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (robot.auto) return;
                robot?.operation?.Add_Mission(mission);
                LogUtil.Info($"{robot.name} 手动发送任务:{mission}");
            }
            catch(Exception ex)
            {
                LogUtil.Error($"手动发送任务出错:{mission}",ex);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                robot?.operation?.Set_Ready();
                LogUtil.Info($"手动点击运行");
            }
            catch (Exception ex)
            {
                LogUtil.Error($"手动点击运行出错", ex);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                robot?.operation?.Set_Pause();
                LogUtil.Info($"手动点击暂停");
            }
            catch (Exception ex)
            {
                LogUtil.Error($"手动点击暂停出错", ex);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                robot?.operation?.Clear_Error();
                LogUtil.Info($"手动点击清除错误");
            }
            catch (Exception ex)
            {
                LogUtil.Error($"手动点击清除错误出错", ex);
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            try
            {
                int.TryParse(txtRegister.Text, out int reg);
                if (robot?.operation?.Get_Register(reg, out double regVal)??false)
                {
                    txtRegisterVal.Text = regVal.ToString();
                    LogUtil.Info($"手动获取寄存器{reg}的值为{regVal}");
                }
              
            }
            catch (Exception ex)
            {
                LogUtil.Error($"手动获取寄存器{txtRegister.Text}的值出错", ex);
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            try
            {
                int.TryParse(txtRegister.Text, out int reg);
                double.TryParse(txtRegisterVal.Text, out double regVal);
                if (robot?.operation?.Set_Register(reg, regVal) ?? false)
                {
                    txtRegisterVal.Text = regVal.ToString();
                    LogUtil.Info($"手动设置寄存器{reg}的值为{regVal}");
                }

            }
            catch (Exception ex)
            {
                LogUtil.Error($"手动设置寄存器{txtRegister.Text}的值为{txtRegisterVal.Text}出错", ex);
            }
        }

        private void btnSetIO_Click(object sender, EventArgs e)
        {
            try
            {
                int.TryParse(txtBoxIOAddr.Text, out int addr);
                int.TryParse(txtIOIdx.Text, out int idx);
                if (robot?.operation?.Set_IO_Output(robot?.io_modules?[addr].ip??"", 
                    new Arg_IO_Status() {  on=true, port=idx}) ?? false)
                {
                    LogUtil.Info($"手动点亮 addr={addr},idx={idx}");
                }

            }
            catch (Exception ex)
            {
                LogUtil.Error($"手动点亮 addr={txtBoxIOAddr.Text},idx={txtIOIdx.Text}出错", ex);
            }
        }

        private void btnSetOff_Click(object sender, EventArgs e)
        {
            try
            {
                int.TryParse(txtBoxIOAddr.Text, out int addr);
                int.TryParse(txtIOIdx.Text, out int idx);
                if (robot?.operation?.Set_IO_Output(robot?.io_modules?[addr].ip ?? "",
                    new Arg_IO_Status() { on = false, port = idx }) ?? false)
                {
                    LogUtil.Info($"手动关闭 addr={addr},idx={idx}");
                }

            }
            catch (Exception ex)
            {
                LogUtil.Error($"手动关闭 addr={txtBoxIOAddr.Text},idx={txtIOIdx.Text}出错", ex);
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            MissionManager.Load();
        }
    }
}