Esempio n. 1
0
        void Timer_Tick_DynamicDataUpdate(object state)
        {
            try
            {
                bool CarpoolingEnabled               = bool.Parse(ConfigurationManager.AppSettings["CarpoolingEnabled"]);
                bool TrafficEnabled                  = bool.Parse(ConfigurationManager.AppSettings["TrafficEnabled"]);
                DynamicDataVersion DynamicData       = new DynamicDataVersion {
                };
                CarPoolerDataVersioned CarPoolingTmp = new CarPoolerDataVersioned {
                };
                TrafficDataVersioned TrafficTmp      = new TrafficDataVersioned {
                };

                /* Get the dynamic data version from the backend web service */
                DynamicData = HTTPRequests.getDynamicDataVersionFromBackend(productionRoutingNetwork.MinPoint, productionRoutingNetwork.MaxPoint);

                if (DynamicData != null)
                {
                    if (CarpoolingEnabled)
                    {
                        /* Carpooling data version */
                        CarPoolingTmp.Version = new CarpoolerVersion(DynamicData.sites.First().carpooling_info.version,
                                                                     DynamicData.sites.First().carpooling_info.updated,
                                                                     DynamicData.sites.First().name,
                                                                     DynamicData.sites.First().carpooling_info.nightly_version,
                                                                     DynamicData.sites.First().carpooling_info.nightly_updated);
                        /* Update the carpooling network */
                        updateCarpoolingNetwork(CarPoolingTmp);
                    }

                    if (TrafficEnabled)
                    {
                        /* Traffic data version */
                        TrafficTmp.Version = new TrafficVersion(DynamicData.sites.First().reports_info.version,
                                                                DynamicData.sites.First().reports_info.updated,
                                                                DynamicData.sites.First().name);
                        /* Update the traffic network */
                        updateTrafficNetwork(TrafficTmp);
                    }
                }
            }
            finally
            {
                m_Timer.Change(DynamicDataIntervalTimeRequest, 0);
            }
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            log.Info("Application starts");

            string host                             = ConfigurationManager.AppSettings["host"];
            string port                             = ConfigurationManager.AppSettings["port"];
            string service                          = ConfigurationManager.AppSettings["service"];
            string protocol                         = ConfigurationManager.AppSettings["protocol"];
            bool   CarpoolingEnabled                = bool.Parse(ConfigurationManager.AppSettings["CarpoolingEnabled"]);
            bool   TrafficEnabled                   = bool.Parse(ConfigurationManager.AppSettings["TrafficEnabled"]);
            double TrafficPropagationMaxDistance    = float.Parse(ConfigurationManager.AppSettings["TrafficPropagationMaxDistance"]);
            int    CarpoolingMaxConnectionsPerTNode = int.Parse(ConfigurationManager.AppSettings["CarpoolingMaxConnectionsPerTNode"]);
            /* This variable is used to decide if update at startup the external carpooling rides to the network or wait the midnight */
            bool updateExtRidesAtStartup = bool.Parse(ConfigurationManager.AppSettings["ExtCarpoolingRideUpdateAtStartup"]);

            //var host = Dns.GetHostAddresses(Dns.GetHostName())[0];
            string url = protocol + "://" + host + ":" + port + service;

            log.Info(url);

            Notification.SendMessageByEmail(Notification.AlarmType.INFO, DateTime.Now.ToString() + "\r\nWebAPI starts at " + url + "\r\nBuilding network...");

            /* Get the site timeZone from the GTFS data (needed for the carpooling date caonversion) */
            string timeZone = null;

            timeZone = DBParser.GetTimeZoneFromAgencyTable();

            /* Get the boundaries from the OSM Map (needed for the backend carpooling http requests) */
            Point  MapMinPoint = null, MapMaxPoint = null;
            string OSMXMLMapFilePath = System.IO.Path.GetFullPath(AppDomain.CurrentDomain.BaseDirectory + ConfigurationManager.AppSettings["dataPath"] + ConfigurationManager.AppSettings["OSMXMLMapFile"]);

            log.Info("Parsing " + OSMXMLMapFilePath + " OSM.");
            routingNetwork1 = new Routing.RoutingNetwork();
            OSM.XMLParser Parser = new OSM.XMLParser();
            Parser.GetBoundaries(OSMXMLMapFilePath, ref MapMinPoint, ref MapMaxPoint);
            if ((MapMinPoint != null) && (MapMaxPoint != null))
            {
                log.Info("Map Boundaries: minlat:" + MapMinPoint.Latitude + ", minlon:" + MapMinPoint.Longitude + ", maxlat:" + MapMaxPoint.Latitude + ", maxlon:" + MapMaxPoint.Longitude);
            }
            else
            {
                string message = "Application ends\r\nMap boundaries not found into the OSM map: minlat:" + MapMinPoint.Latitude + ", minlon:" + MapMinPoint.Longitude + ", maxlat:" + MapMaxPoint.Latitude + ", maxlon:" + MapMaxPoint.Longitude;
                log.Error(message);
                Notification.SendMessageByEmail(Notification.AlarmType.ERROR, DateTime.Now.ToString() + "\r\n" + message);
                Environment.Exit(0);
            }


            /* Get the dynamic data version from the backend web service */
            DynamicData = HTTPRequests.getDynamicDataVersionFromBackend(MapMinPoint, MapMaxPoint);

            if (DynamicData != null)
            {
                if (CarpoolingEnabled)
                {
                    /* Set the Carpooling data version */
                    CarPooling.Version = new CarpoolerVersion(DynamicData.sites.First().carpooling_info.version,
                                                              DynamicData.sites.First().carpooling_info.updated,
                                                              DynamicData.sites.First().name,
                                                              DynamicData.sites.First().carpooling_info.nightly_version,
                                                              DynamicData.sites.First().carpooling_info.nightly_updated);
                    log.Info("CARPOOLING Version:" + CarPooling.Version.version + " Timestamp:" + CarPooling.Version.timestampVersion +
                             " NightlyVersion:" + CarPooling.Version.nightly_version + " NightlyTimestamp: " + CarPooling.Version.nightly_timestampVersion);

                    bool updateExternalRides = false;
                    if ((updateExtRidesAtStartup) || ((DateTime.Now.Hour < ExtCarpoolingAvailableUpdateTimeFrom) && (DateTime.Now.Hour >= ExtCarpoolingAvailableUpdateTimeTo)))
                    {
                        updateExternalRides = true;
                    }

                    /* Get the carpooling rides list from the backend web service */
                    CarPooling.Carpoolers = HTTPRequests.getCarpoolingDataFromBackend(MapMinPoint, MapMaxPoint, updateExternalRides);
                }

                if (TrafficEnabled)
                {
                    /* Set the Traffic/incidents data version */
                    Traffic.Version = new TrafficVersion(DynamicData.sites.First().reports_info.version,
                                                         DynamicData.sites.First().reports_info.updated,
                                                         DynamicData.sites.First().name);
                    log.Info("TRAFFIC Version:" + Traffic.Version.version + " Timestamp:" + Traffic.Version.timestampVersion);

                    /* Get the Traffic/incidents data from the backend web service */
                    Traffic.TrafficReport = HTTPRequests.getTrafficDataFromBackend(MapMinPoint, MapMaxPoint, TrafficPropagationMaxDistance);
                }
            }

            /* Build the network */
            routingNetwork1 = RoutePlanner.Globals.BuildNetwork(ref CParser, ref TParser, CarPooling.Carpoolers, Traffic.TrafficReport, TrafficPropagationMaxDistance, CarpoolingMaxConnectionsPerTNode);

            /* Copy the network */
            //routingNetwork2 = new RoutingNetwork { };
            //routingNetwork1.duplicateNetwork(ref routingNetwork2);

            log.Info("Starting server at " + url);
            Notification.SendMessageByEmail(Notification.AlarmType.INFO, DateTime.Now.ToString() + "\r\nService is running at " + url);
            HTTPAsyncServer Server = new HTTPAsyncServer(url);

            log.Error("Application ends");
            Notification.SendMessageByEmail(Notification.AlarmType.ERROR, "Application ends");
        }
