using System;
using Microsoft.SPOT;

using Microsoft.VisualStudio.TestTools.UnitTesting;
using CPF;

using System.Text;
using System.Threading;
using HWModules;
using SWModules;

namespace CPFUnitTests
{
    class CTDUnitTests
    {
        [TestMethod]
        public void TestFP()
        {
            Init();

            for (int i = 0; i < 20; i++)
            {
                ctd1.sendCRLF();
                Debug.Print("Sleeping: " + i/2 * 100);
                Thread.Sleep(i/2 * 100);
                messageQ.processQMaxLimit();
                ctd1.sendFastPressure();
                for (int j = 0; j < 30; j++)
                {
                    messageQ.processQMaxLimit();
                    Thread.Sleep(100);
                }

            }

        }

        [TestMethod]
        public void TestCTD()
        {
            Init();

            ctd1.sendCRLF();
            for (int i = 0; i < 3; i++)
            {
                messageQ.processQ();
                Thread.Sleep(1000);
            }

            for (int i = 0; i < 3; i++)
            {
                ctd1.sendFastPressure();
                for (int j = 0; j < 5; j++)
                {
                    messageQ.processQ();
                    Thread.Sleep(1000);
                }

            }

            ctd1.sendDC();
            for (int j = 0; j < 5000; j++)
            {
                messageQ.processQ();
                Thread.Sleep(1);
            }

            ctd1.sendDS();
            for (int j = 0; j < 5000; j++)
            {
                messageQ.processQ();
                Thread.Sleep(1);
            }

            ctd1.sendGetHD();
            for (int j = 0; j < 5000; j++)
            {
                messageQ.processQ();
                Thread.Sleep(1);
            }

            ctd1.sendGetCD();
            for (int j = 0; j < 5000; j++)
            {
                messageQ.processQ();
                Thread.Sleep(1);
            }

            for (int j = 0; j < 5000; j++)
            {
                messageQ.processQ();
                Thread.Sleep(1);
            }

            //ctd1.sendPumpon();
            //for (int i = 0; i < 5; i++)
            //{
            //    messageQ.processQ();
            //    Thread.Sleep(1000);
            //}

            //ctd1.sendPumpoff();
            //for (int i = 0; i < 10; i++)
            //{
            //    messageQ.processQ();
            //    Thread.Sleep(1000);
            //}
        }

        [TestMethod]
        public void TestStuckPressure()
        {
            Init();

            Debug.Print("Send Sim Pressures, check stuck pressure count and power cycle count");
            double simPressure = 0.0;
            byte[] pressureBytes = new byte[16];
            QRecord qRecord = new QRecord();
            qRecord.byteArray = new byte[16];
;
            for (int i = 0; i < 200; i++)
            {
                simPressure = simPressure +  0.01;
                if (simPressure > 0.5)
                    simPressure = 1.23;

                Array.Clear(pressureBytes, 0, pressureBytes.Length);
                pressureBytes = UTF8Encoding.UTF8.GetBytes(simPressure.ToString("d2"));

                Array.Copy(pressureBytes, qRecord.byteArray, 4);
                qRecord.errorCode = 0;
                qRecord.state = CPFStates.SMNotRunning;
                qRecord.timeStamp = DateTime.Now;
                ctd1.processMessage(qRecord);
                Debug.Print(i.ToString() + "\tPressure:\t" + simPressure.ToString("d3") + "\tStuck pressure count:\t" + ctd1.getStuckPressureCount().ToString() + "\tPower cycle count:\t" + ctd1.powerCycleCount.ToString());

                Thread.Sleep(100);
            }
        }

        private bool initialized;
        private CTD1 ctd1;
        private MessageQueue messageQ;
        private void Init()
        {
            if (initialized) return;

            HWModules.MotherBoard motherboard = HWModules.MotherBoard.Instance;

            ctd1 = CTD1.Instance;

            messageQ = MessageQueue.Instance;

            motherboard.EnableChannelPower((int)HWModules.MotherBoard.ChannelNames.CTDChannel);

            Debug.Print("Initialize CTD");
            ctd1.init();
            for (int i = 0; i < 10; i++)
            {
                messageQ.processQ();
                Thread.Sleep(1000);
            }

            initialized = true;

        }
    }

}
