示例#1
0
 public IObservable<Unit> FindRoute()
 {
     if (CheckCanFindRoute() != null || IsFindingRoute)
         throw new InvalidOperationException();
     IsFindingRoute = true;
     // build SeachDescription
     SearchDescription sd = new SearchDescription(
         this._route,
         (PreferKey)_preferType,
         _time,
         (TimeType)_timeType);
     Setting.LastSearchDescription = sd;
     Setting.LastPreferKey = sd.PreferKey;
     Setting.UpdateMyRoutes(this._route);
     Setting.LastResult = null;
     return TransferNaviHandler.QueryTransportInfo(sd)
         .Do(result => Setting.LastResult = Setting.LastResult.Append(result))
         .Finally(() => IsFindingRoute = false)
         .Select(_ => new Unit());
 }
示例#2
0
        /// <summary>
        /// 「なごや乗換案内」にクエリして結果を取得します。
        /// </summary>
        public static IObservable<TransportRoute> QueryTransportInfo(SearchDescription searchDescription)
        {
            try
            {
                Dictionary<string, string> postData = new Dictionary<string, string>();
                // 出発・到着点、経由地
                postData.Add("val_from", searchDescription.Origin);
                postData.Add("val_get_from", searchDescription.Origin);
                postData.Add("val_to", searchDescription.Destination);
                postData.Add("val_get_to", searchDescription.Destination);
                if (!String.IsNullOrEmpty(searchDescription.Via))
                    postData.Add("val_via", searchDescription.Via);

                // 時間
                switch (searchDescription.TimeType)
                {
                    case TimeType.DepartureTime:
                    case TimeType.ArrivalTime:
                        postData.Add("val_hour", searchDescription.Time.Hour.ToString());
                        postData.Add("val_min", searchDescription.Time.Minute.ToString());
                        break;
                    case TimeType.FirstTrain:
                    case TimeType.LastTrain:
                        postData.Add("val_hour", LastFirstTrainHour.ToString());
                        postData.Add("val_min", "0");
                        break;
                }

                // 時間種別
                switch (searchDescription.TimeType)
                {
                    case TimeType.DepartureTime:
                    case TimeType.FirstTrain:
                        postData.Add("val_timekb", "DEP");
                        break;
                    case TimeType.ArrivalTime:
                    case TimeType.LastTrain:
                        postData.Add("val_timekb", "ARR");
                        break;
                }

                // 乗り換えキー
                switch (searchDescription.PreferKey)
                {
                    case PreferKey.Cheaper:
                        postData.Add("val_sort", "5");
                        break;
                    case PreferKey.LesserTransfer:
                        postData.Add("val_sort", "2");
                        break;
                }

                // バスのみ?
                if (searchDescription.IsSearchOnlyBus)
                    postData.Add("val_bus_only", "1");

                // さいごに
                postData.Add("val_htmb", "result");

                // ポストデータ組み立て
                var postbyte =
                    InternalEncoding.GetBytes(
                        postData.Select(kv =>
                            kv.Key + "=" + HttpUtil.UrlEncode(kv.Value, InternalEncoding))
                        .JoinString("&"));

                System.Diagnostics.Debug.WriteLine(postData.Select(kv =>
                            kv.Key + "=" + HttpUtility.UrlEncode(kv.Value))
                        .JoinString("&"));
                var req = HttpWebRequest.CreateHttp(TransferNaviUrl);
                req.Method = "POST";
                req.ContentType = "application/x-www-form-urlencoded";
                return req
                    .UploadDataAsync(postbyte)
                    .Select(s =>
                    {
                        using (var sr = new StreamReader(s.GetResponseStream(), InternalEncoding))
                        {
                            return sr.ReadToEnd();
                        }
                    })
                    .Parse();
            }
            catch (Exception ex)
            {
                return Observable.Throw<TransportRoute>(ex);
            }
        }