void client_CommandReceived(object sender, CommandReceivedEventArgs e)
        {
            Dispatcher.BeginInvoke((Action)(() =>
            {
                try {

                    var type = typeof(IFAPIStatus).Assembly.GetType(e.Response.Type);

                    if (type == typeof(APIAircraftState))
                    {
                        var state = Serializer.DeserializeJson<APIAircraftState>(e.CommandString);

                        Console.WriteLine(state.VerticalSpeed);

                        // convert to fpm
                        state.VerticalSpeed = float.Parse(Convert.ToString(state.VerticalSpeed * 200, CultureInfo.InvariantCulture.NumberFormat), CultureInfo.InvariantCulture.NumberFormat); // multiply by 200, this somehow gets it accurate..

                        airplaneStateGrid.DataContext = null;
                        airplaneStateGrid.DataContext = state;
                        pAircraftState = state;
                        if (FMSControl.autoFplDirectActive) { FMSControl.updateAutoNav(state); }
                        AircraftStateControl.AircraftState = state;
                        AttitudeIndicator.updateAttitude(state.Pitch, state.Bank);
                       updateLandingRoll(state);
                    }
                    else if (type == typeof(GetValueResponse))
                    {
                        var state = Serializer.DeserializeJson<GetValueResponse>(e.CommandString);

                        Console.WriteLine("{0} -> {1}", state.Parameters[0].Name, state.Parameters[0].Value);
                    }
                    else if (type == typeof(LiveAirplaneList))
                    {
                        LiveAirplaneList airplaneList = Serializer.DeserializeJson<LiveAirplaneList>(e.CommandString);
                        //airplaneDataGrid.ItemsSource = airplaneList.Airplanes;
                    }
                    else if (type == typeof(FacilityList))
                    {
                        var facilityList = Serializer.DeserializeJson<FacilityList>(e.CommandString);

                        //facilitiesDataGrid.ItemsSource = facilityList.Facilities;
                    }
                    else if (type == typeof(IFAPIStatus))
                    {
                        var status = Serializer.DeserializeJson<IFAPIStatus>(e.CommandString);

                    }
                    else if (type == typeof(APIATCMessage))
                    {
                        var msg = Serializer.DeserializeJson<APIATCMessage>(e.CommandString);
                        // TODO client.ExecuteCommand("Live.GetCurrentCOMFrequencies");
                    }
                    else if (type == typeof(APIFrequencyInfoList))
                    {
                        var msg = Serializer.DeserializeJson<APIFrequencyInfoList>(e.CommandString);
                    }
                    else if (type == typeof(ATCMessageList))
                    {
                        var msg = Serializer.DeserializeJson<ATCMessageList>(e.CommandString);
                        atcMessagesDataGrid.ItemsSource = msg.ATCMessages;

                    }
                    else if (type == typeof(APIFlightPlan))
                    {
                        var msg = Serializer.DeserializeJson<APIFlightPlan>(e.CommandString);
                        Console.WriteLine("Flight Plan: {0} items", msg.Waypoints.Length);
                        FMSControl.fplReceived(msg); //Update FMS with FPL from IF.
                        foreach (var item in msg.Waypoints)
                        {
                            Console.WriteLine(" -> {0} {1} - {2}, {3}", item.Name, item.Code, item.Latitude, item.Longitude);
                        }
                    }
                } catch (System.NullReferenceException) {
                    Console.WriteLine("Disconnected from server!");
                    //Let the client handle the lost connection.
                    //connectionStatus = false;
                }
            }));
        }
        private void updateLandingRoll(APIAircraftState state)
        {
            if (pLastState == null)
            {
                pLastState = state;
            }else if (!state.IsOnGround)
            {
                pLastState = state;
                pLastLandingRoll = 0.0;
                pLandingLocation = null;
                txtLandingRoll.Visibility = Visibility.Hidden;
                txtLandingRollLabel.Visibility = Visibility.Hidden;
                //btnViewLandingStats.Visibility = Visibility.Hidden;
            }
            else if (!pLastState.IsLanded && state.IsLanded) //Just transitioned to "landed" state, so start the roll accumulation
            {
                pLandingLocation = state.Location;
                pStateJustBeforeTouchdown = pLastState;
                pStateJustAfterTouchdown = state;
                pLastState = state;
            }
            else if (state.IsLanded && pLandingLocation != null) //We are in landed state. Calc the roll length.
            {
                Coordinate currentPosition = state.Location;
                var R = 3959; // Radius of the earth in miles
                var dLat = (currentPosition.Latitude - pLandingLocation.Latitude) * (Math.PI / 180);
                var dLon = (currentPosition.Longitude - pLandingLocation.Longitude) * (Math.PI / 180);
                var a =
                  Math.Sin(dLat / 2) * Math.Sin(dLat / 2) +
                  Math.Cos((currentPosition.Latitude) * (Math.PI / 180)) * Math.Cos((pLandingLocation.Latitude) * (Math.PI / 180)) *
                  Math.Sin(dLon / 2) * Math.Sin(dLon / 2)
                  ;
                var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
                var d = R * c; // Distance in miles
                d *= 5280; //Distance in ft
                txtLandingRoll.Text = String.Format("{0:0.00}", d) + " ft";
                txtLandingRoll.Visibility = Visibility.Visible;
                txtLandingRollLabel.Visibility = Visibility.Visible;
                pLastState = state;
                landingDetails.updateLandingStats(pLandingLocation, pAircraftState.Location, pStateJustBeforeTouchdown, pStateJustAfterTouchdown, "");
            }

            if(pLandingLocation !=null && state.IsLanded && state.IsOnGround && state.GroundSpeedKts < 10)
            {
                pLandingStatDlg = new LandingStats();
                pLandingStatDlg.updateLandingStats(pLandingLocation, pAircraftState.Location, pStateJustBeforeTouchdown, pStateJustAfterTouchdown, "");
                landingDetails.updateLandingStats(pLandingLocation, pAircraftState.Location, pStateJustBeforeTouchdown, pStateJustAfterTouchdown, "");
                //btnViewLandingStats.Visibility = Visibility.Visible;
            }
        }
