/* Helps check validation of Initial Location in the given flight plan. */
        private bool CheckInitialLocationIsValid(InitialLocation initial)
        {
            // Missing the whole object.
            if (initial == null)
            {
                return(false);
            }

            if ((initial.Latitude == -200) || (initial.Longitude == -200) ||
                (initial.Date_Time == default))
            {
                return(false);
            }

            if (initial.Latitude < -90 || initial.Latitude > 90)
            {
                return(false);
            }

            if (initial.Longitude < -180 || initial.Longitude > 180)
            {
                return(false);
            }

            // Initial location is valid.
            return(true);
        }
Exemple #2
0
 public FlightPlan(int passengers, string companyName, InitialLocation location, List <Segment> segments)
 {
     Passengers  = passengers;
     CompanyName = companyName;
     Location    = location;
     Segments    = segments;
 }
 public FlightPlan(int pass, string name, InitialLocation initLoc, List <Segment> segs)
 {
     Passengers       = pass;
     Company_Name     = name;
     Initial_Location = initLoc;
     Segments         = segs;
 }
Exemple #4
0
 public FlightPlan(int pass, string company,
                   InitialLocation initialLoc, List <Segment> seg)
 {
     Passengers      = pass;
     CompanyName     = company;
     InitialLocation = initialLoc;
     Segments        = seg;
 }
Exemple #5
0
 /*
  * Ctor
  */
 public FlightPlan(int passengers, string company, InitialLocation initialLocation,
                   IEnumerable <Segment> segments)
 {
     this.flightId        = GenerateFlightID();
     this.passengers      = passengers;
     this.company         = company;
     this.initialLocation = initialLocation;
     this.segments        = segments;
 }
Exemple #6
0
 /**
  * Constructor with given parameters
  **/
 public FlightPlan(int passengersNum, string company, InitialLocation init,
                   List <Segment> seg)
 {
     Id              = FlightId.GetRandomId();
     Passengers      = passengersNum;
     CompanyName     = company;
     InitialLocation = init;
     // the all path
     Segments = seg;
 }
Exemple #7
0
 public bool IsNull()
 {
     //check validation
     if (CompanyName == null ||
         InitialLocation == null || InitialLocation.IsNull() ||
         Segments == null || SegmentNull(Segments) || Passengers < 0)
     {
         return(true);
     }
     return(false);
 }
        public FlightPlan AddFlightPlan(FlightPlan flightPlan)
        {
            // Creating a new flight ID and validating it
            string          id  = GenerateRandomId();
            InitialLocation loc = flightPlan.initial_location;

            using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString()))
            {
                // Inserting the location
                cnn.Execute("insert into Locations (longitude,latitude,date) values " +
                            "(@longitude,@latitude,@date_time)", loc);
                string stm = "SELECT * FROM Locations ORDER BY id DESC LIMIT 1";
                using var con = new SQLiteConnection(LoadConnectionString());
                con.Open();
                using var cmd = new SQLiteCommand(stm, con);
                SQLiteDataReader reader = cmd.ExecuteReader();
                reader.Read();
                string id1 = "";
                id1 = reader["id"].ToString();
                reader.Close();

                // Inserting the FlightPlan
                cmd.CommandText =
                    "INSERT INTO FlightPlans(flight_ID,company_name,passengers,location_id) " +
                    "VALUES(@flight_ID,@company_name,@passengers,@location_id)";
                cmd.Parameters.AddWithValue("@flight_ID", id);
                cmd.Parameters.AddWithValue("@company_name", flightPlan.company_name);
                cmd.Parameters.AddWithValue("@passengers", flightPlan.passengers);
                cmd.Parameters.AddWithValue("@location_id", id1);
                cmd.Prepare();
                cmd.ExecuteNonQuery();

                // Inserting Segments
                foreach (var segment in flightPlan.segments)
                {
                    cmd.CommandText =
                        "INSERT INTO Segments(longitude,latitude,timespan_seconds,flight_ID) " +
                        "VALUES(@longitude,@latitude,@timespan_seconds,@flight_ID)";
                    cmd.Parameters.AddWithValue("@longitude", segment.longitude);
                    cmd.Parameters.AddWithValue("@latitude", segment.latitude);
                    cmd.Parameters.AddWithValue("@timespan_seconds", segment.timespan_seconds);
                    cmd.Parameters.AddWithValue("@flight_ID", id);
                    cmd.Prepare();
                    cmd.ExecuteNonQuery();
                }
            }
            return(flightPlan);
        }