Esempio n. 3
0
        void updateTrafficNetwork(TrafficDataVersioned TrafficTmp)
        {
            TrafficParser TParserTmp = null;

            //Just for testing: simulate a new data version
            //TrafficTmp.Version.version++;

            if ((Program.Traffic.Version != null) && (TrafficTmp.Version != null) &&
                (TrafficTmp.Version.version != Program.Traffic.Version.version))
            {
                log.Info("New Traffic data are available (prevVersion:" + Program.Traffic.Version.version + ", newVersion:" + TrafficTmp.Version.version + ")");

                /* Get the traffic reports from the backend web service */
                TrafficTmp.TrafficReport = HTTPRequests.getTrafficDataFromBackend(productionRoutingNetwork.MinPoint, productionRoutingNetwork.MaxPoint, productionRoutingNetwork.TrafficPropagationMaxDistance);

                /* Get differences between the old version and the new one */
                TrafficDiff TrafficDiff = TrafficParser.checkDifferences(Program.Traffic.TrafficReport, TrafficTmp.TrafficReport);

                if ((TrafficDiff.ElementsToRemove.Count > 0) || (TrafficDiff.ElementsToAdd.Count > 0))
                {
                    /* Update the network only when there are no pending threads (Monitor.Wait) */
                    lock (_ActiveWorkersLock)
                    {
                        while (_CountOfActiveWorkers > 0)
                        {
                            Monitor.Wait(_ActiveWorkersLock);
                        }

                        log.Info("Updating the production network with the traffic reports");

                        /* Remove the OLD Traffic reports from the network */
                        if (TrafficDiff.ElementsToRemove.Count > 0)
                        {
                            log.Info("Removing " + TrafficDiff.ElementsToRemove.Count + " deleted traffic reports from the processNetwork");
                            int i = 1;
                            foreach (TrafficReport el in TrafficDiff.ElementsToRemove)
                            {
                                productionRoutingNetwork.RemoveTrafficReport(el);

                                /* Update the original TrafficParser */
                                Program.TParser.TrafficReport.Remove(el);
                                i++;
                            }

                            log.Info("ProcessNetwork updated! (" + TrafficDiff.ElementsToRemove.Count + " old traffic reports deleted)");

                            /* Force the network update before processing the next request */
                            updateRN = true;
                        }

                        /* Add the NEW Traffic reports to the network */
                        if (TrafficDiff.ElementsToAdd.Count > 0)
                        {
                            /* Add to each connection the traffic report */
                            log.Info("Adding the new " + TrafficDiff.ElementsToAdd.Count + " traffic connections to the processNetwork");
                            TParserTmp = new TrafficParser(productionRoutingNetwork, TrafficDiff.ElementsToAdd);
                            TParserTmp.UpdateNetworkWithTrafficReport();

                            /* Update the original TParser and Traffic report classes */
                            foreach (TrafficReport el in TrafficDiff.ElementsToAdd)
                            {
                                Program.TParser.TrafficReport.Add(el);
                            }

                            log.Info("Processnetwork updated! (" + TrafficDiff.ElementsToAdd.Count + " new traffic reports added)");

                            /* Force the network update before processing the next request */
                            updateRN = true;
                        }

                        log.Info("Production network updated with the traffic reports");
                    }
                }


                /* If there are no modifications too, just update the version number (this happens for example if the driver quickly
                 * enable and disable a ride, so the version number changes but the content doesn't)
                 */
                /* Update the traffic report version */
                Program.Traffic.Version = new TrafficVersion(TrafficTmp.Version.version, TrafficTmp.Version.timestampVersion, TrafficTmp.Version.nameSite);
            }

            else
            {
                if (TrafficTmp.Version != null)
                {
                    //log.Info("Traffic version not changed: version=" + TrafficTmp.Version.version + " timestamp=" + TrafficTmp.Version.timestampVersion);
                }
            }
        }
