FrmStoreBox.cs 20.6 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 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.CompilerServices;
using System.Threading;
using System.IO;
using System.Runtime.InteropServices;
using OnlineStore.Common;
using System.Reflection;
using log4net;
using OnlineStore.DeviceLibrary;
using System.IO.Ports;
using OnlineStore.LoadCSVLibrary;
using CodeLibrary;
using DeviceLib;

namespace OnlineStore.TinPasteStore
{
    public partial class FrmStoreBox : FrmBase
    {

        private VerticalStoreBean store;
        public static readonly ILog LOGGER = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        public FrmStoreBox()
        {
            Control.CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
            chbAuto.Checked = ConfigAppSettings.GetIntValue(Setting_Init.App_AutoRun).Equals(1);
        }

        #region "初始化界面数据"


        private void InitStoreValue()
        {
            this.store = StoreManager.InitStore();
            if (store == null)
            {
                LogUtil.error(LOGGER, "找不到对应的料仓");
                return;
            }
            VerticalPosition ktkPosition = null;
            if (store.PositionNumList.Count > 0)
            {
                cmbPosition.DataSource = store.PositionNumList;
                cmbPosition.SelectedIndex = 0;
                ktkPosition = CSVPositionReader<VerticalPosition>.GetPositon(cmbPosition.Text); 
            }

            timer1.Enabled = true;
            this.ShowInTaskbar = true;
            this.Text = ConfigAppSettings.GetValue(Setting_Init.App_Title);

            notifyIcon1.Text = ConfigAppSettings.GetValue(Setting_Init.App_Title);
            this.chbBuzzer.Checked = store.UseBuzzer;
            axisMoveControl1.LoadData(store, store.moveAxisList.ToArray());
            txtLedIp.Text = store.Config.LED_IP;
            txtMP1.Text = store.Config.MiddleAxis_P1.ToString();
            FrmIOStatus frm = new FrmIOStatus();
            trackBar1.Value = Light.defaultR;
            trackBar2.Value = Light.defaultG;
            trackBar3.Value = Light.defaultB;

            frm.SetStore(store);
            AddForm("  IO状态查看  ", frm);

            LoadOk = true;
            this.Opacity = 1;
        }


        #endregion
 

        private void stop_button_Click(object sender, EventArgs e)
        {
            try
            {
                store.StopRun();
                StoreOpenStatus(false);
            }
            catch (Exception ex)
            {
                LogUtil.error(LOGGER, "出错:" + ex.StackTrace);
            }
        }
        private bool preOpen = false;
        public void StoreOpenStatus(bool isOpen)
        {
            if (preOpen.Equals(isOpen))
            {
                return;
            }
            preOpen = isOpen;

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

            btnStoreStart.Enabled = !isOpen;
            btnSotreReset.Enabled = isOpen;
            btnStoreStop.Enabled = isOpen;

        }
        private bool LoadOk = false;

        private void FrmTest_Load(object sender, EventArgs e)
        { 
            string version = GetVersion(true);
            cmbPosition.Items.Clear();
            LogUtil.logBox = this.richTextBox1;
            InitStoreValue(); 
        }
        private void AddForm(string text, Form form)
        {
            text = text.PadLeft(10, ' ');
            TabPage lineTabPage = new TabPage(text);

            Panel linePan = new Panel();
            linePan.Dock = DockStyle.Fill;
            linePan.AutoScroll = true;
            lineTabPage.Controls.Add(linePan);
            form.FormBorderStyle = FormBorderStyle.None;
            form.TopLevel = false;
            linePan.Controls.Add(form);
            form.Dock = DockStyle.Fill;
            linePan.Anchor = ((AnchorStyles)((AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom | AnchorStyles.Left)));
            form.Anchor = ((AnchorStyles)((AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom | AnchorStyles.Left)));
            form.Show();
            tabControl1.Controls.Add(lineTabPage);
        }
        private DateTime preReadModblsTime = DateTime.Now;
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (!this.Visible)
            {
                return;
            }
             
            lblThisSta.Text = store.GetRunStr() + "  ";
            if (store.MoveInfo.MoveType.Equals(MoveType.None).Equals(false))
            {
                lblThisSta.Text += store.MoveInfo.MoveParam.ToStr();
            }

