/***************************************************************************
 * Copyright (c) 2009 Digi International Inc., All Rights Reserved
 * 
 * This software contains proprietary and confidential information of Digi
 * International Inc.  By accepting transfer of this copy, Recipient agrees
 * to retain this software in confidence, to prevent disclosure to others,
 * and to make no use of this software other than that for which it was
 * delivered.  This is an unpublished copyrighted work of Digi International
 * Inc.  Except as permitted by federal law, 17 USC 117, copying is strictly
 * prohibited.
 * 
 * Restricted Rights Legend
 *
 * Use, duplication, or disclosure by the Government is subject to
 * restrictions set forth in sub-paragraph (c)(1)(ii) of The Rights in
 * Technical Data and Computer Software clause at DFARS 252.227-7031 or
 * subparagraphs (c)(1) and (2) of the Commercial Computer Software -
 * Restricted Rights at 48 CFR 52.227-19, as applicable.
 *
 * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
 *
 ***************************************************************************
 *
 * @Descr: WatchDog application
 *
 * @History:
 *	01/10/07:	aramos      initial version(using timers)
 *  01/11/07:   aramos      using thread with priority
 *  05/29/08:   aramos      Added Device Class and support to ConnectCore 9M platform
 *  09/03/08:   aramos      Removed Device Class
 *  08/12/09:   aramos      Added Digi C# API
 * 
 ***************************************************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Digi.Drivers;

namespace Test_WatchDog
{
    public partial class WatchDog_Main : Form
    {
        public WatchDog_Main()
        {
            InitializeComponent();
        }

        //Global variables
        WatchDog hWatchDog;
        bool bRefresh = true;
        bool bClosed = false;
        uint PeriodWD;
        uint PeriodRf;
        uint DfltAction;

        private void Form1_Load(object sender, EventArgs e)
        {
            //set default values
            cb_DfltAction.SelectedIndex = (int)WatchDog.WatchDogAction.NoDefaultAction;
            cb_Priority.SelectedIndex = (int)System.Threading.ThreadPriority.Normal;
        }

        private void b_test_Click(object sender, EventArgs e)
        {
            if (nud_WDPeriod.Value - nud_RefPeriod.Value < 0) MessageBox.Show("Refresh Period can't be bigger than WatchDog Period", "Test WatchDog");
            else if (nud_WDPeriod.Value - nud_RefPeriod.Value < 200) MessageBox.Show("Test result can be unexpected.\nTime margin to small(<200ms) ", "Test WatchDog");
            else
            {
                nud_RefPeriod.Enabled = false;
                nud_WDPeriod.Enabled = false;
                cb_DfltAction.Enabled = false;
                cb_Priority.Enabled = false;
                b_test.Enabled = false;
                //Thread
                System.Threading.ThreadStart RefreshThread = new System.Threading.ThreadStart(WatchDog_Refresh_Thread);
                System.Threading.Thread ThreadWatchDog = new System.Threading.Thread(RefreshThread);
                switch (cb_Priority.SelectedIndex)
                {
                    case 0: ThreadWatchDog.Priority = System.Threading.ThreadPriority.Highest;
                        break;
                    case 1: ThreadWatchDog.Priority = System.Threading.ThreadPriority.AboveNormal;
                        break;
                    case 2: ThreadWatchDog.Priority = System.Threading.ThreadPriority.Normal;
                        break;
                    case 3: ThreadWatchDog.Priority = System.Threading.ThreadPriority.BelowNormal;
                        break;
                    case 4: ThreadWatchDog.Priority = System.Threading.ThreadPriority.Lowest;
                        break;
                }
                PeriodWD = (uint)nud_WDPeriod.Value;
                PeriodRf = (uint)nud_RefPeriod.Value;
                DfltAction = (uint)cb_DfltAction.SelectedIndex;
                ThreadWatchDog.Start();
            }
        }

        public void WatchDog_Refresh_Thread()
        {
            hWatchDog = new WatchDog(PeriodWD, DfltAction);
           if (!hWatchDog.Start()) MessageBox.Show("StartWatchDogTimer Failed", "Test WatchDog"); 
            while (!bClosed)
            {
                if (bRefresh) hWatchDog.Refresh();
                System.Threading.Thread.Sleep((int)PeriodRf);
            }
        }

        private void cb_EnRefresh_CheckStateChanged(object sender, EventArgs e)
        {
            bRefresh = !bRefresh;
        }

        private void Form1_Closing(object sender, CancelEventArgs e)
        {
            bClosed = true;
        }

        
    }
}