Esempio n. 4
0
        void updateCarpoolingNetwork(CarPoolerDataVersioned CarPoolingTmp)
        {
            CarpoolParser CParserTmp = null;

            //JUST FOR TESTING: simulate a new data version
            //CarPoolingTmp.Version.version++;

            if ((Program.CarPooling.Version != null) && (CarPoolingTmp.Version != null) &&
                ((CarPoolingTmp.Version.version != Program.CarPooling.Version.version) ||
                 (CarPoolingTmp.Version.nightly_version != Program.CarPooling.Version.nightly_version)))
            {
                log.Info("New Carpooling data are available (prevVersion:" + Program.CarPooling.Version.version + ", newVersion:" + CarPoolingTmp.Version.version + ")");
                log.Info("New Carpooling data are available (nightlyPrevVersion:" + Program.CarPooling.Version.nightly_version + ", newNightlyVersion:" + CarPoolingTmp.Version.nightly_version + ")");

                bool updateExternalRides = false;
                if ((DateTime.Now.Hour >= Program.ExtCarpoolingAvailableUpdateTimeFrom) && (DateTime.Now.Hour < Program.ExtCarpoolingAvailableUpdateTimeTo))
                {
                    updateExternalRides = true;
                }

                /* Get the carpooling rides list from the backend web service */
                CarPoolingTmp.Carpoolers = HTTPRequests.getCarpoolingDataFromBackend(productionRoutingNetwork.MinPoint, productionRoutingNetwork.MaxPoint, updateExternalRides);

                ////JUST FOR TESTING: modify element
                //CarPoolingTmp.Carpoolers.First().Id = "pizza";

                ////Just for testing: add new element
                //Carpooler test = new Carpooler("drtgwet", 2789, 4);
                //test.WayPoints.Add(new Point(46.013456, 8.943813));
                //test.WayPoints.Add(new Point(46.011596, 8.964293));
                //CarPoolingTmp.Carpoolers.Add(test);

                ////Just for testing: remove element
                //Carpooler testremove = CarPoolingTmp.Carpoolers.First();
                //CarPoolingTmp.Carpoolers.Remove(testremove);

                ////Just for testing: add modified element
                //Carpooler testaddandchange = CarPoolingTmp.Carpoolers.First();
                //testaddandchange.WayPoints.Add(new Point(1, 1));
                //testaddandchange.WayPoints.Add(new Point(2, 2));
                //CarPoolingTmp.Carpoolers.Add(testaddandchange);


                /* Get differences between the old version and the new one */
                CarpoolDiff CarPoolingDiff = CarpoolParser.checkDifferences(Program.CarPooling.Carpoolers, CarPoolingTmp.Carpoolers, updateExternalRides);

                if ((CarPoolingDiff.ElementsToRemove.Count > 0) || (CarPoolingDiff.ElementsToAdd.Count > 0))
                {
                    /* Update the network only when there are no pending threads (Monitor.Wait) */
                    lock (_ActiveWorkersLock)
                    {
                        while (_CountOfActiveWorkers > 0)
                        {
                            Monitor.Wait(_ActiveWorkersLock);
                        }

                        log.Info("Updating the production network with the carpooling rides");

                        /* Remove the OLD Carpooling connections from the network */
                        if (CarPoolingDiff.ElementsToRemove.Count > 0)
                        {
                            log.Info("Removing " + CarPoolingDiff.ElementsToRemove.Count + " deleted carpooling connections from the processNetwork");

                            foreach (Carpooler el in CarPoolingDiff.ElementsToRemove)
                            {
                                productionRoutingNetwork.RemoveConnection(el);

                                /* Update the original CParser and Carpooling classes */
                                Program.CParser.CarPoolers.Remove(el);
                            }

                            log.Info("ProcessNetwork updated! (" + CarPoolingDiff.ElementsToRemove.Count + " old carpooling connections deleted)");

                            /* Force the network update before processing the next request */
                            updateRN = true;
                        }

                        /* Add the NEW Carpooling connections to the network */
                        if (CarPoolingDiff.ElementsToAdd.Count > 0)
                        {
                            /* Construct the new Carpooling network */
                            log.Info("Adding " + CarPoolingDiff.ElementsToAdd.Count + " new carpooling connections to the processNetwork");
                            CParserTmp = new CarpoolParser(productionRoutingNetwork, CarPoolingDiff.ElementsToAdd);
                            CParserTmp.ConnectWithRoadNetwork();

                            /* Update the original CParser and Carpooling classes */
                            foreach (Carpooler el in CarPoolingDiff.ElementsToAdd)
                            {
                                Program.CParser.CarPoolers.Add(el);
                            }

                            log.Info("Processnetwork updated! (" + CarPoolingDiff.ElementsToAdd.Count + " new carpooling connections added)");

                            /* Force the network update before processing the next request */
                            updateRN = true;
                        }

                        log.Info("Production network updated");
                    }
                }

                if (CarPoolingDiff.ElementsToIgnore.Count > 0)
                {
                    log.Info(CarPoolingDiff.ElementsToIgnore.Count + " rides unchanged");
                }

                /* If there are no modifications too, just update the version number (this happens for example if the driver quickly
                 * enable and disable a ride, so the version number changes but the content doesn't)
                 */
                /* Update the carpooling version */
                Program.CarPooling.Version = new CarpoolerVersion(CarPoolingTmp.Version.version, CarPoolingTmp.Version.timestampVersion, CarPoolingTmp.Version.nameSite,
                                                                  CarPoolingTmp.Version.nightly_version, CarPoolingTmp.Version.nightly_timestampVersion);
            }
            else
            {
                if (CarPoolingTmp.Version != null)
                {
                    //log.Info("Carpooling version not changed: version=" + CarPoolingTmp.Version.version + " timestamp=" + CarPoolingTmp.Version.timestampVersion);
                }
            }
        }