EvseConfigPanel.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. using InitializerModel;
  2. using Newtonsoft.Json;
  3. using Newtonsoft.Json.Converters;
  4. using PhihongEv.Lib;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Net;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows;
  13. using System.Windows.Controls;
  14. using System.Windows.Data;
  15. using System.Windows.Documents;
  16. using System.Windows.Input;
  17. using System.Windows.Media;
  18. using System.Windows.Media.Imaging;
  19. using System.Windows.Navigation;
  20. using System.Windows.Shapes;
  21. namespace ConfigEditor.SubPage.EvseConfig
  22. {
  23. /// <summary>
  24. /// EvseConfigPanel.xaml 的互動邏輯
  25. /// </summary>
  26. public partial class EvseConfigPanel : UserControl
  27. {
  28. public EvseConfigPanel()
  29. {
  30. InitializeComponent();
  31. uxEvseModelNameList.SelectionChanged += UxEvseModelNameList_SelectionChanged;
  32. }
  33. internal void Reset()
  34. {
  35. var modelList = EvseSettingConfig.GetModelList();
  36. uxEvseModelNameList.SelectedIndex = -1;
  37. uxEvseModelNameList.ItemsSource = modelList;
  38. uxButtonTestMode.ItemsSource = Enum.GetValues(typeof(ButtonTestModeType)).Cast<ButtonTestModeType>();
  39. uxFrimwareStackPanel.Children.Clear();
  40. this.DataContext = new EvseSettingConfigModel();
  41. }
  42. private void UxEvseModelNameList_SelectionChanged(object sender, SelectionChangedEventArgs e)
  43. {
  44. ReloadEvseConfig();
  45. }
  46. private void uxSaveConfigBtn_Click(object sender, RoutedEventArgs e)
  47. {
  48. if (!CheckSetting())
  49. {
  50. return;
  51. }
  52. SaveCurrent();
  53. }
  54. private void uxNewConfigBtn_Click(object sender, RoutedEventArgs e)
  55. {
  56. CreateNewConfig();
  57. }
  58. private void ReloadEvseConfig()
  59. {
  60. var modelName = uxEvseModelNameList.SelectedItem as string;
  61. if (string.IsNullOrEmpty(modelName))
  62. {
  63. //this.DataContext = new EvseSettingConfigModel();
  64. Reset();
  65. return;
  66. }
  67. var loadResult = EvseSettingConfig.TryLoad(modelName);
  68. EvseSettingConfigModel config = null;
  69. if (loadResult != null && loadResult.Error == EvseSettingConfigLoadError.LoadConfigModelInitNotfound)
  70. {
  71. config = new EvseSettingConfigModel() {
  72. ModelName = modelName
  73. };
  74. }
  75. if (loadResult != null && loadResult.Error == EvseSettingConfigLoadError.None)
  76. {
  77. config = loadResult.Result;
  78. }
  79. if (config is null)
  80. {
  81. MessageBox.Show($"load failed:{loadResult.Error}");
  82. return;
  83. }
  84. //EvseSettingConfigModel config = loadResult.Result;
  85. //string folderPath = loadResult.ConfigFolderPath;
  86. string folderPath = EvseSettingConfig.GetModelFolder(modelName);
  87. SystemID.TryLooseParse(modelName, out var systemID);
  88. this.DataContext = config;
  89. uxFirstTelcom.Visibility = systemID.ModelName.Network.SupportTelecom ? Visibility.Visible : Visibility.Collapsed;
  90. uxSecondTelcom.Visibility = systemID.ModelName.Network.SupportSubTelecom ? Visibility.Visible : Visibility.Collapsed;
  91. var groupedFrimware = config.FirmwareUpdateList is null ? new List<IGrouping<int, FirmwareUpdateModel>>() : config.FirmwareUpdateList.GroupBy(x => EvseVersion.GetVersionIndex(x.Module));
  92. groupedFrimware = groupedFrimware.OrderBy(x => x.Key);
  93. uxFrimwareStackPanel.Children.Clear();
  94. foreach (var firmwareGroup in groupedFrimware)
  95. {
  96. FirmwareVersionPanel firmwarePanel = CreateFirmwarePanel(folderPath, firmwareGroup.Key, firmwareGroup.ToList());
  97. uxFrimwareStackPanel.Children.Add(new Rectangle() { Height = 10 });
  98. uxFrimwareStackPanel.Children.Add(firmwarePanel);
  99. }
  100. if (!groupedFrimware.Select(x => x.Key).Contains(0))
  101. {
  102. FirmwareVersionPanel firmwarePanel = CreateFirmwarePanel(folderPath, 0, new List<FirmwareUpdateModel>());
  103. uxFrimwareStackPanel.Children.Add(new Rectangle() { Height = 10 });
  104. uxFrimwareStackPanel.Children.Add(firmwarePanel);
  105. }
  106. }
  107. private void ClearUi()
  108. {
  109. //uxIpAddress.Text = string.Empty;
  110. uxFirstTelcom.Visibility = Visibility.Collapsed;
  111. uxSecondTelcom.Visibility = Visibility.Collapsed;
  112. }
  113. private FirmwareVersionPanel CreateFirmwarePanel(string folderPath, int key, List<FirmwareUpdateModel> firmwareUpdateModels)
  114. {
  115. var toReturn = new FirmwareVersionPanel();
  116. toReturn.Index = key;
  117. toReturn.FileFolderPath = folderPath;
  118. toReturn.VersionPairs = firmwareUpdateModels;
  119. //{ Index = key , VersionPairs = firmwareUpdateModels, FileFolderPath = folderPath };
  120. return toReturn;
  121. }
  122. private bool CheckSetting()
  123. {
  124. var modelName = uxEvseModelNameList.SelectedItem as string;
  125. EvseSettingConfigModel config = this.DataContext as EvseSettingConfigModel;
  126. if (string.IsNullOrEmpty(modelName) ||
  127. config == null)
  128. {
  129. MessageBox.Show("load model failed");
  130. return false;
  131. }
  132. if (config.IsSimInsert)
  133. {
  134. if (string.IsNullOrEmpty(config.SimICCID) ||
  135. string.IsNullOrEmpty(config.SimIMSI))
  136. {
  137. MessageBox.Show("ICCID and IMSI should not be empty");
  138. return false;
  139. }
  140. }
  141. if (config.IsSubSimInsert)
  142. {
  143. if (string.IsNullOrEmpty(config.SubSimICCID) ||
  144. string.IsNullOrEmpty(config.SubSimIMSI))
  145. {
  146. MessageBox.Show("Sub ICCID and IMSI should not be empty");
  147. return false;
  148. }
  149. }
  150. return true;
  151. }
  152. private void SaveCurrent()
  153. {
  154. var modelName = uxEvseModelNameList.SelectedItem as string;
  155. EvseSettingConfigModel config = this.DataContext as EvseSettingConfigModel;
  156. var firmwareUpdateList = LoadFirmwareUpdateListFromPanel();
  157. CopyAndRenameFirmwareUpdateList(config.ModelName, firmwareUpdateList);
  158. config.FirmwareUpdateList = firmwareUpdateList;
  159. //var test = JsonConvert.SerializeObject(config);
  160. File.WriteAllText(
  161. EvseSettingConfig.GetModelIniPath(config.ModelName),
  162. JsonConvert.SerializeObject(
  163. config,
  164. Formatting.Indented,
  165. new StringEnumConverter()
  166. ));
  167. Reset();
  168. }
  169. private List<FirmwareUpdateModel> LoadFirmwareUpdateListFromPanel()
  170. {
  171. var toReturn = new List<FirmwareUpdateModel>();
  172. foreach (var uiPanel in uxFrimwareStackPanel.Children)
  173. {
  174. FirmwareVersionPanel firmwareVersionPanel = uiPanel as FirmwareVersionPanel;
  175. if (firmwareVersionPanel is null)
  176. {
  177. continue;
  178. }
  179. toReturn.AddRange(firmwareVersionPanel.VersionPairs);
  180. }
  181. return toReturn;
  182. }
  183. private void CopyAndRenameFirmwareUpdateList(string modelName, List<FirmwareUpdateModel> firmwareUpdateList)
  184. {
  185. if (firmwareUpdateList == null)
  186. {
  187. return;
  188. }
  189. var modelPath = EvseSettingConfig.GetModelFolder(modelName);
  190. foreach (var model in firmwareUpdateList)
  191. {
  192. if (string.IsNullOrEmpty(model.FirmwareFileName))
  193. {
  194. continue;
  195. }
  196. var firmwarePath = System.IO.Path.GetDirectoryName(model.FirmwareFileName);
  197. var firmwareName = System.IO.Path.GetFileName(model.FirmwareFileName);
  198. if (firmwarePath != modelPath)
  199. {
  200. //Copy
  201. var copyToPathFileName = System.IO.Path.Combine(modelPath, firmwareName);
  202. if (File.Exists(copyToPathFileName))
  203. {
  204. File.Delete(copyToPathFileName);
  205. }
  206. File.Copy(model.FirmwareFileName, copyToPathFileName);
  207. }
  208. model.FirmwareFileName = firmwareName;
  209. }
  210. }
  211. private void CreateNewConfig()
  212. {
  213. var dlg = new NewModelNameDlg();
  214. dlg.Owner = MainWindow.Instance;
  215. dlg.ShowDialog();
  216. if (dlg.DialogResult != true)
  217. {
  218. return;
  219. }
  220. var modelName = dlg.ModelName;
  221. var modelFolderPath = EvseSettingConfig.GetModelFolder(modelName);
  222. var modelIniPath = EvseSettingConfig.GetModelIniPath(modelName);
  223. if (!Directory.Exists(modelFolderPath))
  224. {
  225. Directory.CreateDirectory(modelFolderPath);
  226. }
  227. var initModelSetting = new EvseSettingConfigModel() { ModelName = modelName };
  228. File.WriteAllText(
  229. modelIniPath,
  230. JsonConvert.SerializeObject(
  231. initModelSetting,
  232. Formatting.Indented,
  233. new StringEnumConverter()
  234. ));
  235. Reset();
  236. }
  237. private void AddDispenser_Click(object sender, RoutedEventArgs e)
  238. {
  239. List<int> createdFrimwareIndex = new List<int>();
  240. foreach (var uiElement in uxFrimwareStackPanel.Children)
  241. {
  242. FirmwareVersionPanel firmwarePanel = uiElement as FirmwareVersionPanel;
  243. if (firmwarePanel is null)
  244. {
  245. continue;
  246. }
  247. createdFrimwareIndex.Add(firmwarePanel.Index);
  248. //FirmwareVersionPanel firmwarePanel = CreateFirmwarePanel(folderPath, firmwareGroup.Key, firmwareGroup.ToList());
  249. //uxFrimwareStackPanel.Children.Add(new Rectangle() { Height = 10 });
  250. //uxFrimwareStackPanel.Children.Add(firmwarePanel);
  251. }
  252. int toCreateIndex = 0;
  253. while (createdFrimwareIndex.Contains(toCreateIndex))
  254. {
  255. toCreateIndex++;
  256. }
  257. var modelName = uxEvseModelNameList.SelectedItem as string;
  258. var folderPath = EvseSettingConfig.GetModelFolder(modelName);
  259. FirmwareVersionPanel createdFirmwarePanel = CreateFirmwarePanel(folderPath, toCreateIndex, new List<FirmwareUpdateModel>());
  260. uxFrimwareStackPanel.Children.Add(new Rectangle() { Height = 10 });
  261. uxFrimwareStackPanel.Children.Add(createdFirmwarePanel);
  262. }
  263. }
  264. }