/* ---------------------------------------------------------------------------
 * Master.cs                                           (c) 2009 Micro-key bv
 * ---------------------------------------------------------------------------
 * Micro-key bv
 * Industrieweg 28, 9804 TG Noordhorn
 * Postbus 92, 9800 AB Zuidhorn
 * The Netherlands
 * Tel: +31 594 503020
 * Fax: +31 594 505825
 * Email: support@microkey.nl  
 * Web: www.microkey.nl
 * ---------------------------------------------------------------------------
 * Description: This file handles all Actions of the "Test" Group
 * ---------------------------------------------------------------------------
 * Version(s):  0.1, Jan 08, 2009, MMi
 *                   Creation.
 * ---------------------------------------------------------------------------
 */

/* ---------------------------------------------------------------------------
 * System use files
 * ---------------------------------------------------------------------------
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.Ports;
using System.Threading;


/* ---------------------------------------------------------------------------
 * Local function definitions
 * ---------------------------------------------------------------------------
 */

namespace QUA_2475_designtest
{
    public partial class mainWindow
    {


        private void T_MasterInput_TextChanged(object sender, EventArgs e)
        {
            /* Copy input text from textbox to string                       */
            MasterInputString = T_MasterInput.Text;
        }
        private void T_MasterInput_KeyDown(object sender, KeyEventArgs e)
        {
            /* if ENTER is pressed, then                                    */
            if (e.KeyCode == Keys.Enter)
            {
                /* Attach CR to String and send it to Device                */
                com1.Write(MasterInputString + "\r");
                T_MasterInput.Text = "";    /* reset TextBox                */
            }
        }
        private void B_master_send_Click(object sender, EventArgs e)
        {
            /* Attach CR to String and send it to Device                    */
            com1.Write(MasterInputString + "\r");
            T_MasterInput.Text = "";        /* reset Textbox                */
        }


        delegate void CheckForSlaveKeywordCallback(string text);
        private void CheckForSlaveKeyword(string text)
        {

            /* Check if calling function is in same Thread as T_SlaveOutput */
            if (this.T_MasterOutput.InvokeRequired)
            {
                /* Calling Function is in different Thread
                 * Handle Parameter to Slave Thread                         
                 */
                CheckForSlaveKeywordCallback d = new CheckForSlaveKeywordCallback(CheckForSlaveKeyword);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                /* Calling Function is in same Thread                       */

                /* Search for Text fragments in Input String                */
                if (text.LastIndexOf("test: 	PASSED") != -1)
                {
                    /* "test: 	PASSED" indecates that Device successfully 
                     * finished a Test Script or Routine 
                     */
                    activeTest = false;
                    TestResult[actualTest] = true;
                    TestIsPassed(actualTest);
                }
                if (text.LastIndexOf("test: 	 FAILED") != -1)
                {
                    /* "test: 	 FAILED" indecates that Device finished
                     * a Test Script or Routine with Errors 
                     */
                    activeTest = false;
                    TestResult[actualTest] = false;
                    TestIsFailed(actualTest);
                }
            }
        }
        
        
        
        delegate void AddSlaveTextCallback(string text);
        private void AddSlaveText(string text)
        {
            /* Check if calling function is in same Thread as T_SlaveOutput */
            if (this.T_MasterOutput.InvokeRequired)
            {
                /* Calling Function is in different Thread
                 * Handle Parameter to Slave Thread                         
                 */
                AddSlaveTextCallback d = new AddSlaveTextCallback(AddSlaveText);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                /* Calling Function is in same Thread                       */
                /* Attach incoming Text to T_SlaveOutput and scroll
                 * to bottom End of TextBox
                 */

                text = text.Replace("\n\r", Environment.NewLine);

                T_MasterOutput.Text += text;
                T_MasterOutput.Select(T_MasterOutput.Text.Length, 0);
                T_MasterOutput.ScrollToCaret();
            }
        }

        private void com1_DataReceived()
        {
            /* This function runs in an own Thread. 
             * Slave_receiveMessages is type boolean and initialised as
             * TRUE. With setting it to FALSE, the Stream Input can be 
             * stopped
             */
            while (Master_receiveMessages)
            {
                /* Read out length of current Input Stream                  */
                int nrOfBytes;
                nrOfBytes = com1.BytesToRead;
                /* as long as there is active Input Stream                  */
                while (nrOfBytes > 0)
                {
                    /* Attach every incoming CHAR to char-Array             */
                    char[] buffer = new char[nrOfBytes];
                    com1.Read(buffer, 0, nrOfBytes);
                    /* Send Array to TextBox handle                         */
                    this.AddSlaveText(new string(buffer));
                    /* If a test is active, then                            */
                    if (activeTest == true)
                    {
                        /* Copy all incoming Data to TestMessage of
                         * corresponding Test and send the String afterwards
                         * to CehckForKeyword to find specific Keyword
                         */
                        CheckForSlaveKeyword(new string(buffer));
                        //TestMessage[actualTest] += new string(buffer);
                        //CheckForSlaveKeyword(TestMessage[actualTest]);
                    }
                    nrOfBytes = com1.BytesToRead;   /* Actualise Inputlength*/
                }
                /* Set Thread to Sleep as long as possible (managed by System)
                 * to give parallel Threads calculation Time
                 */
                Thread.Sleep(10);
            }
        }
    }
}
