using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using SmartX;
using INT_LKD_2.DataStore;
using INT_LKD_2.Forms;
using INT_LKD_2.DialogForms;

namespace INT_LKD_2.Controls
{
    public partial class ControlMenuManualIoTest : UserControl
    {
        #region Field
        private FormMenu m_ParentForm;
        private Define.E_BoardMode m_BoardMode;

        private Color ColorEnable;
        private Color ColorDisable;

        private Collection<SmartLabel> CollLabelInput;
        #endregion

        #region Constructor
        public ControlMenuManualIoTest(FormMenu parent, Define.E_BoardMode mode)
        {
            InitializeComponent();

            this.ParentForm = parent;
            this.BoardMode = mode;
            this.Initialize();
        } 
        #endregion

        #region Property
        public FormMenu ParentForm
        {
            get { return this.m_ParentForm; }
            private set { this.m_ParentForm = value; }
        }
        public Define.E_BoardMode BoardMode
        {
            get { return this.m_BoardMode; }
            private set { this.m_BoardMode = value; }
        }
        private string GetMainboardID
        {
            get
            {
                if (this.BoardMode == Define.E_BoardMode.Master)
                    return CommunicationID.MainBoardMaster;
                else
                    return CommunicationID.MainBoardSlave;
            }
        }
        private int ServoID
        {
            get
            {
                if (this.BoardMode == Define.E_BoardMode.Master)
                    return 1;
                else
                    return 2;
            }
        }
        #endregion

        #region Method
        private void Initialize()
        {
            this.smartGroupBox1.Text = string.Format("Manual > {0}", this.BoardMode);

            this.ColorEnable = Color.Lime;
            this.ColorDisable = Color.Gainsboro;

            this.CollLabelInput = new Collection<SmartLabel>();
            this.CollLabelInput.Add(this.labelInput1);
            this.CollLabelInput.Add(this.labelInput2);
            this.CollLabelInput.Add(this.labelInput3);
            this.CollLabelInput.Add(this.labelInput4);
            this.CollLabelInput.Add(this.labelInput4);

            this.labelDiff.Text = "0.00";
            this.labelDisp.Text = "0.00";
            this.labelVacuumWorking.Text = "0.0";
            this.labelVacuumMaster.Text = "0.0";
            this.labelSbAlarm1.Visible = false;
            this.labeldispAlarm1.Visible = false;

            for (int i = 0; i < this.CollLabelInput.Count; i++)
                this.CollLabelInput[i].RoundRectFillColor = this.ColorDisable;

            if (this.BoardMode == Define.E_BoardMode.Master)
            {
                this.buttonMotorChamber1.ButtonText = "Chamber 12";
                this.buttonMotorChamber2.ButtonText = "Chamber 11";
                this.buttonMotorChamber3.ButtonText = "Chamber 10";
                this.buttonMotorChamber4.ButtonText = "Chamber 9";
                this.buttonMotorChamber5.ButtonText = "Chamber 8";
                this.buttonMotorChamber6.ButtonText = "Chamber 7";
            }
            else
            {
                this.buttonOut1.ButtonText = "None";
                this.buttonOut2.ButtonText = "None";
                this.buttonOut3.ButtonText = "None";
            }
        }

