SaveSdCard.xaml.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using Microsoft.WindowsAPICodePack.Dialogs;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.IO.Compression;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Navigation;
  17. namespace Phihong_EVSE_UI_Tool
  18. {
  19. /// <summary>
  20. /// SaveSdCard.xaml 的互動邏輯
  21. /// </summary>
  22. public partial class SaveSdCard : UserControl
  23. {
  24. private Properties.Settings mySettings = Properties.Settings.Default;
  25. private string outputFolderPath;
  26. private string settingModelName;
  27. private List<string> toBeSavedFiles = new List<string>();
  28. public SaveSdCard()
  29. {
  30. InitializeComponent();
  31. outputFolderPath = mySettings.OutputFolderPath;
  32. uxOutputDirectoryBrowseTextBox.Text = outputFolderPath;
  33. uxModelNameTestBox.Text = mySettings.ModelName;
  34. }
  35. private void SaveToDirectory()
  36. {
  37. if (Directory.Exists(Utility.ZIP_SOURCE_DIRECTORY))
  38. {
  39. Directory.Delete(Utility.ZIP_SOURCE_DIRECTORY, true);
  40. }
  41. if (Directory.Exists(Utility.ZIP_FILE_DIRECTORY))
  42. {
  43. Directory.Delete(Utility.ZIP_FILE_DIRECTORY, true);
  44. }
  45. Directory.CreateDirectory(Utility.ZIP_SOURCE_DIRECTORY);
  46. Directory.CreateDirectory(Utility.ZIP_FILE_DIRECTORY);
  47. if (uxSaveAllRadioButton.IsChecked.Value)
  48. {
  49. toBeSavedFiles = new List<string>(Utility.ChangedFiles.Keys);
  50. }
  51. else
  52. {
  53. toBeSavedFiles = (from p in Utility.ChangedFiles
  54. where p.Value
  55. select p.Key).ToList<string>();
  56. }
  57. foreach (string fileName in toBeSavedFiles)
  58. {
  59. File.Copy(Path.Combine(Utility.OUTPUT_DIRECTORY, fileName),
  60. Path.Combine(Utility.ZIP_SOURCE_DIRECTORY, fileName), true);
  61. }
  62. foreach (string resName in Utility.REQ_FILELIST)
  63. {
  64. Uri srcPath = new Uri(Path.Combine(Utility.REQ_PARENTFOLDER, resName), UriKind.Relative);
  65. Utility.CopyFileFromResource(srcPath, Path.Combine(Utility.ZIP_SOURCE_DIRECTORY, resName));
  66. }
  67. string modelName = uxModelNameTestBox.Text;
  68. string zipFileName = string.Format("DWIN_SET_{0}_{1}.zip", modelName, DateTime.Now.ToString("yyyyMMddHHmmss"));
  69. string temZipPath = Path.Combine(Utility.ZIP_FILE_DIRECTORY, Utility.TEMP_ZIP_FILE_NAME);
  70. ZipFile.CreateFromDirectory(Utility.ZIP_SOURCE_DIRECTORY, temZipPath, CompressionLevel.Fastest, true);
  71. string tempHeaderBuildedZipPath = Path.Combine(Utility.ZIP_FILE_DIRECTORY, zipFileName);
  72. FirmwareHeaderBuilder.AddFirmwareHeader(temZipPath, modelName, tempHeaderBuildedZipPath);
  73. File.Copy(tempHeaderBuildedZipPath, Path.Combine(outputFolderPath, zipFileName));
  74. switch (toBeSavedFiles.Count())
  75. {
  76. case 0:
  77. uxMessageTextBlock.Text = "Archived no image and 11 required files to File";
  78. break;
  79. case 1:
  80. uxMessageTextBlock.Text = "Archived 1 image and 11 required files to File";
  81. break;
  82. default:
  83. uxMessageTextBlock.Text = "Archived " + toBeSavedFiles.Count() + " images and 11 required files to File";
  84. break;
  85. }
  86. System.Diagnostics.Process process = new System.Diagnostics.Process();
  87. process.StartInfo.FileName = outputFolderPath;
  88. process.Start();
  89. }
  90. private void uxBrowseButton_Click(object sender, RoutedEventArgs e)
  91. {
  92. Button btn = e.Source as Button;
  93. if (btn is null)
  94. {
  95. return;
  96. }
  97. switch (btn.Tag.ToString())
  98. {
  99. case "Browse":
  100. var dlg = new CommonOpenFileDialog()
  101. {
  102. IsFolderPicker = true,
  103. Title = "Locate the output directory",
  104. };
  105. if (dlg.ShowDialog() == CommonFileDialogResult.Ok)
  106. {
  107. outputFolderPath = dlg.FileName;
  108. uxOutputDirectoryBrowseTextBox.Text = outputFolderPath;
  109. //mySettings.SdCardPath = sdFolderPath;
  110. mySettings.OutputFolderPath = outputFolderPath;
  111. mySettings.Save();
  112. }
  113. break;
  114. case "Save":
  115. if (uxModelNameTestBox.Text.Length != 14)
  116. {
  117. MessageBox.Show("Model Name length shoud be 14",
  118. "Invalid Model Name", MessageBoxButton.OK, MessageBoxImage.Error);
  119. break;
  120. }
  121. mySettings.ModelName = uxModelNameTestBox.Text;
  122. mySettings.Save();
  123. if (!Directory.Exists(outputFolderPath))
  124. {
  125. MessageBox.Show("Please locate the output directory first",
  126. "Invalid Path", MessageBoxButton.OK, MessageBoxImage.Error);
  127. break;
  128. }
  129. SaveToDirectory();
  130. break;
  131. default:
  132. break;
  133. }
  134. }
  135. }
  136. }