Mostly async changes for VOID_DataLogger.
[VOID.git] / VOID_DataLogger.cs
blob:a/VOID_DataLogger.cs -> blob:b/VOID_DataLogger.cs
// VOID // VOID
// //
// VOID_DataLogger.cs // VOID_DataLogger.cs
// //
// Copyright © 2014, toadicus // Copyright © 2014, toadicus
// All rights reserved. // All rights reserved.
// //
// Redistribution and use in source and binary forms, with or without modification, // Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met: // are permitted provided that the following conditions are met:
// //
// 1. Redistributions of source code must retain the above copyright notice, // 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer. // this list of conditions and the following disclaimer.
// //
// 2. Redistributions in binary form must reproduce the above copyright notice, // 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation and/or other // this list of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution. // materials provided with the distribution.
// //
// 3. Neither the name of the copyright holder nor the names of its contributors may be used // 3. Neither the name of the copyright holder nor the names of its contributors may be used
// to endorse or promote products derived from this software without specific prior written permission. // to endorse or promote products derived from this software without specific prior written permission.
// //
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   
using KSP; using KSP;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
  using System.IO;
using System.Text; using System.Text;
using ToadicusTools; using ToadicusTools;
using UnityEngine; using UnityEngine;
   
namespace VOID namespace VOID
{ {
public class VOID_DataLogger : VOID_WindowModule, IVOID_BehaviorModule public class VOID_DataLogger : VOID_WindowModule, IVOID_BehaviorModule
{ {
/* /*
* Fields * Fields
* */ * */
protected bool stopwatch1_running; protected bool stopwatch1_running;
   
protected bool csv_logging; protected bool _loggingActive;
protected bool first_write; protected bool first_write;
   
protected double stopwatch1; protected double stopwatch1;
   
protected string csv_log_interval_str; protected string csv_log_interval_str;
   
protected float csv_log_interval; protected float csv_log_interval;
   
protected double csvWriteTimer;  
protected double csvCollectTimer; protected double csvCollectTimer;
   
protected System.Text.UTF8Encoding enc; protected System.Text.UTF8Encoding utf8Encoding;
  protected FileStream _outputFile;
   
protected List<string> csvList = new List<string>(); protected List<string> csvList = new List<string>();
   
/* /*
* Properties * Properties
* */ * */
  // TODO: Add configurable or incremental file names.
  protected bool loggingActive
  {
  get
  {
  return this._loggingActive;
  }
  set
  {
  if (value != this._loggingActive)
  {
  if (value)
  {
   
  }
  else
  {
  if (this._outputFile != null)
  {
  Tools.DebugLogger logger = Tools.DebugLogger.New(this);
   
  logger.Append("CSV logging disabled, ");
   
  logger.Append("disposing file.");
  logger.Print();
  this.outputFile.Dispose();
  this._outputFile = null;
  }
  }
   
  this._loggingActive = value;
  }
  }
  }
  protected string fileName
  {
  get
  {
  return KSP.IO.IOUtils.GetFilePathFor(
  typeof(VOID_Core),
  string.Format(
  "{0}_{1}",
  this.vessel.vesselName,
  "data.csv"
  ),
  null
  );
  }
  }
   
  protected FileStream outputFile
  {
  get
  {
  if (this._outputFile == null)
  {
  Tools.DebugLogger logger = Tools.DebugLogger.New(this);
  logger.AppendFormat("Initializing output file '{0}' with mode ", this.fileName);
   
  if (File.Exists(this.fileName))
  {
  logger.Append("append");
  this._outputFile = new FileStream(this.fileName, FileMode.Append, FileAccess.Write, FileShare.Write, 512, true);
  }
  else
  {
  logger.Append("create");
  this._outputFile = new FileStream(this.fileName, FileMode.Create, FileAccess.Write, FileShare.Write, 512, true);
   
  byte[] bom = utf8Encoding.GetPreamble();
   
  logger.Append(" and writing preamble");
  outputFile.Write(bom, 0, bom.Length);
  }
   
  logger.Append('.');
  logger.Print();
  }
   
  return this._outputFile;
  }
  }
   
/* /*
* Methods * Methods
* */ * */
public VOID_DataLogger() public VOID_DataLogger()
{ {
this._Name = "CSV Data Logger"; this._Name = "CSV Data Logger";
   
this.stopwatch1_running = false; this.stopwatch1_running = false;
   
this.csv_logging = false; this.loggingActive = false;
this.first_write = true; this.first_write = true;
   
this.stopwatch1 = 0; this.stopwatch1 = 0;
this.csv_log_interval_str = "0.5"; this.csv_log_interval_str = "0.5";
   
this.csvWriteTimer = 0;  
this.csvCollectTimer = 0; this.csvCollectTimer = 0;
   
this.WindowPos.x = Screen.width - 520; this.WindowPos.x = Screen.width - 520;
this.WindowPos.y = 85; this.WindowPos.y = 85;
} }
   
public override void ModuleWindow(int _) public override void ModuleWindow(int _)
{ {
GUIStyle txt_white = new GUIStyle(GUI.skin.label); GUIStyle txt_white = new GUIStyle(GUI.skin.label);
txt_white.normal.textColor = txt_white.focused.textColor = Color.white; txt_white.normal.textColor = txt_white.focused.textColor = Color.white;
txt_white.alignment = TextAnchor.UpperRight; txt_white.alignment = TextAnchor.UpperRight;
GUIStyle txt_green = new GUIStyle(GUI.skin.label); GUIStyle txt_green = new GUIStyle(GUI.skin.label);
txt_green.normal.textColor = txt_green.focused.textColor = Color.green; txt_green.normal.textColor = txt_green.focused.textColor = Color.green;
txt_green.alignment = TextAnchor.UpperRight; txt_green.alignment = TextAnchor.UpperRight;
GUIStyle txt_yellow = new GUIStyle(GUI.skin.label); GUIStyle txt_yellow = new GUIStyle(GUI.skin.label);
txt_yellow.normal.textColor = txt_yellow.focused.textColor = Color.yellow; txt_yellow.normal.textColor = txt_yellow.focused.textColor = Color.yellow;
txt_yellow.alignment = TextAnchor.UpperRight; txt_yellow.alignment = TextAnchor.UpperRight;
   
GUILayout.BeginVertical(); GUILayout.BeginVertical();
   
GUILayout.Label("System time: " + DateTime.Now.ToString("HH:mm:ss")); GUILayout.Label("System time: " + DateTime.Now.ToString("HH:mm:ss"));
GUILayout.Label(VOID_Tools.ConvertInterval(stopwatch1)); GUILayout.Label(VOID_Tools.ConvertInterval(stopwatch1));
   
GUILayout.BeginHorizontal(GUILayout.ExpandWidth(true)); GUILayout.BeginHorizontal(GUILayout.ExpandWidth(true));
if (GUILayout.Button("Start")) if (GUILayout.Button("Start"))
{ {
if (stopwatch1_running == false) stopwatch1_running = true; if (stopwatch1_running == false) stopwatch1_running = true;
} }
if (GUILayout.Button("Stop")) if (GUILayout.Button("Stop"))
{ {
if (stopwatch1_running == true) stopwatch1_running = false; if (stopwatch1_running == true) stopwatch1_running = false;
} }
if (GUILayout.Button("Reset")) if (GUILayout.Button("Reset"))
{ {
if (stopwatch1_running == true) stopwatch1_running = false; if (stopwatch1_running == true) stopwatch1_running = false;
stopwatch1 = 0; stopwatch1 = 0;
} }
GUILayout.EndHorizontal(); GUILayout.EndHorizontal();
   
GUIStyle label_style = txt_white; GUIStyle label_style = txt_white;
string log_label = "Inactive"; string log_label = "Inactive";
if (csv_logging && vessel.situation.ToString() == "PRELAUNCH") if (loggingActive && vessel.situation.ToString() == "PRELAUNCH")
{ {
log_label = "Awaiting launch"; log_label = "Awaiting launch";
label_style = txt_yellow; label_style = txt_yellow;
} }
if (csv_logging && vessel.situation.ToString() != "PRELAUNCH") if (loggingActive && vessel.situation.ToString() != "PRELAUNCH")
{ {
log_label = "Active"; log_label = "Active";
label_style = txt_green; label_style = txt_green;
} }
GUILayout.BeginHorizontal(GUILayout.ExpandWidth(true)); GUILayout.BeginHorizontal(GUILayout.ExpandWidth(true));
csv_logging = GUILayout.Toggle(csv_logging, "Data logging: ", GUILayout.ExpandWidth(false)); this.loggingActive = GUILayout.Toggle(loggingActive, "Data logging: ", GUILayout.ExpandWidth(false));
   
GUILayout.Label(log_label, label_style, GUILayout.ExpandWidth(true)); GUILayout.Label(log_label, label_style, GUILayout.ExpandWidth(true));
GUILayout.EndHorizontal(); GUILayout.EndHorizontal();
   
GUILayout.BeginHorizontal(GUILayout.ExpandWidth(true)); GUILayout.BeginHorizontal(GUILayout.ExpandWidth(true));
GUILayout.Label("Interval: ", GUILayout.ExpandWidth(false)); GUILayout.Label("Interval: ", GUILayout.ExpandWidth(false));
csv_log_interval_str = GUILayout.TextField(csv_log_interval_str, GUILayout.ExpandWidth(true)); csv_log_interval_str = GUILayout.TextField(csv_log_interval_str, GUILayout.ExpandWidth(true));
GUILayout.Label("s", GUILayout.ExpandWidth(false)); GUILayout.Label("s", GUILayout.ExpandWidth(false));
GUILayout.EndHorizontal(); GUILayout.EndHorizontal();
   
float new_log_interval; float new_log_interval;
if (float.TryParse(csv_log_interval_str, out new_log_interval)) if (float.TryParse(csv_log_interval_str, out new_log_interval))
{ {
csv_log_interval = new_log_interval; csv_log_interval = new_log_interval;
} }
   
GUILayout.EndVertical(); GUILayout.EndVertical();
GUI.DragWindow(); GUI.DragWindow();
} }
   
public void Update() public void Update()
{ {
// CSV Logging // CSV Logging
// from ISA MapSat // from ISA MapSat
if (csv_logging) if (loggingActive)
{ {
//data logging is on //data logging is on
//increment timers //increment timers
csvWriteTimer += Time.deltaTime;  
csvCollectTimer += Time.deltaTime; csvCollectTimer += Time.deltaTime;
   
if (csvCollectTimer >= csv_log_interval && vessel.situation != Vessel.Situations.PRELAUNCH) if (csvCollectTimer >= csv_log_interval && vessel.situation != Vessel.Situations.PRELAUNCH)
{ {
//data logging is on, vessel is not prelaunch, and interval has passed //data logging is on, vessel is not prelaunch, and interval has passed
//write a line to the list //write a line to the list
line_to_csvList(); //write to the csv line_to_csvList(); //write to the csv
} }
   
if (csvList.Count != 0 && csvWriteTimer >= 15f) if (csvList.Count > 0)
{ {
// csvList is not empty and interval between writings to file has elapsed // csvList is not empty and interval between writings to file has elapsed
//write it //write it
string[] csvData;  
csvData = (string[])csvList.ToArray(); // Tools.PostDebugMessage("")
Innsewerants_writeData(csvData);  
csvList.Clear(); this.AsyncWriteData();
csvWriteTimer = 0f;  
} }
} }
else else
{ {
//data logging is off //data logging is off
//reset any timers and clear anything from csvList //reset any timers and clear anything from csvList
csvWriteTimer = 0f;  
csvCollectTimer = 0f; csvCollectTimer = 0f;
if (csvList.Count > 0) csvList.Clear(); if (csvList.Count > 0) csvList.Clear();
} }
   
if (stopwatch1_running) if (stopwatch1_running)
{ {
stopwatch1 += Time.deltaTime; stopwatch1 += Time.deltaTime;
} }
} }
   
public void FixedUpdate() {} public void FixedUpdate() {}
   
private void Innsewerants_writeData(string[] csvArray) public void OnDestroy()
{ {
if (this.enc == null) Tools.DebugLogger logger = Tools.DebugLogger.New(this);
{  
this.enc = new System.Text.UTF8Encoding(true, true); logger.Append("Destroying...");
}  
  if (this.csvList.Count > 0)
KSP.IO.FileStream bFile; {
  logger.Append(" Writing final data...");
if (KSP.IO.File.Exists<VOID_Core>(vessel.vesselName + "_data.csv", null)) this.AsyncWriteData();
{ }
bFile = KSP.IO.File.Open<VOID_Core>(vessel.vesselName + "_data.csv", KSP.IO.FileMode.Append, null);  
} if (this._outputFile != null)
else {
{ logger.Append(" Closing File...");
bFile = KSP.IO.File.Open<VOID_Core>(vessel.vesselName + "_data.csv", KSP.IO.FileMode.Create, null); this.outputFile.Close();
  }
byte[] bom = enc.GetPreamble();  
  logger.Append(" Done.");
bFile.Write(bom, 0, bom.Length); logger.Print();
} }
   
foreach (string line in csvArray) protected void AsyncWriteCallback(IAsyncResult result)
{ {
byte[] lineBytes = enc.GetBytes(line); Tools.PostDebugMessage(this, "Got async callback, IsCompleted = {0}", result.IsCompleted);
   
bFile.Write(lineBytes, 0, lineBytes.Length); this.outputFile.EndWrite(result);
} }
   
bFile.Dispose(); private void AsyncWriteData()
  {
  if (this.utf8Encoding == null)
  {
  this.utf8Encoding = new System.Text.UTF8Encoding(true, true);
  }
   
  List<byte> bytes = new List<byte>();
   
  foreach (string line in this.csvList)
  {
  byte[] lineBytes = utf8Encoding.GetBytes(line);
  bytes.AddRange(lineBytes);
  }
   
  WriteState state = new WriteState();
   
  state.bytes = bytes.ToArray();
   
  var writeCallback = new AsyncCallback(this.AsyncWriteCallback);
   
  this.outputFile.BeginWrite(state.bytes, 0, state.bytes.Length, writeCallback, state);
   
  this.csvList.Clear();
} }
   
private void line_to_csvList() private void line_to_csvList()
{ {
//called if logging is on and interval has passed //called if logging is on and interval has passed
//writes one line to the csvList //writes one line to the csvList
   
StringBuilder line = new StringBuilder(); StringBuilder line = new StringBuilder();
   
if (first_write) if (first_write)
{ {
first_write = false; first_write = false;
line.Append( line.Append(
"\"Mission Elapsed Time (s)\t\"," + "\"Mission Elapsed Time (s)\t\"," +
"\"Altitude ASL (m)\"," + "\"Altitude ASL (m)\"," +
"\"Altitude above terrain (m)\"," + "\"Altitude above terrain (m)\"," +
"\"Surface Latitude (°)\"," + "\"Surface Latitude (°)\"," +
"\"Surface Longitude (°)\"," + "\"Surface Longitude (°)\"," +
"\"Orbital Velocity (m/s)\"," + "\"Orbital Velocity (m/s)\"," +
"\"Surface Velocity (m/s)\"," + "\"Surface Velocity (m/s)\"," +
"\"Vertical Speed (m/s)\"," + "\"Vertical Speed (m/s)\"," +
"\"Horizontal Speed (m/s)\"," + "\"Horizontal Speed (m/s)\"," +
"\"Gee Force (gees)\"," + "\"Gee Force (gees)\"," +
"\"Temperature (°C)\"," + "\"Temperature (°C)\"," +
"\"Gravity (m/s²)\"," + "\"Gravity (m/s²)\"," +
"\"Atmosphere Density (g/m³)\"," + "\"Atmosphere Density (g/m³)\"," +
"\"Downrange Distance (m)\"," + "\"Downrange Distance (m)\"," +
"\n" "\n"
); );
} }
   
//Mission time //Mission time
line.Append(vessel.missionTime.ToString("F3")); line.Append(vessel.missionTime.ToString("F3"));
line.Append(','); line.Append(',');
   
//Altitude ASL //Altitude ASL
line.Append(vessel.orbit.altitude.ToString("F3")); line.Append(vessel.orbit.altitude.ToString("F3"));
line.Append(','); line.Append(',');
   
//Altitude (true) //Altitude (true)
double alt_true = vessel.orbit.altitude - vessel.terrainAltitude; double alt_true = vessel.orbit.altitude - vessel.terrainAltitude;
if (vessel.terrainAltitude < 0) alt_true = vessel.orbit.altitude; if (vessel.terrainAltitude < 0) alt_true = vessel.orbit.altitude;
line.Append(alt_true.ToString("F3")); line.Append(alt_true.ToString("F3"));
line.Append(','); line.Append(',');
   
// Surface Latitude // Surface Latitude
line.Append('"'); line.Append('"');
line.Append(VOID_Data.surfLatitude.Value); line.Append(VOID_Data.surfLatitude.Value);
line.Append('"'); line.Append('"');
line.Append(','); line.Append(',');
   
// Surface Longitude // Surface Longitude
line.Append('"'); line.Append('"');
line.Append(VOID_Data.surfLongitude.Value); line.Append(VOID_Data.surfLongitude.Value);
line.Append('"'); line.Append('"');
line.Append(','); line.Append(',');
   
//Orbital velocity //Orbital velocity
line.Append(vessel.orbit.vel.magnitude.ToString("F3")); line.Append(vessel.orbit.vel.magnitude.ToString("F3"));
line.Append(','); line.Append(',');
   
//surface velocity //surface velocity
line.Append(vessel.srf_velocity.magnitude.ToString("F3")); line.Append(vessel.srf_velocity.magnitude.ToString("F3"));
line.Append(','); line.Append(',');
   
//vertical speed //vertical speed
line.Append(vessel.verticalSpeed.ToString("F3")); line.Append(vessel.verticalSpeed.ToString("F3"));
line.Append(','); line.Append(',');
   
//horizontal speed //horizontal speed
line.Append(vessel.horizontalSrfSpeed.ToString("F3")); line.Append(vessel.horizontalSrfSpeed.ToString("F3"));
line.Append(','); line.Append(',');
   
//gee force //gee force
line.Append(vessel.geeForce.ToString("F3")); line.Append(vessel.geeForce.ToString("F3"));
line.Append(','); line.Append(',');
   
//temperature //temperature
line.Append(vessel.flightIntegrator.getExternalTemperature().ToString("F2")); line.Append(vessel.flightIntegrator.getExternalTemperature().ToString("F2"));
line.Append(','); line.Append(',');
   
//gravity //gravity
double r_vessel = vessel.mainBody.Radius + vessel.mainBody.GetAltitude(vessel.findWorldCenterOfMass()); double r_vessel = vessel.mainBody.Radius + vessel.mainBody.GetAltitude(vessel.findWorldCenterOfMass());
double g_vessel = (VOID_Core.Constant_G * vessel.mainBody.Mass) / (r_vessel * r_vessel); double g_vessel = (VOID_Core.Constant_G * vessel.mainBody.Mass) / (r_vessel * r_vessel);
line.Append(g_vessel.ToString("F3")); line.Append(g_vessel.ToString("F3"));
line.Append(','); line.Append(',');
   
//atm density //atm density
line.Append((vessel.atmDensity * 1000).ToString("F3")); line.Append((vessel.atmDensity * 1000).ToString("F3"));
line.Append(','); line.Append(',');
   
// Downrange Distance // Downrange Distance
line.Append((VOID_Data.downrangeDistance.Value.ToString("G3"))); line.Append((VOID_Data.downrangeDistance.Value.ToString("G3")));
   
line.Append('\n'); line.Append('\n');
   
csvList.Add(line.ToString()); csvList.Add(line.ToString());
   
csvCollectTimer = 0f; csvCollectTimer = 0f;
} }
   
  private class WriteState
  {
  public byte[] bytes;
  public KSP.IO.FileStream stream;
  }
} }
} }