        private void OutputCommand(string address, string data)
        {
            string command = "", id = "";
            
            command = CommunicationCommand.IOTest;

            switch (this.BoardMode)
            {
                case Define.E_BoardMode.Master:
                    id = CommunicationID.MainBoardMaster;
                    break;
                case Define.E_BoardMode.Slave:
                    id = CommunicationID.MainBoardSlave;
                    break;
                default:
                    id = CommunicationID.MainBoardMaster;
                    break;
            }

            if (this.BoardMode == Define.E_BoardMode.Master)
                this.ParentForm.ParentForm.TransferDataStream_Master(command, id, address, data);
            else
                this.ParentForm.ParentForm.TransferDataStream_Slave(command, id, address, data);
        }
        private void ServoParameterAllRead()
        {
            SmartSplash.Start(SmartSplash.BuiltInLoadingImages.PROCESSING1, 200);
            
            // ServoMotor 1
            this.labelMotor1Origin.Text = this.ParentForm.ParentForm.ServoMotorRead2(this.ServoID, ServoMotorAddress.CMD17);
            this.labelMotorReady.Text = this.ParentForm.ParentForm.ServoMotorRead2(this.ServoID, ServoMotorAddress.CMD1);
            this.labelMotorChamber1.Text = this.ParentForm.ParentForm.ServoMotorRead2(this.ServoID, ServoMotorAddress.CMD2);
            this.labelMotorChamber2.Text = this.ParentForm.ParentForm.ServoMotorRead2(this.ServoID, ServoMotorAddress.CMD3);
            this.labelMotorChamber3.Text = this.ParentForm.ParentForm.ServoMotorRead2(this.ServoID, ServoMotorAddress.CMD4);
            this.labelMotorChamber4.Text = this.ParentForm.ParentForm.ServoMotorRead2(this.ServoID, ServoMotorAddress.CMD5);
            this.labelMotorChamber5.Text = this.ParentForm.ParentForm.ServoMotorRead2(this.ServoID, ServoMotorAddress.CMD6);
            this.labelMotorChamber6.Text = this.ParentForm.ParentForm.ServoMotorRead2(this.ServoID, ServoMotorAddress.CMD7);
            this.labelMotorDischarge.Text = this.ParentForm.ParentForm.ServoMotorRead2(this.ServoID, ServoMotorAddress.CMD8);

            SmartSplash.Finish();

            this.ParentForm.Enabled = true;
            
            switch (this.ParentForm.ParentForm.CurrentSystemStatus.CurrentUser.Group)
            {
                case Define.E_UserGroup.None:
                    this.Enabled = false;
                    break;
                case Define.E_UserGroup.Level1:
                    this.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level1.IsMenuIOTest;
                    break;
                case Define.E_UserGroup.Level2:
                    this.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level2.IsMenuIOTest;
                    break;
                case Define.E_UserGroup.Level3:
                    this.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level3.IsMenuIOTest;
                    break;
                case Define.E_UserGroup.Admin:
                    this.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level3.IsMenuIOTest;
                    break;
                case Define.E_UserGroup.Developer:
                    this.Enabled = true;
                    break;
                case Define.E_UserGroup.NotLogin:
                    this.Enabled = false;
                    break;
                case Define.E_UserGroup.LogOut:
                    this.Enabled = false;
                    break;
                default:
                    break;
            }

        }

        // 변위센서 데이터
        public void UpdateDisplayDispData(LeakData1 data)
        {
            this.labelDisp.Text = data.DispData.RData;
        }
        // 차압센서 데이터
        public void UpdateDisplayDiffData(LeakData1 data)
        {
            this.labelDiff.Text = data.DiffData.MAdc;
        }
        // 압력센서 데이터
        public void UpdateDisplayPresData(LeakData1 data)
        {
            this.labelVacuumWorking.Text = data.PresData.WorkingChamber;
            this.labelVacuumMaster.Text = data.PresData.MasterChamber;
        }

        public void UpdateDisplayInputData(Collection<string> datas)
        {
            Color value = this.ColorDisable;
            for (int i = 0; i < datas.Count; i++)
            {
                if (datas[i] == "0")
                    value = this.ColorDisable;
                else
                    value = this.ColorEnable;

                this.CollLabelInput[i].RoundRectFillColor = value;
            }
        }
        public void UpdateDisplayAlarmView(AlarmList alarm)
        {

        }

        public void DisplayRefresh()
        {
            this.ParentForm.ParentForm.CurrentSystemStatus.CurrentDisplayMode = Define.E_DisplayModeStore.IOTest;
            this.ParentForm.ParentForm.SetDisplayMode(Define.E_EquipmentMode.IOTest);

            // Servo Parameter Read
            // 서보설정 읽는동안 Enable = false
            this.ParentForm.Enabled = false;
            this.smartTimer.Start();
        }
        #endregion

