Example #1
0
        private static void UpdateEstimateTime(CancellationToken cancellationToken)
        {
            lock (busInfos)
            {
                if (busInfos.Count == 0)
                {
                    MemoryStream   uncompressed = Utilities.GetUnzipDataStream(URL);
                    IList <JToken> jsonInfos    = ParseJson(uncompressed);
                    uncompressed.Close();

                    foreach (JToken jsonInfo in jsonInfos)
                    {
                        BusInfo bus = JsonConvert.DeserializeObject <BusInfo>(jsonInfo.ToString());
                        if (!busInfos.ContainsKey(bus.RouteID))
                        {
                            busInfos.Add(bus.RouteID, new List <BusInfo>());
                        }
                        busInfos[bus.RouteID].Add(bus);
                    }
                }
            }

            while (!cancellationToken.IsCancellationRequested)
            {
                Thread.Sleep(60000);
                MemoryStream   uncompressed = Utilities.GetUnzipDataStream(URL);
                IList <JToken> jsonInfos    = ParseJson(uncompressed);
                uncompressed.Close();

                lock (busInfos)
                {
                    busInfos.Clear();
                    foreach (JToken jsonInfo in jsonInfos)
                    {
                        BusInfo bus = JsonConvert.DeserializeObject <BusInfo>(jsonInfo.ToString());
                        if (!busInfos.ContainsKey(bus.RouteID))
                        {
                            busInfos.Add(bus.RouteID, new List <BusInfo>());
                        }
                        busInfos[bus.RouteID].Add(bus);
                    }
                }
            }
        }
Example #2
0
        // GET api/Route
        static RouteController()
        {
            busInfos        = new Dictionary <string, List <BusInfo> >();
            busName2RouteId = new Dictionary <string, string>();
            string         path     = System.Web.HttpContext.Current.Server.MapPath(string.Format("~/App_Data/GetRoute"));
            string         jsonText = System.IO.File.ReadAllText(path);
            JObject        json     = JObject.Parse(jsonText);
            IList <JToken> results  = json["BusInfo"].Children().ToList();

            foreach (JToken result in results)
            {
                BusInfo bus = JsonConvert.DeserializeObject <BusInfo>(result.ToString());
                if (!busInfos.ContainsKey(bus.NameZh))
                {
                    busInfos.Add(bus.NameZh, new List <BusInfo>());
                }
                busInfos[bus.NameZh].Add(bus);
                busName2RouteId[bus.NameZh] = bus.Id;
            }
        }