MainWindow.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. using MesAdaptor;
  2. using Microsoft.Win32;
  3. using ST_CUBE_MES.Service;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Reflection;
  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 ST_CUBE_MES
  22. {
  23. /// <summary>
  24. /// MainWindow.xaml 的互動邏輯
  25. /// </summary>
  26. public partial class MainWindow : Window
  27. {
  28. public MainWindow()
  29. {
  30. InitializeComponent();
  31. Title = Title + string.Format(" V{0}", Assembly.GetEntryAssembly().GetName().Version);
  32. this.stlinkService = new STLinkCliPrograrmService(
  33. AppSettingService.Instance.STCubeCliPath,
  34. AppSettingService.Instance.Port,
  35. AppSettingService.Instance.CustomDefaultOptions
  36. );
  37. this.fileRrecordService = new FileRrecordService();
  38. //this.loginWindow = new LoginWindow();
  39. this.resultDialog = new ResultDialog();
  40. Loaded += MainWindow_Loaded;
  41. }
  42. private delegate void GenericDelegate();
  43. //private readonly LoginWindow loginWindow;
  44. private ResultDialog resultDialog;
  45. private readonly STLinkCliPrograrmService stlinkService;
  46. private readonly FileRrecordService fileRrecordService;
  47. public string UserId { get; internal set; }
  48. public string WorkOrder { get; internal set; }
  49. protected override void OnClosing(CancelEventArgs e)
  50. {
  51. base.OnClosing(e);
  52. var currentSetting = AppSettingService.Instance;
  53. var setting = new AppSetting()
  54. {
  55. LOCK = currentSetting.LOCK,
  56. MES = currentSetting.MES,
  57. MechineCode = currentSetting.MechineCode,
  58. BinPath = uxBinFilePath.Text,
  59. STCubeCliPath = currentSetting.STCubeCliPath,
  60. Port = currentSetting.Port,
  61. CustomDefaultOptions = currentSetting.CustomDefaultOptions,
  62. };
  63. currentSetting.Save(setting);
  64. }
  65. private void MainWindow_Loaded(object sender, RoutedEventArgs e)
  66. {
  67. var stopResult = SajetConnect.SajetTransStart();
  68. //loginWindow.Owner = this;
  69. resultDialog.Owner = this;
  70. //Reset();
  71. uxBinFilePath.Text = AppSettingService.Instance.BinPath;
  72. if (string.IsNullOrEmpty(AppSettingService.Instance.STCubeCliPath))
  73. {
  74. MessageBox.Show("STCubeCliPath should not be empty");
  75. }
  76. if (AppSettingService.Instance.LOCK)
  77. {
  78. uxBinFilePath.IsEnabled = false;
  79. uxBinFileBtn.IsEnabled = false;
  80. }
  81. }
  82. private void uxBinFileBtn_Click(object sender, RoutedEventArgs e)
  83. {
  84. OpenFileDialog dlg = new OpenFileDialog();
  85. var dlgRsult = dlg.ShowDialog();
  86. if (dlgRsult != true)
  87. {
  88. return;
  89. }
  90. uxBinFilePath.Text = dlg.FileName;
  91. }
  92. private async void uxRunBtn_Click(object sender, RoutedEventArgs e)
  93. {
  94. uxRunBtn.IsEnabled = false;
  95. try
  96. {
  97. uxTerminal.Document.Blocks.Clear();
  98. if (string.IsNullOrEmpty(uxSN.Text))
  99. {
  100. AddTerminalMsg("ERROR: SN Should not be empty");
  101. return;
  102. }
  103. if (!ValidateSN())
  104. {
  105. return;
  106. }
  107. string filePath = uxBinFilePath.Text;
  108. if (!File.Exists(filePath))
  109. {
  110. AddTerminalMsg("ERROR: Program file not found");
  111. return;
  112. }
  113. string cliPath = stlinkService.CliPath;
  114. if (!File.Exists(filePath))
  115. {
  116. AddTerminalMsg("ERROR: STM32_Programmer_CLI not found");
  117. return;
  118. }
  119. fileRrecordService.Start(uxSN.Text);
  120. var task = new Task(() =>
  121. {
  122. //GenericDelegate porgramFunc;
  123. //porgramFunc = () =>
  124. //{
  125. // StartProgram(filePath);
  126. //};
  127. //this.Dispatcher.BeginInvoke(porgramFunc);
  128. StartProgram(filePath);
  129. });
  130. task.Start();
  131. await task;
  132. }
  133. catch (Exception error)
  134. {
  135. MessageBox.Show(error.Message);
  136. }
  137. finally
  138. {
  139. uxRunBtn.IsEnabled = true;
  140. }
  141. }
  142. private void StartProgram(string filePath)
  143. {
  144. UpdateProgressBar(0, 100);
  145. stlinkService.OnMsgReceviced += StlinkService_OnMsgReceviced;
  146. stlinkService.OnProgressChanged += StlinkService_OnProgressChanged;
  147. var result = stlinkService.StartProgramProcess(filePath);
  148. stlinkService.OnMsgReceviced -= StlinkService_OnMsgReceviced;
  149. stlinkService.OnProgressChanged -= StlinkService_OnProgressChanged;
  150. if (result is null)
  151. {
  152. //report empty error
  153. SajetConnect.SajetTranFinishFail(MesErrorCode.ProgramFail);
  154. return;
  155. }
  156. if (!string.IsNullOrEmpty(result.ErrorMsg))
  157. {
  158. fileRrecordService.Log(result.ErrorMsg);
  159. AddTerminalMsg(result.ErrorMsg);
  160. }
  161. var reportDatas = new ValueReportDatas();
  162. foreach (var logData in result.Data)
  163. {
  164. reportDatas.Add(logData.Key, logData.Value, true);
  165. }
  166. this.Dispatcher.Invoke(() => {
  167. SajetConnect.SajetTransReport(reportDatas);
  168. });
  169. if (!result.IsSuccess)
  170. {
  171. var errCode = GetErrorCode(result.Step);
  172. //report
  173. this.Dispatcher.Invoke(() => {
  174. SajetConnect.SajetTranFinishFail(errCode);
  175. resultDialog = new ResultDialog();
  176. resultDialog.Owner = this;
  177. resultDialog.MouseDown += ResultDialog_MouseDown;
  178. resultDialog.ShowResult(false);
  179. });
  180. }
  181. if (result.IsSuccess)
  182. {
  183. //report
  184. this.Dispatcher.Invoke(() => {
  185. SajetConnect.SajetTranFinishSuccess();
  186. resultDialog = new ResultDialog();
  187. resultDialog.Owner = this;
  188. resultDialog.MouseDown += ResultDialog_MouseDown;
  189. resultDialog.ShowResult(true);
  190. });
  191. }
  192. fileRrecordService.Complete();
  193. }
  194. private MesErrorCode GetErrorCode(int step)
  195. {
  196. switch (step)
  197. {
  198. case 0:
  199. return MesErrorCode.None;
  200. case 1:
  201. return MesErrorCode.EraseFail;
  202. case 2:
  203. return MesErrorCode.ProgramFail;
  204. case 3:
  205. return MesErrorCode.GetCheckSumFail;
  206. default:
  207. return MesErrorCode.None;
  208. }
  209. }
  210. private void ResultDialog_MouseDown(object sender, MouseButtonEventArgs e)
  211. {
  212. resultDialog.MouseDown -= ResultDialog_MouseDown;
  213. resultDialog.Close();
  214. //this.Close();
  215. }
  216. private void ResultDialog_Closing(object sender, CancelEventArgs e)
  217. {
  218. resultDialog.Closing -= ResultDialog_Closing;
  219. //this.Close();
  220. }
  221. private bool ValidateSN()
  222. {
  223. string tmpSN;
  224. bool bResult = false;
  225. tmpSN = string.Format("{0}", uxSN.Text);
  226. var mesSetting = AppSettingService.Instance.MES.ToLower();
  227. if (mesSetting == "php" || mesSetting == "shinewave")
  228. {
  229. bResult = SajetConnect.SajetTransSnCheck(ref tmpSN);
  230. if (!bResult)
  231. {
  232. MessageBox.Show("SN not found");
  233. }
  234. return bResult;
  235. }
  236. else if (mesSetting == "phv" || mesSetting == "sajet2")
  237. {
  238. bResult = SajetConnect.SajetTransSnCheck(ref tmpSN, "");
  239. if (!bResult)
  240. {
  241. MessageBox.Show("SN not found");
  242. }
  243. return bResult;
  244. }
  245. else if (mesSetting == "test")
  246. {
  247. return true;
  248. }
  249. MessageBox.Show("Not supported MES");
  250. return false;
  251. }
  252. private void StlinkService_OnProgressChanged(int currentStep, int maxStep)
  253. {
  254. UpdateProgressBar(currentStep, maxStep);
  255. }
  256. private void StlinkService_OnMsgReceviced(string msg)
  257. {
  258. AddTerminalMsg(msg);
  259. fileRrecordService.Log(msg);
  260. }
  261. private void AddTerminalMsg(string msg)
  262. {
  263. GenericDelegate onMsgReceviced;
  264. onMsgReceviced = () =>
  265. {
  266. var paragraph = new Paragraph();
  267. paragraph.Inlines.Add(GetRun(msg));
  268. uxTerminal.Document.Blocks.Add(paragraph);
  269. uxTerminal.ScrollToEnd();
  270. };
  271. this.Dispatcher.BeginInvoke(onMsgReceviced);
  272. }
  273. private void UpdateProgressBar(int currentStep, int maxStep)
  274. {
  275. GenericDelegate onUpdateProgressBar;
  276. onUpdateProgressBar = () =>
  277. {
  278. uxProgressBar.Maximum = maxStep;
  279. uxProgressBar.Value = currentStep;
  280. };
  281. this.Dispatcher.BeginInvoke(onUpdateProgressBar);
  282. }
  283. private Run GetRun(string msg)
  284. {
  285. if (msg is null)
  286. {
  287. msg = "";
  288. }
  289. Color textColor = Colors.White;
  290. if (msg.StartsWith("ERROR:") ||
  291. msg == "Unable to connect to ST-LINK!")
  292. {
  293. textColor = Colors.Red;
  294. }
  295. if (msg == "Programming Complete." ||
  296. msg == "No difference found." ||
  297. msg == "MCU Reset." ||
  298. msg.StartsWith("checksum:"))
  299. {
  300. textColor = Colors.LightGreen;
  301. }
  302. return new Run() { Text = msg, Foreground = new SolidColorBrush(textColor) };
  303. }
  304. }
  305. }