using System;
using Microsoft.SPOT;

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

using System.Text;
using System.Threading;
using HWModules;
using SWModules;
using Microsoft.SPOT.Hardware;

namespace CPFUnitTests
{
    class PressureNoiseUnitTest
    {
        [TestMethod]
        public void TestNoise()
        {
            HWModules.MotherBoard motherboard = HWModules.MotherBoard.Instance;
            motherboard.EnableChannelPower(MotherBoard.ChannelNames.CTDChannel);  //Slot 1 channel A
            motherboard.EnableChannelPower(MotherBoard.ChannelNames.PositionSensorChannel); //Brent's version 0 digital IO board J4: position sensor\
            
            var lheBoard = HWModules.LheBoard.Instance;
            lheBoard.EnablePumpPower(true);

            CTD1 ctd1 = CTD1.Instance;
            motherboard.EnableChannelPower((int)HWModules.MotherBoard.ChannelNames.CTDChannel);

            MessageQueue messageQ = MessageQueue.Instance;

            BuoyancyEngine buoyancyEngine = BuoyancyEngine.Instance;
            buoyancyEngine.init();

            double innerBellowsPosition = double.NaN;

            EnergyMonitorPump emPump = EnergyMonitorPump.Instance;
            EnergyMonitorLTC2946.EM_VandI emData;

            StringBuilder sbTemp = new StringBuilder(128);
            StringBuilder tab = new StringBuilder("\t");
            StringBuilder semiColon = new StringBuilder("\t");

            sbTemp.Clear();
            sbTemp.Append("Pressure Noise UT");
            EngrLogger.init(sbTemp);
 
            ctd1.init();
            Thread.Sleep(1000);
            ctd1.sendCRLF();
            for (int i = 0; i < 3; i++)
            {
                messageQ.processQ();
                Thread.Sleep(1000);
            }

            TimeSpan startTime = new TimeSpan();
            startTime = Utility.GetMachineTime();

            double minPressure = 0.0;
            double maxPressure = 0.0;
            
            while (true)
            {
                ctd1.sendFastPressure();
                
                innerBellowsPosition = buoyancyEngine.getBellowsPositionStringPot();
                emData = emPump.getVoltsAndMilliAmps();

                for (int i = 0; i < 6; i++)
                {
                    EngrLogger.writeToColumns("Noise message abcdefghijklmnopqrstuvwxyz0123456789 abcdefghijklmnopqrstuvwxyz0123456789 abcdefghijklmnopqrstuvwxyz0123456789");
                    Thread.Sleep(500);
                }
                messageQ.processQ();

                if (SV.Pressure > maxPressure)
                    maxPressure = SV.Pressure;
                if (SV.Pressure < minPressure)
                    minPressure = SV.Pressure;

                sbTemp.Clear();
                sbTemp.Append("Pressure = ");
                sbTemp.Append(SV.Pressure.ToString("f2"));
                sbTemp.Append("   Min pressure = ");
                sbTemp.Append(minPressure.ToString("f2"));
                sbTemp.Append("   Max pressure = ");
                sbTemp.Append(maxPressure.ToString("f2"));

                EngrLogger.writeToColumns(  EngrLiterals.ColumnNums.comment, sbTemp,
                                            EngrLiterals.ColumnNums.pressure, SV.Pressure,
                                            EngrLiterals.ColumnNums.bellowsPosition, innerBellowsPosition,
                                            EngrLiterals.ColumnNums.pumpVolts, emData.volts,
                                            EngrLiterals.ColumnNums.pumpMilliamps, emData.amps);

              


            }
        }
    }
}
