using System.Runtime.InteropServices;
using System.Text;

namespace Pti4
{
    public partial class Form1 : Form
    {
        short m_Id;    // ID

        public Form1()
        {
            InitializeComponent();
        }

        // Initialize dialog
        private void Pti4_Load(object sender, System.EventArgs e)
        {
            // Get the list of devices registered
            int ret;
            StringBuilder device = new StringBuilder(50);
            StringBuilder devicename = new StringBuilder(50);
            string str = new string(' ', 100);
            short index = 0;
            short group_id, unit_id, device_id;
            group_id = 0;
            unit_id = 0;
            device_id = 0;
            do
            {
                ret = Ccappt.PtQueryDeviceName(index, ref group_id, ref unit_id, ref device_id, devicename, device);
                if (ret == 0)
                {
                    str = string.Format("{0} [{1}]", devicename, device);
                    cboDevice.Items.Add(str);
                    index++;
                }
            } while (ret == 0);
            cboDevice.SelectedIndex = 0;

            txtCh0.Text = "0";
            txtCh1.Text = "0";
            txtCh2.Text = "0";
            txtCh3.Text = "0";
        }

        // Click the [Initialization] button
        private void btnInit_Click(object sender, System.EventArgs e)
        {
            int ret;
            StringBuilder devicename = new StringBuilder(50);
            StringBuilder tmp = new StringBuilder(50);
            short index;
            short group_id, unit_id, device_id;
            // Detect device
            index = (short)cboDevice.SelectedIndex;
            group_id = 0;
            unit_id = 0;
            device_id = 0;
            ret = Ccappt.PtQueryDeviceName(index, ref group_id, ref unit_id, ref device_id, devicename, tmp);
            if (ret != 0)
            {
                ErrorHandling("PtQueryDeviceName", ret);
                return;
            }

            // Initialization
            ret = Ccappt.PtInit(devicename, ref m_Id);
            if (ret != 0)
            {
                ErrorHandling("PtInit", ret);
                return;
            }

            txtRet.Text = "Initialization : Successed";
            ShowRemoteStatus();
        }

        // Click the [Measurement Setting] button
        private void btnMSet_Click(object sender, System.EventArgs e)
        {
            Measure MeasureDlg = new Measure();

            // Invoke the Measurement Setting dialog
            MeasureDlg.SetId(m_Id);
            MeasureDlg.ShowDialog();
            MeasureDlg.Dispose();
        }

        // Click the [Alarm Setting] button
        private void btnASet_Click(object sender, System.EventArgs e)
        {
            Alarm AlarmDlg = new Alarm();

            // Invoke the Alarm Setting dialog
            AlarmDlg.SetId(m_Id);
            AlarmDlg.ShowDialog();
            AlarmDlg.Dispose();
        }

        // Click the [Start] button
        private void btnStart_Click(object sender, System.EventArgs e)
        {
            //-----------------------------------
            // Start
            //-----------------------------------
            int ret;
            ret = Ccappt.PtStartIO(m_Id, 0x01);
            //-----------------------------------
            // Process error
            //-----------------------------------
            if (ret != 0)
            {
                ErrorHandling("PtStartIO", ret);
                return;
            }
            txtRet.Text = "Start : Successed";
            ShowRemoteStatus();
        }

        // Click the [Stop] button
        private void btnStop_Click(object sender, System.EventArgs e)
        {
            //-----------------------------------
            // Stop
            //-----------------------------------
            int ret;
            ret = Ccappt.PtStartIO(m_Id, 0x00);
            //-----------------------------------
            // Process error
            //-----------------------------------
            if (ret != 0)
            {
                ErrorHandling("PtStartIO", ret);
                return;
            }
            txtRet.Text = "Stop : Successed";
            ShowRemoteStatus();
        }

        // Click the [Input Value] button
        private void btnInput_Click(object sender, System.EventArgs e)
        {
            int ret;
            float[] data = new float[4];

            // Input the temperature or capacity value for 4 channels
            GCHandle Pter = GCHandle.Alloc(data, GCHandleType.Pinned);
            ret = Ccappt.PtMultiInputEx(m_Id, 4, data);
            Pter.Free();
            if (ret != 0)
            {
                ErrorHandling("PtMultiInputEx", ret);
                if (ret != 20102)
                {
                    return;
                }
            }

            txtCh0.Text = string.Format("{0:f}", data[0]);
            txtCh1.Text = string.Format("{0:f}", data[1]);
            txtCh2.Text = string.Format("{0:f}", data[2]);
            txtCh3.Text = string.Format("{0:f}", data[3]);

            txtRet.Text = "Input Value : Successed";
        }

        // Click the [Status] button
        private void btnStatus_Click(object sender, System.EventArgs e)
        {
            int ret;
            int disconnectstatus, upperstatus, lowerstatus;
            string str = new string(' ', 32);

            // Get the disconnect status
            disconnectstatus = 0;
            upperstatus = 0;
            lowerstatus = 0;
            ret = Ccappt.PtGetDisconnectStatus(m_Id, ref disconnectstatus);
            if (ret != 0)
            {
                ErrorHandling("PtGetDisconnectStatus", ret);
                return;
            }

            // Get the alarm status
            ret = Ccappt.PtGetCurrentAlarmStatus(m_Id, ref upperstatus, ref lowerstatus);
            if (ret != 0)
            {
                ErrorHandling("PtGetCurrentAlarmStatus", ret);
                return;
            }

            str = string.Format("{0:X}", disconnectstatus);
            txtStsDc.Text = str;
            str = string.Format("{0:X}", upperstatus);
            txtStsUAlarm.Text = str;
            str = string.Format("{0:X}", lowerstatus);
            txtStsLAlarm.Text = str;

            txtRet.Text = "Getting Status : Successed";
        }

        // Click the [Termination] button
        private void btnExit_Click(object sender, System.EventArgs e)
        {
            int ret;

            // Termination
            ret = Ccappt.PtExit(m_Id);
            if (ret != 0)
            {
                ErrorHandling("PtExit", ret);
                return;
            }

            txtRet.Text = "Termination : Successed";
        }

        // Exit dialog
        private void btnOK_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }

        // Process error
        private void ErrorHandling(string Dfunction, int ret)
        {
            StringBuilder errstr = new StringBuilder(256);
            string str = new string(' ', 256);

            Ccappt.PtGetErrorString(ret, errstr);
            str = string.Format("[{0}] {1}", Dfunction, errstr);
            txtRet.Text = str;
        }

        // Display remote status
        private void ShowRemoteStatus()
        {
            byte Start, Err;
            string szText = new string(' ', 100);
            int ret;

            // Input remote status
            Start = 0;
            Err = 0;
            ret = Ccappt.PtGetRemoteStatus(m_Id, ref Start, ref Err);
            // Process error
            if (ret != 0)
            {
                ErrorHandling("PtGetRemoteStatus", ret);
                return;
            }
            // Display remote status
            szText = string.Format("{0:X2}", Start);
            lblStart.Text = szText;
            szText = string.Format("{0:X2}", Err);
            lblErr.Text = szText;
        }

        private void Pti4_Closed(object sender, System.EventArgs e)
        {
            int ret;

            // Termination
            ret = Ccappt.PtExit(m_Id);
        }
    }
}