            if (store.runStatus > StoreRunStatus.Wait)
            {
                if (btnStoreStart.Enabled.Equals(true))
                {
                    StoreOpenStatus(true);
                }

                //复位按钮状态显示
                if (btnSotreReset.Enabled == false)
                {
                    if ((store.runStatus.Equals(StoreRunStatus.HomeMoving) || store.runStatus.Equals(StoreRunStatus.Reset))
                        && store.alarmType.Equals(StoreAlarmType.None))
                    {
                    }
                    else
                    {
                        if (btnSotreReset.Enabled.Equals(false))
                        {
                            btnSotreReset.Enabled = true;
                        }
                    }
                }
                else
                {
                    if ((store.runStatus.Equals(StoreRunStatus.HomeMoving) || store.runStatus.Equals(StoreRunStatus.Reset))
                        && store.alarmType.Equals(StoreAlarmType.None))
                    {
                        if (btnStoreStart.Enabled)
                        {
                            btnStoreStart.Enabled = false;
                            btnSotreReset.Enabled = false;
                        }
                    }
                }
                lblWarnMsg.Text = store.WarnMsg;

                //if (store.Config.IsHasDoorLimit.Equals(1))
                //{
                //    if (IOManager.IOValue(IO_Type.Door_Limit).Equals(IO_VALUE.LOW))
                //    {
                //        lblWarnMsg.Text = lblWarnMsg.Text + "   前门未关";
                //    }
                //    else if (store.Config.StoreDIList.ContainsKey(IO_Type.Door_LeftLimit) && IOManager.IOValue(IO_Type.Door_LeftLimit).Equals(IO_VALUE.LOW))
                //    {
                //        lblWarnMsg.Text = lblWarnMsg.Text + "   左侧门未关";
                //    }
                //    else if (store.Config.StoreDIList.ContainsKey(IO_Type.Door_RightLimit) && IOManager.IOValue(IO_Type.Door_RightLimit).Equals(IO_VALUE.LOW))
                //    {
                //        lblWarnMsg.Text = lblWarnMsg.Text + "   右侧门未关";
                //    }
                //}
                ////如果不在出入库中,且叉子上有信号,需要提示检查叉子
                //if (store.runStatus.Equals(StoreRunStatus.Runing) && IOManager.IOValue(IO_Type.TinCheck_Fixture).Equals(IO_VALUE.HIGH))
                //{
                //    lblWarnMsg.Text = lblWarnMsg.Text + "   叉子料盘检测有料,请检查";
                //}

                if (store.autoNext)
                {
                    string msg = store.autoMsg;
                    lblMsg.Text = msg;
                    if (btnStartAuTo.Text.Equals("开始自动出入库"))
                    {
                        btnStartAuTo.Text = "停止自动出入库";
                    }
                    try
                    {
                        msg = msg.Replace("自动出库:", "");
                        msg = msg.Replace("自动入库:", "");
                        int index = store.PositionNumList.IndexOf(msg);
                        if (index >= 0 && (!msg.Equals("")))
                        {
                            cmbPosition.SelectedIndex = index;
                        }
                    }
                    catch (Exception ex) { }
                }
                else
                {
                    // lblMsg.Text = "没有开启自动出入库";
                    if (btnStartAuTo.Text.Equals("停止自动出入库"))
                    {
                        btnStartAuTo.Text = "开始自动出入库";
                    }
                }
            }
            else
            {
                lblThisSta.Text = "等待启动";
                lblWarnMsg.Text = "";
                btnStartAuTo.Text = "开始自动出入库";
            }
        } 



