uc_boxdebug.cs 17.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
using DeviceLibrary;
using log4net;
using OnlineStore;
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();
            timer1.Enabled = true;
            crc.LanguageChangeEvent += Crc_LanguageChangeEvent;
            return;
        }

        private void Crc_LanguageChangeEvent(object sender, EventArgs e)
        {
            FillBoxPos();
        }

        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);
            }
            checkVerify();
        }
        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;
        }
        void checkVerify()
        {
            int finish = 0;
            for (int i = 0; i < RobotManage.PositionNumList.Count; i++)
            {
                var posid = RobotManage.PositionNumList[i];
                if (htpp.ContainsKey(posid) && posState.ContainsKey(posid))
                {
                    var pps = (PosState)posState[posid];
                    if (pps.InStoreCheck == 1 && pps.OutStoreCheck == 1)
                    {
                        finish++;
                    }
                }
            }
            double s = (double)finish * 100 / RobotManage.PositionNumList.Count;
            label_verify.Text = s.ToString("0.0") + "%";
        }
        Hashtable htpp = new Hashtable();
        class PosPos {
            public string Posid;
            public int index;
            public int row;
            public int col;
        }
        int PosCompare(string a, string b) {
            if (a.StartsWith("fix"))
                return -1;
            if (b.StartsWith("fix"))
                return 1;
            

            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];
            var pb2 = pb[2];
            if (pa[2].Length == 2)
            {
                if (pa[2][1].Equals('A') || pa[2][1].Equals('B'))
                {
                    pa2 = (pa[2][1].Equals('A') ? "1" : "2") + pa[2][0];
                }
                else if (pa[2][1].Equals('1') || pa[2][1].Equals('2'))
                {
                    pa2 = (pa[2][1].Equals('1') ? "1" : "2") + pa[2][0];
                }
            }

            if (pb[2].Length == 2)
            {
                if ((pb[2][1].Equals('A') || pb[2][1].Equals('B')))
                {
                    pb2 = (pb[2][1].Equals('A') ? "1" : "2") + pb[2][0];
                }
                else if ((pb[2][1].Equals('1') || pb[2][1].Equals('2')))
                {
                    pb2 = (pb[2][1].Equals('1') ? "1" : "2") + pb[2][0];
                }
            }

            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();

            int colcount = 9;

            for (int i = 0; i < colcount; i++)
            {
                dataGridView1.Columns.Add("Col" + i, "Col" + i);
            }
            for (int i = 0; i < dataGridView1.Columns.Count; i++)
            {
                dataGridView1.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                dataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
            }
            dataGridView1.Rows.Add();
            int currentRowIndex = 0;
            int currentColIndex = -1;
            string lastAB = "";
            for (int i = 0; i < RobotManage.PositionNumList.Count; i++)
            {
                string posname = RobotManage.PositionNumList[i];
                string memo = "";
                if (RobotManage.allPositionMap.ContainsKey(posname))
                {
                    memo = RobotManage.allPositionMap[posname].memo;
                }
                
                string ab = memo.Substring(0, 2);

                if (posname.StartsWith("LabelP") || posname.StartsWith("RFIDP"))
                {
                    ab = "aa";
                }

                var showname = posname;
                if (ab != lastAB)
                {
                    currentRowIndex = 0;
                    currentColIndex++;
                    lastAB = ab;
                    //if (posname.StartsWith("L"))
                    //{
                    //    dataGridView1.Columns[currentColIndex].HeaderText = "(" + posname.Substring(0, 2) + "";
                    //}
                    //else
                    {
                        dataGridView1.Columns[currentColIndex].HeaderText = crc.GetString("Res0031","取放料点");

                    }
                }
                //if (!posname.StartsWith("L"))
                {

                    if (memo.IndexOf("_") >= 0)
                    {
                        var id = memo.IndexOf("_");
                        var m = memo.Substring(0,id);
                        var m2 = memo.Substring(id, memo.Length-id);
                        showname = posname + "(" + crc.GetString(m, m)+ m2 + ")";
                    }
                    else
                        showname = posname + "(" + crc.GetString(memo, memo) + ")";
                }

                var currentRow = dataGridView1.Rows[currentRowIndex];


                currentRow.Cells[currentColIndex].Value = showname;
                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++;
                if (currentRowIndex >= dataGridView1.Rows.Count)
                    dataGridView1.Rows.Add();
            }
            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.allPositionMap.Keys.ToList().Last());
            checkVerify();

        }
        void setData(string posid) {
            ACStorePosition ktkPosition=null;
            cmbPosition.Text = posid;
            if (RobotManage.allPositionMap.ContainsKey(posid))
            {
                ktkPosition = RobotManage.allPositionMap[posid];
                storePosControl1.LoadPos(ktkPosition);
            }
            
        }

        private void btnInStore_Click(object sender, EventArgs e)
        {            

            if (RobotManage.mainMachine.runStatus == RunStatus.Running)
            {
                string selectPositionNum = cmbPosition.Text;
                ACStorePosition ktkPosition = CSVPositionReader<ACStorePosition>.GetPositon(selectPositionNum);

                LogUtil.info($"手动入库:{selectPositionNum}");
            }
            else
            {
                MessageBox.Show(crc.GetString("Res0032","请先启动料仓!"));
            }
        }

        private void btnOutStore_Click(object sender, EventArgs e)
        {
            if (RobotManage.mainMachine.runStatus == RunStatus.Running)
            {
                string selectPositionNum = cmbPosition.Text;
                ACStorePosition position = CSVPositionReader<ACStorePosition>.GetPositon(selectPositionNum);
               
            }
            else
            {
                MessageBox.Show(crc.GetString("Res0032","请先启动料仓!"));
            }
        }

        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;

        }
        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;
            if (!cb_inoutdebugmode.Checked)
            {
                RobotManage.mainMachine.AutoInOutTest = false;
                //RobotManage.mainMachine.StopAutoInOut = false;
            }
            btnInStore.Enabled= cb_inoutdebugmode.Checked;
            btnOutStore.Enabled= cb_inoutdebugmode.Checked;
            btn_autoinout.Enabled= cb_inoutdebugmode.Checked;
        }

        private void cb_disableupdownprotect_CheckedChanged(object sender, EventArgs e)
        {
            //RobotManage.DisableUpdownProtect = cb_disableupdownprotect.Checked;
            //LogUtil.info("手动点击屏蔽升降轴旋转轴安全检测:"+ cb_disableupdownprotect.Checked.ToString());
        }

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

            if (RobotManage.mainMachine.runStatus == RunStatus.Running)
            {

                if (!RobotManage.mainMachine.IsInStoreReady)
                {
                    MessageBox.Show(crc.GetString("Res0034","入库料盘没有准备好,请先点击[上料准备],无法启动"));
                    return;
                }

                DialogResult res = MessageBox.Show(crc.GetString("Res0035","确定开始自动库位测试?\n请确保料仓库位全部为空."), crc.GetString("Res0036","提示"), MessageBoxButtons.YesNo);
                if (res == DialogResult.No)
                    return;
               int posindex = RobotManage.StorePositionNumList.IndexOf(cmbPosition.Text);
                if (posindex < 0)
                {
                    MessageBox.Show(crc.GetString("Res0037","非库位无法使用自动测试"), crc.GetString("Res0036","提示"));
                    return;
                }
                LogUtil.info($"库位:{cmbPosition.Text},index:{posindex}");
                if (!RobotManage.mainMachine.StartAutoInOutTest(posindex, out string errmsg))
                {
                    MessageBox.Show(errmsg);
                    return;
                }
                (sender as Button).Text = crc.GetString("Res0038","停止自动库位测试!");
            }
            else
            {
                MessageBox.Show(crc.GetString("Res0032","请先启动料仓!"));
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            RobotManage.mainMachine.Z_Axis.AbsMove(null, Config.Z_Axis_P1, Config.Z_Axis_P1_speed);
            LogUtil.info("手动点击Z轴回P1");
        }
    }
}