Skip to content

Commit b664a51

Browse files
authored
Update Program.cs
1 parent 19832c6 commit b664a51

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

PiAware2SQLV4/Program.cs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ static class Program
2626
{
2727
private static SqlConnection con1;
2828
private static SqlConnection con2;
29-
/// <summary>
29+
3030
public class Flightrec
3131
{
3232
public DateTime dt { get; set; } // Date Time from system
@@ -95,7 +95,6 @@ static SqlConnection AzureSqlconnect()
9595
var localcon = new SqlConnection();
9696
localcon.ConnectionString = "Data Source=piawaredbserver.database.windows.net;Initial Catalog=PiAwaredb;user id=youruser;Password=yourpassword";
9797
localcon.Open();
98-
9998
return localcon;
10099
}
101100

@@ -107,7 +106,6 @@ static SqlConnection SQLDBconnect()
107106
var localcon2 = new SqlConnection();
108107
localcon2.ConnectionString = "Data Source=SQLDB;Initial Catalog=PiAwaredb;Integrated Security = True";
109108
localcon2.Open();
110-
111109
return localcon2;
112110
}
113111

@@ -117,19 +115,17 @@ public static class StaticItems
117115
*/
118116
{
119117
public static string EndPoint = "http://192.168.0.129/dump1090-fa/data/aircraft.json";
120-
121-
122118
}
119+
123120
static void Main()
124121
{
125122
con1 = AzureSqlconnect();
126123
con2 = SQLDBconnect();
127-
128-
/*
129-
**** Microsoft Las Colinas Office ****
130-
**** GPS Coordinates ****
124+
125+
//**** Microsoft Las Colinas Office ****
126+
//**** GPS Coordinates ****
131127
//32.900076025507246, -96.96343451541534
132-
*/
128+
133129
double mylat = 32.90007;
134130
double mylon = -96.96343;
135131

@@ -138,13 +134,13 @@ static void Main()
138134
/*
139135
**** Variables used in main processing loop ****
140136
*/
137+
141138
double distance = 0;
142139
var tcount = 0;
143-
144140
var webClient = new WebClient();
145141
webClient.BaseAddress = StaticItems.EndPoint;
146142

147-
Console.Clear();
143+
Console.Clear(); //clear the console
148144

149145
while (true)
150146
{
@@ -155,7 +151,7 @@ static void Main()
155151
JToken token = JToken.Parse(json);
156152
JArray aircraft = (JArray)token.SelectToken("aircraft");
157153
JArray saircraft = new JArray(aircraft.OrderBy(obj => (string)obj["flight"]));
158-
//Console.Clear();
154+
// Write headers at top of console screen
159155
Console.SetCursorPosition(0, 0);
160156
Console.WriteLine("----------------------------------------------------------------------------------------");
161157
Console.WriteLine("| -- Write to two SQL Databases |");
@@ -165,6 +161,7 @@ static void Main()
165161

166162
var i = 0;
167163

164+
// Only process records with the hex,flight,lat,lon,alt_baro,baro_rate,track and gs columns
168165
foreach (JToken ac in saircraft)
169166
{
170167
if (ac["hex"] != null &
@@ -179,6 +176,7 @@ static void Main()
179176
{
180177
i++;
181178

179+
// set variables with the contents of record in JArray. One record is written to the databases and console for each pass of the loop
182180
Flightrec prec = new Flightrec
183181
{
184182
dt = DateTime.Now,
@@ -214,6 +212,7 @@ static void Main()
214212
distance = distance = Miles(Convert.ToDouble(ac["lat"]), mylat, Convert.ToDouble(ac["lon"]), mylon)
215213
};
216214

215+
// Strings to display variable values on the console in 10 character wide columns
217216
string dflight = prec.flight.PadRight(10);
218217
string dlat = Convert.ToString(prec.lat).PadLeft(10);
219218
string dlon = Convert.ToString(prec.lon).PadLeft(10);
@@ -222,12 +221,14 @@ static void Main()
222221
string dvr = Convert.ToString(prec.baro_rate).PadLeft(10);
223222
string dem = Convert.ToString(prec.emergency).PadLeft(7);
224223

224+
// Set up connections to the SQL Databases I could do this outside of the loop, but doing it here provides more resiliance during a timeout
225225
SqlCommand cmd1 = new SqlCommand();
226226
cmd1.Connection = con1;
227227

228228
SqlCommand cmd2 = new SqlCommand();
229229
cmd2.Connection = con2;
230230

231+
// Open the connections to the databases
231232
try
232233
{
233234
con1.Open();
@@ -240,6 +241,7 @@ static void Main()
240241
}
241242
catch { }
242243

244+
// Create the insert statement and populate parameters with current record values
243245
cmd1.CommandText = "INSERT INTO KDFW (dt,hex,squawk,flight,lat,lon,distance, nucp,seen_pos,altitude,vr,track,speed,category,messages,seen,rssi,acode) VALUES(@param1,@param2,@param3,@param4,@param5,@param6,@param7,@param8,@param9,@param10,@param11,@param12,@param13,@param14,@param15,@param16,@param17,@param18)";
244246

245247
cmd1.Parameters.AddWithValue("@param1", prec.dt);
@@ -261,6 +263,7 @@ static void Main()
261263
cmd1.Parameters.AddWithValue("@param17", prec.rssi);
262264
cmd1.Parameters.AddWithValue("@param18", prec.acode);
263265

266+
// Write the record to SQL. Try/Catch with recover from a closed SQL connection due to long period with no flight. Early AM hours.
264267
try
265268
{
266269
cmd1.ExecuteNonQuery();
@@ -305,7 +308,7 @@ static void Main()
305308
con2.Open();
306309
cmd2.ExecuteNonQuery();
307310
}
308-
311+
// Write the records to the console
309312
Console.WriteLine(dflight + " | " + dlat + " | " + dlon + " | " + dalt + " | " + dgs + " | " + dvr + " | " + dem + " | " + " ");
310313

311314
}
@@ -316,11 +319,11 @@ static void Main()
316319

317320
}
318321
catch { }
319-
322+
// Write blank lines to console at the end of the records for passes with fewer records than the previous pass
320323
Console.WriteLine("----------------------------------------------------------------------------------------");
321324
Console.WriteLine(" ");
322325
Console.WriteLine(" ");
323-
Thread.Sleep(1000);
326+
Thread.Sleep(1000); \\ Wait 1 second between set of record writes. Without this it will write dozens of records to the database per second.
324327
}
325328
}
326329
}

0 commit comments

Comments
 (0)