        private void FrmTest_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)//当用户点击窗体右上角X按钮或(Alt + F4)时 发生           
            {
                e.Cancel = true;
                HideForm();
            }
        }
        private void btnOutStore_Click(object sender, EventArgs e)
        {
            if (store.runStatus >= StoreRunStatus.HomeMoving)
            {
                string selectPositionNum = cmbPosition.Text;
                //StoreMoveP ktk = GetMoveP();
              //  store.StartOutStore(new InOutParam(MoveType.OutStore, selectPositionNum, ktk));
            }
            else
            {
                MessageBox.Show("请先启动料仓!");
            }
        }

        private void btnInStore_Click(object sender, EventArgs e)
        {
            if (store.runStatus >= StoreRunStatus.HomeMoving)
            {
                string selectPositionNum = cmbPosition.Text;
              //  StoreMoveP ktk = GetMoveP();
              //  store.StartInStore(new InOutParam(MoveType.InStore, selectPositionNum, ktk));
            }
            else
            {
                MessageBox.Show("请先启动料仓!");
            }
        }
         
        private void cmbPosition_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbPosition.SelectedIndex >= 0)
            {
                string PosId = cmbPosition.Text;
                VerticalPosition position = CSVPositionReader<VerticalPosition>.GetPositon(PosId);
                txtMP2.Text = position.MiddleAxis_P2.ToString();
                txtLeds.Text = position.LedIndex;
            }
        }


        private void btnSavePosition_Click(object sender, EventArgs e)
        {
            string selectPositionNum = cmbPosition.Text;
            VerticalPosition position = CSVPositionReader<VerticalPosition>.GetPositon(selectPositionNum);
            position.MiddleAxis_P2 = FormUtil.GetIntValue(txtMP2);
            position.LedIndex = txtLeds.Text;
            string appPath = Application.StartupPath;
            string positionConfigFile = appPath + ConfigAppSettings.GetValue(Setting_Init.Store_Position_Config);
            if (!File.Exists(positionConfigFile))
            {
                positionConfigFile = appPath + ConfigAppSettings.GetValue(Setting_Init.Store_Position_Config, "_" + store.StoreID.ToString());
            }
            bool result = CSVPositionReader<VerticalPosition>.SavePostion(positionConfigFile, position);
            if (!result)
            {
                MessageBox.Show(" 保存失败 ");
            }
            else
            {
                MessageBox.Show(" 保存成功 ");
            }
        } 

        private void AxisABSMove(ConfigMoveAxis moveAxis, TextBox txt, int targetSpeed)
        { 
            int targetPosition = FormUtil.GetIntValue(txt);
            moveAxis.TargetPosition = targetPosition;
            ACServerManager.AbsMove(moveAxis.DeviceName, moveAxis.GetAxisValue(), targetPosition, targetSpeed);
        } 
        private void btnMiddleP2_Click(object sender, EventArgs e)
        {
            AxisABSMove(store.Config.Middle_Axis, txtMP2, store.Config.MiddleAxis_P2_Speed);
        } 
        private void btnMiddleP1_Click(object sender, EventArgs e)
        {
           AxisABSMove(store.Config.Middle_Axis, txtMP1, store.Config.MiddleAxis_P1_Speed);
        } 


        #region 菜单操作
         
        private void btnStartAuTo_Click(object sender, EventArgs e)
        {
            if (store.runStatus >= StoreRunStatus.HomeMoving)
            {
                if (store.autoNext)
                {
                    store.autoNext = false;
                    btnStartAuTo.Text = "开始自动出入库";
                }
                else
                {
                    DialogResult res = MessageBox.Show("确定开始自动出入库?", "提示", MessageBoxButtons.YesNo);
                    if (res.Equals(DialogResult.Yes))
                    {
                        store.autoNext = true;
                        int jiange = FormUtil.GetIntValue(txtJiange);
                        store.autoJiange = jiange;
                        if (cmbPosition.SelectedIndex >= 0)
                        {
                            int currIndex = cmbPosition.SelectedIndex;
                            store.autoPositionIndex = currIndex;
                            store.AutoStartIndex = currIndex;
                            string poText = cmbPosition.Text;
                            store.autoMsg = "自动出库:" + poText;
                            LogUtil.info(LOGGER, store.Name + "开启自动出入库模式,开始位置【" + poText + "】(索引=" + currIndex + "),间隔=" + jiange + ",入库开始!");
                            //store.StartOutStoreMove(new InOutStoreParam("", poText));
                            store.StartInStore(new InOutParam(MoveType.InStore, poText));
                        }
                        btnStartAuTo.Text = "停止自动出入库";
                    }
                }
            }
            else
            {
                MessageBox.Show("请先启动料仓!");
            }
        } 

        private void 启动ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            store.StartRun();
            if (store.runStatus >= StoreRunStatus.HomeMoving)
            {
                StoreOpenStatus(true);
            }
        }

        private void 停止ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            stop_button_Click(null, null);
        }

        private void 复位ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.store.Reset();
        } 
        private void chbAuto_CheckedChanged(object sender, EventArgs e)
        {
            if (!LoadOk)
            {
                return;
            }
            if (chbAuto.Checked)
            {
                ConfigAppSettings.SaveValue(Setting_Init.App_AutoRun, 1);
                ManagerUtil.AutoRun(Application.ExecutablePath, true);
            }
            else
            {
                ConfigAppSettings.SaveValue(Setting_Init.App_AutoRun, 0);
                ManagerUtil.AutoRun(Application.ExecutablePath, false);
            }
        }
        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("是否确定退出?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result.Equals(DialogResult.Yes))
            {
                ExitApp();
            }
        }
        private void 显示ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                FrmPwd fw = new FrmPwd(10);
                DialogResult result = fw.ShowDialog();
                if (!result.Equals(DialogResult.OK))
                {
                    LogUtil.info("切换界面显示时,没有正确输入密码");
                    return;
                }
                this.Visible = true;
                this.WindowState = FormWindowState.Maximized;
                this.notifyIcon1.Visible = false;
                this.ShowInTaskbar = true;
                DebugStatus(false);
            }
            catch (Exception ex)
            {
                LogUtil.error("显示界面出错:" + ex.ToString());
            }
        }
        private void button4_Click(object sender, EventArgs e)
        {
            FrmAbout about = new FrmAbout();
            about.ShowDialog();
        }
        private void btnDebug_Click(object sender, EventArgs e)
        {
            if (btnDebug.Text.Equals("启用调试"))
            {
                DebugStatus(true);
            }
            else
            {
                DebugStatus(false);
            }
        }

        private void chbBuzzer_CheckedChanged(object sender, EventArgs e)
        {
            if (!LoadOk)
            {
                return;
            }
            if (chbBuzzer.Checked)
            {
                ConfigAppSettings.SaveValue(Setting_Init.UseBuzzer, 1);
                store.UseBuzzer = true;
                LogUtil.info("勾选:启用蜂鸣器");
            }
            else
            {
                ConfigAppSettings.SaveValue(Setting_Init.UseBuzzer, 0);
                store.UseBuzzer = false;
                LogUtil.info("去掉:启用蜂鸣器");
            }
        }

       
        #endregion

         
        private void ExitApp()
        {
            try
            {
                if (this.timer1.Enabled)
                {
                    this.timer1.Enabled = false;
                }
                if (this.btnStoreStop.Enabled)
                {
                    stop_button_Click(null, null);
                }

                IOManager.instance.CloseAllDO();
                StoreOpenStatus(false);
                IOManager.instance.CloseAllConnection();
                ACServerManager.CloseAllPort();
                HumitureController.Release();

                System.Environment.Exit(System.Environment.ExitCode);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace, "Exception(异常)", MessageBoxButtons.OK, MessageBoxIcon.Error);
                LogUtil.error(LOGGER, ex.StackTrace);
            }
        }

        private void HideForm()
        {
            this.ShowInTaskbar = false;
            this.notifyIcon1.Visible = true;
            this.Hide();
            DebugStatus(false);
        }
        private void FrmStoreBox_FormClosed(object sender, FormClosedEventArgs e)
        {
            ExitApp();
        }
         

        private void btnClearL_Click(object sender, EventArgs e)
        {
            LogUtil.ClearLog();
        }

        private void DebugStatus(bool status)
        {
            if (status)
            {
                btnDebug.Text = "禁用调试";
            }
            else
            {
                btnDebug.Text = "启用调试";
            }
            axisMoveControl1.Enabled = status;
            groupInout.Enabled = status;
         
        } 
        private void btnOpenLed_Click(object sender, EventArgs e)
        {
            string ip = txtLedIp.Text;
            int index = FormUtil.GetIntValue(txtLedIndex);
            int dmxId = (int)numYu.Value;
            LEDManager.GetLedModule(ip).LightOn(Light.DefaultLight(dmxId, index));
        } 
        private void trackBar1_ValueChanged(object sender, EventArgs e)
        {
            if (!trackBar1.Value.Equals(Light.defaultR))
            {
                Light.defaultR = (byte)trackBar1.Value;
            }
            lblR.Text = Light.defaultR.ToString();
        }

        private void trackBar2_ValueChanged(object sender, EventArgs e)
        { 
            if (!trackBar2.Value.Equals(Light.defaultG))
            {
                Light.defaultG = (byte)trackBar2.Value;
            }

            lblG.Text = Light.defaultG.ToString();
        }

        private void trackBar3_ValueChanged(object sender, EventArgs e)
        {
            if (!trackBar3.Value.Equals(Light.defaultB))
            {
                Light.defaultB = (byte)trackBar3.Value;
            }

            lblB.Text = Light.defaultB.ToString();
        }

        private void btnCloseLed_Click(object sender, EventArgs e)
        {
            string ip = txtLedIp.Text;
            int index = FormUtil.GetIntValue(txtLedIndex);
            int dmxId = (int)numYu.Value;
            LEDManager.GetLedModule(ip).LightOff(Light.DefaultLight(dmxId, index));
        }

        private void btOAll_Click(object sender, EventArgs e)
        {
            string ip = txtLedIp.Text;
            LEDManager.GetLedModule(ip).AllLightOn(Light.DefaultLight(0, 0));
        }

        private void btnCAll_Click(object sender, EventArgs e)
        {
            string ip = txtLedIp.Text;
            LEDManager.GetLedModule(ip).AllLightOn(Light.DefaultLight(0, 0));
        }
    }
}