/// <summary> /// Handle the information returned from the async request /// </summary> /// <param name="asyncResult"></param> private void HandleIndexResponse(IAsyncResult asyncResult) { LndrUpdateState lndrUpdateState = (LndrUpdateState)asyncResult.AsyncState; HttpWebRequest lndrRequest = (HttpWebRequest)lndrUpdateState.AsyncRequest; lndrUpdateState.AsyncResponse = (HttpWebResponse)lndrRequest.EndGetResponse(asyncResult); try { Stream streamResult = lndrUpdateState.AsyncResponse.GetResponseStream(); StreamReader sr = new StreamReader(streamResult); string json = sr.ReadToEnd(); IList<ApplianceViewModel> appliances = new List<ApplianceViewModel>(); JsonArray applianceArray = JsonArray.Parse(json); foreach (JsonValue jsonValue in applianceArray) { JsonObject applianceObject = jsonValue.GetObject(); ApplianceViewModel avm = new ApplianceViewModel(); try { avm.Id = (int) applianceObject.GetNamedNumber("id"); avm.Name = applianceObject.GetNamedString("name"); avm.Busy = applianceObject.GetNamedBoolean("busy"); avm.FreeAt = DateTime.Parse(applianceObject.GetNamedString("free_at")).ToUniversalTime(); appliances.Add(avm); } catch (Exception e) { } } //IList<ApplianceViewModel> appliances = JsonConvert.DeserializeObject<IList<ApplianceViewModel>>(json); Clear(); foreach (ApplianceViewModel appliance in appliances) { AddAppliance(appliance); } this.IsDataLoaded = true; //Window.Current.CoreWindow.Dispatcher.Invoke(CoreDispatcherPriority.Normal, // (s, a) => // { // }, this, asyncResult //); } catch (FormatException) { // there was some kind of error processing the response from the web // additional error handling would normally be added here return; } }
public void OnClaim(ApplianceViewModel avm) { UriBuilder fullUri = new UriBuilder("http://lndr.me/receive.json"); fullUri.Query = String.Format("key={0}&id={1}&email={2}",_("ServerKey"),avm.Id,_("DefaultEMail")); // initialize a new WebRequest HttpWebRequest lndrRequest = (HttpWebRequest)WebRequest.Create(fullUri.Uri); // set up the state object for the async request LndrUpdateState lndrUpdateState = new LndrUpdateState(); lndrUpdateState.AsyncRequest = lndrRequest; // start the asynchronous request lndrRequest.BeginGetResponse(new AsyncCallback(HandleUpdateResponse), lndrUpdateState); }
private void AddAppliance(ApplianceViewModel avm) { avm.ClaimCommand = new ClaimApplianceCommand(this, avm); this.AllAppliances.Add(avm); switch (avm.Appliance) { case ApplianceViewModel.ApplianceType.DRYER: this.Dryers.Add(avm); break; case ApplianceViewModel.ApplianceType.WASHER: this.Washers.Add(avm); break; } }
public ClaimApplianceCommand(MainViewModel mvm, ApplianceViewModel avm) { this.mvm = mvm; this.avm = avm; }