Commit 1ed6e774 authored by Francesco Carraro's avatar Francesco Carraro
Browse files

adding saving of spectra

parent 244aca04
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -4,5 +4,6 @@
	<operationUrls>
		<operation type="Authentication">Account/LoginForJwt</operation>
		<operation type="SaveAlignedFile">AppActions/SaveAlignedSpectrum</operation>
		<operation type="SaveFileOfType">AppActions/SaveSpectrumOfType</operation>
	</operationUrls>
</remoteOperationsData>
 No newline at end of file
+5 −1
Original line number Diff line number Diff line
@@ -60,7 +60,11 @@ namespace INAF.Apps.Uwp.SLabDataManager.Charts
        public bool IsAnySpectrumLoaded
        {
            get { return isAnySpectrumLoaded; }
            set { SetProperty(ref isAnySpectrumLoaded, value); }
            set
            {
                SetProperty(ref isAnySpectrumLoaded, value);
                isAnySpectrumLoaded = false; // reset value to make it working @ the next spectrum loading
            }
        }

        public bool IsSpectrumAligned { get; set; }
+62 −0
Original line number Diff line number Diff line
@@ -8,9 +8,38 @@ using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using static INAF.Apps.Uwp.SLabDataManager.Constants.Enums;
using static INAF.Libraries.NetStandard.SLabCommonModels.Enums.Enums;
using INAF.Libraries.NetStandard.ScienceModels.Extensions;
using Microsoft.Toolkit.Mvvm.Input;
using INAF.Libraries.NetStandard.ScienceModels.Spectra;
using INAF.Apps.Uwp.SLabDataManager.ViewModels;

namespace INAF.Apps.Uwp.SLabDataManager.Converters
{
    public sealed class SpectrumModelConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            try
            {
                string _spectrumType = (string)parameter;
                SpectrumType spectrumType = _spectrumType.ToSpectrumType();

                SpectraContainer spectraContainer = Ioc.Default.GetService<SpectraContainer>();
                return spectraContainer.tryGetSpectrumOfType(spectrumType);
            }
            catch (Exception)
            {
                return string.Empty;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }

    public sealed class SpectrumSummaryFilepathConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
@@ -144,4 +173,37 @@ namespace INAF.Apps.Uwp.SLabDataManager.Converters
            throw new NotImplementedException();
        }
    }

    public sealed class SpectrumSummarySaveFileConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            try
            {
                string _spectrumType = (string)parameter;
                SpectrumType spectrumType = _spectrumType.ToSpectrumType();

                SpectraContainer spectraContainer = Ioc.Default.GetService<SpectraContainer>();

                MainViewModel mainViewModel = Ioc.Default.GetService<MainViewModel>();
                var spectrum = spectraContainer.tryGetSpectrumOfType(spectrumType);
                //if (mainViewModel.CommandSaveSpectrumFile.CanExecute(null))
                //    mainViewModel.CommandSaveSpectrumFile.Execute(spectrum);

                return new RelayCommand(() =>
                {
                    mainViewModel.saveFile(spectrum);
                });
            }
            catch (Exception)
            {
                return string.Empty;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }
}
+25 −12
Original line number Diff line number Diff line
using INAF.Libraries.NetStandard.SLabCommonModels.Authentication;
using INAF.Libraries.NetStandard.ScienceModels.Spectra;
using INAF.Libraries.NetStandard.SLabCommonModels.Authentication;
using INAF.Libraries.NetStandard.SLabCommonModels.Models.Files;
using INAF.Libraries.NetStandard.SLabCommonModels.Models.SaveAlignedSpectrum;
using INAF.Libraries.Uwp.Logging;
using INAF.Libraries.Uwp.Settings;
@@ -7,7 +9,7 @@ using System;
using System.Threading.Tasks;
using Windows.Web.Http;
using Windows.Web.Http.Headers;
using static INAF.Libraries.NetStandard.SLabCommonModels.RemoteOperations.Enums;
using static INAF.Libraries.NetStandard.SLabCommonModels.Enums.Enums;

