Keeping track

Next I wanted to be able to store the data somewhere, keeping a log of my temperature sensor, and future ones.

Following controller is work in progress in trying to save to an SD Card.
It’s not working yet..
Update! It works now, I should have used “\SD\” in my path. Apparently you can’t write to the root folder. Only scan for files or directories.

 public static class StorageController
 {
 public static void SaveTemperature(double value, TemperatureEnum setting)
 {
 WriteTemperatureLine(value, setting);
 }

 public static void PrintTodayLogToDebug()
 {
 Debug.Print("----");
 ReadTextFile(GetTodayFileName(), true);
 }

 public static void WriteLogLine(string line)
 {
 WriteTextFile(GetTodayLogFileName(), line);
 }

 private static string GetTodayFileName()
 {
 //TODO Doesn't work, internal Time is incorrect, fix with InternetConnectivity
 //return @"\SD\" + DateTime.Today.ToString("yyyyMMdd ") + ".log"; ;
 return "\\SD\\log.txt";
 }
 private static string GetTodayLogFileName()
 {
 //TODO Doesn't work, internal Time is still incorrect, fix with InternetConnectivity
 //return @"\SD\" + DateTime.Today.ToString("yyyyMMdd") + "_Error.log"; ;
 return "\\SD\\error.txt";
 }

 private static void WriteTemperatureLine(double value, TemperatureEnum setting)
 {
 var line = value + TemperatureController.GetTemperatureFormat(setting);
 WriteTextFile(GetTodayFileName(), line);
 }

 public static void CleanFile()
 {
 var path = GetTodayFileName();
 File.Create(path).Close();
 }

 private static void WriteTextFile(string fileName, string line)
 {
 try
 {
 var existingContent = ReadTextFile(fileName, false);

 using (var filestream = new FileStream(fileName, FileMode.Create))
 {
 var streamWriter = new StreamWriter(filestream);
 if (existingContent != null && existingContent != string.Empty) // string.IsNullOrEmpty is not available in .NET MF 4.1
 streamWriter.WriteLine(existingContent);
 streamWriter.WriteLine(line);
 streamWriter.Close();
 }
 }
 catch (Exception ex)
 {
 Debug.Print("WriteTextFile Error:" + ex.Message);
 }
 }

 private static string ReadTextFile(string fileName, bool readoutToDebugWindow)
 {
 var content = string.Empty;
 try
 {
 if (!File.Exists(fileName)) return string.Empty;
 using (var reader = new StreamReader(fileName))
 {
 content = reader.ReadToEnd();
 reader.Close();
 }
 }
 catch (Exception ex)
 {
 Debug.Print("ReadTextFile Error:" + ex.Message);
 }

 return TrimOutDoubleNewLines(content, readoutToDebugWindow);
 }

 private static string TrimOutDoubleNewLines(string content, bool readoutToDebugWindow)
 {
 var sb = new StringBuilder(content);
 sb.Replace("\r\n\r\n", "\n");
 var returnString = sb.ToString();
 if (readoutToDebugWindow) Debug.Print(returnString);
 return returnString;
 }
 }
}

I have got the device plugged into a normal USB charger now and I’m going to let it log throughout the night and post the results tomorrow.
Results (interval is 30 min):

25° C
24.125° C
24.125° C
24.1875° C
24.0625° C
24.0625° C
23.5625° C
23.125° C
22.8125° C
22.5625° C
22.375° C
22.1875° C
22.125° C
22.0625° C
21.875° C
21.875° C
21.75° C
21.625° C
21.625° C
21.625° C
21.5625° C

Advertisements

Care to comment?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s