I have a few different classes in my application, but this specific class is the one I'm interested about. Please review for good practices and potential problems.
using ExactEarth.Utilities.Jaguar.Properties;
using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;
public class Downlink
{
public string PassId { get; set; }
public string DataId { get; set; }
public string NoradId { get; set; }
public string Spacecraft { get; set; }
public string GroundStation { get; set; }
public string Start { get; set; }
public string End { get; set; }
public string Status { get; set; }
private static string connectionString;
private static SqlConnection connection;
public Downlink(string passId)
{
using (StreamReader reader = new StreamReader(AppDomain.CurrentDomain.BaseDirectory + Resources.connectionStringPath))
{
connectionString = reader.ReadLine();
}
using (connection = new SqlConnection(connectionString))
{
connection.Open();
this.PassId = passId;
this.DataId = GetDataId();
this.NoradId = GetSatelliteId();
this.Spacecraft = GetSpacecraft();
this.GroundStation = GetGroundStation();
this.Start = GetStartTime();
this.End = GetEndTime();
this.Status = GetPassStatus();
}
}
public string GetDataId()
{
string dataId = null;
using (SqlCommand command = new SqlCommand())
{
command.Parameters.Add("@passId", SqlDbType.NVarChar).Value = this.PassId;
command.CommandText = Resources.passDataQuery;
command.Connection = connection;
SqlDataReader reader = command.ExecuteReader();
reader.Read();
dataId = reader["gs_pass_data_id"].ToString();
reader.Close();
}
return dataId;
}
public string GetSatelliteId()
{
string satelliteId = null;
using (SqlCommand command = new SqlCommand())
{
command.Parameters.Add("@dataId", SqlDbType.NVarChar).Value = this.DataId;
command.CommandText = Resources.ausInPassQuery;
command.Connection = connection;
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
reader.Read();
satelliteId = reader["acq_id"].ToString().Substring(0, 5);
}
return satelliteId;
}
}
public string GetSpacecraft()
{
string path = AppDomain.CurrentDomain.BaseDirectory;
path += Resources.spacecraftPath;
using (StreamReader reader = new StreamReader(path))
{
string input = null;
while ((input = reader.ReadLine()) != null)
{
string satelliteId = input.Split(',')[0];
if (this.NoradId == satelliteId)
return input.Split(',')[1];
}
}
// Spacecraft not known
return Resources.unknownSpacecraft;
}
public string GetGroundStation()
{
string station = null;
using (SqlCommand command = new SqlCommand())
{
command.Parameters.Add("@dataId", SqlDbType.NVarChar).Value = this.DataId;
command.CommandText = Resources.groundStationQuery;
command.Connection = connection;
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
reader.Read();
station = reader["ground_station_name"].ToString();
}
return station;
}
}
public string GetStartTime()
{
string start = null;
using (SqlCommand command = new SqlCommand())
{
command.Parameters.Add("@dataId", SqlDbType.NVarChar).Value = this.DataId;
command.CommandText = Resources.passStartQuery;
command.Connection = connection;
SqlDataReader reader = command.ExecuteReader();
reader.Read();
start = Convert.ToDateTime(reader["downlink_start_time"]).ToString("yyyy-MM-dd HH':'mm':'ss");
reader.Close();
}
return start;
}
public string GetEndTime()
{
string end = null;
using (SqlCommand command = new SqlCommand())
{
command.Parameters.Add("@dataId", SqlDbType.NVarChar).Value = this.DataId;
command.CommandText = Resources.passEndQuery;
command.Connection = connection;
SqlDataReader reader = command.ExecuteReader();
reader.Read();
end = Convert.ToDateTime(reader["downlink_end_time"]).ToString("yyyy-MM-dd HH':'mm':'ss");
reader.Close();
}
return end;
}
public string GetPassStatus()
{
string status = null;
if (!JaguarUtilities.IsSatelliteTracked(this.Spacecraft))
status = Resources.statusNotTracked;
else
{
using (SqlCommand command = new SqlCommand())
{
command.Parameters.Add("@passId", SqlDbType.NVarChar).Value = this.PassId;
command.CommandText = Resources.sourceFileQuery;
command.Connection = connection;
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
reader.Close();
using (SqlCommand newCommand = new SqlCommand())
{
newCommand.Parameters.Add("@dataId", SqlDbType.NVarChar).Value = this.DataId;
newCommand.CommandText = Resources.passStatusQuery;
newCommand.Connection = connection;
reader = newCommand.ExecuteReader();
status = Resources.statusProcessing;
if (reader.HasRows)
status = Resources.statusProcessed;
}
}
else
{
status = Resources.statusScheduled;
if (Convert.ToDateTime(this.End) < DateTime.UtcNow)
{
TimeSpan span = DateTime.UtcNow.Subtract(Convert.ToDateTime(this.End));
TimeSpan failTime = JaguarUtilities.GetFailTime(this.Spacecraft, this.GroundStation);
if (TimeSpan.Compare(span, failTime) <= 0)
status = Resources.statusUpdate + (Convert.ToDateTime(this.End) + failTime).ToString("HH':'mm");
else
status = Resources.statusWaiting;
}
}
}
}
return status;
}
public bool IsEscalated()
{
bool escalated;
using (connection)
{
if (IsLastSpacePassFail())
escalated = true;
else if (IsNextSpacePassFail())
escalated = true;
else if (IsLastGroundPassFail())
escalated = true;
else if (IsNextGroundPassFail())
escalated = true;
else
escalated = false;
}
return escalated;
}
private bool IsProcessingTooLong()
{
string path = AppDomain.CurrentDomain.BaseDirectory + Resources.processingTimePath;
using (StreamReader reader = new StreamReader(path))
{
string time = reader.ReadLine();
TimeSpan span = DateTime.UtcNow.Subtract(Convert.ToDateTime(this.End));
TimeSpan failTime = JaguarUtilities.GetFailTime(this.Spacecraft, this.GroundStation);
TimeSpan fail = new TimeSpan(0, int.Parse(time), 0);
if (span > (fail + failTime))
return true;
else
return false;
}
}
private bool IsLastSpacePassFail()
{
string passId = null;
connection.ConnectionString = connectionString;
connection.Open();
using (SqlCommand command = new SqlCommand())
{
command.Parameters.Add("@satelliteId", SqlDbType.NVarChar).Value = this.NoradId;
command.Parameters.Add("@downlinkTime", SqlDbType.NVarChar).Value = this.Start;
command.CommandText = Resources.lastSpaceDataQuery;
command.Connection = connection;
SqlDataReader reader = command.ExecuteReader();
reader.Read();
passId = reader["gs_pass_id"].ToString();
}
Downlink lastDownlink = new Downlink(passId);
if (lastDownlink.Status == Resources.statusWaiting)
return true;
else if (lastDownlink.Status == Resources.statusProcessing)
{
if (lastDownlink.IsProcessingTooLong())
return true;
else
return false;
}
else
return false;
}
private bool IsNextSpacePassFail()
{
string passId = null;
connection.ConnectionString = connectionString;
connection.Open();
using (SqlCommand command = new SqlCommand())
{
command.Parameters.Add("@satelliteId", SqlDbType.NVarChar).Value = this.NoradId;
command.Parameters.Add("@downlinkTime", SqlDbType.NVarChar).Value = this.End;
command.CommandText = Resources.nextSpaceDataQuery;
command.Connection = connection;
SqlDataReader reader = command.ExecuteReader();
reader.Read();
passId = reader["gs_pass_id"].ToString();
}
Downlink lastDownlink = new Downlink(passId);
if (lastDownlink.Status == Resources.statusWaiting)
return true;
else if (lastDownlink.Status == Resources.statusProcessing)
{
if (lastDownlink.IsProcessingTooLong())
return true;
else
return false;
}
else
return false;
}
private bool IsLastGroundPassFail()
{
string passId = null;
connection.ConnectionString = connectionString;
connection.Open();
using (SqlCommand command = new SqlCommand())
{
command.Parameters.Add("@groundId", SqlDbType.NVarChar).Value = this.GroundStation;
command.Parameters.Add("@downlinkTime", SqlDbType.NVarChar).Value = this.Start;
command.CommandText = Resources.lastGroundDataQuery;
command.Connection = connection;
SqlDataReader reader = command.ExecuteReader();
reader.Read();
passId = reader["gs_pass_id"].ToString();
}
Downlink lastDownlink = new Downlink(passId);
if (lastDownlink.Status == Resources.statusWaiting)
return true;
else if (lastDownlink.Status == Resources.statusProcessing)
{
if (lastDownlink.IsProcessingTooLong())
return true;
else
return false;
}
else
return false;
}
private bool IsNextGroundPassFail()
{
string passId = null;
connection.ConnectionString = connectionString;
connection.Open();
using (SqlCommand command = new SqlCommand())
{
command.Parameters.Add("@groundId", SqlDbType.NVarChar).Value = this.GroundStation;
command.Parameters.Add("@downlinkTime", SqlDbType.NVarChar).Value = this.End;
command.CommandText = Resources.nextGroundDataQuery;
command.Connection = connection;
SqlDataReader reader = command.ExecuteReader();
reader.Read();
passId = reader["gs_pass_id"].ToString();
}
Downlink lastDownlink = new Downlink(passId);
if (lastDownlink.Status == Resources.statusWaiting)
return true;
else if (lastDownlink.Status == Resources.statusProcessing)
{
if (lastDownlink.IsProcessingTooLong())
return true;
else
return false;
}
else
return false;
}
public int GetNumberAusInPass(string query)
{
int planned = 0;
using (connection)
{
connection.ConnectionString = connectionString;
connection.Open();
using (SqlCommand command = new SqlCommand())
{
command.Parameters.Add("@dataId", SqlDbType.NVarChar).Value = this.DataId;
command.CommandText = query;
command.Connection = connection;
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
++planned;
}
reader.Close();
}
return planned;
}
}
public int GetNumberAusMatched()
{
int matched = 0;
using (connection)
{
connection.ConnectionString = connectionString;
connection.Open();
using (SqlCommand command = new SqlCommand())
{
command.Parameters.Add("@dataId", SqlDbType.NVarChar).Value = this.DataId;
command.CommandText = Resources.producedAusQuery;
command.Connection = connection;
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
long num;
if (long.TryParse(reader["acquisition_id"].ToString().Split('_')[1], out num))
++matched;
}
reader.Close();
}
return matched;
}
}
}