// GET: BusRoute public async Task <ActionResult> Index() { //initial variable DateTime now = DateTime.Now; int count = 1; IBusRoute repos = DataFactory.BusRouteRepository(); repos.clearBusRouteTable(); for (int i = 0; i <= listCity.Count - 1; i++) { //Setting target Url string targetURI = ConfigurationManager.AppSettings["BusRouteURL"].ToString() + "/" + listCity[i] + "?$format=JSON"; HttpClient client = new HttpClient(); client.MaxResponseContentBufferSize = Int32.MaxValue; //Get Json String var response = await client.GetStringAsync(targetURI); //Deserialize var collection = JsonConvert.DeserializeObject <IEnumerable <BusRouteDeserialize> >(response); //將JSON反序列化的資料填進資料庫中 count = repos.AddBusRoute(collection, count, i + 1); } string afterNow = (DateTime.Now - now).ToString(); return(Content("公車路線取得時間:" + afterNow)); }
/// <summary> /// 取得公車路線資訊並轉成JSON輸出 /// </summary> /// <param name="city">鄉鎮英文</param> /// <param name="routeName">公車路線名稱</param> /// <param name="direction">去、返(0、1)</param> /// <returns></returns> public async Task <ActionResult> GetBusEstimatedTime(string city, string routeName = "", int direction = 0) { //initial variable DateTime now = DateTime.Now; IBusRoute repos = DataFactory.BusRouteRepository(); int flag = 0; if (city == "Taipei") { flag = 1; } //Setting target Url string targetURI = ConfigurationManager.AppSettings["BusEstimatedTimeURL"].ToString() + "/" + city + "/" + routeName + "?$format=JSON"; HttpClient client = new HttpClient(); client.MaxResponseContentBufferSize = Int32.MaxValue; //Get Json String var response = await client.GetStringAsync(targetURI); //Deserialize var collection = JsonConvert.DeserializeObject <IEnumerable <BusEstimatedTimeDeserialize> >(response); string afterNow = (DateTime.Now - now).ToString(); return(Content(JsonConvert.SerializeObject(repos.GetBusEstimatedTime(collection, flag)), "application/json")); }
public async Task <IHttpActionResult> GetBusEstimatedTime(string city, string routeName = "", int direction = 0) { //initial variable IHttpActionResult responseResult; DateTime now = DateTime.Now; IBusRoute repos = DataFactory.BusRouteRepository(); int flag = 0; if (city == "Taipei") { flag = 1; } //Setting target Url string targetURI = ConfigurationManager.AppSettings["BusEstimatedTimeURL"].ToString() + "/" + city + "/" + routeName + "?$format=JSON"; HttpClient client = new HttpClient(); client.MaxResponseContentBufferSize = Int32.MaxValue; //Get Json String var response = await client.GetStringAsync(targetURI); //Deserialize var collection = JsonConvert.DeserializeObject <IEnumerable <BusEstimatedTimeDeserialize> >(response); //將需要的欄位取出後序列化 var jsonSerialize = JsonConvert.SerializeObject(repos.GetBusEstimatedTime(collection, flag)); //做成JSON字串包裝到最後輸出 StringContent responseMsgString = new StringContent(jsonSerialize, System.Text.Encoding.UTF8, "application/json"); HttpResponseMessage responseMsg = new HttpResponseMessage() { Content = responseMsgString }; responseResult = ResponseMessage(responseMsg); return(responseResult); }
/// <summary> /// 取得公車路線 /// </summary> /// <returns></returns> public ActionResult GetRoute(string city, string keyword = "") { IBusRoute repos = DataFactory.BusRouteRepository(); Dictionary <string, int> dicCity = new Dictionary <string, int>(); for (int i = 0; i <= listCity.Count - 1; i++) { dicCity.Add(listCity[i], i + 1); } return(Content(JsonConvert.SerializeObject(repos.GetRoute(dicCity.ContainsKey(city)?dicCity[city]:1, keyword)), "application/json")); }
/// <summary> /// Creates and returns an <see cref="TContextData"/> instance based on the supplied resource. /// </summary> public TContextData GetContextData(IBusRoute resource) { if (resource == null) { throw new ArgumentNullException("resource", "The 'busRoute' resource for obtaining authorization context data cannot be null."); } var entity = resource as BusRoute; dynamic contextData = new TContextData(); // EducationOrganizationId = entity.EducationOrganizationId, // Not part of primary key // StaffUSI = entity.StaffUSI, // Not part of primary key return((TContextData)contextData); }
// GET: BusEstimate public async Task <ActionResult> Index() { //var BusRouteSource = await GetBusRouteData(); //Setting target Url string targetURI = ConfigurationManager.AppSettings["BusEstimatedTimeURL"].ToString() + "?$format=JSON"; HttpClient client = new HttpClient(); client.MaxResponseContentBufferSize = Int32.MaxValue; //Get Json String var response = await client.GetStringAsync(targetURI); //Deserialize var collection = JsonConvert.DeserializeObject <IEnumerable <BusEstimatedTimeDeserialize> >(response); IBusRoute repos = DataFactory.BusRouteRepository(); //將JSON反序列化的資料填進資料庫中 repos.AddBusEstimate(collection); return(View()); }
public IHttpActionResult GetRoute(string city, string keyword = "") { //Initial IHttpActionResult responseResult; IBusRoute repos = DataFactory.BusRouteRepository(); Dictionary <string, int> dicCity = new Dictionary <string, int>(); for (int i = 0; i <= listCity.Count - 1; i++) { dicCity.Add(listCity[i], i + 1); } //序列化撈出來的資料 var jsonSerialize = JsonConvert.SerializeObject(repos.GetRoute(dicCity.ContainsKey(city) ? dicCity[city] : 1, keyword)); //做成JSON字串包裝到最後輸出 StringContent responseMsgString = new StringContent(jsonSerialize, System.Text.Encoding.UTF8, "application/json"); HttpResponseMessage responseMsg = new HttpResponseMessage() { Content = responseMsgString }; responseResult = ResponseMessage(responseMsg); return(responseResult); }
/// <summary> /// 關鍵字搜尋公車路線 /// </summary> /// <param name="keyword">關鍵字(路線名稱、路線代號)</param> /// <returns></returns> public ActionResult JsonSearchRoute(string keyword) { IBusRoute repos = DataFactory.BusRouteRepository(); return(Content(JsonConvert.SerializeObject(repos.GetSearchRoute(keyword)), "application/json")); }
/// <summary> /// 取得所有公車路線(含關鍵字) /// </summary> /// <param name="cityType">Taipei: 1, NewTaipei: 2, Taoyuan: 3, Taichung: 4, Tainan: 5, ///Kaohsiung: 6, Keelung: 7, Hsinchu: 8, HsinchuCounty: 9, MiaoliCounty: 10, ///ChanghuaCounty: 11, NantouCounty: 12, YunlinCounty: 13, ChiayiCounty: 14, Chiayi: 15, ///PingtungCounty: 16, YilanCounty: 17, HualienCounty: 18, TaitungCounty: 19, KinmenCounty: 20, ///PenghuCounty: 21, PenghuCounty: 22</param> ///<param name="keyWord">關鍵字搜尋</param> /// <returns></returns> public List <GetRoute> GetRoute(int cityType, string keyWord) { IBusRoute repos = DataFactory.BusRouteRepository(); return(repos.GetRoute(cityType, keyWord)); }