@@ -26,7 +26,7 @@ static class Program
26
26
{
27
27
private static SqlConnection con1 ;
28
28
private static SqlConnection con2 ;
29
- /// <summary>
29
+
30
30
public class Flightrec
31
31
{
32
32
public DateTime dt { get ; set ; } // Date Time from system
@@ -95,7 +95,6 @@ static SqlConnection AzureSqlconnect()
95
95
var localcon = new SqlConnection ( ) ;
96
96
localcon . ConnectionString = "Data Source=piawaredbserver.database.windows.net;Initial Catalog=PiAwaredb;user id=youruser;Password=yourpassword" ;
97
97
localcon . Open ( ) ;
98
-
99
98
return localcon ;
100
99
}
101
100
@@ -107,7 +106,6 @@ static SqlConnection SQLDBconnect()
107
106
var localcon2 = new SqlConnection ( ) ;
108
107
localcon2 . ConnectionString = "Data Source=SQLDB;Initial Catalog=PiAwaredb;Integrated Security = True" ;
109
108
localcon2 . Open ( ) ;
110
-
111
109
return localcon2 ;
112
110
}
113
111
@@ -117,19 +115,17 @@ public static class StaticItems
117
115
*/
118
116
{
119
117
public static string EndPoint = "http://192.168.0.129/dump1090-fa/data/aircraft.json" ;
120
-
121
-
122
118
}
119
+
123
120
static void Main ( )
124
121
{
125
122
con1 = AzureSqlconnect ( ) ;
126
123
con2 = SQLDBconnect ( ) ;
127
-
128
- /*
129
- **** Microsoft Las Colinas Office ****
130
- **** GPS Coordinates ****
124
+
125
+ //**** Microsoft Las Colinas Office ****
126
+ //**** GPS Coordinates ****
131
127
//32.900076025507246, -96.96343451541534
132
- */
128
+
133
129
double mylat = 32.90007 ;
134
130
double mylon = - 96.96343 ;
135
131
@@ -138,13 +134,13 @@ static void Main()
138
134
/*
139
135
**** Variables used in main processing loop ****
140
136
*/
137
+
141
138
double distance = 0 ;
142
139
var tcount = 0 ;
143
-
144
140
var webClient = new WebClient ( ) ;
145
141
webClient . BaseAddress = StaticItems . EndPoint ;
146
142
147
- Console . Clear ( ) ;
143
+ Console . Clear ( ) ; //clear the console
148
144
149
145
while ( true )
150
146
{
@@ -155,7 +151,7 @@ static void Main()
155
151
JToken token = JToken . Parse ( json ) ;
156
152
JArray aircraft = ( JArray ) token . SelectToken ( "aircraft" ) ;
157
153
JArray saircraft = new JArray ( aircraft . OrderBy ( obj => ( string ) obj [ "flight" ] ) ) ;
158
- //Console.Clear();
154
+ // Write headers at top of console screen
159
155
Console . SetCursorPosition ( 0 , 0 ) ;
160
156
Console . WriteLine ( "----------------------------------------------------------------------------------------" ) ;
161
157
Console . WriteLine ( "| -- Write to two SQL Databases |" ) ;
@@ -165,6 +161,7 @@ static void Main()
165
161
166
162
var i = 0 ;
167
163
164
+ // Only process records with the hex,flight,lat,lon,alt_baro,baro_rate,track and gs columns
168
165
foreach ( JToken ac in saircraft )
169
166
{
170
167
if ( ac [ "hex" ] != null &
@@ -179,6 +176,7 @@ static void Main()
179
176
{
180
177
i ++ ;
181
178
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
182
180
Flightrec prec = new Flightrec
183
181
{
184
182
dt = DateTime . Now ,
@@ -214,6 +212,7 @@ static void Main()
214
212
distance = distance = Miles ( Convert . ToDouble ( ac [ "lat" ] ) , mylat , Convert . ToDouble ( ac [ "lon" ] ) , mylon )
215
213
} ;
216
214
215
+ // Strings to display variable values on the console in 10 character wide columns
217
216
string dflight = prec . flight . PadRight ( 10 ) ;
218
217
string dlat = Convert . ToString ( prec . lat ) . PadLeft ( 10 ) ;
219
218
string dlon = Convert . ToString ( prec . lon ) . PadLeft ( 10 ) ;
@@ -222,12 +221,14 @@ static void Main()
222
221
string dvr = Convert . ToString ( prec . baro_rate ) . PadLeft ( 10 ) ;
223
222
string dem = Convert . ToString ( prec . emergency ) . PadLeft ( 7 ) ;
224
223
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
225
225
SqlCommand cmd1 = new SqlCommand ( ) ;
226
226
cmd1 . Connection = con1 ;
227
227
228
228
SqlCommand cmd2 = new SqlCommand ( ) ;
229
229
cmd2 . Connection = con2 ;
230
230
231
+ // Open the connections to the databases
231
232
try
232
233
{
233
234
con1 . Open ( ) ;
@@ -240,6 +241,7 @@ static void Main()
240
241
}
241
242
catch { }
242
243
244
+ // Create the insert statement and populate parameters with current record values
243
245
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)" ;
244
246
245
247
cmd1 . Parameters . AddWithValue ( "@param1" , prec . dt ) ;
@@ -261,6 +263,7 @@ static void Main()
261
263
cmd1 . Parameters . AddWithValue ( "@param17" , prec . rssi ) ;
262
264
cmd1 . Parameters . AddWithValue ( "@param18" , prec . acode ) ;
263
265
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.
264
267
try
265
268
{
266
269
cmd1 . ExecuteNonQuery ( ) ;
@@ -305,7 +308,7 @@ static void Main()
305
308
con2 . Open ( ) ;
306
309
cmd2 . ExecuteNonQuery ( ) ;
307
310
}
308
-
311
+ // Write the records to the console
309
312
Console . WriteLine ( dflight + " | " + dlat + " | " + dlon + " | " + dalt + " | " + dgs + " | " + dvr + " | " + dem + " | " + " " ) ;
310
313
311
314
}
@@ -316,11 +319,11 @@ static void Main()
316
319
317
320
}
318
321
catch { }
319
-
322
+ // Write blank lines to console at the end of the records for passes with fewer records than the previous pass
320
323
Console . WriteLine ( "----------------------------------------------------------------------------------------" ) ;
321
324
Console . WriteLine ( " " ) ;
322
325
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 .
324
327
}
325
328
}
326
329
}
0 commit comments