예제 #1
0
        private void BusInfoAvailable(object senderDUMMY, EventArgs eaDUMMY)
        {
            if (m_disposed)
            {
                return;
            }
            BusInfoAvailableEventArgs e = m_fetchedInfoResult;

            m_refreshing = true;

            // Don't include buses that left more than x minutes ago
            ArrayList listToDisplay = new ArrayList(e.BusInfos.Count);

            for (int i = 0; i < e.BusInfos.Count; i++)
            {
                BusInfo b = (BusInfo)e.BusInfos[i];
                if (b.MinutesToDeparture >= e.LoadedStop.TimeThreshold)
                {
                    listToDisplay.Add(b);
                }
            }

            // Convert to an array
            BusInfo[] infosAsArray = new BusInfo[listToDisplay.Count];
            for (int i = 0; i < listToDisplay.Count; i++)
            {
                infosAsArray[i] = (BusInfo)listToDisplay[i];
            }

            m_stopControls[e.StopIndex].Refresh(infosAsArray);

            m_refreshing = false;
        }
예제 #2
0
 private void m_infoFetcher_BusInfoAvailable(object sender, BusInfoAvailableEventArgs e)
 {
     if (!m_disposed)
     {
         m_fetchedInfoResult = e;
         Invoke(new EventHandler(BusInfoAvailable));
     }
 }
예제 #3
0
        private void MainRoutine()
        {
            int errorCount = 0;

            if (m_shouldRun)
            {
                m_shouldRun = false;

                for (int i = 0; i < m_stopsToLoad.Length && errorCount < ERROR_LIMIT; i++)
                {
                    try
                    {
                        StopLocation   sl  = m_stopsToLoad[i];
                        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(sl.Url);
                        req.Timeout   = 10000;
                        req.KeepAlive = true;
                        WebResponse  response      = req.GetResponse();
                        Stream       receiveStream = response.GetResponseStream();
                        Encoding     encoding      = Encoding.GetEncoding("utf-8");
                        StreamReader sr            = new StreamReader(receiveStream, encoding);
                        string       data          = sr.ReadToEnd();
                        sr.Close();

                        m_busInfoList.Clear();
                        for (Match m = m_busInfoRegex.Match(data); m.Success; m = m.NextMatch())
                        {
                            string routeNumber        = m.Groups[1].Value.Trim();
                            string destination        = m.Groups[2].Value.Trim();
                            string arrivalTime        = m.Groups[3].Value.Trim();
                            string arrivalStatus      = m.Groups[4].Value.Trim();
                            string minutesToDeparture = m.Groups[5].Value.Trim();

                            BusInfo b = new BusInfo();
                            b.RouteNumber   = routeNumber;
                            b.Destination   = HttpUtility.HtmlDecode(destination);
                            b.ArrivalTime   = arrivalTime;
                            b.ArrivalStatus = arrivalStatus;
                            if (minutesToDeparture.ToUpper().Equals("NOW"))
                            {
                                b.MinutesToDeparture = 0;
                                b.IsDepartingNow     = true;
                            }
                            else
                            {
                                b.IsDepartingNow     = false;
                                b.MinutesToDeparture = Convert.ToInt32(minutesToDeparture);
                            }

                            m_busInfoList.Add(b);
                        }

                        // A successful request. We're online!
                        errorCount = 0;
                        UpdateInfoSourceAvailable(true);

                        BusInfoAvailableEventArgs ea = new BusInfoAvailableEventArgs(i, m_busInfoList, sl);
                        BusInfoAvailable(this, ea);
                    }
                    catch (WebException)
                    {
                        // try again
                        i--;
                        errorCount++;
                    }
                    catch (SocketException)
                    {
                        // try again
                        i--;
                        errorCount++;
                    }
                }

                if (errorCount >= ERROR_LIMIT)
                {
                    UpdateInfoSourceAvailable(false);
                }
                else
                {
                    BusInfoFetchComplete(this, EventArgs.Empty);
                }
            }
        }