using MesAdaptor; using Microsoft.Win32; using ST_CUBE_MES.Service; using System; using System.Collections.Generic; using System.ComponentModel; using System.IO; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace ST_CUBE_MES { /// /// MainWindow.xaml 的互動邏輯 /// public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); Title = Title + string.Format(" V{0}", Assembly.GetEntryAssembly().GetName().Version); this.stlinkService = new STLinkCliPrograrmService( AppSettingService.Instance.STCubeCliPath, AppSettingService.Instance.Port, AppSettingService.Instance.CustomDefaultOptions ); this.fileRrecordService = new FileRrecordService(); //this.loginWindow = new LoginWindow(); this.resultDialog = new ResultDialog(); Loaded += MainWindow_Loaded; KeyDown += MainWindow_KeyDown; } private delegate void GenericDelegate(); //private readonly LoginWindow loginWindow; private ResultDialog resultDialog; private readonly STLinkCliPrograrmService stlinkService; private readonly FileRrecordService fileRrecordService; public string UserId { get; internal set; } public string WorkOrder { get; internal set; } protected override void OnClosing(CancelEventArgs e) { base.OnClosing(e); var currentSetting = AppSettingService.Instance; var setting = new AppSetting() { LOCK = currentSetting.LOCK, MES = currentSetting.MES, MechineCode = currentSetting.MechineCode, BinPath = uxBinFilePath.Text, STCubeCliPath = currentSetting.STCubeCliPath, Port = currentSetting.Port, CustomDefaultOptions = currentSetting.CustomDefaultOptions, }; currentSetting.Save(setting); } private void MainWindow_Loaded(object sender, RoutedEventArgs e) { var stopResult = SajetConnect.SajetTransStart(); //loginWindow.Owner = this; resultDialog.Owner = this; //Reset(); uxBinFilePath.Text = AppSettingService.Instance.BinPath; if (string.IsNullOrEmpty(AppSettingService.Instance.STCubeCliPath)) { MessageBox.Show("STCubeCliPath should not be empty"); } if (AppSettingService.Instance.LOCK) { uxBinFilePath.IsEnabled = false; uxBinFileBtn.IsEnabled = false; } uxSN.Focus(); } private async void MainWindow_KeyDown(object sender, KeyEventArgs e) { if (e.Key != Key.Enter) { return; } await StartRun(); } private void uxBinFileBtn_Click(object sender, RoutedEventArgs e) { OpenFileDialog dlg = new OpenFileDialog(); var dlgRsult = dlg.ShowDialog(); if (dlgRsult != true) { return; } uxBinFilePath.Text = dlg.FileName; } private async void uxRunBtn_Click(object sender, RoutedEventArgs e) { await StartRun(); } private async Task StartRun() { if (uxRunBtn.IsEnabled == false) { return; } uxRunBtn.IsEnabled = false; try { uxTerminal.Document.Blocks.Clear(); if (string.IsNullOrEmpty(uxSN.Text)) { AddTerminalMsg("ERROR: SN Should not be empty"); return; } if (!ValidateSN()) { return; } string filePath = uxBinFilePath.Text; if (!File.Exists(filePath)) { AddTerminalMsg("ERROR: Program file not found"); return; } string cliPath = stlinkService.CliPath; if (!File.Exists(filePath)) { AddTerminalMsg("ERROR: STM32_Programmer_CLI not found"); return; } fileRrecordService.Start(uxSN.Text); var task = new Task(() => { //GenericDelegate porgramFunc; //porgramFunc = () => //{ // StartProgram(filePath); //}; //this.Dispatcher.BeginInvoke(porgramFunc); StartProgram(filePath); }); task.Start(); await task; } catch (Exception error) { MessageBox.Show(error.Message); } finally { uxRunBtn.IsEnabled = true; } } private void StartProgram(string filePath) { UpdateProgressBar(0, 100); stlinkService.OnMsgReceviced += StlinkService_OnMsgReceviced; stlinkService.OnProgressChanged += StlinkService_OnProgressChanged; var result = stlinkService.StartProgramProcess(filePath); stlinkService.OnMsgReceviced -= StlinkService_OnMsgReceviced; stlinkService.OnProgressChanged -= StlinkService_OnProgressChanged; if (result is null) { //report empty error SajetConnect.SajetTranFinishFail(MesErrorCode.ProgramFail); return; } if (!string.IsNullOrEmpty(result.ErrorMsg)) { fileRrecordService.Log(result.ErrorMsg); AddTerminalMsg(result.ErrorMsg); } var reportDatas = new ValueReportDatas(); foreach (var logData in result.Data) { reportDatas.Add(logData.Key, logData.Value, true); } this.Dispatcher.Invoke(() => { SajetConnect.SajetTransReport(reportDatas); }); if (!result.IsSuccess) { var errCode = GetErrorCode(result.Step); //report this.Dispatcher.Invoke(() => { SajetConnect.SajetTranFinishFail(errCode); resultDialog = new ResultDialog(); resultDialog.Owner = this; resultDialog.OnAnyInput += ResultDialog_OnAnyInput; resultDialog.ShowResult(false); }); } if (result.IsSuccess) { //report this.Dispatcher.Invoke(() => { SajetConnect.SajetTranFinishSuccess(); resultDialog = new ResultDialog(); resultDialog.Owner = this; resultDialog.OnAnyInput += ResultDialog_OnAnyInput; resultDialog.ShowResult(true); }); } fileRrecordService.Complete(); } private MesErrorCode GetErrorCode(int step) { switch (step) { case 0: return MesErrorCode.None; case 1: return MesErrorCode.SetOptionFail; case 2: return MesErrorCode.EraseFail; case 3: return MesErrorCode.ProgramFail; case 4: return MesErrorCode.GetCheckSumFail; default: return MesErrorCode.None; } } private void ResultDialog_OnAnyInput(object sender, EventArgs e) { CloseResultDialog(); } private void CloseResultDialog() { resultDialog.OnAnyInput -= ResultDialog_OnAnyInput; resultDialog.Close(); //this.Close(); if (resultDialog.IsSuccess) { uxSN.Text = string.Empty; } } private void ResultDialog_Closing(object sender, CancelEventArgs e) { resultDialog.Closing -= ResultDialog_Closing; //this.Close(); } private bool ValidateSN() { string tmpSN; bool bResult = false; tmpSN = string.Format("{0}", uxSN.Text); var mesSetting = AppSettingService.Instance.MES.ToLower(); if (mesSetting == "php" || mesSetting == "shinewave") { bResult = SajetConnect.SajetTransSnCheck(ref tmpSN); if (!bResult) { MessageBox.Show("SN not found"); } return bResult; } else if (mesSetting == "phv" || mesSetting == "sajet2") { bResult = SajetConnect.SajetTransSnCheck(ref tmpSN, ""); if (!bResult) { MessageBox.Show("SN not found"); } return bResult; } else if (mesSetting == "test") { return true; } MessageBox.Show("Not supported MES"); return false; } private void StlinkService_OnProgressChanged(int currentStep, int maxStep) { UpdateProgressBar(currentStep, maxStep); } private void StlinkService_OnMsgReceviced(string msg) { AddTerminalMsg(msg); fileRrecordService.Log(msg); } private void AddTerminalMsg(string msg) { GenericDelegate onMsgReceviced; onMsgReceviced = () => { var paragraph = new Paragraph(); paragraph.Inlines.Add(GetRun(msg)); uxTerminal.Document.Blocks.Add(paragraph); uxTerminal.ScrollToEnd(); }; this.Dispatcher.BeginInvoke(onMsgReceviced); } private void UpdateProgressBar(int currentStep, int maxStep) { GenericDelegate onUpdateProgressBar; onUpdateProgressBar = () => { uxProgressBar.Maximum = maxStep; uxProgressBar.Value = currentStep; }; this.Dispatcher.BeginInvoke(onUpdateProgressBar); } private Run GetRun(string msg) { if (msg is null) { msg = ""; } Color textColor = Colors.White; if (msg.StartsWith("ERROR:") || msg == "Unable to connect to ST-LINK!") { textColor = Colors.Red; } if (msg == "Programming Complete." || msg == "No difference found." || msg == "MCU Reset." || msg.StartsWith("checksum:")) { textColor = Colors.LightGreen; } return new Run() { Text = msg, Foreground = new SolidColorBrush(textColor) }; } } }