using System; using System.Collections; using System.IO; using System.Text; using GHI.Processor; namespace SWModules { public static class SciLogger { private static Queue FileQ = new Queue(); private static FileStream sciFile; public static bool samplingInstruments = false; public static void openSciFile(StringBuilder fileName) { sciFile = new FileStream(fileName.ToString() + ".sci", FileMode.OpenOrCreate, FileAccess.Write); } public static void closeSciFile() { // Add filename to transfer buffer FileQ.Enqueue(sciFile.Name); // TODO P2 limit queue size sciFile.Close(); } 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"); public static bool gotCTD = false; public static bool gotFLBB = false; public static bool gotOptode = false; public static bool firstSample = true; //TODO P2 Reorganize sci file so it looks like APEX .msg file public static void logSciSample(QRecordType recordType, byte[] instrumentBytes) { if (firstSample) { logSciTimestamp(); firstSample = false; } switch (recordType) { case (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 (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 (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) { SciLogger.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 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 void startNewSampleCycle() { SciLogger.gotCTD = false; SciLogger.gotFLBB = false; SciLogger.gotOptode = false; SciLogger.firstSample = true; } } }