|
@@ -0,0 +1,296 @@
|
|
|
+using MesAdaptor;
|
|
|
+using Microsoft.Win32;
|
|
|
+using ST_LINK_MES.Model;
|
|
|
+using ST_LINK_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_LINK_MES
|
|
|
+{
|
|
|
+ /// <summary>
|
|
|
+ /// Interaction logic for MainWindow.xaml
|
|
|
+ /// </summary>
|
|
|
+ public partial class MainWindow : Window
|
|
|
+ {
|
|
|
+ public MainWindow()
|
|
|
+ {
|
|
|
+ InitializeComponent();
|
|
|
+
|
|
|
+ Title = Title + string.Format(" V{0}", Assembly.GetEntryAssembly().GetName().Version);
|
|
|
+
|
|
|
+ this.stlinkService = new STLinkCliPrograrmService(AppSettingService.Instance.DLinkCliPath);
|
|
|
+ //this.loginWindow = new LoginWindow();
|
|
|
+ this.resultDialog = new ResultDialog();
|
|
|
+
|
|
|
+ Loaded += MainWindow_Loaded;
|
|
|
+ }
|
|
|
+
|
|
|
+ private delegate void GenericDelegate();
|
|
|
+ //private readonly LoginWindow loginWindow;
|
|
|
+ private readonly ResultDialog resultDialog;
|
|
|
+ private readonly STLinkCliPrograrmService stlinkService;
|
|
|
+
|
|
|
+ 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
|
|
|
+ };
|
|
|
+ currentSetting.Save(setting);
|
|
|
+
|
|
|
+ var stopResult = SajetConnect.SajetTransClose();
|
|
|
+ }
|
|
|
+
|
|
|
+ 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 (AppSettingService.Instance.LOCK)
|
|
|
+ {
|
|
|
+ uxBinFilePath.IsEnabled = false;
|
|
|
+ uxBinFileBtn.IsEnabled = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ 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 void uxRunBtn_Click(object sender, RoutedEventArgs e)
|
|
|
+ {
|
|
|
+ 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: ST-LINK_CLI not found");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ var task = new Task(() =>
|
|
|
+ {
|
|
|
+ StartProgram(filePath);
|
|
|
+ });
|
|
|
+ task.Start();
|
|
|
+ }
|
|
|
+
|
|
|
+ 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))
|
|
|
+ {
|
|
|
+ AddTerminalMsg(result.ErrorMsg);
|
|
|
+ }
|
|
|
+
|
|
|
+ var reportDatas = new ValueReportDatas();
|
|
|
+ foreach (var logData in result.Data)
|
|
|
+ {
|
|
|
+ reportDatas.Add(logData.Key, logData.Value, true);
|
|
|
+ }
|
|
|
+ SajetConnect.SajetTransReport(reportDatas);
|
|
|
+
|
|
|
+ if (!result.IsSuccess)
|
|
|
+ {
|
|
|
+ var errCode = GetErrorCode(result.Step);
|
|
|
+ //report
|
|
|
+ SajetConnect.SajetTranFinishFail(errCode);
|
|
|
+ resultDialog.MouseDown += ResultDialog_MouseDown;
|
|
|
+ resultDialog.ShowResult(false);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (result.IsSuccess)
|
|
|
+ {
|
|
|
+ //report
|
|
|
+ SajetConnect.SajetTranFinishSuccess();
|
|
|
+ resultDialog.MouseDown += ResultDialog_MouseDown;
|
|
|
+ resultDialog.ShowResult(true);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private MesErrorCode GetErrorCode(int step)
|
|
|
+ {
|
|
|
+ switch(step)
|
|
|
+ {
|
|
|
+ case 0:
|
|
|
+ return MesErrorCode.None;
|
|
|
+ case 1:
|
|
|
+ return MesErrorCode.SetOptionsFail;
|
|
|
+ case 2:
|
|
|
+ return MesErrorCode.ProgramFail;
|
|
|
+ case 3:
|
|
|
+ return MesErrorCode.RestFail;
|
|
|
+ case 4:
|
|
|
+ return MesErrorCode.ProgramCheckFail;
|
|
|
+ case 5:
|
|
|
+ return MesErrorCode.GetCheckSumFail;
|
|
|
+ default:
|
|
|
+ return MesErrorCode.None;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void ResultDialog_MouseDown(object sender, MouseButtonEventArgs e)
|
|
|
+ {
|
|
|
+ resultDialog.MouseDown -= ResultDialog_MouseDown;
|
|
|
+ this.Close();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void ResultDialog_Closing(object sender, CancelEventArgs e)
|
|
|
+ {
|
|
|
+ resultDialog.Closing -= ResultDialog_Closing;
|
|
|
+ this.Close();
|
|
|
+ }
|
|
|
+
|
|
|
+ private bool ValidateSN()
|
|
|
+ {
|
|
|
+ string tmpSN;
|
|
|
+ bool bResult = false;
|
|
|
+ tmpSN = 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;
|
|
|
+ }
|
|
|
+
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+
|
|
|
+ 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) };
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|