        #region Event Handler
        // Output 1~10
        private void buttonOut1_Click(object sender, EventArgs e)
        {
            string address = "", data = "";
            SmartButton button = sender as SmartButton;

            if (button == null)
                return;

            // adress
            address = Helper.StringBlankFillDigits4(CommunicationAddress._7701_Output1);

            // data
            if (button.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
                data = Helper.StringBlankFillDigits4("1");
            else
                data = Helper.StringBlankFillDigits4("0");

            this.OutputCommand(address, data);
        }
        private void buttonOut2_Click(object sender, EventArgs e)
        {
            string address = "", data = "";
            SmartButton button = sender as SmartButton;

            if (button == null)
                return;

            // adress
            address = Helper.StringBlankFillDigits4(CommunicationAddress._7702_Output2);

            // data
            if (button.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
                data = Helper.StringBlankFillDigits4("1");
            else
                data = Helper.StringBlankFillDigits4("0");

            this.OutputCommand(address, data);
        }
        private void buttonOut3_Click(object sender, EventArgs e)
        {
            string address = "", data = "";
            SmartButton button = sender as SmartButton;

            if (button == null)
                return;

            // adress
            address = Helper.StringBlankFillDigits4(CommunicationAddress._7703_Output3);

            // data
            if (button.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
                data = Helper.StringBlankFillDigits4("1");
            else
                data = Helper.StringBlankFillDigits4("0");

            this.OutputCommand(address, data);
        }
        private void buttonOut4_Click(object sender, EventArgs e)
        {
            string address = "", data = "";
            SmartButton button = sender as SmartButton;

            if (button == null)
                return;

            // adress
            address = Helper.StringBlankFillDigits4(CommunicationAddress._7704_Output4);

            // data
            if (button.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
                data = Helper.StringBlankFillDigits4("1");
            else
                data = Helper.StringBlankFillDigits4("0");

            this.OutputCommand(address, data);
        }
        private void buttonOut5_Click(object sender, EventArgs e)
        {
            string address = "", data = "";
            SmartButton button = sender as SmartButton;

            if (button == null)
                return;

            // adress
            address = Helper.StringBlankFillDigits4(CommunicationAddress._7705_Output5);

            // data
            if (button.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
                data = Helper.StringBlankFillDigits4("1");
            else
                data = Helper.StringBlankFillDigits4("0");

            this.OutputCommand(address, data);
        }
        private void buttonOut6_Click(object sender, EventArgs e)
        {
            string address = "", data = "";
            SmartButton button = sender as SmartButton;

            if (button == null)
                return;

            // adress
            address = Helper.StringBlankFillDigits4(CommunicationAddress._7706_Output6);

            // data
            if (button.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
                data = Helper.StringBlankFillDigits4("1");
            else
                data = Helper.StringBlankFillDigits4("0");

            this.OutputCommand(address, data);
        }
        private void buttonOut7_Click(object sender, EventArgs e)
        {
            string address = "", data = "";
            SmartButton button = sender as SmartButton;

            if (button == null)
                return;

            // adress
            address = Helper.StringBlankFillDigits4(CommunicationAddress._7707_Output7);

            // data
            if (button.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
                data = Helper.StringBlankFillDigits4("1");
            else
                data = Helper.StringBlankFillDigits4("0");

            this.OutputCommand(address, data);
        }
        private void buttonOut8_Click(object sender, EventArgs e)
        {
            string address = "", data = "";
            SmartButton button = sender as SmartButton;

            if (button == null)
                return;

            // adress
            address = Helper.StringBlankFillDigits4(CommunicationAddress._7708_Output8);

            // data
            if (button.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
                data = Helper.StringBlankFillDigits4("1");
            else
                data = Helper.StringBlankFillDigits4("0");

            this.OutputCommand(address, data);
        }
        private void buttonOut9_Click(object sender, EventArgs e)
        {
            string address = "", data = "";
            SmartButton button = sender as SmartButton;

            if (button == null)
                return;

            // adress
            address = Helper.StringBlankFillDigits4(CommunicationAddress._7709_Output9);

            // data
            if (button.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
                data = Helper.StringBlankFillDigits4("1");
            else
                data = Helper.StringBlankFillDigits4("0");

            this.OutputCommand(address, data);
        }
        private void buttonOut10_Click(object sender, EventArgs e)
        {
            string address = "", data = "";
            SmartButton button = sender as SmartButton;

            if (button == null)
                return;

            // adress
            address = Helper.StringBlankFillDigits4(CommunicationAddress._7710_Output10);

            // data
            if (button.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
                data = Helper.StringBlankFillDigits4("1");
            else
                data = Helper.StringBlankFillDigits4("0");

            this.OutputCommand(address, data);
        }
        private void buttonOut11_Click(object sender, EventArgs e)
        {
            string address = "", data = "";
            SmartButton button = sender as SmartButton;

            if (button == null)
                return;

            // adress
            address = Helper.StringBlankFillDigits4(CommunicationAddress._7711_Output11);

            // data
            if (button.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
                data = Helper.StringBlankFillDigits4("1");
            else
                data = Helper.StringBlankFillDigits4("0");

            this.OutputCommand(address, data);
        }
        private void buttonOut12_Click(object sender, EventArgs e)
        {
            string address = "", data = "";
            SmartButton button = sender as SmartButton;

            if (button == null)
                return;

            // adress
            address = Helper.StringBlankFillDigits4(CommunicationAddress._7712_Output12);

            // data
            if (button.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
                data = Helper.StringBlankFillDigits4("1");
            else
                data = Helper.StringBlankFillDigits4("0");

            this.OutputCommand(address, data);
        }

        private void buttonJogCW_MouseDown(object sender, MouseEventArgs e)
        {
            //this.ParentForm.Set_Equipment_ButtonEnable(true);
            if (this.BoardMode == Define.E_BoardMode.Master)
                this.ParentForm.ParentForm.TransferData_Master(CommunicationCommand.MotorJogCwOn, this.GetMainboardID);
            else
                this.ParentForm.ParentForm.TransferData_Slave(CommunicationCommand.MotorJogCwOn, this.GetMainboardID);
        }
        private void buttonJogCW_MouseMove(object sender, MouseEventArgs e)
        {
            if (this.BoardMode == Define.E_BoardMode.Master)
                this.ParentForm.ParentForm.TransferData_Master(CommunicationCommand.MotorJogCwOff, this.GetMainboardID);
            else
                this.ParentForm.ParentForm.TransferData_Slave(CommunicationCommand.MotorJogCwOff, this.GetMainboardID);

            this.buttonJogCW.Enabled = false;
            this.buttonJogCCW.Enabled = false;
            this.smartTimerJog.Start();
        }

        private void buttonJogCCW_MouseDown(object sender, MouseEventArgs e)
        {
            //this.ParentForm.Set_Equipment_ButtonEnable(true);

            if (this.BoardMode == Define.E_BoardMode.Master)
                this.ParentForm.ParentForm.TransferData_Master(CommunicationCommand.MotorJogCcwOn, this.GetMainboardID);
            else
                this.ParentForm.ParentForm.TransferData_Slave(CommunicationCommand.MotorJogCcwOn, this.GetMainboardID);
        }
        private void buttonJogCCW_MouseMove(object sender, MouseEventArgs e)
        {
            if (this.BoardMode == Define.E_BoardMode.Master)
                this.ParentForm.ParentForm.TransferData_Master(CommunicationCommand.MotorJogCcwOff, this.GetMainboardID);
            else
                this.ParentForm.ParentForm.TransferData_Slave(CommunicationCommand.MotorJogCcwOff, this.GetMainboardID);

            this.buttonJogCW.Enabled = false;
            this.buttonJogCCW.Enabled = false;
            this.smartTimerJog.Start();
        }

        // Motor 1
        private void buttonMotor1Origin_Click(object sender, EventArgs e)
        {
            string command = "";

            if (this.buttonOut7.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
                return;

            command = string.Format("{0}{1}", CommunicationCommand.MotorOrigin, this.GetMainboardID);

            if (this.BoardMode == Define.E_BoardMode.Master)
                this.ParentForm.ParentForm.TransferData_Master(command, this.GetMainboardID);
            else
                this.ParentForm.ParentForm.TransferData_Slave(command, this.GetMainboardID);
        }
        private void buttonMotor1AlarmClear_Click(object sender, EventArgs e)
        {
            string command = "";

            command = string.Format("{0}{1}", CommunicationCommand.MotorAlarmReset, this.GetMainboardID);

            if (this.BoardMode == Define.E_BoardMode.Master)
                this.ParentForm.ParentForm.TransferData_Master(command, this.GetMainboardID);
            else
                this.ParentForm.ParentForm.TransferData_Slave(command, this.GetMainboardID);
        }
        private void buttonMotorReady_Click(object sender, EventArgs e)
        {
            string command = "";

            if (this.buttonOut7.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
                return;

            command = string.Format("{0}{1}", CommunicationCommand.MotorMove1, this.GetMainboardID);

            if (this.BoardMode == Define.E_BoardMode.Master)
                this.ParentForm.ParentForm.TransferData_Master(command, CommunicationID.SubBoard1);
            else
                this.ParentForm.ParentForm.TransferData_Slave(command, CommunicationID.SubBoard1);
        }
        private void buttonMotorChamber1_Click(object sender, EventArgs e)
        {
            string command = "";

            if (this.buttonOut7.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
                return;

            command = string.Format("{0}{1}", CommunicationCommand.MotorMove1, this.GetMainboardID);

            if (this.BoardMode == Define.E_BoardMode.Master)
                this.ParentForm.ParentForm.TransferData_Master(command, CommunicationID.SubBoard2);
            else
                this.ParentForm.ParentForm.TransferData_Slave(command, CommunicationID.SubBoard2);
        }
        private void buttonMotorChamber2_Click(object sender, EventArgs e)
        {
            string command = "";

            if (this.buttonOut7.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
                return;

            command = string.Format("{0}{1}", CommunicationCommand.MotorMove1, this.GetMainboardID);

            if (this.BoardMode == Define.E_BoardMode.Master)
                this.ParentForm.ParentForm.TransferData_Master(command, CommunicationID.SubBoard3);
            else
                this.ParentForm.ParentForm.TransferData_Slave(command, CommunicationID.SubBoard3);
        }
        private void buttonMotorChamber3_Click(object sender, EventArgs e)
        {
            string command = "";

            if (this.buttonOut7.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
                return;

            command = string.Format("{0}{1}", CommunicationCommand.MotorMove1, this.GetMainboardID);

            if (this.BoardMode == Define.E_BoardMode.Master)
                this.ParentForm.ParentForm.TransferData_Master(command, CommunicationID.SubBoard4);
            else
                this.ParentForm.ParentForm.TransferData_Slave(command, CommunicationID.SubBoard4);
        }
        private void buttonMotorChamber4_Click(object sender, EventArgs e)
        {
            string command = "";

            if (this.buttonOut7.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
                return;

            command = string.Format("{0}{1}", CommunicationCommand.MotorMove1, this.GetMainboardID);

            if (this.BoardMode == Define.E_BoardMode.Master)
                this.ParentForm.ParentForm.TransferData_Master(command, CommunicationID.SubBoard5);
            else
                this.ParentForm.ParentForm.TransferData_Slave(command, CommunicationID.SubBoard5);
        }
        private void buttonMotorChamber5_Click(object sender, EventArgs e)
        {
            string command = "";

            if (this.buttonOut7.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
                return;

            command = string.Format("{0}{1}", CommunicationCommand.MotorMove1, this.GetMainboardID);

            if (this.BoardMode == Define.E_BoardMode.Master)
                this.ParentForm.ParentForm.TransferData_Master(command, CommunicationID.SubBoard6);
            else
                this.ParentForm.ParentForm.TransferData_Slave(command, CommunicationID.SubBoard6);
        }
        private void buttonMotorChamber6_Click(object sender, EventArgs e)
        {
            string command = "";

            if (this.buttonOut7.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
                return;

            command = string.Format("{0}{1}", CommunicationCommand.MotorMove1, this.GetMainboardID);

            if (this.BoardMode == Define.E_BoardMode.Master)
                this.ParentForm.ParentForm.TransferData_Master(command, CommunicationID.SubBoard7);
            else
                this.ParentForm.ParentForm.TransferData_Slave(command, CommunicationID.SubBoard7);
        }
        private void buttonMotorDischarge_Click(object sender, EventArgs e)
        {
            string command = "";

            if (this.buttonOut7.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
                return;

            command = string.Format("{0}{1}", CommunicationCommand.MotorMove1, this.GetMainboardID);

            if (this.BoardMode == Define.E_BoardMode.Master)
                this.ParentForm.ParentForm.TransferData_Master(command, CommunicationID.SubBoard8);
            else
                this.ParentForm.ParentForm.TransferData_Slave(command, CommunicationID.SubBoard8);
        }
        private void labelMotor1Origin_Click(object sender, EventArgs e)
        {
            string caption = "Origin", before = "", after = "";

            before = this.labelMotor1Origin.Text;
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(caption, this.labelMotor1Origin.Text, 7, 0, false);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue == 0)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.LANGUAGE);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.ParentForm.ParentForm.ServoMotorWrite2(this.ServoID, ServoMotorAddress.CMD1, myKeyPad.IntValue, this.labelMotorReady);
                    this.ParentForm.ParentForm.ServoMotorWrite2(this.ServoID, ServoMotorAddress.CMD17, myKeyPad.IntValue, this.labelMotor1Origin);
                    before = myKeyPad.StringValue;

                    this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.M_Origin, "", before, after);

                    this.ParentForm.ParentForm.ServoMotorWrite1(this.ServoID, ServoMotorAddress.SaveAllParameters, ServoMotorAddress.EEPROM);
                }
            }
        }
        private void labelMotorReady_Click(object sender, EventArgs e)
        {
            string caption = "", before = "", after = "";

            before = this.labelMotorReady.Text;
            caption = string.Format("{0} Location - {1}", this.buttonMotorReady.ButtonText, before);
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(caption, this.labelMotorReady.Text, 7, 0, false);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue == 0)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.LANGUAGE);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.ParentForm.ParentForm.ServoMotorWrite2(this.ServoID, ServoMotorAddress.CMD1, myKeyPad.IntValue, this.labelMotorReady);
                    this.ParentForm.ParentForm.ServoMotorWrite2(this.ServoID, ServoMotorAddress.CMD17, myKeyPad.IntValue, this.labelMotor1Origin);
                    after = myKeyPad.StringValue;

                    this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.M_Ready, "", before, after);

                    this.ParentForm.ParentForm.ServoMotorWrite1(this.ServoID, ServoMotorAddress.SaveAllParameters, ServoMotorAddress.EEPROM);
                }
            }
        }
        private void labelMotorChamber1_Click(object sender, EventArgs e)
        {
            string caption = "", before = "", after = "";

            before = this.labelMotorChamber1.Text;
            caption = string.Format("{0} Location - {1}", this.buttonMotorChamber1.ButtonText, before);
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(caption, this.labelMotorChamber1.Text, 7, 0, false);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue == 0)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.LANGUAGE);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.ParentForm.ParentForm.ServoMotorWrite2(this.ServoID, ServoMotorAddress.CMD2, myKeyPad.IntValue, this.labelMotorChamber1);
                    after = myKeyPad.StringValue;

                    this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.M_Chamber1, "", before, after);

                    this.ParentForm.ParentForm.ServoMotorWrite1(this.ServoID, ServoMotorAddress.SaveAllParameters, ServoMotorAddress.EEPROM);
                }
            }
        }
        private void labelMotorChamber2_Click(object sender, EventArgs e)
        {
            string caption = "", before = "", after = "";

            before = this.labelMotorChamber2.Text;
            caption = string.Format("{0} Location - {1}", this.buttonMotorChamber2.ButtonText, before);
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(caption, this.labelMotorChamber2.Text, 7, 0, false);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue == 0)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.LANGUAGE);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.ParentForm.ParentForm.ServoMotorWrite2(this.ServoID, ServoMotorAddress.CMD3, myKeyPad.IntValue, this.labelMotorChamber2);
                    after = myKeyPad.StringValue;

                    this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.M_Chamber2, "", before, after);

                    this.ParentForm.ParentForm.ServoMotorWrite1(this.ServoID, ServoMotorAddress.SaveAllParameters, ServoMotorAddress.EEPROM);
                }
            }
        }
        private void labelMotorChamber3_Click(object sender, EventArgs e)
        {
            string caption = "", before = "", after = "";

            before = this.labelMotorChamber3.Text;
            caption = string.Format("{0} Location - {1}", this.buttonMotorChamber3.ButtonText, before);
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(caption, this.labelMotorChamber3.Text, 7, 0, false);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue == 0)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.LANGUAGE);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.ParentForm.ParentForm.ServoMotorWrite2(this.ServoID, ServoMotorAddress.CMD4, myKeyPad.IntValue, this.labelMotorChamber3);
                    after = myKeyPad.StringValue;

