private void UpdateMap(AirTelemetry at, Pushpin pin, MapPolyline polyline, MapLayer layer) { Label lbl = new Label(); lbl.Content = at.airline + " " + flight; FlightMap.Center.Latitude = at.lat; FlightMap.Center.Longitude = at.lng; FlightMap.Center.Altitude = at.altitude; Location centerd = FlightMap.Center; double zoomd = FlightMap.ZoomLevel; FlightMap.SetView(centerd, zoomd); pin.Location = centerd; FlightMap.Children.Clear(); FlightMap.Children.Add(polyline); FlightMap.Children.Add(pin); layer.Children.Clear(); layer.AddChild(lbl, centerd); WeatherImageLayer(layer); FlightMap.Children.Add(layer); }
private void UpdateFlightInfoBox(AirTelemetry at, bool arrived) { double compassheading = at.heading < 0 ? at.heading + 360.0 : at.heading; FlightInfoBox.Content = "Flight " + airline + " " + flight + " travelling " + miles.ToString("N0") + " miles on a heading of " + at.heading.ToString("N0")+ " degrees"; if (arrived) FlightInfoBox.Content += " Flight has arrived."; }
async Task TrackFlight(AirTelemetry AT, Location origin, Location destination, double miles) { string json=""; var eventHubConnectionString = GetEventHubConnectionString(); var eventHubClient = EventHubClient.CreateFromConnectionString(eventHubConnectionString, eventHubName); int runningMileCount = 0; int updateWeatherInverval = 200; int updateWeatherRadius = 300; /* how many miles we go per tick */ int mileageIncrement = 30; Pushpin pin = new Pushpin(); Label infoLabel = new Label(); MapLayer InfoLayer = new MapLayer(); AT.RowKey += DateTime.Now.ToShortDateString(); /* get rid of illegal characters in row key */ AT.RowKey = Regex.Replace(AT.RowKey, @"[\ /?#]", ""); MapPolyline polyline = new MapPolyline(); polyline.Stroke = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Blue); polyline.StrokeThickness = 5; polyline.Opacity = 0.7; /* pre-plot the course so that we can draw the flight path on the map. we recalculate the bearing every * 100 miles (could/should be configurable) to get a true Great Circle route. */ Location prev = origin; Location nxtl; double hdg = AT.heading; polyline.Locations=new LocationCollection(); polyline.Locations.Add(origin); int j; for (j = 2; ;j++ ) { nxtl = Haversine.PositionDistanceFromOrigin(prev, mileageIncrement / 0.00062137, hdg); polyline.Locations.Add(nxtl); hdg = Haversine.Bearing(nxtl, destination); prev = nxtl; if (Haversine.InRange(nxtl, destination.Latitude, destination.Longitude, 100)) { polyline.Locations.Add(destination); break; } } string rkey = AT.RowKey; AT.lat = origin.Latitude; AT.lng = origin.Longitude; UpdateMap(AT, pin, polyline, InfoLayer); UpdateWeatherFromTable(origin, 300, new TimeSpan(3, 0, 0)); int i; for (i = 0; i <= polyline.Locations.Count; i++) { await Task.Delay(2000); try { AT.RowKey = rkey +'-'+ i.ToString(); /* row key must be unique or will overwrite existing */ FlightMap.Children.Clear(); FlightMap.Children.Add(polyline); InfoLayer.Children.Clear(); /* we have two time stamps. the first is inherited from TableEntity and normally should not be used by applications. We * initialize it here because Stream Analytics will barf on an illegal data (dates must be greater than * 12:00 midnight, January 1, 1601 A.D. (C.E.), UTC.). We do not use the inherited time stamp after this; this is what * AT.dto is for */ AT.Timestamp = DateTime.Now; AT.dto = DateTimeOffset.Now; AT.inflight = "true"; /* send distance in meters */ Location p = Haversine.PositionDistanceFromOrigin(new Location(AT.lat, AT.lng), mileageIncrement / 0.00062137, AT.heading); AT.lat = p.Latitude; AT.lng = p.Longitude; double b2 = Haversine.Bearing(p, destination); AT.heading = b2; /* altitude in meters */ if (i < 5) AT.altitude = 10000 / (6 - (i + 1)); /* ascending */ else if ((polyline.Locations.Count - i) < 5) AT.altitude = 10000 / (polyline.Locations.Count+1 - i); /* descending */ else AT.altitude = 10000; /* initialize the data structure */ AT.ice = 0.0; AT.windspeed = 0.0; AT.windshear = 0.0; AT.lightning = 0.0; /* encode weather conditions */ if(ltice) { AT.ice = 5.0; WeatherEvent we = new WeatherEvent(WeatherEventTypes.LIGHT_ICE, DateTime.Now, p); weatherevents.Add(we); ltice = false; } else if(mdice) { AT.ice = 10.0; WeatherEvent we = new WeatherEvent(WeatherEventTypes.MODERATE_ICE, DateTime.Now, p); weatherevents.Add(we); mdice = false; } else if (hvice) { AT.ice = 20.0; WeatherEvent we = new WeatherEvent(WeatherEventTypes.HEAVY_ICE, DateTime.Now, p); weatherevents.Add(we); hvice = false; } if(mdtub) { AT.windspeed = 20.0; WeatherEvent we = new WeatherEvent(WeatherEventTypes.MODERATE_TURB, DateTime.Now, p); weatherevents.Add(we); mdtub = false; } else if(hvtub) { AT.windspeed = 40.0; WeatherEvent we = new WeatherEvent(WeatherEventTypes.HEAVY_TURB, DateTime.Now, p); weatherevents.Add(we); hvtub = false; } if(mdshr) { AT.windshear = 20.0; WeatherEvent we = new WeatherEvent(WeatherEventTypes.MODERATE_SHEAR, DateTime.Now, p); weatherevents.Add(we); mdshr = false; } else if(hvshr) { AT.windshear = 40.0; WeatherEvent we = new WeatherEvent(WeatherEventTypes.HEAVY_SHEAR, DateTime.Now, p); weatherevents.Add(we); hvshr = false; } if(tstrm) { AT.lightning = 50.0; WeatherEvent we = new WeatherEvent(WeatherEventTypes.TSTORM, DateTime.Now, p); weatherevents.Add(we); tstrm = false; } json = Newtonsoft.Json.JsonConvert.SerializeObject(AT); Console.WriteLine("{0} > Sending message: {1}", DateTime.Now.ToString(), json); FlightMap.Center.Latitude = p.Latitude; FlightMap.Center.Longitude = p.Longitude; FlightMap.Center.Altitude = AT.altitude; Location center = FlightMap.Center; double zoom = FlightMap.ZoomLevel; FlightMap.SetView(center, zoom); pin.Location = center; FlightMap.Children.Add(pin); infoLabel.Content = AT.airline + " " + AT.flight; InfoLayer.AddChild(infoLabel, center); WeatherImageLayer(InfoLayer); FlightMap.Children.Add(InfoLayer); UpdateFlightInfoBox(AT, false); if(Haversine.InRange(new Location(AT.lat, AT.lng), destination.Latitude,destination.Longitude,50)) break; await eventHubClient.SendAsync(new EventData(Encoding.UTF8.GetBytes(json))); runningMileCount += mileageIncrement; if(runningMileCount>=updateWeatherInverval) { runningMileCount = 0; weatherevents.Clear(); UpdateWeatherFromTable(new Location(AT.lat, AT.lng), updateWeatherRadius, new TimeSpan(3, 0, 0)); } } catch (Exception exception) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("{0} > Exception: {1}", DateTime.Now.ToString(), exception.Message); Console.ResetColor(); } } AT.arrival = DateTime.Now; AT.inflight = "false"; /* fudge the destination position */ AT.lat=FlightMap.Center.Latitude = destination.Latitude; AT.lng=FlightMap.Center.Longitude = destination.Longitude; FlightMap.Center.Altitude = AT.altitude; UpdateMap(AT, pin, polyline, InfoLayer); json = Newtonsoft.Json.JsonConvert.SerializeObject(AT); await eventHubClient.SendAsync(new EventData(Encoding.UTF8.GetBytes(json))); UpdateFlightInfoBox(AT, true); }
async private void StartFlight_Click(object sender, RoutedEventArgs e) { airline=AirlineNameBox.Text; ; flight=AirlineFlightNumberBox.Text; origin=DepartureCityBox.Text; destination=ArrivalCityBox.Text; origin = origin.ToUpper(); destination = destination.ToUpper(); if (string.IsNullOrEmpty(airline) || string.IsNullOrEmpty(flight) || string.IsNullOrEmpty(origin) || string.IsNullOrEmpty(destination)) { FlightInfoBox.Content = "Missing data, please re-enter"; return; } if(origin.Length != 3 || destination.Length!=3) { FlightInfoBox.Content = "Cities not encoded properly, use 3-letter airport code"; } double originlat = 0.0; double originlng = 0.0; double destlat = 0.0; double destlong = 0.0; string origincity = airports[origin]; string destcity = airports[destination]; int error = 0; int erro2 = 0; error=GetLatLongFromName(origincity, out originlat, out originlng); error=GetLatLongFromName(destcity, out destlat, out destlong); /* possible errors on Bing lookup: * call error == -1 * if both lat and long of a lookup are 0.0 then Bing couldn't resolve the name (fix * the airport-codes.csv file) */ if(error<0 || erro2<0 || (originlat==0.0&&originlng==0.0) || (destlat==0.0 && destlong==0.0)) { FlightInfoBox.Content = "Error; please try again."; return; } /* get distance and heading */ Location o = new Location(originlat, originlng); Location d = new Location(destlat, destlong); double distance = Haversine.HaversineDistance(o, d); miles = distance * 0.00062137; double heading = Haversine.Bearing(o, d); /* create the packet */ AirTelemetry AT = new AirTelemetry(); AT.PartitionKey = "airtelemetry"; AT.RowKey = airline.ToLower() + flight; AT.airline = airline; AT.flight = flight; AT.lat = o.Latitude; AT.lng = o.Longitude; AT.heading = heading; AT.airspeed = 0.0; AT.temp = 38.0; AT.ice = 0.0; AT.lightning = 0.0; AT.windshear = 0.0; AT.windspeed = 15.0; AT.arrival = DateTime.Now; UpdateFlightInfoBox(AT, false); if (CreateEventHub()) { await TrackFlight(AT, o, d, miles); } }