MainWindow.xaml.cs 11 KB

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