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; }
public void UpdateBusInfo(BusInfo b) { string route = b.RouteNumber; if (route.Length > 3) { route = route.Substring(0, 3); } m_routeNumberLabel.Text = route; m_destinationLabel.Text = GetCanonicalDestinationText(b.Destination); if (b.IsDepartingNow) { m_departureLabel.Text = "!"; } else { m_departureLabel.Text = b.MinutesToDeparture.ToString(); } if (b.IsDepartingNow) { m_departureLabel.ForeColor = ColorService.DEPARTING_BUS; } else if (b.ArrivalStatus.ToLower().IndexOf("on time") >= 0) { m_departureLabel.ForeColor = ColorService.ON_TIME_BUS; } else if (b.ArrivalStatus.ToLower().IndexOf("early") >= 0) { m_departureLabel.ForeColor = ColorService.EARLY_BUS; } else if (b.ArrivalStatus.ToLower().IndexOf("delay") >= 0) { m_departureLabel.ForeColor = ColorService.DELAYED_BUS; } else if (b.ArrivalStatus.ToLower().IndexOf("scheduled departure") >= 0) { m_departureLabel.ForeColor = ColorService.UNKNOWN_BUS; } else { m_departureLabel.ForeColor = ColorService.NORMAL_BUS; } /*Graphics g = this.CreateGraphics(); * * if (g.MeasureString(m_destinationLabel.Text, m_largeDestinationFont).Width > (m_destinationLabel.Width + 2)) * { * m_destinationLabel.Font = m_smallDestinationFont; * float textHeight = g.MeasureString(m_destinationLabel.Text, m_smallDestinationFont).Height; * * // Detect if wrapping occurs even with small text, and if it doesn't, fix * // up the vertical alignment * if (g.MeasureString(m_destinationLabel.Text, m_smallDestinationFont).Width > (m_destinationLabel.Width + 1)) * { * m_destinationLabel.Top = m_routeNumberLabel.Top; * } * else * { * m_destinationLabel.Top = m_routeNumberLabel.Top + (int)(textHeight / 2); * } * } * else*/ { m_destinationLabel.Font = m_largeDestinationFont; } /*int fontIndex = m_fonts.Length - 1; * while (g.MeasureString(m_destinationLabel.Text, m_fonts[fontIndex]).Width > (m_destinationLabel.Width + 2)) * { * fontIndex--; * } * m_destinationLabel.Font = m_fonts[fontIndex]; */ // If text was made smaller, fix up the vertical alignment. /*if (fontIndex != m_fonts.Length - 1) * { * float sizeDifference = g.MeasureString(m_routeNumberLabel.Text, m_mainFont).Height - * g.MeasureString(m_destinationLabel.Text, m_fonts[fontIndex]).Height; * * m_destinationLabel.Top = m_routeNumberLabel.Top + (int)(sizeDifference / 2); * } * else * { * m_destinationLabel.Top = m_routeNumberLabel.Top; * } * */ }
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); } } }