uc_boxdebug.cs 14.4 KB
using DeviceLibrary;
using log4net;
using OnlineStore.Common;
using OnlineStore.DeviceLibrary;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TheMachine
{
    public partial class uc_boxdebug : UserControl
    {
        private Robot_Config _Config;
        public Robot_Config Config
        {
            get { return _Config; }
            set
            {
                if (value == null)
                    return;
                _Config = value;
                storePosControl1.Config = _Config;
                Init();
            }
        }
        Dictionary<string,PosState> posState = new Dictionary<string, PosState>();
        //如果总配置文件存在,保存到总的配置文件
        static string appPath = Application.StartupPath;
        static string positionConfigFile = Path.Combine(appPath, "StoreConfig\\linePositions.csv");
        class PosState {
            public int InStoreCheck = 0;
            public int OutStoreCheck = 0;

        }
        public uc_boxdebug()
        {
            InitializeComponent();
        }

        public void Init()
        {
            FillBoxPos();
            RobotManage.mainMachine.InOutEndProcessEvent += Store_InOutEndProcessEvent;
            timer1.Enabled = true;
            return;
        }
        private void Store_InOutEndProcessEvent(string arg1, StoreMoveType arg2, bool arg3)
        {
            //LogUtil.info($"出入库完成状态:{arg2},库位:{arg1}");
            int InStoreCheck = 0;
            int OutStoreCheck = 0;
            if (arg2 == StoreMoveType.InStore)
                InStoreCheck = 1;
            else if (arg2 == StoreMoveType.OutStore)
                OutStoreCheck = 1;
            var pps = SavePosCheck(arg1, InStoreCheck, OutStoreCheck);

            if (htpp.ContainsKey(arg1))
            {
                var pp = (PosPos)htpp[arg1];
                var cells = dataGridView1.Rows[pp.row].Cells[pp.col];
                cells.Style = GetStyle(arg1);
            }
        }
        DataGridViewCellStyle GetStyle(string posid) {
            if (htpp.ContainsKey(posid) && posState.ContainsKey(posid))
            {
                var pp = (PosPos)htpp[posid];
                var pps = (PosState)posState[posid];
                var cells = dataGridView1.Rows[pp.row].Cells[pp.col];
                cells.Selected = true;
                if (pps.InStoreCheck == 1 && pps.OutStoreCheck == 1)
                    return dgv_ok;
                else if (pps.InStoreCheck == 1)
                    return dgv_oin;
                else if (pps.OutStoreCheck == 1)
                    return dgv_oout;
            }
            return dgv_none;
        }
        Hashtable htpp = new Hashtable();
        class PosPos {
            public string Posid;
            public int index;
            public int row;
            public int col;
        }
        int PosCompare(string a, string b) {
            string[] pa = a.Split('_');
            string[] pb = b.Split('_');

            if (int.TryParse(pa[1], out _))
                pa[1] += "A";
            if (int.TryParse(pb[1], out _))
                pb[1] += "A";

            var pa1 = pa[1][0] + (pa[1][1].Equals('A') ? "1" : "2");
            var pb1 = pb[1][0] + (pb[1][1].Equals('A') ? "1" : "2");

            var pa2 = pa[2].Length==2? (pa[2][1].Equals('A') ? "1" : "2")+ pa[2][0] : pa[2];
            var pb2 = pb[2].Length == 2 ? (pb[2][1].Equals('A') ? "1" : "2")+ pb[2][0] : pb[2];

            var isa = Regex.Replace(pa[1], @"[^\d]*", "") + pa1 + pa2 + pa[3].PadLeft(2, '0');
            var isb = Regex.Replace(pb[1], @"[^\d]*", "") + pb1 + pb2 + pb[3].PadLeft(2, '0');

            if (int.Parse(isa) == int.Parse(isb))
                return 0;
            else if (int.Parse(isa) > int.Parse(isb))
                return 1;
            else
                return -1;

        }
        void LoadPosCheck() {
            var poscheckfile = positionConfigFile + ".check";
            if (File.Exists(poscheckfile))
            {
                try
                {
                    posState = JsonHelper.DeserializeJsonToObject<Dictionary<string, PosState>>(File.ReadAllText(poscheckfile));
                    File.Copy(poscheckfile, poscheckfile + ".backup", true);
                }
                catch {
                    if (File.Exists(poscheckfile + ".backup"))
                    {
                        File.Copy(poscheckfile + ".backup", poscheckfile, true);
                        File.Delete(poscheckfile + ".backup");
                        LoadPosCheck();
                    }
                }
            }
            else
            {
                File.WriteAllText(poscheckfile, JsonHelper.SerializeObject(posState));
            }
        }
        PosState SavePosCheck(string posid,int inStoreCheck,int outStoreCheck) {

            if (!posState.ContainsKey(posid))
                posState.Add(posid, new PosState());
            if (inStoreCheck>0)
                posState[posid].InStoreCheck = inStoreCheck;
            if (outStoreCheck > 0)
                posState[posid].OutStoreCheck = outStoreCheck;
            var poscheckfile = positionConfigFile + ".check";
            File.WriteAllText(poscheckfile, JsonHelper.SerializeObject(posState));
            return posState[posid];
        }
        static Font font = new Font("宋体",10);
        DataGridViewCellStyle dgv_ok = new DataGridViewCellStyle() { BackColor = Color.LightGreen, Font = font };
        DataGridViewCellStyle dgv_oin = new DataGridViewCellStyle() { BackColor = Color.LightGray, Font = font };
        DataGridViewCellStyle dgv_oout = new DataGridViewCellStyle() { BackColor = Color.LightSeaGreen, Font = font };
        DataGridViewCellStyle dgv_none = new DataGridViewCellStyle() { BackColor = Color.White,Font= font };
        void FillBoxPos() {
            htpp.Clear();
            dataGridView1.Columns.Clear();
            dataGridView1.Rows.Clear();
            LoadPosCheck();
            Comparison<string> comparison = new Comparison<string>(PosCompare);
            RobotManage.PositionNumList.Sort(comparison);
            //store.PositionNumList.Reverse();

            string[] posstr = RobotManage.PositionNumList[RobotManage.PositionNumList.Count-1].Split('_');
            int colcount = int.Parse(Regex.Replace(posstr[1], @"[^\d]*", "")) + 1;
            for (int i = 0; i < colcount; i++)
            {
                dataGridView1.Columns.Add("Col" + i + "_A", "Col" + i + "_A");
                dataGridView1.Columns.Add("Col" + i + "_B", "Col" + i + "_B");
                
            }
            for (int i = 0; i < dataGridView1.Columns.Count; i++)
            {
                dataGridView1.Columns[i].Width = 64;
                dataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
            }
            dataGridView1.Rows.Add();
            int currentRowIndex = 0;
            int lastcol = -1;
            string lastAB = "";
            for (int i = 0; i < RobotManage.PositionNumList.Count; i++)
            {

                int ci = RobotManage.PositionNumList.Count-1 - i;
                string posname = RobotManage.PositionNumList[ci];
                string[] _posstr = posname.Split('_');
                int col = int.Parse(Regex.Replace(_posstr[1], @"[^\d]*", ""));
                string ab = "A";
                if (_posstr[2].Length>1)
                    ab= _posstr[2][1].ToString();

                if (col != lastcol || lastAB!= ab)
                    currentRowIndex = 0;

                lastcol = col;
                lastAB = ab;
                if (currentRowIndex >= dataGridView1.Rows.Count-1)
                {
                    int x = dataGridView1.Rows.Add();
                }
                
                var currentRow = dataGridView1.Rows[currentRowIndex];

                int currentColIndex = col * 2;
                if (ab.ToLower() == "b")
                    currentColIndex++;
                currentRow.Cells[currentColIndex].Value = $"{_posstr[1]}_{_posstr[2]}_{_posstr[3]}";
                currentRow.Cells[currentColIndex].Tag = posname;
                htpp.Add(posname, new PosPos() { Posid = posname, index = i, row = currentRowIndex, col = currentColIndex });
                currentRow.Cells[currentColIndex].Style = GetStyle(posname);
                
                currentRowIndex++;
            }
            for (int i = 0; i < dataGridView1.Columns.Count; i++)
            {
                if (dataGridView1.Rows[0].Cells[i].Value==null) {
                    dataGridView1.Columns[i].Width = 0;
                }
            }
            if (dataGridView1.Rows[0].Cells[dataGridView1.Columns.Count - 1].Value != null)
            {
                dataGridView1.Rows[0].Cells[dataGridView1.Columns.Count - 1].Selected = true;
            }
            else if (dataGridView1.Rows[0].Cells[dataGridView1.Columns.Count - 2].Value != null)
            {
                dataGridView1.Rows[0].Cells[dataGridView1.Columns.Count - 2].Selected = true;
            }
            dataGridView1.Rows.RemoveAt(dataGridView1.Rows.Count - 1);
            if (dataGridView1.SelectedCells.Count>0)
                setData(dataGridView1.SelectedCells[0].Tag.ToString());
            else
                setData(RobotManage.PositionNumList.Last());
        }
        void setData(string posid) {
            ACStorePosition ktkPosition = CSVPositionReader<ACStorePosition>.GetPositon(posid);
            cmbPosition.Text = posid;
            label_size.Text = $"[{ktkPosition.BagWidth}x{ktkPosition.BagHigh}]";

            storePosControl1.LoadPos(ktkPosition);
        }

        private void btnInStore_Click(object sender, EventArgs e)
        {            

            if (RobotManage.mainMachine.runStatus == RunStatus.Running)
            {
                string selectPositionNum = cmbPosition.Text;
                JobInfo jobInfo = new JobInfo("IN_TEST", selectPositionNum);
                if (!RobotManage.mainMachine.StartInStore(jobInfo)) {
                    MessageBox.Show("周转箱没有到位,无法手动入库");
                    return;
                }
                LogUtil.info($"手动入库:{selectPositionNum}");
            }
            else
            {
                MessageBox.Show("请先启动料仓!");
            }
        }

        private void btnOutStore_Click(object sender, EventArgs e)
        {
            if (RobotManage.mainMachine.runStatus == RunStatus.Running)
            {
                string selectPositionNum = cmbPosition.Text;
                if (RobotManage.mainMachine.boxTransportIsFree) {
                    LogUtil.info($"手动出库:{selectPositionNum}");
                    RobotManage.mainMachine.AddOutStoreTask(selectPositionNum);
                }
                else
                {
                    MessageBox.Show("料仓正在出入库中,无法手动出库");
                }                
            }
            else
            {
                MessageBox.Show("请先启动料仓!");
            }
        }

        bool preOpen = false;
        public void StoreOpenStatus(bool isOpen)
        {
            if (preOpen.Equals(isOpen))
            {
                return;
            }
            preOpen = isOpen;

            btnOutStore.Enabled = isOpen;
            btnInStore.Enabled = isOpen;

        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (!Visible)
                return;

            if (!cb_inoutdebugmode.Checked)
                return;

            if (RobotManage.mainMachine.AutoInOutTest)
            {
                btnOutStore.Enabled = false;
                btnInStore.Enabled = false;
                btn_autoinout.Text = "停止自动库位测试";
            }
            else
            {
                btnOutStore.Enabled = true;
                btnInStore.Enabled = true;
                btn_autoinout.Text = "自动库位测试";
            }
        }
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex<0 || dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Tag == null)
                return;
            var posid = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Tag.ToString();
            setData(posid);
        }
        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex < 0 || dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Tag == null)
                return;
            var posid = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Tag.ToString();
            setData(posid);
        }

        private void cb_inoutdebugmode_CheckedChanged(object sender, EventArgs e)
        {
            RobotManage.InoutDebugMode = cb_inoutdebugmode.Checked;
            btnInStore.Enabled= cb_inoutdebugmode.Checked;
            btnOutStore.Enabled= cb_inoutdebugmode.Checked;
            btn_autoinout.Enabled=cb_inoutdebugmode.Checked;
        }

        private void btn_autoinout_Click(object sender, EventArgs e)
        {
            if (RobotManage.mainMachine.AutoInOutTest)
            {
                RobotManage.mainMachine.StopAutoInOutTest();
                MessageBox.Show("自动出入库过程会再周转箱送至出口后自动停止");
                return;
            }

            if (RobotManage.mainMachine.runStatus == RunStatus.Running)
            {
                DialogResult res = MessageBox.Show("确定开始自动库位测试?\n请确保料仓库位全部为空,请先在入口放入一个周转箱.", "提示", MessageBoxButtons.YesNo);
                if (res == DialogResult.No)
                    return;
                int posindex = RobotManage.PositionNumList.IndexOf(cmbPosition.Text);
                if (!RobotManage.mainMachine.StartAutoInOutTest(posindex,out string errmsg))
                {
                    MessageBox.Show(errmsg);
                    return;
                }
                (sender as Button).Text = "停止自动库位测试";
            }
            else
            {
                MessageBox.Show("请先启动料仓!");
            }
        }
    }
}