private void Mclient_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e) //Received LocationDirective { try { //Received JSONified Location Directive string json = Encoding.UTF8.GetString(e.Result); Log.Debug("ReceiveLocationDirective", "Received json: " + json); //Deserialize data response = JsonConvert.DeserializeObject <LocationDirective>(json); Log.Debug("JsonConvertResponse", "Convert json: " + response); //Check to see we didn't receive someone else's spot if (response.userID != userToken) { Toast.MakeText(this, "Response from server was not meant for this user! Go back and try again.", ToastLength.Long).Show(); return; } //Check to see if we got the wrong lot else if (response.lotID != lotIndex) // WE ARE ASSUMING LOTINDEX == LocationDirective.lotID { Toast.MakeText(this, "Response from server was for wrong lot! Go back and try again.", ToastLength.Long).Show(); return; } //Everything looks ok with our parking spot assignment else { //Flip spot received global flag spotReceived = true; //Server sends back (0,0) if a lot is full, check if the lot's full if (response.latitude != 0 && response.longitude != 0) { //Lot's not full, start GPS navigation to the spot! statusText.Text = GetString(Resource.String.LetsGoText); StartNavService(response.latitude, response.longitude); Log.Debug("StartMapIntent", "Starting map intent with lat:" + response.latitude + ", long:" + response.longitude); var geoUri = Android.Net.Uri.Parse("geo:" + response.latitude + "," + response.longitude + "?q=" + response.latitude + "," + response.longitude); var mapIntent = new Intent(Intent.ActionView, geoUri); //"geo:<lat>,<long>?q=<lat>,<long>" StartActivity(mapIntent); } //Lot is full else { //Break the bad news statusText.Text = "Looks like this lot is full! Please go back to the main menu and try a different lot."; } } } //Catch JSON error catch (System.Reflection.TargetInvocationException) { Toast.MakeText(this, "There was an error retrieving data from the server. Please close the app and try again later.", ToastLength.Long).Show(); } }
private void MClient_DownloadUpdateDataCompleted(object sender, DownloadDataCompletedEventArgs e) { try { //Decode and deserialize server update string json = Encoding.UTF8.GetString(e.Result); Log.Debug("NavUpdateService", "Received json: " + json); response = JsonConvert.DeserializeObject <LocationDirective>(json); Log.Debug("NavUpdateService", "Converted json: " + response); //We got someone else's update if (response.userID != userID) { Toast.MakeText(this, "Response from server was not meant for this user! Go back and try again.", ToastLength.Long).Show(); return; } //We got a response for another lot else if (response.lotID != lotID) { Toast.MakeText(this, "Response from server was for wrong lot! Go back and try again.", ToastLength.Long).Show(); return; } //We're close enough to the lot and we've been assigned a spot! else if (response.latitude != startLat || response.longitude != startLon) { //Parse response into a location then begin navigation to that spot Log.Debug("NavUpdateService\\StartMapIntent", "Starting map intent with lat:" + response.latitude + ", long:" + response.longitude); var geoUri = Android.Net.Uri.Parse("geo:" + response.latitude + "," + response.longitude + "?q=" + response.latitude + "," + response.longitude); var mapIntent = new Intent(Intent.ActionView, geoUri); Toast.MakeText(this, "Your spot has been assigned!", ToastLength.Long).Show(); StartActivity(mapIntent); StopForeground(true); lmanager = null; StopService(new Intent(this, typeof(NavUpdateService))); } } //Catch JSON parse errors catch (System.Reflection.TargetInvocationException) { Toast.MakeText(this, "There was an error retrieving data from the server. Please close the app and try again later.", ToastLength.Long).Show(); } }