public List <Flights> getFlightsList(IMemoryCache cache, string queryString, string relativeTo) { //if those word exist in the request- return a list of a all the flights. bool syncAll = queryString.Contains("sync_all"); List <Flights> FlightList; Dictionary <string, FlightPlan> FlightPlanDictionary; //check if the dictionary exist in cache- if so, return a list of all the flights if (cache.TryGetValue("FlightPlanDictionary", out FlightPlanDictionary)) { FlightManager FlightManager = new FlightManager(); FlightPlanManager flightPlanManager = new FlightPlanManager(); List <string> FlightPlanListId = new List <string>(); if (!cache.TryGetValue("FlightPlanListId", out FlightPlanListId)) { FlightPlanListId = flightPlanManager.getFlightPlanListId(); cache.Set("FlightPlanListId", FlightPlanListId); } FlightList = FlightManager.createFlightList(FlightPlanDictionary, relativeTo, syncAll, FlightPlanListId); return(FlightList); } //else- return all external flights else if (syncAll) { ServerManager serverManager = new ServerManager(); FlightList = serverManager.externalFlightList(relativeTo); return(FlightList); } return(null); }
//create a list from the flights. public List <Flights> createFlightList(Dictionary <string, FlightPlan> myFlightPlanDictionary, string relativeTo, bool syncAll, List <string> myFlightPlanListId) { //make new objects. List <Flights> FlightList = new List <Flights>(); Dictionary <string, FlightPlan> FlightPlanDictionary = myFlightPlanDictionary; FlightPlanManager FlightPlanManager = new FlightPlanManager(); // List<string> FlightPlanListId = FlightPlanManager.getFlightPlanListId(); List <string> FlightPlanListId = myFlightPlanListId; int i; //check if to sync the flights. bool externalList = checkSyncAll(relativeTo); if (syncAll) { ServerManager serverManager = new ServerManager(); List <Flights> externalFlights = serverManager.externalFlightList(relativeTo); int l; for (l = 0; l < externalFlights.Count; l++) { FlightList.Add(externalFlights[l]); } } for (i = 0; i < FlightPlanListId.Count; i++) { string Id = FlightPlanListId[i]; FlightPlan FlightPlanObject; //get a flight plan by id. if (!FlightPlanDictionary.TryGetValue(Id, out FlightPlanObject)) { continue; } Flights Flights; //if this flight isn't active- continue. if (!activeFligth(FlightPlanObject, relativeTo)) { continue; } //if this flight exist- create flight from flight plan. else { Flights = createFlight(FlightPlanObject, Id, relativeTo); } //if this flight is external, and we need only internal flight- continue. if ((!syncAll) && (Flights.is_external == true)) { continue; } //if this flight is external and we need external flights- add it to the list. else { FlightList.Add(Flights); } } //return the flight list. return(FlightList); }
//create flight plan from json. public FlightPlan createFlightPlan(string value) { FlightPlanManager FlightPlanManager = new FlightPlanManager(); JObject data = JObject.Parse(value); JToken jToken = data; FlightPlan FlightPlanObject = new FlightPlan(); FlightPlanObject.passengers = (int)jToken["passengers"]; FlightPlanObject.company_name = (string)jToken["company_name"]; //parse "initial_location" property Object initialLocation = (Object)jToken["initial_location"]; JObject dataInitialLocation = JObject.Parse(initialLocation.ToString()); JToken jTokenLoc = dataInitialLocation; double longitude = (double)jTokenLoc["longitude"]; double latitude = (double)jTokenLoc["latitude"]; string date_time = (string)jTokenLoc["date_time"]; FlightPlanObject.loc = new initial_location(longitude, latitude, date_time); //parse "segments" property Object segmentObj = (Object)jToken["segments"]; JArray jsonArray = JArray.Parse(segmentObj.ToString()); int i; dynamic segmentData; for (i = 0; i < jsonArray.Count; i++) { segmentData = JObject.Parse(jsonArray[i].ToString()); longitude = (double)segmentData["longitude"]; latitude = (double)segmentData["latitude"]; int timespan_seconds = (int)segmentData["timespan_seconds"]; string segmentEndTime = FlightPlanManager.TimeCalculate(date_time, i, timespan_seconds, FlightPlanObject); FlightPlanObject.segmentsList.Add(new segment(segmentEndTime, longitude, latitude, timespan_seconds)); } FlightPlanObject.segments = FlightPlanObject.segmentsList; return(FlightPlanObject); }
//exept flight plan from external server by id. public FlightPlan externalFlightPlanById(string Id, string serverId) { Dictionary <string, Server> ServersDictionary = getServersDictionary(); FlightPlan FlightPlanObject; Server serverObject; if (ServersDictionary.TryGetValue(serverId, out serverObject)) { string url = serverObject.ServerURL + "/api/FlightPlan/" + Id; string strFlightPlan = connectToExternalServer(url); FlightPlanManager FlightPlanManager = new FlightPlanManager(); FlightPlanObject = createFlightPlan(strFlightPlan); return(FlightPlanObject); } else { //error } return(null); }
internal FlightManager(FlightPlanManager flightPlanManager, DBInteractor newDB) { this.db = newDB; this.flightPlanManager = flightPlanManager; }