Example #3
0
        public void updateAutoNav(APIAircraftState acState)
        {
            if (pFplState == null)
            {
                client.ExecuteCommand("FlightPlan.GetFlightPlan");
            }

            //Set initial next waypoint as first waypoint
            if (pFplState.nextWpt == null)
            {
                pFplState.legIndex = 0;
                pFplState.nextWpt = pFplState.fpl.Waypoints[1];
                pFplState.dest = pFplState.fpl.Waypoints.Last();
                pFplState.nextAltitude = pFplState.fplDetails.waypoints[pFplState.legIndex + 1].Altitude;
                pFplState.thisSpeed = pFplState.fplDetails.waypoints[pFplState.legIndex].Airspeed;
                pFplState.nextSpeed = pFplState.fplDetails.waypoints[pFplState.legIndex + 1].Airspeed;
            }

            //Get dist to next wpt
            pFplState.distToNextWpt = getDistToWaypoint(acState.Location, pFplState.nextWpt);

            //If dist<2nm, go to next wpt
            if (pFplState.distToNextWpt < 2)
            {
                pFplState.legIndex++; //next leg
                pFplState.prevWpt = pFplState.nextWpt; //current wpt is not prev wpt
                if (pFplState.legIndex >= pFplState.fpl.Waypoints.Count())
                {
                    //We hit the destination!
                    lblNextWpt.Content = "Destination Reached!";
                    lblDist2Next.Content = "";
                    lblHdg2Next.Content = "";
                    autoFplDirectActive = false;
                    return;
                }
                pFplState.nextWpt = pFplState.fpl.Waypoints[pFplState.legIndex]; //target next wpt
                pFplState.nextAltitude = pFplState.fplDetails.waypoints[pFplState.legIndex].Altitude;
                pFplState.thisSpeed = pFplState.fplDetails.waypoints[pFplState.legIndex - 1].Airspeed;
                pFplState.nextSpeed = pFplState.fplDetails.waypoints[pFplState.legIndex].Airspeed;
                //Get dist to new next wpt
                pFplState.distToNextWpt = getDistToWaypoint(acState.Location, pFplState.nextWpt);
            }

            lblNextWpt.Content = pFplState.nextWpt.Name;
            lblDist2Next.Content = String.Format("{0:0.000}", pFplState.distToNextWpt);
            lblAirspeedSet.Content = String.Format("{0:0.000}", pFplState.thisSpeed);
            lblAltitudeSet.Content = pFplState.nextAltitude.ToString();

            //Adjust heading for magnetic declination
            double declination = acState.HeadingTrue - acState.HeadingMagnetic;

            //Get heading to next
            pFplState.hdgToNextWpt = getHeadingToWaypoint(acState.Location, pFplState.nextWpt) - declination;
            lblHdg2Next.Content = String.Format("{0:0.000}", pFplState.hdgToNextWpt);

            //Calculate VS to hit target altitude
            if (pFplState.nextAltitude > 0) {
            double vs = calcVs(acState.AltitudeMSL, pFplState.nextAltitude, acState.GroundSpeedKts, pFplState.distToNextWpt);
            lblVsSet.Content = string.Format("{0:0.000}", vs);
            //Adjust AutoPilot
            setAutoPilotParams(pFplState.nextAltitude, pFplState.hdgToNextWpt, vs, pFplState.nextSpeed);
            }
            else
            {
                //Adjust AutoPilot
                setAutoPilotParams(pFplState.nextAltitude, pFplState.hdgToNextWpt, 999999, pFplState.nextSpeed);
            }

            ////Dont think we need this
            //APIWaypoint closestWpt = pFplState.fpl.Waypoints.First();
            //foreach (APIWaypoint  wpt in pFplState.fpl.Waypoints)
            //{
            //   double distToClosest = getDistToWaypoint(acState.Location, closestWpt);
            //   if (getDistToWaypoint(acState.Location,wpt) < distToClosest)
            //    {
            //        closestWpt = wpt;
            //    }
            //}
        }