/// <summary> /// Deletes the selected object in the listview. If the target has been uploaded to the server, we will mark it to delete. /// If the target is only local, we ask before deleting it forever. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void DeleteTarget_Click(object sender, RoutedEventArgs e) { ODLC objectToDelete = (ODLC)Listbox_ODLC.SelectedItem; //Object is local, we don't have to make changes on the server. if (objectToDelete.SyncStatus == ODLC.ODLCSyncStatus.UNSYNCED) { MessageBoxResult result = MessageBox.Show("Are you sure you want to delete this target?\r\nThis cannot be undone.", "Confirm Delete", MessageBoxButton.YesNo); if (result == MessageBoxResult.Yes) { try { odlcList.Remove((ODLC)Listbox_ODLC.SelectedItem); } catch { throw new NotImplementedException(); } } } //Object is on the server. We instruct the server to delete the object next time we sync. else { objectToDelete.SyncStatus = ODLC.ODLCSyncStatus.DELETE; } }
/// <summary> /// Gets a single ODLC object uploaded from the server. Returns NULL if id is invalid. /// </summary> /// <returns> /// A single ODLC object with the given id. Null if ID is invalid. /// </returns> private ODLC getOLDC(int id) { if (!gLoggedIn) { return(null); } ODLC odlcObject = null; try { HttpResponseMessage response = gHttpClient.GetAsync(Properties.Settings.Default.url + "/api/odlcs/" + id.ToString()).Result; if (response.StatusCode == HttpStatusCode.OK) { odlcObject = JsonConvert.DeserializeObject <ODLC>(response.Content.ReadAsStringAsync().Result); } else { //Response code was not "ok" MessageBox.Show("Error: " + response.StatusCode.ToString(), "Error", MessageBoxButton.OK, MessageBoxImage.Error); } } catch (Exception Ex) { //Log something return(null); } return(odlcObject); }
/// <summary> /// Posts the image for the sepcific ODLC object. We get the target ID from the object itself. /// </summary> /// <param name="odlcObject"></param> /// <returns> /// True if post succeeded, False if post failed. /// </returns> private async Task <bool> postImage(ODLC odlcObject) { try { int?id = odlcObject.ID; if (id == null) { return(false); } //convert filestream to byte array MemoryStream stream = new MemoryStream(); odlcObject.ThumbnailImage.Save(stream, ImageFormat.Jpeg); byte[] fileBytes; fileBytes = stream.ToArray(); stream.Close(); //load the image byte[] into a System.Net.Http.ByteArrayContent ByteArrayContent imageBinaryContent = new ByteArrayContent(fileBytes); //create a System.Net.Http.MultiPartFormDataContent ByteArrayContent content2 = new ByteArrayContent(fileBytes); content2.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("image/jpeg"); //make the POST request using the URI enpoint and the MultiPartFormDataContent HttpResponseMessage response = await gHttpClient.PostAsync(Properties.Settings.Default.url + "/api/odlcs/" + id + "/image", content2); if (response.StatusCode == HttpStatusCode.OK) { string result = await response.Content.ReadAsStringAsync(); return(true); } else { //Response code was not "ok" MessageBox.Show("Error: " + response.StatusCode.ToString(), "Error", MessageBoxButton.OK, MessageBoxImage.Error); return(false); } } catch (Exception ex) { return(false); } }
/// <summary> /// Used to copy the object returned from the server, since it has been validated. /// </summary> /// <param name="odlcObject"></param> public void CopyObjectSettingsFromServer(ODLC odlcObject) { ID = odlcObject.id; Mission = odlcObject.mission; Type = odlcObject.type; Latitude = odlcObject.latitude; Longitude = odlcObject.longitude; Orientation = odlcObject.orientation; Shape = odlcObject.shape; ShapeColor = odlcObject.shapeColor; Alphanumeric = odlcObject.alphanumeric; AlphanumericColor = odlcObject.alphanumericColor; Description = odlcObject.description; Autonomous = false; SyncStatus = ODLCSyncStatus.SYNCED; ServerResponse = odlcObject.serverResponse; }
/// <summary> /// Updates the uploaded ODLC object and image on the server. Returns the updated object. /// </summary> /// <param name="odlcObject"></param> /// <returns> /// Null if invalid call. The ODLC object being uploaded with any updated fields, or error codes. /// </returns> private async Task <ODLC> updateODLC(ODLC odlcObject) { if (!gLoggedIn) { return(null); } //If the ID is not set, then the object has not been uploaded yet. if (odlcObject.ID == null) { return(null); } ODLC odlcResponse = null; string id = odlcObject.ID.ToString(); try { StringContent content = new StringContent(odlcObject.getJson(), Encoding.UTF8, "application/json"); HttpResponseMessage response = await gHttpClient.PutAsync(Properties.Settings.Default.url + "/api/odlcs/" + id, content); if (response.StatusCode == HttpStatusCode.OK) { string temp = await response.Content.ReadAsStringAsync(); odlcResponse = JsonConvert.DeserializeObject <ODLC>(await response.Content.ReadAsStringAsync()); odlcResponse.ServerResponse = response.ReasonPhrase; } else { //Response code was not "ok" MessageBox.Show("Error: " + response.StatusCode.ToString(), "Error", MessageBoxButton.OK, MessageBoxImage.Error); return(null); } //await postImage(odlcObject, (int)odlcObject.ID); } catch (Exception Ex) { //Log something return(null); } return(odlcResponse); }