using System; using System.Threading; using System.Text; using System.IO; using Microsoft.SPOT; using GHI.Processor; namespace CPF { public static class InstrumentHandler { private static StructQueue.qStruct qS = new StructQueue.qStruct(); private static FileStream sciFile; public static void init() { qS.byteArray = new byte[StructQueue.byteArrayWidth]; } public static void openSciFile(StringBuilder fileName) { sciFile = new FileStream(fileName.ToString() + ".sci", FileMode.OpenOrCreate, FileAccess.Write); } public static void closeSciFile() { sciFile.Close(); } private const String startMsg = "Start Sampling Instruments"; public static bool samplingInstruments = false; public static void sampleInstruments(int stationNum) { //TODO: Eventually need to sample the instruments per the mission config file EngrLogger.writeToColumns(false, startMsg); samplingInstruments = true; gotCTD = false; gotFLBB = false; gotOptode = false; firstSample = true; Optode.doSample(); FLBB.sendRunCmd(); //Don't take a PTS sample in ascend mode because SBE41 is in CP mode and already outputting a PTS sample if (Program.CurrentState != CPFStates.ascend) { SBE41.sbe41SamplePTS = true; } } private static DateTime timeNow; private static StringBuilder timeStamp = new StringBuilder(256); private static StringBuilder sbTemp = new StringBuilder(2000); private const char token = '\t'; private static readonly byte[] CRLF = new byte[] { 13, 10 }; private static readonly byte[] flbbID = UTF8Encoding.UTF8.GetBytes("99/99/99"); private static bool gotCTD = false; private static bool gotFLBB = false; private static bool gotOptode = false; private static bool firstSample = true; public static void logSciSample(StructQueue.QRecordType recordType, byte[] instrumentBytes) { if (firstSample) { logSciTimestamp(); firstSample = false; } switch (recordType) { case (StructQueue.QRecordType.SBE41): if ( (numberOfCharsInByteArray(2, ',', instrumentBytes)) || (instrumentBytes.Length == 14)) { sciFile.Write(instrumentBytes, 0, Array.IndexOf(instrumentBytes, 0) + 1); sbTemp.Clear(); sbTemp.Append(UTF8Encoding.UTF8.GetChars(instrumentBytes)); //Debug.Print(sbTemp.ToString()); gotCTD = true; } break; case (StructQueue.QRecordType.FLBB): if (Array.IndexOf(instrumentBytes, (byte)'/') >= 0) { sciFile.Write(instrumentBytes, 18, Array.IndexOf(instrumentBytes, 0) - 18); sciFile.Write(CRLF, 0, CRLF.Length); sbTemp.Clear(); sbTemp.Append(UTF8Encoding.UTF8.GetChars(instrumentBytes)); //Debug.Print(sbTemp.ToString()); gotFLBB = true; } break; case (StructQueue.QRecordType.Optode): if (Array.IndexOf(instrumentBytes, (byte)',') > 0) { sciFile.Write(instrumentBytes, 0, Array.IndexOf(instrumentBytes, 0)); sciFile.Write(CRLF, 0, CRLF.Length); sbTemp.Clear(); sbTemp.Append(UTF8Encoding.UTF8.GetChars(instrumentBytes)); //Debug.Print(sbTemp.ToString()); gotOptode = true; } break; } if (gotCTD && gotFLBB && gotOptode) { samplingInstruments = false; gotCTD = false; gotFLBB = false; gotOptode = false; firstSample = false; logSciTimestamp(); sciFile.Write(CRLF, 0, CRLF.Length); } } private static bool gotDAH = false; private static readonly byte[] dahByteArray = UTF8Encoding.UTF8.GetBytes("dah"); private static readonly byte[] uploadCompleteByteArray = UTF8Encoding.UTF8.GetBytes("upload complete"); private static StringBuilder sbdwtCPData = new StringBuilder(300*14); public static void logSciCPData(byte[] instrumentBytes) { if(GHI.Utilities.Arrays.Contains(instrumentBytes, dahByteArray) >= 0) { gotDAH = true; return; } if(gotDAH) { if (GHI.Utilities.Arrays.Contains(instrumentBytes, uploadCompleteByteArray) >= 0) { gotDAH = false; return; } else { sciFile.Write(instrumentBytes, 0, Array.IndexOf(instrumentBytes, 0) + 1); sbdwtCPData.Append(UTF8Encoding.UTF8.GetChars(instrumentBytes, 0, Array.IndexOf(instrumentBytes, 0) - 2)); sbdwtCPData.Append(','); return; } } } public static StringBuilder trimSBDWTCPData() { if( sbdwtCPData.Length > 1600) sbdwtCPData.Remove(0, sbdwtCPData.Length - 1600); sbTemp.Clear(); sbTemp.Append(",..,"); sbTemp.Append(sbdwtCPData); return (sbTemp); } public static void clearSBDWTCPData() { sbdwtCPData.Clear(); } private static StringBuilder sbSBDWTMsg = new StringBuilder(1800); private static byte[] tempByteArray = new byte[StructQueue.byteArrayWidth]; //public static StringBuilder prepSBDWTArray() //{ // int skipNum = 0; // sbSBDWTMsg.Clear(); // skipNum = (sbdwtCPArrayIndex % 120) + 1; // for (int i = 0; i < sbdwtCPArrayIndex - 1; i=i+skipNum) // { // Array.Copy(UTF8Encoding.UTF8.GetBytes(sbdwtCPArray[i].ToString()), tempByteArray, sbdwtCPArray[i].Length); // for(int j = 0; j < tempByteArray.Length; j++) // { // if( (tempByteArray[j] < 33) || (tempByteArray[j] > 126) ) // sbdwtCPArray[i].Remove(j, 1); // } // sbSBDWTMsg.Append(sbdwtCPArray[i]); // sbSBDWTMsg.Append("\r\n"); // } // return (sbSBDWTMsg); //} private static void logSciTimestamp() { timeNow = RealTimeClock.GetDateTime(); timeStamp.Clear(); timeStamp.Append(timeNow.ToString("dd-MMM-yyyy")); timeStamp.Append(token); timeStamp.Append(timeNow.ToString("HH:mm:ss")); timeStamp.Append("\r\n"); sciFile.Write(UTF8Encoding.UTF8.GetBytes(timeStamp.ToString()), 0, timeStamp.Length); //Debug.Print(timeStamp.ToString()); } private static bool numberOfCharsInByteArray(int n, char testChar, byte[] byteArray) { int numChars = 0; int index = -1; for (int i = 0; i < n; i++) { index = Array.IndexOf(byteArray, (byte)testChar, index + 1); if (index < 0) break; else numChars++; } if (numChars >= n) return true; else return false; } public static Timer instrumentTimer = new Timer(new TimerCallback(instrumentTimerCallback), null, -1, -1); private static void instrumentTimerCallback(object o) { sampleInstruments(SV.PressureTableNum); } } }