using System; using Microsoft.SPOT; using Microsoft.SPOT.Hardware; using Microsoft.SPOT.IO; using System.Text; using System.IO; using GHI.Premium.IO; namespace miniCPF { class Logger { public enum ColumnNums : int { //After the date and time and current state goes the comment comment = 0, //Then the buoyancy control stuff pressure, velocitySetpoint, platformVelocity, bellowsPosition, y1, y2, v, u, uCPS, //ADuC Stuff batteryVoltage, batteryCurrent, current12V, current33V, //GPS Stuff lat, lon, lastColumn, } private static PersistentStorage sdCard = new PersistentStorage("SD"); private static VolumeInfo sdVol; private static FileStream logFile; private static StringBuilder fileName = new StringBuilder(256); private static StringBuilder tempSB = new StringBuilder(256); private static StringBuilder writeSB = new StringBuilder(256); private static byte[] tempQueueArray = new byte[256]; //get rid of me private static object loggerLock = new object(); private static byte[] writeArray = new byte[256]; private static DateTime timeNow; private static char token = '\t'; private static int numValues = 0; public static void init() { sdCard.MountFileSystem(); sdVol = new VolumeInfo("SD"); timeNow = DateTime.Now; fileName.Clear(); fileName.Append(sdVol.RootDirectory); fileName.Append("\\"); fileName.Append(timeNow.Year.ToString()); fileName.Append("-"); fileName.Append(timeNow.Month.ToString()); fileName.Append("-"); fileName.Append(timeNow.Day.ToString()); fileName.Append("T"); fileName.Append(timeNow.Hour.ToString()); fileName.Append("-"); fileName.Append(timeNow.Minute.ToString()); fileName.Append("-"); fileName.Append(timeNow.Second.ToString()); logFile = new FileStream(fileName.ToString(), FileMode.OpenOrCreate, FileAccess.Write); tempSB.Clear(); tempSB.Append("File Name = "); tempSB.Append(fileName.ToString()); lock (loggerLock) { write(ColumnNums.comment, tempSB); } tempSB.Clear(); tempSB.Append("Volume Total Size: "); tempSB.Append(sdVol.TotalSize.ToString()); lock (loggerLock) { write(ColumnNums.comment, tempSB); } tempSB.Clear(); tempSB.Append("Volume Free Space: "); tempSB.Append(sdVol.TotalFreeSpace.ToString()); lock (loggerLock) { write(ColumnNums.comment, tempSB); } } public static void write(params object[] values) { timeNow = DateTime.Now; numValues = values.Length; writeSB.Clear(); writeSB.Append(timeNow.ToString("MMM dd, yyyy")); writeSB.Append(token); writeSB.Append(timeNow.ToString("HH:mm:ss.fff")); writeSB.Append(token); writeSB.Append(Program.stateName[(int)Program.CPFState]); writeSB.Append(token); for (ColumnNums i = 0; i < ColumnNums.lastColumn; i++) { for (int j = 0; j < numValues - 1; j = j + 2) { if ((ColumnNums)values[j] == i) { //TOD need to do a better job formatting values to minimize length if (i == ColumnNums.comment) writeSB.Append(values[j + 1].ToString()); else writeSB.Append(((double)values[j + 1]).ToString("f6")); break; } } writeSB.Append(token); } Array.Clear(writeArray, 0, writeArray.Length); Array.Copy(UTF8Encoding.UTF8.GetBytes(writeSB.ToString()), writeArray, writeSB.Length); if(Array.IndexOf(writeArray, 0) > 0) logFile.Write(writeArray, 0, Array.IndexOf(writeArray, 0)); else logFile.Write(writeArray, 0, writeArray.Length); Debug.Print(writeSB.ToString()); } private static void printVolInfo() { tempSB.Clear(); tempSB.Append("Volume Total Size: "); tempSB.Append(sdVol.TotalSize.ToString()); lock (loggerLock) { write(ColumnNums.comment, tempSB); } tempSB.Clear(); tempSB.Append("Volume Free Space: "); tempSB.Append(sdVol.TotalFreeSpace.ToString()); lock (loggerLock) { write(ColumnNums.comment, tempSB); } //String[] fileNames = Directory.GetFiles(sdVol.RootDirectory); //for (int i = 0; i < fileNames.Length; i++) //{ // FileInfo fileInfo = new FileInfo(fileNames[i]); // Debug.Print(i.ToString() + " File name: " + fileNames[i] // + " Creation Date = " + fileInfo.CreationTime.ToString() // + " Length = " + fileInfo.Length.ToString()); //} } public static void close() { logFile.Close(); } public static void unMount() { sdCard.UnmountFileSystem(); } public static byte[] getNewLogArrayHeader(byte[] comment) { //timeNow = DateTime.Now; //Array.Clear(tempQueueArray, 0, tempQueueArray.Length); //sb.Clear(); //sb.Append(timeNow.ToString()); //// Year/Month/Day token //for (int i = 6; i < 10; i++) // tempQueueArray[i - 6] = (byte)sb[i]; //tempQueueArray[4] = (byte)'/'; //for (i = 0; i < 2; i++) // tempQueueArray[i + 5] = (byte)sb[i]; //tempQueueArray[7] = (byte)'/'; //for (i = 3; i < 5; i++) // tempQueueArray[i + 5] = (byte)sb[i]; //tempQueueArray[10] = token; //// Hour:Minute:Second token //for (i = 11; i < 19; i++) // tempQueueArray[i] = (byte)sb[i]; //tempQueueArray[19] = token; //// Add in comment ////trim off the trailing empty array elements ////Note: comment array must be (byte)characters for this to work //endOfCommentIndex = Array.IndexOf(comment, 0); //if (endOfCommentIndex >= 0) //{ // Array.Copy(comment, 0, tempQueueArray, 20, endOfCommentIndex); // tempQueueArray[20 + endOfCommentIndex] = token; // Array.Copy(Program.stateName[(int)Program.CPFState], 0, tempQueueArray, (21 + endOfCommentIndex), Program.stateName[(int)Program.CPFState].Length); //} //else //{ // Array.Copy(comment, 0, tempQueueArray, 20, comment.Length); // tempQueueArray[20 + comment.Length] = token; // Array.Copy(Program.stateName[(int)Program.CPFState], 0, tempQueueArray, (21 + comment.Length), Program.stateName[(int)Program.CPFState].Length); //} ////tempCharArray = UTF8Encoding.UTF8.GetChars(tempQueueArray); return tempQueueArray; } } }