                    this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.M_Chamber3, "", before, after);

                    this.ParentForm.ParentForm.ServoMotorWrite1(this.ServoID, ServoMotorAddress.SaveAllParameters, ServoMotorAddress.EEPROM);
                }
            }
        }
        private void labelMotorChamber4_Click(object sender, EventArgs e)
        {
            string caption = "", before = "", after = "";

            before = this.labelMotorChamber4.Text;
            caption = string.Format("{0} Location - {1}", this.buttonMotorChamber4.ButtonText, before);
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(caption, this.labelMotorChamber4.Text, 7, 0, false);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue == 0)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.LANGUAGE);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.ParentForm.ParentForm.ServoMotorWrite2(this.ServoID, ServoMotorAddress.CMD5, myKeyPad.IntValue, this.labelMotorChamber4);
                    after = myKeyPad.StringValue;

                    this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.M_Chamber4, "", before, after);

                    this.ParentForm.ParentForm.ServoMotorWrite1(this.ServoID, ServoMotorAddress.SaveAllParameters, ServoMotorAddress.EEPROM);
                }
            }
        }
        private void labelMotorChamber5_Click(object sender, EventArgs e)
        {
            string caption = "", before = "", after = "";

            before = this.labelMotorChamber5.Text;
            caption = string.Format("{0} Location - {1}", this.buttonMotorChamber5.ButtonText, before);
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(caption, this.labelMotorChamber5.Text, 7, 0, false);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue == 0)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.LANGUAGE);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.ParentForm.ParentForm.ServoMotorWrite2(this.ServoID, ServoMotorAddress.CMD6, myKeyPad.IntValue, this.labelMotorChamber5);
                    after = myKeyPad.StringValue;

                    this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.M_Chamber5, "", before, after);

                    this.ParentForm.ParentForm.ServoMotorWrite1(this.ServoID, ServoMotorAddress.SaveAllParameters, ServoMotorAddress.EEPROM);
                }
            }
        }
        private void labelMotorChamber6_Click(object sender, EventArgs e)
        {
            string caption = "", before = "", after = "";

            before = this.labelMotorChamber6.Text;
            caption = string.Format("{0} Location - {1}", this.buttonMotorChamber6.ButtonText, before);
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(caption, this.labelMotorChamber6.Text, 7, 0, false);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue == 0)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.LANGUAGE);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.ParentForm.ParentForm.ServoMotorWrite2(this.ServoID, ServoMotorAddress.CMD7, myKeyPad.IntValue, this.labelMotorChamber6);
                    after = myKeyPad.StringValue;

                    this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.M_Chamber6, "", before, after);

                    this.ParentForm.ParentForm.ServoMotorWrite1(this.ServoID, ServoMotorAddress.SaveAllParameters, ServoMotorAddress.EEPROM);
                }
            }
        }
        private void labelMotorDischarge_Click(object sender, EventArgs e)
        {
            string caption = "", before = "", after = "";

            before = this.labelMotorDischarge.Text;
            caption = string.Format("{0} Location - {1}", this.buttonMotorDischarge.ButtonText, before);
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(caption, this.labelMotorDischarge.Text, 7, 0, false);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue == 0)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.LANGUAGE);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.ParentForm.ParentForm.ServoMotorWrite2(this.ServoID, ServoMotorAddress.CMD8, myKeyPad.IntValue, this.labelMotorDischarge);
                    after = myKeyPad.StringValue;

                    this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.M_ChamberDischarge, "", before, after);

                    this.ParentForm.ParentForm.ServoMotorWrite1(this.ServoID, ServoMotorAddress.SaveAllParameters, ServoMotorAddress.EEPROM);
                }
            }
        }

        private void smartTimer_Tick(object sender, EventArgs e)
        {
            this.smartTimer.Stop();

            this.ServoParameterAllRead();

            this.ParentForm.UpdateButtonColor();
        }
        private void smartTimerJog_Tick(object sender, EventArgs e)
        {
            this.smartTimerJog.Stop();

            this.buttonJogCW.Enabled = true;
            this.buttonJogCCW.Enabled = true;
        }
        #endregion
    }
}
