MainWindow.xaml.cs 9.1 KB

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