namespace INAF.Apps.Uwp.SLabDataManager.Helpers.RemoteOperations
{
@@ -27,7 +29,23 @@ namespace INAF.Apps.Uwp.SLabDataManager.Helpers.RemoteOperations
            this.logger = logger;
        }

        public async Task<bool> saveAlignedSpectrumAsync(SaveAlignedSpectrumRequestModel saveAlignedSpectrumRequestModel)
        public async Task<bool> saveAlignedSpectrumAsync(SaveAlignedSpectrumRequestModel model)
        {
            return await postAsync(model, RemoteOperationType.SaveAlignedFile);
        }

        public async Task<bool> saveSpectrumOfTypeAsync(SpectrumFileModel model)
        {
            return await postAsync(model, RemoteOperationType.SaveFileOfType);
        }

        #region private
        private HttpStringContent getHttpStringContent(Object objectToBeSerialized)
        {
            return new HttpStringContent(JsonConvert.SerializeObject(objectToBeSerialized), Windows.Storage.Streams.UnicodeEncoding.Utf8, "application/json");
        }

        private async Task<bool> postAsync(object model, RemoteOperationType remoteOperationType)
        {
            bool isOk = true;

@@ -37,11 +55,10 @@ namespace INAF.Apps.Uwp.SLabDataManager.Helpers.RemoteOperations
                {
                    JwtTokenModel tokenModel = settingsHelper.get<JwtTokenModel>(Constants.Constants.AUTHENTICATION_TOKEN);
                    httpClient.DefaultRequestHeaders.Authorization = new HttpCredentialsHeaderValue("Bearer", tokenModel.Token);
                    //saveAlignedSpectrumRequestModel.setAuthorization(tokenModel.Token);

                    using (var httpContent = getHttpStringContent(saveAlignedSpectrumRequestModel))
                    using (var httpContent = getHttpStringContent(model))
                    {
                        var url = remoteOperationsRepository.getUrl(RemoteOperationType.SaveAlignedFile);
                        var url = remoteOperationsRepository.getUrl(remoteOperationType);
                        using (var response = await httpClient.PostAsync(new Uri(url), httpContent))
                        {
                            if (response.IsSuccessStatusCode)
@@ -58,17 +75,12 @@ namespace INAF.Apps.Uwp.SLabDataManager.Helpers.RemoteOperations
            catch (Exception ex)
            {
                isOk = false;
                logger.Write<RemoteOperationsManager>($"{nameof(saveAlignedSpectrumAsync)} - ex: {ex.Message}", Serilog.Events.LogEventLevel.Error);
                logger.Write<RemoteOperationsManager>($"{nameof(postAsync)} - ex: {ex.Message}", Serilog.Events.LogEventLevel.Error);
            }

            return isOk;
        }

        private HttpStringContent getHttpStringContent(Object objectToBeSerialized)
        {
            return new HttpStringContent(JsonConvert.SerializeObject(objectToBeSerialized), Windows.Storage.Streams.UnicodeEncoding.Utf8, "application/json");
        }

        private void Dispose(bool disposing)
        {
            if (!disposedValue)
@@ -97,5 +109,6 @@ namespace INAF.Apps.Uwp.SLabDataManager.Helpers.RemoteOperations
            Dispose(disposing: true);
            GC.SuppressFinalize(this);
        }
        #endregion
    }
}
+1 −1
Original line number Diff line number Diff line
using INAF.Libraries.NetStandard.SLabCommonModels.RemoteOperations;
using System.Collections.Generic;
using System.Linq;
using static INAF.Libraries.NetStandard.SLabCommonModels.RemoteOperations.Enums;
using static INAF.Libraries.NetStandard.SLabCommonModels.Enums.Enums;

namespace INAF.Apps.Uwp.SLabDataManager.Helpers.RemoteOperations
{
Loading