Exemple #9
0
        //throw exception if not found
        public FlightPlan GetFlightPlanById(string id)
        {
            //get flight plan from DB by specific id
            FlightPlan f   = null;
            string     stm = "SELECT * FROM FlightPlanTable WHERE id=\'" + id + "\'";

            using var cmd1             = new SQLiteCommand(stm, connection);
            using SQLiteDataReader rdr = cmd1.ExecuteReader();
            if (rdr.Read())
            {
                List <Segment> seg = GetSegmentById(rdr.GetString(0));

                InitialLocation initLoc = new InitialLocation(
                    rdr.GetDouble(4), rdr.GetDouble(5), rdr.GetString(3));
                f = new FlightPlan(rdr.GetInt32(2),
                                   rdr.GetString(1), initLoc, seg);
            }
            return(f);
        }
 /**
  * Checks if the flightPlan location with valid parameters
  **/
 public static bool IsValidLocation(InitialLocation location)
 {
     if (location.Longitude < -180 || location.Longitude > 180)
     {
         return(false);
     }
     else if (location.Latitude < -90 || location.Latitude > 90)
     {
         return(false);
     }
     else if (location.DateTime == DateTime.MinValue)
     {
         return(false);
     }
     else
     {
         return(true);
     }
 }
Exemple #11
0
        public List <FlightPlanId> GetFlightPlans()
        {
            //get all flight plan from DB
            List <FlightPlanId> flights = new List <FlightPlanId>();
            string stm = "SELECT * FROM FlightPlanTable";

            using var cmd1             = new SQLiteCommand(stm, connection);
            using SQLiteDataReader rdr = cmd1.ExecuteReader();

            //(id, companyName, passengers, dateTime, longitude, latitude )
            while (rdr.Read())
            {
                List <Segment>  seg     = GetSegmentById(rdr.GetString(0));
                InitialLocation initLoc = new InitialLocation(
                    rdr.GetDouble(4), rdr.GetDouble(5), rdr.GetString(3));
                flights.Add(new FlightPlanId(rdr.GetString(0), new FlightPlan(
                                                 rdr.GetInt32(2), rdr.GetString(1), initLoc, seg)));
            }
            return(flights);
        }
