Ejemplo n.º 1
0
        public int startLiveDownloadTask(DateTime pollingStart, DateTime pollingEnd, double pollingFrequency)
        {
            lasttimeTimeURLField = 0;
            int downloadState = 0;

            //NOTE: polling freq: 19s < bk freq: 60s < save freq: 3600s
            downloadedGPSXMLData_UNSAVED = new ConcurrentDictionary <Tuple <long, string>, Vehicle>(); //GPSTime & VehID as tuple index
            DateTime nextDownloadTime;                                                                 // = DateTime.Now.ToLocalTime().AddSeconds(pollingFrequency);
            DateTime nextBackupTime;                                                                   // = DateTime.Now.ToLocalTime().AddSeconds(bkSaveFreq);
            //DateTime nextSaveTime = DateTime.Now.ToLocalTime().AddSeconds(fileSaveFreq);
            DateTime nextSaveTime;                                                                     // = pollingStart.Date.AddSeconds(pollingStart.Hour * 3600 - 1 + fileSaveFreq);//save on the last second of the next hour
            long     lastLogUpdatedSave = 0;
            string   bkfilename         = "current-GPSXML-bk.xml";

            ////load last saved backup, if it is from within the last 5 minutes
            //if ((DateTime.Now.DateTimeToEpochTime() - File.GetLastWriteTime(bkfilename).DateTimeToEpochTime()) < (5 * 60))
            //{
            //    List<Vehicle> bkGPSPoints = new List<Vehicle>();
            //    bkGPSPoints = DeSerializeXMLObject<List<Vehicle>>(xmlGPSFolderPath + bkfilename);
            //    foreach (Vehicle aGPSPoint in bkGPSPoints)
            //    {
            //        downloadedGPSXMLData_UNSAVED.AddOrUpdate(new Tuple<long, string>(aGPSPoint.GPStime, aGPSPoint.Id), aGPSPoint, (k, v) => aGPSPoint);//replaces existing if it exists
            //    }
            //}

            if (pollingStart.DateTimeToEpochTime() >= DateTime.Now.ToLocalTime().DateTimeToEpochTime())//future download - put thread to sleep until start - proper state
            {
                //return pollingStart.DateTimeToEpochTime() - downloadStartTime.DateTimeToEpochTime();
                Thread.Sleep(Convert.ToInt32(1000 * (pollingStart.DateTimeToEpochTime() - DateTime.Now.ToLocalTime().DateTimeToEpochTime() - 1)));
                nextDownloadTime = pollingStart.AddSeconds(0);
                nextBackupTime   = pollingStart.AddSeconds(0);
                nextSaveTime     = pollingStart.Date.AddSeconds(pollingStart.Hour * 3600 - 1 + fileSaveFreq);                                                                                           //assume file save frequency is at the hour
            }
            else if ((pollingStart.DateTimeToEpochTime() <= DateTime.Now.ToLocalTime().DateTimeToEpochTime()) && (pollingEnd.DateTimeToEpochTime() > DateTime.Now.ToLocalTime().DateTimeToEpochTime())) //some missing downloads - start period is before download can take place
            {
                //polling reschedule message
                LogUpdateEventArgs args = new LogUpdateEventArgs();
                args.logMessage = String.Format("NextBus Polling to Start at: {0}.", DateTime.Now.ToLocalTime().Date.AddSeconds(DateTime.Now.ToLocalTime().Hour * 3600 + fileSaveFreq).DateTimeISO8601Format());
                OnLogUpdate(args);

                Thread.Sleep(Convert.ToInt32(1000 * (3600 - (DateTime.Now.ToLocalTime().Minute * 60 + DateTime.Now.ToLocalTime().Second - 1))));
                downloadState    = -1;
                nextDownloadTime = DateTime.Now.ToLocalTime();
                nextBackupTime   = DateTime.Now.ToLocalTime();
                nextSaveTime     = DateTime.Now.ToLocalTime().Date.AddSeconds(DateTime.Now.ToLocalTime().Hour * 3600 - 1 + fileSaveFreq);//assume file save frequency is at the hour
            }
            else //if (pollingEnd.DateTimeToEpochTime() <= DateTime.Now.ToLocalTime().DateTimeToEpochTime())//nothing to download - end period is before download can take place
            {
                nextDownloadTime = new DateTime();
                nextBackupTime   = new DateTime();
                nextSaveTime     = new DateTime();
                downloadState    = -2;
                return(downloadState);
            }

            //initial save message
            if (lastLogUpdatedSave != nextSaveTime.DateTimeToEpochTime())
            {
                lastLogUpdatedSave = nextSaveTime.DateTimeToEpochTime();
                LogUpdateEventArgs args = new LogUpdateEventArgs();
                args.logMessage = String.Format("NextBus Upcoming Save: {0}.", nextSaveTime.DateTimeISO8601Format());
                OnLogUpdate(args);
            }

            //download started after a proper wait - proper state, or start immediate if some data can be downloaded (state: -1)
            while (pollingEnd.DateTimeToEpochTime() >= DateTime.Now.ToLocalTime().DateTimeToEpochTime())
            {
                List <long> nextOpTime = new List <long>();
                nextOpTime.Add(nextDownloadTime.DateTimeToEpochTime());
                nextOpTime.Add(nextBackupTime.DateTimeToEpochTime());
                nextOpTime.Add(nextSaveTime.DateTimeToEpochTime());
                nextOpTime.Sort();
                if ((nextOpTime[0] > DateTime.Now.ToLocalTime().DateTimeToEpochTime()))
                {
                    if (lastLogUpdatedSave != nextSaveTime.DateTimeToEpochTime())
                    {
                        lastLogUpdatedSave = nextSaveTime.DateTimeToEpochTime();
                        LogUpdateEventArgs args = new LogUpdateEventArgs();
                        args.logMessage = String.Format("NextBus Upcoming Save: {0}.", nextSaveTime.DateTimeISO8601Format());
                        OnLogUpdate(args);
                    }

                    long sleepTime = nextOpTime[0] - DateTime.Now.ToLocalTime().DateTimeToEpochTime();
                    Thread.Sleep(Convert.ToInt32(1000 * sleepTime));//brief sleep in between operations
                }
                //this condition structure prioritize download to mem, then backup, then save
                if (nextDownloadTime.DateTimeToEpochTime() <= DateTime.Now.ToLocalTime().DateTimeToEpochTime())
                {
                    nextDownloadTime = nextDownloadTime.AddSeconds(pollingFrequency);
                    downloadCurrentGPSXMLFromWeb(pollingFrequency);
                    //List<Vehicle> newGPSPoints = downloadCurrentGPSXMLFromWeb(pollingFrequency);
                    //foreach (Vehicle aGPSPoint in newGPSPoints)
                    //{
                    //    downloadedGPSXMLData_UNSAVED.AddOrUpdate(new Tuple<long, string>(aGPSPoint.GPStime, aGPSPoint.Id), aGPSPoint, (k, v) => aGPSPoint);//replaces existing if it exists
                    //}
                }
                else if (nextBackupTime.DateTimeToEpochTime() <= DateTime.Now.ToLocalTime().DateTimeToEpochTime())
                {
                    nextBackupTime = nextBackupTime.AddSeconds(bkSaveFreq);
                    SSUtil.SerializeXMLObject(downloadedGPSXMLData_UNSAVED.Values.ToList(), xmlGPSFolderPath + bkfilename);
                }
                else if (nextSaveTime.DateTimeToEpochTime() <= DateTime.Now.ToLocalTime().DateTimeToEpochTime())
                {
                    if (downloadedGPSXMLData_UNSAVED.Values.Count > 0)//avoid saving empty file
                    {
                        string newfilename = string.Format(xmlGPSFilenameFormat, nextSaveTime.AddSeconds(-fileSaveFreq + 1).ToUniversalTime().DateTimeNamingFormat(), nextSaveTime.ToUniversalTime().DateTimeNamingFormat());
                        SSUtil.SerializeXMLObject(downloadedGPSXMLData_UNSAVED.Values.ToList(), xmlGPSFolderPath + newfilename);
                        downloadedGPSXMLData_UNSAVED.Clear();//clear mem
                    }
                    nextSaveTime = nextSaveTime.AddSeconds(fileSaveFreq);
                }
            }
            return(downloadState);
            //downloadState = -2;//no download has taken place, date out of range
            //downloadState = -1;//download completed with some error - some missing data
            //downloadState = 0;//download completed with no error
        }
        public int startLiveDownloadTask(DateTime pollingStart, DateTime pollingEnd, double pollingFrequency)
        {
            //fileSaveFreq must be 1 day, otherwise, needs to modify nextSaveTime
            int downloadState = 0;

            //NOTE: polling freq: 60s < bk freq: 120s < save freq: 3600s * 24 = 1 day
            downloadedINCIXMLData_UNSAVED = new ConcurrentDictionary <string, Closure>(); //INCITime & VehID as tuple index
            DateTime nextDownloadTime;                                                    // = DateTime.Now.ToLocalTime().AddSeconds(pollingFrequency);
            DateTime nextBackupTime;                                                      // = DateTime.Now.ToLocalTime().AddSeconds(bkSaveFreq);
            //DateTime nextSaveTime = DateTime.Now.ToLocalTime().AddSeconds(fileSaveFreq);
            DateTime nextSaveTime;                                                        // = pollingStart.Date.AddSeconds(pollingStart.Hour * 3600 - 1 + fileSaveFreq);//save on the last second of the next hour
            long     lastLogUpdatedSave = 0;
            string   bkfilename         = "current-RRXML-bk.xml";

            //load last saved backup, if it is from within the last 5 minutes
            if ((DateTime.Now.DateTimeToEpochTime() - File.GetLastWriteTime(bkfilename).DateTimeToEpochTime()) < (5 * 60))
            {
                List <Closure> bkINCIPoints = new List <Closure>();
                bkINCIPoints = SSUtil.DeSerializeXMLObject <List <Closure> >(xmlINCIFolderPath + bkfilename);
                foreach (Closure aINCIPoint in bkINCIPoints)
                {
                    downloadedINCIXMLData_UNSAVED.AddOrUpdate(aINCIPoint.Id, aINCIPoint, (k, v) => aINCIPoint);//replaces existing if it exists
                }
            }

            if (pollingStart.DateTimeToEpochTime() >= DateTime.Now.ToLocalTime().DateTimeToEpochTime())//future download - put thread to sleep until start - proper state
            {
                //return pollingStart.DateTimeToEpochTime() - downloadStartTime.DateTimeToEpochTime();
                Thread.Sleep(Convert.ToInt32(1000 * (pollingStart.DateTimeToEpochTime() - DateTime.Now.ToLocalTime().DateTimeToEpochTime() - 1)));
                nextDownloadTime = pollingStart.AddSeconds(0);
                nextBackupTime   = pollingStart.AddSeconds(0);
                nextSaveTime     = DateTime.Now.ToLocalTime().Date.AddSeconds(-1 + fileSaveFreq);                                                                                                       //asume save frequencies are in the scale of days, save at midnighht
            }
            else if ((pollingStart.DateTimeToEpochTime() <= DateTime.Now.ToLocalTime().DateTimeToEpochTime()) && (pollingEnd.DateTimeToEpochTime() > DateTime.Now.ToLocalTime().DateTimeToEpochTime())) //some missing downloads - start period is before download can take place
            {
                //download at next hour, catch whatever data can be retrived.
                //Thread.Sleep(Convert.ToInt32(1000 * (3600 - (DateTime.Now.ToLocalTime().Minute * 60 + DateTime.Now.ToLocalTime().Second) - 1)));
                downloadState    = -1;
                nextDownloadTime = DateTime.Now.ToLocalTime().AddSeconds(0);                      //no delay for download
                nextBackupTime   = DateTime.Now.ToLocalTime().AddSeconds(0);                      //no delay for backup
                nextSaveTime     = DateTime.Now.ToLocalTime().Date.AddSeconds(-1 + fileSaveFreq); //asume save frequencies are in the scale of days, save at midnighht
            }
            else //if (pollingEnd.DateTimeToEpochTime() <= DateTime.Now.ToLocalTime().DateTimeToEpochTime())//nothing to download - end period is before download can take place
            {
                nextDownloadTime = new DateTime();
                nextBackupTime   = new DateTime();
                nextSaveTime     = new DateTime();
                downloadState    = -2;
                return(downloadState);
            }

            //initial save message
            if (lastLogUpdatedSave != nextSaveTime.DateTimeToEpochTime())
            {
                lastLogUpdatedSave = nextSaveTime.DateTimeToEpochTime();
                LogUpdateEventArgs args = new LogUpdateEventArgs();
                args.logMessage = String.Format("Rd Closures Upcoming Save: {0}.", nextSaveTime.DateTimeISO8601Format());
                OnLogUpdate(args);
            }

            //download started after a proper wait - proper state, or start immediate if some data can be downloaded (state: -1)
            while (pollingEnd.DateTimeToEpochTime() >= DateTime.Now.ToLocalTime().DateTimeToEpochTime())
            {
                List <long> nextOpTime = new List <long>();
                nextOpTime.Add(nextDownloadTime.DateTimeToEpochTime());
                nextOpTime.Add(nextBackupTime.DateTimeToEpochTime());
                nextOpTime.Add(nextSaveTime.DateTimeToEpochTime());
                nextOpTime.Sort();
                if ((nextOpTime[0] > DateTime.Now.ToLocalTime().DateTimeToEpochTime()))
                {
                    if (lastLogUpdatedSave != nextSaveTime.DateTimeToEpochTime())
                    {
                        lastLogUpdatedSave = nextSaveTime.DateTimeToEpochTime();
                        LogUpdateEventArgs args = new LogUpdateEventArgs();
                        args.logMessage = String.Format("Rd Closures Upcoming Save: {0}.", nextSaveTime.DateTimeISO8601Format());
                        OnLogUpdate(args);
                    }

                    long sleepTime = nextOpTime[0] - DateTime.Now.ToLocalTime().DateTimeToEpochTime();
                    Thread.Sleep(Convert.ToInt32(1000 * sleepTime));//brief sleep in between operations
                }
                //this condition structure prioritize download to mem, then backup, then save
                if (nextDownloadTime.DateTimeToEpochTime() <= DateTime.Now.ToLocalTime().DateTimeToEpochTime())
                {
                    nextDownloadTime = nextDownloadTime.AddSeconds(pollingFrequency);
                    downloadCurrentINCIXMLFromWeb(pollingFrequency);
                    //List < Closure > newINCIPoints = downloadCurrentINCIXMLFromWeb(pollingFrequency);
                    //foreach (Closure aINCIPoint in downloadedINCIXML)
                    //{
                    //    downloadedINCIXMLData_UNSAVED.AddOrUpdate(aINCIPoint.Id, aINCIPoint, (k, v) => aINCIPoint);//replaces existing if it exists
                    //}
                }
                else if (nextBackupTime.DateTimeToEpochTime() <= DateTime.Now.ToLocalTime().DateTimeToEpochTime())
                {
                    nextBackupTime = nextBackupTime.AddSeconds(bkSaveFreq);
                    SSUtil.SerializeXMLObject(downloadedINCIXMLData_UNSAVED.Values.ToList(), xmlINCIFolderPath + bkfilename);
                }
                else if (nextSaveTime.DateTimeToEpochTime() <= DateTime.Now.ToLocalTime().DateTimeToEpochTime())
                {
                    if (downloadedINCIXMLData_UNSAVED.Values.Count > 0)//avoid saving empty file
                    {
                        string newfilename = string.Format(xmlINCIFilenameFormat, nextSaveTime.AddSeconds(-fileSaveFreq + 1).ToUniversalTime().DateTimeNamingFormat(), nextSaveTime.ToUniversalTime().DateTimeNamingFormat());
                        SSUtil.SerializeXMLObject(downloadedINCIXMLData_UNSAVED.Values.ToList(), xmlINCIFolderPath + newfilename);
                        downloadedINCIXMLData_UNSAVED.Clear();//clear mem
                    }
                    nextSaveTime = nextSaveTime.AddSeconds(fileSaveFreq);
                }
            }
            return(downloadState);
            //downloadState = -2;//no download has taken place, date out of range
            //downloadState = -1;//download completed with some error - some missing data
            //downloadState = 0;//download completed with no error
        }