Esempio n. 1
0
        private async void BulkImportButton_OnClick(object sender, RoutedEventArgs e)
        {
            var fd = new OpenFileDialog();

            fd.Multiselect = false;
            fd.Filter      = "CSV Files (*.csv)|*.csv|All Files (*.*)|*.*";
            var showDialog = fd.ShowDialog();
            var res        = showDialog != null && (bool)showDialog;

            if (!res || string.IsNullOrEmpty(fd.FileName) || !File.Exists(fd.FileName))
            {
                return;
            }

            string[] secrets;
            try
            {
                secrets = File.ReadAllText(fd.FileName).Split(',');
            }
            catch (Exception)
            {
                var nd = new NoticeDialog("Bulk Account Import",
                                          "Error: CSV file could not be parsed.\r\nPlease correct the file and try again.");
                nd.ShowDialog();
                return;
            }

            using (var avm = new AuthViewModel {
                ActionDescription = "Bulk Import Accounts "
            })
            {
                var rmpw = new AuthRequestDialog(avm);
                rmpw.ShowDialog();
                if (!avm.Accepted)
                {
                    return;
                }
                if (rmpw.DialogResult == null || rmpw.DialogResult == false || string.IsNullOrEmpty(avm.Password))
                {
                    return;
                }
                var i  = 0;
                var ii = 1;
                var pd = new ProcessingDialog("Importing Accounts",
                                              "Please wait while your accounts are imported. This may take some time depending on the number of accounts.");
                pd.Show();
                foreach (var sec in secrets)
                {
                    ii++;
                    pd.ProgressTextBlock.Text = ii + " of " + secrets.Length;
                    var act = await Globals.API.Accounts_Open(sec);

                    if (!act.success)
                    {
                        continue;
                    }

                    var hasrecord = (from a in Globals.AppViewModel.AccountsViewModel.Accounts
                                     where a.Address == act.account.address
                                     select a).Any();
                    if (hasrecord)
                    {
                        continue;
                    }

                    var ssece = AppHelpers.EncryptString(sec, avm.Password);
                    var bal   = LiskAPI.LSKLongToDecimal(act.account.balance);
                    var ni    = new Account
                    {
                        Address      = act.account.address,
                        PublicKey    = act.account.publicKey,
                        FriendlyName = act.account.address,
                        SecretHash   = ssece,
                        Balance      = bal,
                        LastUpdate   = DateTime.UtcNow
                    };
                    try
                    {
                        await AccountsViewModel.AddAccountAsync(ni);

                        i++;
                    }
                    catch (Exception crap)
                    {
                        Console.WriteLine("Error saving imported account " + act.account.address + " | " + crap.Message);
                    }
                }
                pd.Hide();
                var nd = new NoticeDialog("Bulk Account Import",
                                          "Bulk account import complete.\r\n" + i + " of " + secrets.Length + " accounts imported.");
                nd.ShowDialog();
                NavigationCommands.BrowseBack.Execute(null, null);
            }
        }
        bool isSkip = true;  //接ProcessingDialog 回傳值 略過

        private void Button_AutoImport_Click(object sender, RoutedEventArgs e)
        {
            Button btnAutoImport = (Button)sender;

            //先載入原本樣板的圖片
            //如果略過就塞回原圖
            btnAutoImport.Dispatcher.Invoke(() =>
            {
                bool isEverChanged = false;

                btnAutoImport.IsEnabled = false;

                pd     = new ProcessingDialog();
                Task t = Task.Factory.StartNew(() =>
                {
                    pd.Dispatcher.Invoke(() =>
                    {
                        pd.PText                = "圖片偵測中";
                        pd.PIsIndeterminate     = true;
                        pd.ButtonContent        = "跳過";
                        pd.ReturnValueCallback += new ProcessingDialog.ReturnValueDelegate(this.SetReturnValueCallbackFun);

                        pd.Show();
                    });

                    //病患資料夾路徑
                    if (pf == null)
                    {
                        pf = new PatientsFolder(Agencys, Patients, Registration_Date);
                    }

                    //資料表Registrations 處理
                    if (dbr == null)
                    {
                        dbr = new DBRegistrations();
                    }

                    //資料表Images 處理
                    if (dbi == null)
                    {
                        dbi = new DBImages();
                    }

                    //處理載入BitmapImage
                    if (lbi == null)
                    {
                        lbi = new LoadBitmapImage();
                    }

                    //載入TemplateImages
                    if (dbti == null)
                    {
                        dbti = new DBTemplateImages();
                    }

                    tICollection = new ObservableCollection <TemplateImages>();
                    tICollection = dbti.GetTemplateImagesCollection(Agencys, Patients, TemplateItem, tfvm.TemplateImportDate);

                    //default Image[i] in UserControl Templates
                    int Imagei           = 0;
                    int ImageCount       = (int)TemplateItem.Template_ImageCount;
                    int DecodePixelWidth = (int)TemplateItem.Template_DecodePixelWidth;
                    while (Imagei < ImageCount)
                    {
                        pd.PText = "圖片 " + (Imagei + 1) + " 偵測中";

                        //目前處理的Image[i]
                        Image iTarget;

                        TemplateContent.Dispatcher.Invoke(() =>
                        {
                            iTarget = new Image();
                            iTarget = (Image)TemplateContent.FindName("Image" + Imagei);

                            iTarget.Source = new BitmapImage(new Uri(@"/DigiDental;component/Resource/yes.png", UriKind.RelativeOrAbsolute));
                            //iTarget.Source = lbi.SettingBitmapImage(@"/DigiDental;component/Resource/yes.png", DecodePixelWidth);
                        });

                        //set the paramater default
                        bool isChanged = false;
                        bool detecting = true;
                        while (true)
                        {
                            //開始偵測wifi card路徑
                            foreach (string f in Directory.GetFiles(Agencys.Agency_WifiCardPath))
                            {
                                Thread.Sleep(500);

                                string extension   = Path.GetExtension(f).ToUpper();
                                string newFileName = DateTime.Now.ToString("yyyyMMddHHmmssffff");
                                if (!Directory.Exists(pf.PatientFullFolderPathOriginal))
                                {
                                    Directory.CreateDirectory(pf.PatientFullFolderPathOriginal);
                                }

                                //System.Drawing.RotateFlipType rft = ImageHelper.RotateImageByExifOrientationData(f, pf.PatientFullFolderPathOriginal + @"\" + newFileName + @"ori" + extension, GetImageFormat(extension), true);

                                ////複製原圖到目的Original
                                //File.Copy(f, pf.PatientFullFolderPathOriginal + @"\" + newFileName + @"ori" + extension);

                                // Rotate the image according to EXIF data
                                var bmp = new System.Drawing.Bitmap(f);
                                System.Drawing.RotateFlipType fType = ImageHelper.RotateImageByExifOrientationData(bmp, true);
                                if (fType != System.Drawing.RotateFlipType.RotateNoneFlipNone)
                                {
                                    bmp.Save(pf.PatientFullFolderPathOriginal + @"\" + newFileName + @"ori" + extension, GetImageFormat(extension));
                                }
                                else
                                {
                                    File.Copy(f, pf.PatientFullFolderPathOriginal + @"\" + newFileName + @"ori" + extension);
                                }

                                Registration_ID = dbr.CreateRegistrationsAndGetID(Patients, Registration_Date);

                                string imagePath      = @"\" + pf.PatientFolderPathOriginal + @"\" + newFileName + @"ori" + extension;
                                string imageFileName  = newFileName + @"ori" + extension;
                                string imageSize      = "Original";
                                string imageExtension = extension;
                                //寫資料庫
                                //INSERT Images
                                int imageID   = dbi.InsertImage(imagePath, imageFileName, imageSize, imageExtension, Registration_ID);
                                isEverChanged = true;
                                TemplateContent.Dispatcher.Invoke(() =>
                                {
                                    iTarget = new Image();
                                    iTarget = (Image)TemplateContent.FindName("Image" + Imagei);

                                    //INSERT TemplateImages
                                    dbti.InsertOrUpdateImage(Patients, TemplateItem, tfvm.TemplateImportDate, imageID, imagePath, iTarget.Uid);
                                    iTarget.Source = lbi.SettingBitmapImage(pf.PatientFullFolderPathOriginal + @"\" + newFileName + @"ori" + extension, DecodePixelWidth);
                                    isChanged      = true;
                                });

                                File.Delete(f);
                                detecting = false;
                            }
                            //ProcessingDialog STOP
                            if (isStop)
                            {
                                isStop = false;
                                TemplateContent.Dispatcher.Invoke(() =>
                                {
                                    iTarget          = new Image();
                                    iTarget          = (Image)TemplateContent.FindName("Image" + Imagei);
                                    var findOriImage = from tc in tICollection
                                                       where tc.TemplateImage_Number == Imagei.ToString()
                                                       select tc;
                                    if (findOriImage.Count() > 0)
                                    {
                                        iTarget.Source = lbi.SettingBitmapImage(findOriImage.First().Image_Path, DecodePixelWidth);
                                    }
                                    else
                                    {
                                        iTarget.Source = new BitmapImage(new Uri(@"/DigiDental;component/Resource/no.png", UriKind.RelativeOrAbsolute));
                                        //iTarget.Source = lbi.SettingBitmapImage(@"/DigiDental;component/Resource/key.ico", DecodePixelWidth);
                                    }
                                });
                                return;
                            }
                            else
                            {
                                // import pic OR skip import (NEXT)
                                if (!detecting || !isSkip)
                                {
                                    if (!isChanged)
                                    {
                                        TemplateContent.Dispatcher.Invoke(() =>
                                        {
                                            iTarget          = new Image();
                                            iTarget          = (Image)TemplateContent.FindName("Image" + Imagei);
                                            var findOriImage = from tc in tICollection
                                                               where tc.TemplateImage_Number == Imagei.ToString()
                                                               select tc;
                                            if (findOriImage.Count() > 0)
                                            {
                                                iTarget.Source = lbi.SettingBitmapImage(findOriImage.First().Image_Path, DecodePixelWidth);
                                            }
                                            else
                                            {
                                                iTarget.Source = new BitmapImage(new Uri(@"/DigiDental;component/Resource/no.png", UriKind.RelativeOrAbsolute));
                                                //iTarget.Source = lbi.SettingBitmapImage(@"/DigiDental;component/Resource/key.ico", DecodePixelWidth);
                                            }
                                        });
                                    }
                                    Imagei++;
                                    isSkip = true;
                                    break;
                                }
                            }
                        }
                    }
                }).ContinueWith(cw =>
                {
                    //結束
                    pd.PText = "處理完畢";
                    pd.Close();
                    //委派回傳MainWindow
                    //刷新Registrations 資料
                    //刷新Images 資料
                    if (isEverChanged)
                    {
                        ReturnValueCallback(Registration_ID, Registration_Date);
                    }
                    GC.Collect();

                    btnAutoImport.IsEnabled = true;
                }, TaskScheduler.FromCurrentSynchronizationContext());
            });
        }