MainWindow.xaml.cs 9.7 KB

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