Example #1
0
        public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
        {
            CheckAndDownloadAditionalContent downloader = new CheckAndDownloadAditionalContent();
            IComosWeb m_ComosWeb = Services.XServices.Instance.GetService <IComosWeb>();
            var       platform   = Services.XServices.Instance.GetService <Services.IPlatformSystem>();

            platform.SendNotification("COMOS MRO", Services.TranslateExtension.TranslateText("downloading_tasks"));

            Task.Run(async() =>
            {
                string[] uids = intent.GetStringArrayExtra("uids");

                m_ComosWeb.Lock();
                int i = 0;
                foreach (var item in uids)
                {
                    i++;
                    platform.SendNotification("COMOS MRO", Services.TranslateExtension.TranslateText("downloading_tasks"), false, true, uids.Length, i);

                    try
                    {
                        var page = await downloader.DownloadDeviceContent(item, false);

                        if (page == null)
                        {
                            platform.SendNotification("COMOS MRO", TranslateExtension.TranslateText("download_failed") + ": " + TranslateExtension.TranslateText("task_not_found"), true, true, uids.Length, 0, 0);
                            m_ComosWeb.UnLock();
                            return(StartCommandResult.NotSticky);
                        }
                    }
                    catch (Exception ex)
                    {
                        platform.SendNotification("COMOS MRO", TranslateExtension.TranslateText("download_failed") + ": " + ex.Message, true, true, uids.Length, 0, 0);
                        m_ComosWeb.UnLock();
                        return(StartCommandResult.NotSticky);
                    }
                }

                m_ComosWeb.UnLock();
                platform.SendNotification("COMOS MRO", Services.TranslateExtension.TranslateText("download_tasks_done"), true, true, uids.Length, 0, 0);

                return(StartCommandResult.NotSticky);
            });

            return(StartCommandResult.NotSticky);
        }
Example #2
0
        public async Task <CDocument> DownloadDocument(string UID, bool open)
        {
            IComosWeb m_ComosWeb = Services.XServices.Instance.GetService <ComosWebSDK.IComosWeb>();

            ViewModels.ProjectData ProjectData = Services.XServices.Instance.GetService <ViewModels.ProjectData>();

            // Need to Lock to wait big files (on sleep will logout comosweb)
            m_ComosWeb.Lock();
            CSystemObject o;

            try
            {
                o = await m_ComosWeb.GetObject(
                    ProjectData.SelectedDB.Key,
                    ProjectData.SelectedProject.UID,
                    ProjectData.SelectedLayer.UID,
                    UID, ProjectData.SelectedLanguage.LCID);
            }
            catch (TaskCanceledException) { return(await Task.FromResult <CDocument>(null)); } // If there is a Logout Request
            catch (Exception e)
            {
                await App.Current.MainPage.DisplayAlert("Error", "Error al descargar documento: " + e.Message, Services.TranslateExtension.TranslateText("OK"));

                return(await Task.FromResult <CDocument>(null));
            }

            if (o == null)
            {
                return(await Task.FromResult <CDocument>(null));
            }
            if (o.DocumentType != null)
            {
                bool exportpdf = string.Compare(o.DocumentType.Name, "ComosReport", StringComparison.CurrentCultureIgnoreCase) == 0;
                if (!exportpdf)
                {
                    exportpdf = string.Compare(o.DocumentType.Name, "ComosIReport", StringComparison.CurrentCultureIgnoreCase) == 0;
                }
                // Is a comos report.
                CDocument result = await Task.Run <CDocument>(async() =>
                {
                    HttpResponseMessage response;
                    try
                    {
                        response = await m_ComosWeb.ComosWeb.GetDocumentStream(
                            ProjectData.SelectedDB.Key,
                            ProjectData.SelectedProject.UID,
                            ProjectData.SelectedLayer.UID,
                            UID, exportpdf);
                    }
                    catch (TaskCanceledException) { return(null); } // If there is a Logout Request
                    catch (Exception ex)
                    {
                        await App.Current.MainPage.DisplayAlert("Error", "Error al cargar documento: " + ex.Message, Services.TranslateExtension.TranslateText("OK"));
                        return(null);
                    }

                    if (response.StatusCode == System.Net.HttpStatusCode.OK)
                    {
                        string filename = response.Content.Headers.ContentDisposition.FileName;
                        System.Diagnostics.Debug.WriteLine(response.Content.Headers.ContentDisposition.DispositionType);
                        filename              = filename.Trim(new char[] { '"' });
                        filename              = filename.Replace("/", "-");
                        var stream            = await response.Content.ReadAsStreamAsync();
                        var platform          = Services.XServices.Instance.GetService <Services.IPlatformSystem>();
                        string filename_saved = await platform.SaveAndOpenDocument(filename, stream, response.Content.Headers.ContentType.MediaType, open);
                        return(new CDocument()
                        {
                            Description = o.Description,
                            Name = o.Name,
                            UID = o.UID,
                            FileName = filename_saved,
                            Picture = o.Picture,
                            MimeType = response.Content.Headers.ContentType.MediaType
                        });
                    }
                    else if (response.StatusCode == System.Net.HttpStatusCode.NotFound)
                    {
                        return(await Task.FromResult <CDocument>(null));
                    }
                    else
                    {
                        var platform = Services.XServices.Instance.GetService <Services.IPlatformSystem>();
                        platform.ShowToast("Error:" + response.ReasonPhrase);
                        return(await Task.FromResult <CDocument>(null));
                        //throw new Exception(response.ReasonPhrase);
                    }
                });

                return(result);
            }

            m_ComosWeb.UnLock();

            return(await Task.FromResult <CDocument>(null));
        }