Exemple #12
0
        public List <Flight> GetInternalFlights(DateTime dateTime)
        {
            List <Flight> internals = new List <Flight>();

            using (SQLiteConnection cnn = new SQLiteConnection(LoadConnectionString()))
            {
                //open the connection
                cnn.Open();

                SQLiteCommand sqlComm = cnn.CreateCommand();
                sqlComm = new SQLiteCommand("SELECT * FROM FlightPlans", cnn);
                SQLiteDataReader    reader      = sqlComm.ExecuteReader();
                MyFlightPlanManager planManager = new MyFlightPlanManager();
                // For each flightPlan in the database, we will create p1 Flight object
                while (reader.Read())
                {
                    string     flightPlanID = reader["flight_id"].ToString();
                    FlightPlan flightPlan   = planManager.GetFlightPlanByID(flightPlanID).Result;
                    if (OnTime(flightPlan, dateTime))
                    {
                        InitialLocation flightLocation = GetPosition(flightPlan, dateTime);
                        DateTime        dateTime1      = dateTime;
                        string          date1          = dateTime1.ToString();
                        internals.Add(new Flight()
                        {
                            passengers   = flightPlan.passengers,
                            flight_id    = flightPlanID,
                            Company_name = flightPlan.company_name,
                            isExternal   = false,
                            Longitude    = flightLocation.longitude,
                            Latitude     = flightLocation.latitude,
                            date_time    = date1
                        });
                    }
                }
                reader.Close();
            }
            return(internals);
        }
        // Make flight plan from json if it's valid.
        private FlightPlan MakeFlightPlanFromJson(JToken flightPlan)
        {
            if (flightPlan == null)
            {
                return(null);
            }
            int    passengers  = (int)flightPlan["passengers"];
            string companyName = (string)flightPlan["company_name"];
            double longitude   = (double)flightPlan["initial_location"]["longitude"];
            double latitude    = (double)flightPlan["initial_location"]["latitude"];
            string dateTime    = (string)flightPlan["initial_location"]["date_time"];

            if (companyName == null || dateTime == null)
            {
                return(null);
            }

            // if it's not a valid date and time throws exception
            DateTime.Parse(dateTime);
            var location     = new InitialLocation(longitude, latitude, dateTime);
            var jsonSegments = (JArray)flightPlan["segments"];
            var segments     = new List <Segment>();

            foreach (var segment in jsonSegments)
            {
                double longitudeSegment = (double)segment["longitude"];
                double latitudeSegment  = (double)segment["latitude"];
                double timeSpan         = (double)segment["timespan_seconds"];
                if (!IsValidLonLat(longitudeSegment, latitudeSegment) || timeSpan <= 0)
                {
                    return(null);
                }
                var newSegment = new Segment(longitudeSegment, latitudeSegment, timeSpan);
                segments.Add(newSegment);
            }

            return(new FlightPlan(passengers, companyName, location, segments));
        }
        public async Task <FlightPlan> GetFlightPlanByID(string flightID)
        {
            using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString()))
            {
                // If we get the ID with {}
                if (flightID.Contains('{'))
                {
                    int k = flightID.Length - 1;
                    flightID = flightID.Remove(flightID.Length - 1);
                    flightID = flightID.Remove(0, 1);
                }
                // Checking if the Flight is in the DB
                using var con = new SQLiteConnection(LoadConnectionString());
                con.Open();
                SQLiteCommand cmd =
                    new SQLiteCommand("SELECT * FROM FlightPlans WHERE flight_ID = " +
                                      "\"" + flightID + "\"", con);
                SQLiteDataReader reader = cmd.ExecuteReader();
                reader.Read();
                if (reader.HasRows) // The FlightPlan we want is in the database
                {
                    // When we are asked to retrieve a flight plan, we will fetch it from the data base
                    string company_name = reader["company_name"].ToString();
                    int    passengers   = Int32.Parse(reader["passengers"].ToString());
                    string location_ID  = reader["location_ID"].ToString();
                    reader.Close();

                    // Retrieving the the location
                    cmd = new SQLiteCommand("SELECT * FROM Locations WHERE id = " +
                                            "\"" + location_ID + "\"", con);
                    reader = cmd.ExecuteReader();
                    reader.Read();
                    InitialLocation initialLocation = new InitialLocation
                    {
                        longitude = Double.Parse(reader["longitude"].ToString()),
                        latitude  = Double.Parse(reader["latitude"].ToString()),
                        date_time = reader["date"].ToString()
                    };
                    reader.Close();

                    // Retrieving the segments
                    List <Segment> segments = new List <Segment>();
                    cmd = new SQLiteCommand("SELECT * FROM Segments WHERE flight_ID = " +
                                            "\"" + flightID + "\"", con);
                    reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        segments.Add(new Segment()
                        {
                            longitude        = Double.Parse(reader["longitude"].ToString()),
                            latitude         = Double.Parse(reader["latitude"].ToString()),
                            timespan_seconds = Double.Parse(reader["timespan_seconds"].ToString())
                        });
                    }

                    // Creating the new Flight Plan from the data we gathered
                    return(new FlightPlan()
                    {
                        company_name = company_name,
                        passengers = passengers,
                        initial_location = initialLocation,
                        segments = segments
                    });
                }
                else // If the flight is not in the database, we will check if its in any of the servers
                {
                    SQLiteCommand sqlComm = con.CreateCommand();
                    sqlComm = new SQLiteCommand("SELECT * FROM Servers", con);
                    reader  = sqlComm.ExecuteReader();
                    MyFlightPlanManager planManager = new MyFlightPlanManager();
                    if (reader.HasRows)       // there are servers
                    {
                        while (reader.Read()) // iterating on all the servers
                        {
                            Server server = new Server
                            {
                                ServerID  = reader["ServerID"].ToString(),
                                ServerURL = reader["ServerURL"].ToString()
                            };
                            HttpClient client = new HttpClient();
                            try
                            {
                                string msg = server.ServerURL + "/api/FlightPlan/" + flightID;
                                HttpResponseMessage response = await client.GetAsync(msg);

                                response.EnsureSuccessStatusCode();
                                string responseBody = await response.Content.ReadAsStringAsync();

                                if (response.IsSuccessStatusCode && !responseBody.Contains("fail"))
                                {
                                    return(Newtonsoft.Json.JsonConvert.DeserializeObject <FlightPlan>(responseBody));
                                }
                                else
                                {
                                    continue; // the flightplan is not in this server
                                }
                            }
                            catch (HttpRequestException e)
                            {
                                Console.WriteLine("\nException Caught!");
                                Console.WriteLine("Message :{0} ", e.Message);
                            }
                            client.Dispose();
                        }
                        // There is no flight with that ID in the servers
                        return(null);
                    }
                    else // no servers - the flight id requested is not in the db nor the servers
                    {
                        return(null);
                    }
                }
            }
        }