OTX-Runtime for DotNet  
Sample Program

The Sample program demonstrates the main functionality of the OTX-Runtime API. It can be used as a reference for the expected runtime behavior of the API and the source code below is like a reference guide about the proper programming of the API.

Code Example

Code snippet of the OTX-Runtime API Sample Program.

8 using OpenTestSystem.Otx.Runtime2.Api.DotNet.Sample.OutputCustomImplementation;
9 using OpenTestSystem.Otx.Runtime2.Api.DotNet.Sample.Setting;
10 using System;
11 using System.Collections.Generic;
12 using System.Diagnostics;
13 using System.Drawing;
14 using System.IO;
15 using System.Linq;
16 using System.Threading;
17 using System.Threading.Tasks;
18 using System.Windows.Forms;
19 using Microsoft.Win32;
20 
21 namespace OpenTestSystem.Otx.Runtime2.Api.DotNet.Sample
22 {
23  public partial class SampleForm : Form
24  {
25  private const int VALUE_STRING_MAX_LENGTH = 1024;
26 
27  private UserSettings userSettings = UserSettings.Default;
28 
29  // TODO: handle multithreading
30  private List<IRuntimeContext> runtimeContexts = new List<IRuntimeContext>();
31  private Dictionary<IRuntimeContext, DateTime> runtimeContextsExecutionStartTime = new Dictionary<IRuntimeContext, DateTime>();
32 
33  private IProject project = null;
34  private IPlayerProject playerProject = null;
35  private IProcedure procedureToExecute = null;
36 
37  private TreeNode startupNode = null;
38 
39  private IRuntimeManager globalRuntimeManager = null;
40 
41  private HmiWindow hmiWindow;
42 
43  private DateTime lastTime = DateTime.Now;
44 
45  private DefaultCustomScreenImplementation customScreenImplementation;
46  private ContextVariableImplementation contextVariableImplementation;
47 
48  private StateVariableImplementation stateVariableImplementation;
49  private DefaultMeasureImplementation measureImplementation;
50  private DefaultExternalServiceProviderImplementation serviceProviderImplementation;
51 
52  private BasicScreenOutputImpl basicScreenOutputImpl;
53  private CustomScreenOutputImpl customScreenOutputImpl;
54  private LoggingOutputImpl loggingOutputImpl;
55  private ContextVariableOutputImpl contextVariableOutputImpl;
56 
57  private StateVariableOutputImpl stateVariableOutputImpl;
58  private MeasureOutputImpl measureOutputImpl;
59  private I18nOutputImpl i18NOutputImpl;
60  private ExternalServiceProviderOutputImpl serviceProviderOutputImpl;
61 
62  private SqlOutputImpl sqlOutputImpl;
63  private CommonDialogsOutputImpl commonDialogsOutputImpl;
64 
65  private SampleForm creatorForm;
66  private string title;
67  private string runtimeContextName;
68  private long procedureExecutionCount;
69  private long cyclicExecutionCount;
70 
71  private static string fileVersion;
72 
73  private static int defaultPollingTime = 500;
74  private static int defaultBatteryVoltageThreshold = 6000;
75  private bool useConnectionState = false;
76  private bool? KL15State = null;
77  private ExpectedState expectedConnectionState = ExpectedState.None;
78 
79  private bool isLoadding = false;
80 
81  private ushort defaultDiagPort = SampleConstants.DEFAULT_DM_PORT;
82  private ushort defaultRuntimePort = SampleConstants.DEFAULT_RT_PORT;
83  private string defaultDiagPipeName = SampleConstants.DEFAULT_DM_PIPE_NAME;
84  private string defaultRuntimePipeName = SampleConstants.DEFAULT_RT_PIPE_NAME;
85 
86  private readonly object printTextLock = new object();
87  private readonly object eventListenerLock = new object();
88 
89  private bool cyclicExecuteAsyncIsProcessing = false;
90  private System.Windows.Forms.Timer webServerTimer = new System.Windows.Forms.Timer();
91 
92  public static bool isAcceptEmptyLicenseValue;
93  private static List<SampleForm> listForms = new List<SampleForm>();
94 
95  static SampleForm()
96  {
97  fileVersion = RuntimeConfig.Instance.Version;
98  String licenseKeyFromOTForOTPLicense = OpenTestSystem.Common.LicensingV5.Util.CreateLicenseKeyFromOTForOTPLicense();
99 
100  isAcceptEmptyLicenseValue = licenseKeyFromOTForOTPLicense.Equals("") ? false : true;
101 
102  if (!String.IsNullOrEmpty(licenseKeyFromOTForOTPLicense) && LicensingUtil.CheckLicenseKeyFormat(licenseKeyFromOTForOTPLicense))
103  {
104  LicenseManager.SetLicenseKey(licenseKeyFromOTForOTPLicense);
105  }
106  else
107  {
108  String licenseKey = Setting.UserSettings.Default.LicenseKey;
109  licenseKey = LicensingUtil.Decrypt(licenseKey);
110  if (LicensingUtil.CheckLicenseKeyFormat(licenseKey))
111  {
112  LicenseManager.SetLicenseKey(licenseKey);
113  }
114  else
115  {
116  MessageBox.Show("There is an invalid license key format. Please set your new key in license box.", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
117  }
118  }
119 
120  SystemEvents.DisplaySettingsChanged += new EventHandler(SystemEvents_DisplaySettingsChanged);
121  }
122 
123  static void SystemEvents_DisplaySettingsChanged(object sender, EventArgs e)
124  {
125  try
126  {
127  foreach (Form frm in listForms)
128  {
129  try
130  {
131  if (frm.InvokeRequired)
132  {
133  frm.Invoke(new Action(() => { frm.Refresh(); }));
134  }
135  else
136  {
137  frm.Refresh();
138  }
139  }
140  catch { }
141  }
142  }
143  catch { }
144  }
145 
146  public SampleForm(string title = SampleConstants.MAIN_INSTANCE_NAME, SampleForm creatorForm = null)
147  {
148  this.ShowInTaskbar = false;
149  this.title = title;
150  this.creatorForm = creatorForm;
151 
152  this.webServerTimer.Interval = 1000;
153  this.webServerTimer.Enabled = true;
154  this.webServerTimer.Tick += WebServerTimer_Tick;
155 
156  InitializeComponent();
157 
158  listForms.Add(this);
159 
160  PrintText("Application started");
161  }
162 
163  private void SampleForm_Load(object sender, EventArgs e)
164  {
165  PrintText("Start Initialization...");
166 
167  CreateCustomImpl();
168  InitializeSampleDefaultValues();
169  this.UpdateWebServerButton();
170 
171  if (this.creatorForm != null)
172  {
173  this.checkBoxStartAllParents.Visible = true;
174  this.checkBoxStartAllParents.Checked = this.creatorForm.checkBoxStartAllParents.Checked;
175  }
176 
177  SetRuntimeContextName();
178  SetTitle(this.title);
179  SetLocation(this.creatorForm);
180 
181  this.toolTip1.SetToolTip(labelProcedureExecutionTimes, "Total procedure execution time.");
182  this.toolTip1.SetToolTip(textBoxTimeout, "Timeout for procedure execution in milliseconds. The default value 0 means without timeout.");
183  this.toolTip1.SetToolTip(checkBoxUseConnectionState, "Use expected connection state during procedure execution.");
184  this.toolTip1.SetToolTip(labelBatteryState, "State of the battery (KL 30)");
185  this.toolTip1.SetToolTip(checkBoxIgnition, "Expected ignition (KL 15) state during procedure execution");
186  this.toolTip1.SetToolTip(labelIgnitionState, "State of the ignition (KL 15)");
187  this.toolTip1.SetToolTip(buttonCheckBatteryIgnition, "Check current battery (KL30) and ignition (KL15) state.");
188  this.toolTip1.SetToolTip(textBoxPollingTime, "Time in milliseconds between the next check of the connection state during the procedure execution (Polling). The default value is 500 ms.");
189  this.toolTip1.SetToolTip(textBoxVoltageThreshold, "Voltage threshold in millivolt from which the battery voltage, see GetBatteryVoltage is enough for KL30 = On. The default value is 6000 mV.");
190 
191  LoadContextFile(globalRuntimeManager);
192 
193  PrintText("... Initialization finished.");
194  this.ShowInTaskbar = true;
195  }
196 
197  private void SampleForm_Shown(object sender, EventArgs e)
198  {
199  this.cbFilePath.SelectionLength = 0;
200  }
201 
202  private void SetRuntimeContextName()
203  {
204  if (this.creatorForm != null && !String.IsNullOrEmpty(this.creatorForm.runtimeContextName))
205  {
206  System.Text.RegularExpressions.Match regExpMatch = System.Text.RegularExpressions.Regex.Match(this.creatorForm.runtimeContextName, @"\d*$");
207  int number;
208  if (int.TryParse(regExpMatch.Value, out number))
209  {
210  number++;
211  this.textBoxRuntimeContextName.Text = this.creatorForm.runtimeContextName.Substring(0, regExpMatch.Index) + number.ToString();
212  }
213  }
214  }
215 
216  private void SetTitle(string title)
217  {
218  if (String.IsNullOrWhiteSpace(title))
219  title = SampleConstants.MAIN_INSTANCE_NAME;
220 
221  bool runningAsAdmin = System.Security.Principal.WindowsIdentity.GetCurrent().Owner.IsWellKnown(System.Security.Principal.WellKnownSidType.BuiltinAdministratorsSid);
222 
223  this.Text = String.Format(
224  "emotive OTX-Runtime API for DotNet - Reference Application - Version {0} - {1} Bit - {2} - Thread-ID {3}{4}",
225  fileVersion,
226  Environment.Is64BitProcess ? "64" : "32",
227  title,
228  Thread.CurrentThread.ManagedThreadId,
229  runningAsAdmin ? " - Administrator" : string.Empty
230  );
231 
232  if (!String.IsNullOrEmpty(this.cbFilePath.Text))
233  this.Text = this.Text + " - " + Path.GetFileName(this.cbFilePath.Text);
234 
235  if (!String.IsNullOrEmpty(this.runtimeContextName))
236  this.Text = this.Text + " - " + this.runtimeContextName;
237  }
238 
239  private const int FORM_OFFSET = 25;
240 
241  private void SetLocation(SampleForm creatorForm)
242  {
243  if (creatorForm != null)
244  {
245  int xOffset = FORM_OFFSET;
246  int yOffset = FORM_OFFSET;
247 
248  if (creatorForm.creatorForm != null)
249  {
250  xOffset = creatorForm.Location.X - creatorForm.creatorForm.Location.X;
251  yOffset = creatorForm.Location.Y - creatorForm.creatorForm.Location.Y;
252  }
253 
254  this.Location = new Point(creatorForm.Location.X + xOffset, creatorForm.Location.Y + yOffset);
255  this.Size = creatorForm.Size;
256  }
257  }
258 
259  private void CreateCustomImpl()
260  {
261  CreateDefaultCustomImpl();
262  CreateOutputWindowCustomImpl();
263  }
264 
265  private void CreateDefaultCustomImpl()
266  {
267  PrintText("Create default custom implementation");
268 
269  customScreenImplementation = new DefaultCustomScreenImplementation();
270  customScreenImplementation.KeyDown += CustomScreenImplementation_KeyDown;
271 
272  contextVariableImplementation = new ContextVariableImplementation();
273  contextVariableImplementation.ContextVariableRead += ContextVariableRead;
274 
275  stateVariableImplementation = new StateVariableImplementation();
276  stateVariableImplementation.StateVariableValueChanged += StateVariableValueChanged;
277 
278  measureImplementation = new DefaultMeasureImplementation();
279  serviceProviderImplementation = new DefaultExternalServiceProviderImplementation();
280  }
281 
282  private void CreateOutputWindowCustomImpl()
283  {
284  PrintText("Create output window custom implementation");
285 
286  basicScreenOutputImpl = new BasicScreenOutputImpl();
287  customScreenOutputImpl = new CustomScreenOutputImpl();
288  loggingOutputImpl = new LoggingOutputImpl();
289  contextVariableOutputImpl = new ContextVariableOutputImpl();
290 
291  stateVariableOutputImpl = new StateVariableOutputImpl();
292  measureOutputImpl = new MeasureOutputImpl();
293  i18NOutputImpl = new I18nOutputImpl();
294  serviceProviderOutputImpl = new ExternalServiceProviderOutputImpl();
295 
296  sqlOutputImpl = new SqlOutputImpl();
297  commonDialogsOutputImpl = new CommonDialogsOutputImpl();
298 
299  basicScreenOutputImpl.LogEvent += PrintText;
300  customScreenOutputImpl.LogEvent += PrintText;
301  loggingOutputImpl.LogEvent += PrintText;
302  contextVariableOutputImpl.LogEvent += PrintText;
303 
304  stateVariableOutputImpl.LogEvent += PrintText;
305  measureOutputImpl.LogEvent += PrintText;
306  i18NOutputImpl.LogEvent += PrintText;
307  serviceProviderOutputImpl.LogEvent += PrintText;
308 
309  sqlOutputImpl.LogEvent += PrintText;
310  commonDialogsOutputImpl.LogEvent += PrintText;
311  }
312 
313  private void InitializeSampleDefaultValues()
314  {
315  PrintText("Initialize default values");
316 
317  this.comboBoxTraceLevel.Items.AddRange(Enum.GetNames(typeof(TraceLevels)));
318 
319  // Avoid changing order
320  treeViewOtxProject.ImageList = imageList1;
321 
322  textBoxRtPortPipe.Name = SampleConstants.RT_PORT_PIPE_TEXT_BOX_NAME;
323  textBoxDiagPortPipe.Name = SampleConstants.DM_PORT_PIPE_TEXT_BOX_NAME;
324 
325  LoadSetting();
326 
327  this.EnableConnectionState();
328  }
329 
330  private void LoadSetting()
331  {
332  SaveSettingUtil.Reload();
333  try
334  {
335  SetupTraceLevel();
336  SetupTraceFolder();
337  SetupTraceFileMaxCountAndSize();
338 
339  // Avoid changing order of this line
340  SetupDefaultPortAndPipeName();
341 
342  cbFilePath.Text = userSettings.Ptx_Ppx_Directory;
343  if (!String.IsNullOrEmpty(userSettings.Ptx_Ppx_DirectoryList))
344  {
345  this.cbFilePath.Items.AddRange(userSettings.Ptx_Ppx_DirectoryList.Split(';'));
346  }
347  AddComboBoxFileOrPath(this.cbFilePath);
348 
349  SetupCustomImplType();
350  SetupWindowSize();
351  SetupWindowLocation();
352 
353  checkBoxAsyncExecution.Checked = userSettings.Asynchron;
354  checkBoxCyclicExecution.Checked = userSettings.Cyclic;
355  checkBoxCyclicReload.Checked = userSettings.CyclicReload;
356  checkBoxNewRuntimeManager.Checked = userSettings.NewRunTimeManager;
357  checkBoxAdd2Output.Checked = userSettings.AddMessageToOutput;
358  checkBoxStartAllParents.Checked = userSettings.StartAllParents;
359 
360  SetupTimeOut();
361  checkBoxUseConnectionState.Checked = userSettings.ConnectionState;
362  checkBoxIgnition.CheckState = userSettings.Ignition == 1 ? CheckState.Checked : (userSettings.Ignition == 0 ? CheckState.Unchecked : CheckState.Indeterminate);
363  SetupPollingTime();
364  SetupVoltageThreshold();
365 
366  this.textBoxRuntimeContextName.Text = userSettings.RuntimeContextName.ToString();
367  }
368  catch (System.Exception e)
369  {
370  PrintText(e.Message);
371  userSettings.Reset();
372  }
373  }
374 
375  bool isPathChanging = false;
376  private void AddComboBoxFileOrPath(ComboBox comboBox)
377  {
378  if (comboBox != null && !isPathChanging)
379  {
380  isPathChanging = true;
381 
382  string path = comboBox.Text;
383 
384  if (Directory.Exists(path) || File.Exists(path))
385  {
386  while (comboBox.Items.Contains(path))
387  {
388  comboBox.Items.Remove(path);
389  }
390 
391  while (comboBox.Items.Contains(path.TrimEnd(new char[] { '/', '\\' })))
392  {
393  comboBox.Items.Remove(path.TrimEnd(new char[] { '/', '\\' }));
394  }
395 
396  path = path.TrimEnd(new char[] { '/', '\\' });
397 
398  comboBox.Items.Insert(0, path);
399  comboBox.Text = path;
400  comboBox.SelectAll();
401  }
402 
403  isPathChanging = false;
404  }
405  }
406 
407  private void SetupTraceFileMaxCountAndSize()
408  {
409  textBoxTraceFileMaxCount.Text = userSettings.TraceFileMaxCount.ToString();
410  textBoxTraceFileMaxSize.Text = userSettings.TraceFileMaxSize.ToString();
411  }
412 
413  private void SetupDefaultPortAndPipeName()
414  {
415  defaultDiagPort = userSettings.DiagManagerPort;
416  defaultRuntimePort = userSettings.RuntimePort;
417 
418  defaultDiagPipeName = userSettings.DiagManagerPipeName;
419  defaultRuntimePipeName = userSettings.RuntimePipeName;
420 
421  IpcTypes ipcTypes = IpcTypes.SOCKET;
422  Enum.TryParse<IpcTypes>(userSettings.IpcType, out ipcTypes);
423 
424  if (comboBoxIpcType.Items.Count == 0)
425  {
426  comboBoxIpcType.Items.Add(IpcTypes.SOCKET);
427  comboBoxIpcType.Items.Add(IpcTypes.PIPE);
428  }
429 
430  comboBoxIpcType.SelectedItem = ipcTypes;
431  }
432 
433  private void SetupTraceLevel()
434  {
435  RuntimeConfig.Instance.TraceLevel = userSettings.TraceLevels;
436  comboBoxTraceLevel.SelectedItem = RuntimeConfig.Instance.TraceLevel.ToString();
437  }
438 
439  private void SetupTraceFolder()
440  {
441  if (userSettings.TracingDirectory != string.Empty)
442  RuntimeConfig.Instance.TraceFolder = userSettings.TracingDirectory;
443 
444  textBoxTraceFolder.Text = RuntimeConfig.Instance.TraceFolder;
445  }
446 
447  private void SetupCustomImplType()
448  {
449  if (userSettings.CustomImplTypes == CustomImplTypes.OutputImpl)
450  {
451  radioButtonOuputWindow.Checked = true;
452  }
453  else if (userSettings.CustomImplTypes == CustomImplTypes.NoCustom)
454  {
455  radioButtonNoCustomImplementation.Checked = true;
456  }
457  else
458  {
459  radioButtonDefaultImplementation.Checked = true;
460  }
461  }
462 
463  private void SetupWindowSize()
464  {
465  int width = userSettings.WindowWidth;
466  int height = userSettings.WindowHeight;
467  this.Size = new Size(width, height);
468  }
469 
470  private void SetupWindowLocation()
471  {
472  int locationX = userSettings.WindowLocationX;
473  int locationY = userSettings.WindowLocationY;
474  Point location = new Point(locationX, locationY);
475 
476  if (CheckFormIsInBound(location))
477  {
478  this.Location = location;
479  return;
480  }
481 
482  this.CenterToScreen();
483  }
484 
485  private bool CheckFormIsInBound(Point location)
486  {
487  try
488  {
489  Screen formContainScreens = Screen.AllScreens.Where(screen => screen.Bounds.Contains(location) == true).FirstOrDefault();
490 
491  return formContainScreens != null;
492  }
493  catch (System.Exception)
494  {
495  return false;
496  }
497  }
498 
499  private void SetupTimeOut()
500  {
501  string timeOutString = userSettings.TimeOut.ToString();
502  this.textBoxTimeout.Text = timeOutString;
503  }
504 
505  private void SetupPollingTime()
506  {
507  string pollingTimeString = userSettings.PollingTime.ToString();
508  this.textBoxPollingTime.Text = pollingTimeString;
509  }
510 
511  private void SetupVoltageThreshold()
512  {
513  string voltageThreshold = userSettings.VoltageThreshold.ToString();
514  this.textBoxVoltageThreshold.Text = voltageThreshold;
515  }
516 
517  private void comboBoxIpcType_SelectedValueChanged(object sender, EventArgs e)
518  {
519  // Avoid RuntimeManager creation on DiagManager's port changed in this method
520  textBoxDiagPortPipe.TextChanged -= textBoxPortPipe_TextChanged;
521  var ipcType = (IpcTypes)comboBoxIpcType.SelectedItem;
522 
523  switch (ipcType)
524  {
525  case IpcTypes.SOCKET:
526 
527  portPipeLabel.Text = "Runner / DiagManager Ports";
528 
529  textBoxDiagPortPipe.Text = defaultDiagPort.ToString();
530  textBoxDiagPortPipe.TextChanged += textBoxPortPipe_TextChanged;
531 
532  textBoxRtPortPipe.Text = defaultRuntimePort.ToString();
533  break;
534 
535  case IpcTypes.PIPE:
536 
537  portPipeLabel.Text = "Runner / DiagManager Pipes";
538 
539  textBoxDiagPortPipe.Text = defaultDiagPipeName;
540  textBoxDiagPortPipe.TextChanged += textBoxPortPipe_TextChanged;
541 
542  textBoxRtPortPipe.Text = defaultRuntimePipeName;
543  break;
544 
545  default:
546  break;
547  }
548  }
549 
550  private void textBoxPortPipe_TextChanged(object sender, EventArgs e)
551  {
552  PrintText("Create RuntimeManager...");
553 
554  try
555  {
556  this.globalRuntimeManager = CreateRuntimeManager();
557  }
558  catch (System.Exception ex)
559  {
560  switch ((sender as TextBox).Name)
561  {
562  case SampleConstants.RT_PORT_PIPE_TEXT_BOX_NAME:
563 
564  PrintText(String.Format("Invalid Otx Runtime {0}", IpcPortPipeString()));
565  break;
566 
567  case SampleConstants.DM_PORT_PIPE_TEXT_BOX_NAME:
568 
569  PrintText(String.Format("Invalid Diag Manager {0}", IpcPortPipeString()));
570  break;
571 
572  default:
573  break;
574  }
575 
576  PrintException(ex);
577  PrintText("No RuntimeManager created");
578  }
579 
580  if (this.globalRuntimeManager == null)
581  {
582  PrintText("RuntimeManager is null.");
583  }
584  }
585 
586  private IRuntimeManager CreateRuntimeManager()
587  {
588  IRuntimeManager runtimeManager = null;
589  try
590  {
591  string ipcType = "";
592  switch ((IpcTypes)comboBoxIpcType.SelectedItem)
593  {
594  case IpcTypes.SOCKET:
595 
596  runtimeManager = CreateSocketRuntimeManager();
597  ipcType = "Socket";
598  break;
599 
600  case IpcTypes.PIPE:
601 
602  runtimeManager = CreatePipeRuntimeManager();
603  ipcType = "Pipe";
604  break;
605  }
606 
607  PrintText($"... {ipcType} RuntimeManager created (MinBinVersion: {RuntimeConfig.Instance.MinBinVersion})");
608 
609  InitializeRuntimeEvents(runtimeManager);
610  SetCustomImplementation(runtimeManager);
611  }
612  catch (InvalidLicenseException ex)
613  {
614  PrintException(ex);
615  PrintText("... No RuntimeManager created.");
616 
617  return runtimeManager;
618  }
619 
620  return runtimeManager;
621  }
622 
623  private string IpcPortPipeString()
624  {
625  switch ((IpcTypes)comboBoxIpcType.SelectedItem)
626  {
627  case IpcTypes.SOCKET:
628  return SampleConstants.PORT_STRING;
629  case IpcTypes.PIPE:
630  return SampleConstants.PIPE_STRING;
631  default:
632  // Should not reach here
633  return string.Empty;
634  }
635  }
636 
637  private IRuntimeManager CreateSocketRuntimeManager()
638  {
639  ushort rtPort = Convert.ToUInt16(textBoxRtPortPipe.Text);
640 
641  if (NoDiag())
642  {
643  return RuntimeManagerFactory.CreateSocketRuntimeManager(rtPort);
644  }
645  else
646  {
647  ushort diagPort = Convert.ToUInt16(textBoxDiagPortPipe.Text);
648  return RuntimeManagerFactory.CreateSocketRuntimeManager(rtPort, diagPort);
649  }
650  }
651 
652  private IRuntimeManager CreatePipeRuntimeManager()
653  {
654  if (NoDiag())
655  {
656  return RuntimeManagerFactory.CreatePipeRuntimeManager(textBoxRtPortPipe.Text);
657  }
658  else
659  {
660  return RuntimeManagerFactory.CreatePipeRuntimeManager(textBoxRtPortPipe.Text, textBoxDiagPortPipe.Text);
661  }
662  }
663 
664  private bool NoDiag()
665  {
666  return String.IsNullOrWhiteSpace(textBoxDiagPortPipe.Text);
667  }
668 
669  private void InitializeRuntimeEvents(IRuntimeManager runtimeManager)
670  {
671  if (runtimeManager == null)
672  return;
673 
674  runtimeManager.ProcedurePending -= ProcedurePending;
675  runtimeManager.ProcedureStarted -= ProcedureStarted;
676  runtimeManager.ProcedurePaused -= ProcedurePaused;
677  runtimeManager.ProcedureContinued -= ProcedureContinued;
678  runtimeManager.ProcedureFinished -= ProcedureFinished;
679  runtimeManager.ProcedureAborted -= ProcedureAborted;
680  runtimeManager.ProcedureStopped -= ProcedureStopped;
681  runtimeManager.ProcedureTimeout -= ProcedureTimeout;
682  runtimeManager.DiagConnectionStateChanged -= DiagConnectionStateChanged;
683  runtimeManager.InOutParameterValueChanged -= InOutParameterValueChanged;
684 
685  runtimeManager.ProcedurePending += ProcedurePending;
686  runtimeManager.ProcedureStarted += ProcedureStarted;
687  runtimeManager.ProcedurePaused += ProcedurePaused;
688  runtimeManager.ProcedureContinued += ProcedureContinued;
689  runtimeManager.ProcedureFinished += ProcedureFinished;
690  runtimeManager.ProcedureAborted += ProcedureAborted;
691  runtimeManager.ProcedureStopped += ProcedureStopped;
692  runtimeManager.ProcedureTimeout += ProcedureTimeout;
693  runtimeManager.DiagConnectionStateChanged += DiagConnectionStateChanged;
694  runtimeManager.InOutParameterValueChanged += InOutParameterValueChanged;
695 
696  PrintText("Initialization of runtime events finished");
697  }
698 
699  private void radioButtonCustomImpl_CheckedChanged(object sender = null, System.EventArgs e = null)
700  {
701  if (this.globalRuntimeManager == null)
702  return;
703 
704  if (sender != null && (sender as RadioButton).Checked == false)
705  return;
706 
707  this.SetCustomImplementation(this.globalRuntimeManager);
708  }
709 
710  private void SetCustomImplementation(IRuntimeManager runtimeManager)
711  {
712  if (runtimeManager == null)
713  return;
714 
715  var currentCustomImpl = GetCurrentCustomImpl();
716 
717  switch (currentCustomImpl)
718  {
719  case CustomImplTypes.OutputImpl:
720 
721  SetOutputWindowImplementation(runtimeManager);
722  break;
723 
724  case CustomImplTypes.DefaultCustomImpl:
725 
726  SetDefaultCustomImplementation(runtimeManager);
727  break;
728 
729  case CustomImplTypes.NoCustom:
730 
731  RemoveCustomImplementation(runtimeManager);
732  break;
733 
734  default:
735  break;
736  }
737 
738  if (currentCustomImpl != CustomImplTypes.None)
739  {
740  PrintText("Set up custom implementation finished");
741  }
742  }
743 
744  private CustomImplTypes GetCurrentCustomImpl()
745  {
746  if (radioButtonOuputWindow.Checked)
747  return CustomImplTypes.OutputImpl;
748  else if (radioButtonDefaultImplementation.Checked)
749  return CustomImplTypes.DefaultCustomImpl;
750  else if (radioButtonNoCustomImplementation.Checked)
751  return CustomImplTypes.NoCustom;
752 
753  return CustomImplTypes.None;
754  }
755 
756  private void SetOutputWindowImplementation(IRuntimeManager runtimeManager)
757  {
758  if (runtimeManager == null)
759  return;
760 
761  runtimeManager.SetCustomImplementation(basicScreenOutputImpl);
762  runtimeManager.SetCustomImplementation(customScreenOutputImpl);
763  runtimeManager.SetCustomImplementation(loggingOutputImpl);
764  runtimeManager.SetCustomImplementation(contextVariableOutputImpl);
765 
766  runtimeManager.SetCustomImplementation(stateVariableOutputImpl);
767  runtimeManager.SetCustomImplementation(measureOutputImpl);
768  runtimeManager.SetCustomImplementation(i18NOutputImpl);
769  runtimeManager.SetCustomImplementation(serviceProviderOutputImpl);
770 
771  runtimeManager.SetCustomImplementation((ISqlImplementation)null); // TODO: create?
772  runtimeManager.SetCustomImplementation(commonDialogsOutputImpl);
773  }
774 
775  private void SetDefaultCustomImplementation(IRuntimeManager runtimeManager)
776  {
777  if (runtimeManager == null)
778  return;
779 
780  runtimeManager.SetCustomImplementation((IBasicScreenImplementation)null); // TODO: create?
781  runtimeManager.SetCustomImplementation(customScreenImplementation);
782  runtimeManager.SetCustomImplementation((ILoggingImplementation)null); // TODO: create?
783  runtimeManager.SetCustomImplementation(contextVariableImplementation);
784 
785  runtimeManager.SetCustomImplementation(stateVariableImplementation);
786  runtimeManager.SetCustomImplementation(measureImplementation);
787  runtimeManager.SetCustomImplementation((Ii18nImplementation)null); // TODO: create?
788  runtimeManager.SetCustomImplementation(serviceProviderImplementation);
789 
790  runtimeManager.SetCustomImplementation(sqlOutputImpl);
791  runtimeManager.SetCustomImplementation((ICommonDialogsImplementation)null); // TODO: create?
792  }
793 
794  private void RemoveCustomImplementation(IRuntimeManager runtimeManager)
795  {
796  if (runtimeManager == null)
797  return;
798 
799  runtimeManager.SetCustomImplementation((IBasicScreenImplementation)null);
800  runtimeManager.SetCustomImplementation((ICustomScreenImplementation)null);
801  runtimeManager.SetCustomImplementation((ILoggingImplementation)null);
802  runtimeManager.SetCustomImplementation((IContextVariableImplementation)null);
803 
804  runtimeManager.SetCustomImplementation((IStateVariableImplementation)null);
805  runtimeManager.SetCustomImplementation((IMeasureImplementation)null);
806  runtimeManager.SetCustomImplementation((Ii18nImplementation)null);
807  runtimeManager.SetCustomImplementation((IExternalServiceProviderImplementation)null);
808 
809  runtimeManager.SetCustomImplementation((ISqlImplementation)null);
810  runtimeManager.SetCustomImplementation((ICommonDialogsImplementation)null);
811  }
812 
813  private void ProcedurePending(IRuntimeContext context)
814  {
815  if (InvokeRequired)
816  {
817  Invoke(new MethodInvoker(delegate ()
818  {
819  ProcedurePending(context);
820  }));
821  return;
822  }
823 
824  lock (eventListenerLock)
825  {
826  this.PrintText(string.Format("Event ProcedurePending occurred - {0} - ExecutionState = {1}", context.Procedure.FullName, context.ExecutionState.ToString()));
827  UpdateExecutionState(context);
828  ShowConnectionStateMessage(context);
829  }
830  }
831 
832  private void ProcedureStarted(IRuntimeContext context)
833  {
834  if (InvokeRequired)
835  {
836  Invoke(new MethodInvoker(delegate ()
837  {
838  ProcedureStarted(context);
839  }));
840  return;
841  }
842 
843  lock (eventListenerLock)
844  {
845  this.PrintText(string.Format("Event ProcedureStarted occurred - {0} - ExecutionState = {1} - RuntimeId = {2} - ProcessId = {3}", context.Procedure.FullName, context.ExecutionState.ToString(), context.RuntimeId, context.ProcessId));
846  UpdateExecutionState(context);
847  }
848  }
849 
850  private void ProcedurePaused(IRuntimeContext context, ExecutionStateChangeReason reason)
851  {
852  if (InvokeRequired)
853  {
854  Invoke(new MethodInvoker(delegate ()
855  {
856  ProcedurePaused(context, reason);
857  }));
858  return;
859  }
860 
861  lock (eventListenerLock)
862  {
863  this.PrintText(string.Format("Event ProcedurePaused occurred - {0} - ExecutionState = {1} - ExecutionStateChangeReason = {2}", context.Procedure.FullName, context.ExecutionState.ToString(), reason.ToString()));
864  UpdateExecutionState(context, reason);
865  if (reason == ExecutionStateChangeReason.UnexpectedDiagConnectionState)
866  {
867  ShowConnectionStateMessage(context);
868  }
869  }
870  }
871 
872  private void ProcedureContinued(IRuntimeContext context)
873  {
874  if (InvokeRequired)
875  {
876  Invoke(new MethodInvoker(delegate ()
877  {
878  ProcedureContinued(context);
879  }));
880  return;
881  }
882 
883  lock (eventListenerLock)
884  {
885  this.PrintText(string.Format("Event ProcedureContinued occurred - {0} - ExecutionState = {1}", context.Procedure.FullName, context.ExecutionState.ToString()));
886  UpdateExecutionState(context);
887  }
888  }
889 
890  private void ProcedureStopped(IRuntimeContext context)
891  {
892  if (InvokeRequired)
893  {
894  Invoke(new MethodInvoker(delegate ()
895  {
896  ProcedureStopped(context);
897  }));
898  return;
899  }
900 
901  lock (eventListenerLock)
902  {
903  this.PrintText(string.Format("Event ProcedureStopped occurred - {0} - ExecutionState = {1}", context.Procedure.FullName, context.ExecutionState.ToString()));
904  UpdateExecutionState(context);
905  }
906  }
907 
908  private void ProcedureTimeout(IRuntimeContext context)
909  {
910  if (InvokeRequired)
911  {
912  Invoke(new MethodInvoker(delegate ()
913  {
914  ProcedureTimeout(context);
915  }));
916  return;
917  }
918 
919  lock (eventListenerLock)
920  {
921  cyclicExecuteAsyncIsProcessing = false;
922  this.PrintText(string.Format("Event ProcedureTimeout occurred - {0} - ExecutionState = {1}", context.Procedure.FullName, context.ExecutionState.ToString()));
923  UpdateExecutionState(context);
924  }
925  }
926 
927  private void DiagConnectionStateChanged(ClampState batteryState, ClampState ignitionState)
928  {
929  if (InvokeRequired)
930  {
931  Invoke(new MethodInvoker(delegate ()
932  {
933  DiagConnectionStateChanged(batteryState, ignitionState);
934  }));
935  return;
936  }
937 
938  lock (eventListenerLock)
939  {
940  this.PrintText(string.Format("Event DiagConnectionStateChanged occurred - BatteryState = {0}, IgnitionState = {1}", batteryState.ToString(), ignitionState.ToString()));
941  System.Media.SystemSounds.Beep.Play();
942 
943  SetBatteryIgnitionState(batteryState, ignitionState);
944  }
945  }
946 
947  // TODO: refactor. handle multithread?
948  private void ProcedureFinished(IRuntimeContext context)
949  {
950  if (InvokeRequired)
951  {
952  Invoke(new MethodInvoker(delegate ()
953  {
954  ProcedureFinished(context);
955  }));
956  return;
957  }
958 
959  lock (eventListenerLock)
960  {
961  IProcedureParameter procedureParameter;
962  string procedureName = context.Procedure.FullName;
963  string parameterOuput;
964 
965  for (int i = 0; i < context.Procedure.Parameters.Count(); i++)
966  {
967  procedureParameter = context.Procedure.Parameters.ElementAt(i);
968  parameterOuput = "Parameter Changed - " + procedureParameter.Name + "(" + procedureParameter.DataType + ") = ";
969 
970  if (procedureParameter.Value != null)
971  {
972  parameterOuput += ShortenedValueString(procedureParameter);
973  }
974  else
975  {
976  parameterOuput += null;
977  }
978 
979  this.PrintText(parameterOuput);
980  }
981 
982  UpdateGridviewParameter(context.Procedure);
983 
984  this.PrintText(string.Format("ProcedureFinished({0}) ExecutionState({1}) - Execution duration {2} - Running time {3} - Used memory {4})",
985  context.Procedure.FullName,
986  context.ExecutionState.ToString(),
987  (DateTime.Now - this.runtimeContextsExecutionStartTime[context]).ToString("hh':'mm':'ss','fff"),
988  TimeSpan.FromMilliseconds(context.ExecutionTime).ToString("hh':'mm':'ss','fff"),
989  string.Format("{0:0,0} kB", context.ProcessMemory / 1024))
990  );
991 
992  UpdateExecutionState(context);
993 
994  cyclicExecuteAsyncIsProcessing = false;
995  }
996  }
997 
998  private void ProcedureAborted(IRuntimeContext context)
999  {
1000  if (InvokeRequired)
1001  {
1002  Invoke(new MethodInvoker(delegate ()
1003  {
1004  ProcedureAborted(context);
1005  }));
1006  return;
1007  }
1008 
1009  this.PrintText(string.Format("ProcedureAborted({0}) ExecutionState({1})", context.Procedure.FullName, context.ExecutionState.ToString()));
1010 
1011  if (context.HasRuntimeException)
1012  {
1013  PrintException(context.RuntimeException, "Reason: API ");
1014  }
1015 
1016  if (context.HasOtxException)
1017  {
1018  PrintException(context.OtxException, "Reason: OTX ");
1019  }
1020  cyclicExecuteAsyncIsProcessing = false;
1021  UpdateExecutionState(context);
1022  }
1023 
1024  private void UpdateExecutionState(IRuntimeContext context, ExecutionStateChangeReason? reason = null)
1025  {
1026  if (context != null)
1027  {
1028  switch (context.ExecutionState)
1029  {
1030  case ExecutionState.Running:
1031 
1032  this.procedureExecutionCount++;
1033 
1034  AddRuntimeContext(context);
1035  DisplayProcedureExecutionState(context);
1036 
1037  this.buttonExecuteMain.Enabled = checkBoxAsyncExecution.Checked;
1038  this.buttonExecuteSelectedProcedure.Enabled = checkBoxAsyncExecution.Checked;
1039 
1040  UpdateExecutionStateButtons(true);
1041  UpdatePauseButton(true);
1042 
1043  break;
1044 
1045  case ExecutionState.Paused:
1046 
1047  this.procedureExecutionCount--;
1048  if (reason == ExecutionStateChangeReason.Explicit)
1049  {
1050  DisplayProcedureExecutionState(context);
1051  }
1052 
1053  UpdatePauseButton(false, reason == ExecutionStateChangeReason.Explicit);
1054 
1055  break;
1056 
1057  case ExecutionState.Pending:
1058 
1059  DisplayProcedureExecutionState(context);
1060  UpdateExecutionStateButtons(true);
1061  UpdatePauseButton(true, false);
1062 
1063  break;
1064 
1065  case ExecutionState.Finished:
1066 
1067  this.procedureExecutionCount--;
1068 
1069  RemoveRuntimeContext(context);
1070  DisplayProcedureExecutionState(context);
1071 
1072  this.buttonExecuteMain.Enabled = !checkBoxCyclicExecution.Checked;
1073  this.buttonExecuteSelectedProcedure.Enabled = !checkBoxCyclicExecution.Checked;
1074 
1075  UpdateExecutionStateButtons(false);
1076  UpdatePauseButton(true, false);
1077 
1078  this.labelBatteryState.Image = OpenTestSystem.Otx.Runtime2.Api.DotNet.Sample.Properties.Resources.IconBattery16;
1079  this.labelIgnitionState.Image = OpenTestSystem.Otx.Runtime2.Api.DotNet.Sample.Properties.Resources.IconIgnition16;
1080 
1081  break;
1082 
1083  case ExecutionState.Stopped:
1084 
1085  this.procedureExecutionCount--;
1086 
1087  RemoveRuntimeContext(context);
1088  DisplayProcedureExecutionState(context);
1089 
1090  this.buttonExecuteMain.Enabled = true;
1091  this.buttonExecuteSelectedProcedure.Enabled = true;
1092 
1093  UpdateExecutionStateButtons(false);
1094  UpdatePauseButton(true, false);
1095 
1096  this.labelBatteryState.Image = OpenTestSystem.Otx.Runtime2.Api.DotNet.Sample.Properties.Resources.IconBattery16;
1097  this.labelIgnitionState.Image = OpenTestSystem.Otx.Runtime2.Api.DotNet.Sample.Properties.Resources.IconIgnition16;
1098 
1099  break;
1100 
1101  case ExecutionState.Aborted:
1102 
1103  this.procedureExecutionCount--;
1104 
1105  RemoveRuntimeContext(context);
1106  DisplayProcedureExecutionState(context);
1107 
1108  this.buttonExecuteMain.Enabled = true;
1109  this.buttonExecuteSelectedProcedure.Enabled = true;
1110 
1111  UpdateExecutionStateButtons(false);
1112  UpdatePauseButton(true, false);
1113 
1114  this.labelBatteryState.Image = OpenTestSystem.Otx.Runtime2.Api.DotNet.Sample.Properties.Resources.IconBattery16;
1115  this.labelIgnitionState.Image = OpenTestSystem.Otx.Runtime2.Api.DotNet.Sample.Properties.Resources.IconIgnition16;
1116 
1117  break;
1118  case ExecutionState.Timeout:
1119 
1120  System.Media.SystemSounds.Hand.Play();
1121 
1122  this.procedureExecutionCount--;
1123 
1124  RemoveRuntimeContext(context);
1125  DisplayProcedureExecutionState(context);
1126 
1127  this.buttonExecuteMain.Enabled = true;
1128  this.buttonExecuteSelectedProcedure.Enabled = true;
1129 
1130  UpdateExecutionStateButtons(false);
1131  UpdatePauseButton(true, false);
1132 
1133  this.labelBatteryState.Image = OpenTestSystem.Otx.Runtime2.Api.DotNet.Sample.Properties.Resources.IconBattery16;
1134  this.labelIgnitionState.Image = OpenTestSystem.Otx.Runtime2.Api.DotNet.Sample.Properties.Resources.IconIgnition16;
1135 
1136  break;
1137  }
1138 
1139  StartStopUpdateGuiTimer();
1140  }
1141  }
1142 
1143  private void StartStopUpdateGuiTimer()
1144  {
1145  if (this.procedureExecutionCount <= 0)
1146  {
1147  this.updateGuiTimer.Enabled = false;
1148  this.labelSeparator1.Visible = false;
1149  this.labelProcedureExecutionTimes.Visible = false;
1150  }
1151  else if (!this.updateGuiTimer.Enabled)
1152  {
1153  this.updateGuiTimer.Enabled = true;
1154  }
1155  }
1156 
1157  private void updateGuiTimer_Tick(object sender, EventArgs e)
1158  {
1159  DisplayProcedureExecutionTimes();
1160  }
1161 
1162  private void DisplayProcedureExecutionTimes()
1163  {
1164  string text = string.Empty;
1165 
1166  if (this.runtimeContextsExecutionStartTime.Count > 0)
1167  {
1168  foreach (var item in this.runtimeContextsExecutionStartTime)
1169  {
1170  var duration = DateTime.Now - item.Value;
1171  text += string.Format("{0}: {1} | ", item.Key.Procedure.Name, duration.ToString("hh':'mm':'ss"));
1172  }
1173  text = text.TrimEnd(new char[] { ' ', '|' });
1174  }
1175 
1176  this.labelProcedureExecutionTimes.Text = text;
1177  this.labelSeparator1.Visible = true;
1178  this.labelProcedureExecutionTimes.Visible = true;
1179  }
1180 
1181  private void InOutParameterValueChanged(IRuntimeContext context, IProcedureInOutParameter parameter)
1182  {
1183  if (InvokeRequired)
1184  {
1185  Invoke(new MethodInvoker(delegate ()
1186  {
1187  InOutParameterValueChanged(context, parameter);
1188  }));
1189  return;
1190  }
1191 
1192  lock (eventListenerLock)
1193  {
1194  try
1195  {
1196  string valueStr = ShortenedValueString(parameter);
1197  string message = String.Format("Parameter '{0}' new value = {1}", parameter.Name, valueStr);
1198  PrintText(message);
1199  UpdateGridviewParameter(parameter);
1200  }
1201  catch (InvalidCastException ex)
1202  {
1203  PrintText(ex.Message);
1204  }
1205  }
1206  }
1207 
1208  private static string ShortenedValueString(IProcedureParameter parameter)
1209  {
1210  try
1211  {
1212  var valueStr = ValueConverter.Value2String(parameter.Value);
1213  if (valueStr.Length > VALUE_STRING_MAX_LENGTH)
1214  return valueStr.Substring(0, VALUE_STRING_MAX_LENGTH) + "...";
1215 
1216  return valueStr;
1217  }
1218  catch
1219  { }
1220 
1221  return string.Empty;
1222  }
1223 
1224  private void UpdatePauseButton(bool pauseAvailable, bool enabled = true)
1225  {
1226  this.buttonPause.Enabled = enabled;
1227  this.buttonPause.Checked = !pauseAvailable;
1228  this.buttonPause.Image = pauseAvailable
1229  ? global::OpenTestSystem.Otx.Runtime2.Api.DotNet.Sample.Properties.Resources.Pause
1230  : global::OpenTestSystem.Otx.Runtime2.Api.DotNet.Sample.Properties.Resources.Continue;
1231  }
1232 
1233  private void UpdateExecutionStateButtons(bool wasExecuted)
1234  {
1235  if (this.useConnectionState)
1236  {
1237  this.labelTimeout.Enabled = !wasExecuted;
1238  this.textBoxTimeout.Enabled = !wasExecuted;
1239  this.checkBoxUseConnectionState.Enabled = !wasExecuted;
1240  this.labelExpectedState.Enabled = !wasExecuted;
1241  this.checkBoxIgnition.Enabled = !wasExecuted;
1242  this.labelPollingTime.Enabled = !wasExecuted;
1243  this.textBoxPollingTime.Enabled = !wasExecuted;
1244  this.labelVoltageThreshold.Enabled = !wasExecuted;
1245  this.textBoxVoltageThreshold.Enabled = !wasExecuted;
1246  }
1247 
1248  if (wasExecuted)
1249  {
1250  if (checkBoxAsyncExecution.Checked == false)
1251  {
1252  this.buttonExecuteMain.Enabled = false;
1253  this.buttonExecuteSelectedProcedure.Enabled = false;
1254  }
1255 
1256  this.buttonStop.Enabled = true;
1257  }
1258  else
1259  {
1260  if (project != null && project.MainProcedure != null)
1261  {
1262  this.buttonExecuteMain.Enabled = true;
1263  }
1264 
1265  this.buttonExecuteSelectedProcedure.Enabled = true;
1266 
1267  this.buttonStop.Enabled = false;
1268  }
1269  }
1270 
1271  private void DisplayProcedureExecutionState(IRuntimeContext context, string errorMessage = "")
1272  {
1273  if (string.IsNullOrEmpty(errorMessage))
1274  {
1275  this.labelIconProcedureExecutionState.Image = global::OpenTestSystem.Otx.Runtime2.Api.DotNet.Sample.Properties.Resources.Info16;
1276 
1277  if (this.procedureExecutionCount <= 0)
1278  {
1279  this.procedureExecutionCount = 0;
1280  if (context.ExecutionState == ExecutionState.Paused)
1281  {
1282  this.labelProcedureExecutionState.Text = "Execution is paused, click \"Continue\"...";
1283  }
1284  else
1285  {
1286  this.labelProcedureExecutionState.Text = "Select a procedure and click \"Start\"...";
1287  }
1288  }
1289  else if (this.procedureExecutionCount == 1)
1290  {
1291  this.labelProcedureExecutionState.Text = "1 Procedure is running";
1292  }
1293  else
1294  {
1295  this.labelProcedureExecutionState.Text = string.Format("{0} Procedures are running", this.procedureExecutionCount);
1296  }
1297  }
1298  else
1299  {
1300  this.labelIconProcedureExecutionState.Image = global::OpenTestSystem.Otx.Runtime2.Api.DotNet.Sample.Properties.Resources.Error16;
1301  this.labelProcedureExecutionState.Text = errorMessage;
1302  }
1303  }
1304  private void SyncWithParent(object parentControl, object control = null, bool newThread = false)
1305  {
1306  if (parentControl != null)
1307  {
1308  if (this.creatorForm != null && this.checkBoxStartAllParents.Checked)
1309  {
1310  if (parentControl is Button)
1311  {
1312  if (newThread)
1313  {
1314  ThreadPool.QueueUserWorkItem(state => ParentButtonClick(parentControl as Button));
1315  }
1316  else
1317  {
1318  ParentButtonClick(parentControl as Button);
1319  }
1320  }
1321  else if (parentControl is TextBox && control != null)
1322  {
1323  string text = (control as TextBox).Text;
1324  if (newThread)
1325  {
1326  ThreadPool.QueueUserWorkItem(state => ParentTextBoxSetText(parentControl as TextBox, text));
1327  }
1328  else
1329  {
1330  ParentTextBoxSetText(parentControl as TextBox, text);
1331  }
1332  }
1333  else if (parentControl is CheckBox && control != null)
1334  {
1335  CheckState checkState = (control as CheckBox).CheckState;
1336  if (newThread)
1337  {
1338  ThreadPool.QueueUserWorkItem(state => ParentCheckBoxSetCheckState(parentControl as CheckBox, checkState));
1339  }
1340  else
1341  {
1342  ParentCheckBoxSetCheckState(parentControl as CheckBox, checkState);
1343  }
1344  }
1345  else if (parentControl is ComboBox && control != null)
1346  {
1347  int index = (control as ComboBox).SelectedIndex;
1348  if (newThread)
1349  {
1350  ThreadPool.QueueUserWorkItem(state => ParentComboBoxSetIndex(parentControl as ComboBox, index));
1351  }
1352  else
1353  {
1354  ParentComboBoxSetIndex(parentControl as ComboBox, index);
1355  }
1356  }
1357  else if (parentControl is NumericUpDown && control != null)
1358  {
1359  int value = decimal.ToInt32((control as NumericUpDown).Value);
1360  if (newThread)
1361  ThreadPool.QueueUserWorkItem(state => ParentNumericUpDownSetValue(parentControl as NumericUpDown, value));
1362  else
1363  ParentNumericUpDownSetValue(parentControl as NumericUpDown, value);
1364  }
1365  }
1366  }
1367  }
1368 
1369  private void ParentButtonClick(Button button)
1370  {
1371  if (button.InvokeRequired)
1372  {
1373  this.creatorForm.Invoke(new Action(() => button.PerformClick()));
1374  }
1375  else
1376  {
1377  button.PerformClick();
1378  }
1379  }
1380 
1381  private void ParentTextBoxSetText(TextBox textBox, string text)
1382  {
1383  if (textBox.InvokeRequired)
1384  {
1385  this.creatorForm.Invoke(new Action(() => textBox.Text = text));
1386  }
1387  else
1388  {
1389  textBox.Text = text;
1390  }
1391  }
1392 
1393  private void ParentCheckBoxSetCheckState(CheckBox checkBox, CheckState checkState)
1394  {
1395  if (checkBox.InvokeRequired)
1396  {
1397  this.creatorForm.Invoke(new Action(() => checkBox.CheckState = checkState));
1398  }
1399  else
1400  {
1401  checkBox.CheckState = checkState;
1402  }
1403  }
1404 
1405  private void ParentComboBoxSetIndex(ComboBox comboBox, int index)
1406  {
1407  if (comboBox.InvokeRequired)
1408  {
1409  this.creatorForm.Invoke(new Action(() => comboBox.SelectedIndex = index));
1410  }
1411  else
1412  {
1413  comboBox.SelectedIndex = index;
1414  }
1415  }
1416 
1417  private void buttonBrowseTraceFolder_Click(object sender, EventArgs e)
1418  {
1419  folderBrowserDialog1.SelectedPath = this.textBoxTraceFolder.Text;
1420  folderBrowserDialog1.ShowNewFolderButton = true;
1421 
1422  DialogResult result = folderBrowserDialog1.ShowDialog();
1423  if (result == DialogResult.OK)
1424  {
1425  this.textBoxTraceFolder.Text = folderBrowserDialog1.SelectedPath;
1426  }
1427  }
1428 
1429  private void buttonOpenTraceFolder_Click(object sender, EventArgs e)
1430  {
1431  string path = this.textBoxTraceFolder.Text;
1432  if (Directory.Exists(path))
1433  {
1434  Process.Start(path);
1435  }
1436  }
1437 
1438  // This will not automatically update GUI if it is modified internally
1439  private void textBoxTraceFolder_TextChanged(object sender, EventArgs e)
1440  {
1441  if (this.globalRuntimeManager == null)
1442  return;
1443 
1444  string path = this.textBoxTraceFolder.Text;
1445 
1446  if (path != RuntimeConfig.Instance.TraceFolder)
1447  {
1448  if (Directory.Exists(path))
1449  {
1450  RuntimeConfig.Instance.TraceFolder = textBoxTraceFolder.Text;
1451  this.buttonOpenTraceFolder.Enabled = true;
1452 
1453  PrintText("Set trace folder to " + RuntimeConfig.Instance.TraceFolder);
1454  }
1455  else
1456  {
1457  this.buttonOpenTraceFolder.Enabled = false;
1458  }
1459  }
1460  else
1461  {
1462  this.buttonOpenTraceFolder.Enabled = true;
1463  }
1464 
1465  SyncWithParent(this.creatorForm?.textBoxTraceFolder, this.textBoxTraceFolder);
1466  }
1467 
1468  // This will not automatically update GUI if it is modified internally
1469  private void comboBoxTraceLevel_SelectedValueChanged(object sender, EventArgs e)
1470  {
1471  if (this.globalRuntimeManager == null)
1472  return;
1473 
1474  var traceLevel = (TraceLevels)comboBoxTraceLevel.SelectedIndex;
1475  if (traceLevel == RuntimeConfig.Instance.TraceLevel)
1476  return;
1477 
1478  RuntimeConfig.Instance.TraceLevel = traceLevel;
1479  PrintText("Updated trace level");
1480  }
1481 
1482  private void buttonBrowseFile_Click(object sender, EventArgs e)
1483  {
1484  DialogResult result = openFileDialog1.ShowDialog();
1485  if (result == DialogResult.OK)
1486  {
1487  cbFilePath.Text = openFileDialog1.FileName;
1488 
1489  this.buttonReload.PerformClick();
1490  }
1491  }
1492 
1493  private void cbFilePath_TextChanged(object sender, EventArgs e)
1494  {
1495  AddComboBoxFileOrPath(cbFilePath);
1496 
1497  if (String.IsNullOrWhiteSpace(cbFilePath.Text) || !File.Exists(cbFilePath.Text.Trim()))
1498  {
1499  buttonReload.Enabled = false;
1500  }
1501  else
1502  {
1503  buttonReload.Enabled = true;
1504  }
1505 
1506  SyncWithParent(this.creatorForm?.cbFilePath, this.cbFilePath);
1507  }
1508 
1509  private void cbFilePath_SelectedValueChanged(object sender, EventArgs e)
1510  {
1511  if (String.IsNullOrWhiteSpace(cbFilePath.Text) || !File.Exists(cbFilePath.Text.Trim()))
1512  {
1513  buttonReload.Enabled = false;
1514  }
1515  else
1516  {
1517  buttonReload.Enabled = true;
1518  if (isPathChanging == false)
1519  {
1520  this.buttonReload.PerformClick();
1521  }
1522  }
1523 
1524  SyncWithParent(this.creatorForm?.cbFilePath, this.cbFilePath);
1525  }
1526 
1527  private void buttonReload_Click(object sender, EventArgs e)
1528  {
1529  this.LoadContextFile(this.globalRuntimeManager);
1530 
1531  SyncWithParent(this.creatorForm?.buttonReload);
1532  }
1533 
1534  private void LoadContextFile(IRuntimeManager runtimeManager)
1535  {
1536  PrintText("Load '" + cbFilePath.Text + "'...");
1537 
1538  if (runtimeManager == null)
1539  {
1540  PrintText("... No RuntimeManager exists!");
1541  return;
1542  }
1543 
1544  if (String.IsNullOrWhiteSpace(cbFilePath.Text.Trim()))
1545  {
1546  PrintText("... wrong file name.");
1547  return;
1548  }
1549 
1550  SetTitle(this.title);
1551 
1552  try
1553  {
1554  // TODO: move these 3 set up away
1555  this.isLoadding = true;
1556  this.playerProject = null;
1557  this.project = null;
1558  this.contextVariableImplementation.ResetAllValues();
1559  this.stateVariableImplementation.ResetAllValues();
1560  ClearSampleGUI();
1561 
1562  if (Path.GetExtension(cbFilePath.Text.Trim()).Contains("ptx"))
1563  {
1564  this.project = LoadPtx(runtimeManager);
1565 
1566  if (this.project != null) // TODO: might be obsolete in the future
1567  {
1568  PrintText("... Project '" + project.Name + "' successfully loaded.");
1569 
1570  ClearCustomImplemetationCaches();
1571 
1572  this.LoadPackage(this.project);
1573 
1574  ReadSettings(this.project.Settings);
1575 
1576  this.isLoadding = false;
1577  return;
1578  }
1579  }
1580  else if (Path.GetExtension(cbFilePath.Text.Trim()).Contains("ppx"))
1581  {
1582  this.playerProject = LoadPpx(runtimeManager);
1583 
1584  if (this.playerProject != null) // TODO: might be obsolete in the future
1585  {
1586  ClearCustomImplemetationCaches();
1587 
1588  PrintText("... Player '" + playerProject.Name + "' successfully loaded.");
1589 
1590  foreach (IProject item in this.playerProject.Projects)
1591  {
1592  project = item; // TODO: what is this?
1593  this.LoadPackage(item);
1594  }
1595 
1596  ReadSettings(this.playerProject.Settings);
1597 
1598  this.isLoadding = false;
1599  return;
1600  }
1601  }
1602  }
1603  catch (System.Exception ex)
1604  {
1605  PrintException(ex);
1606  }
1607 
1608  this.isLoadding = false;
1609  }
1610 
1611  private void ClearSampleGUI()
1612  {
1613  this.treeViewOtxProject.Nodes.Clear();
1614  this.gridViewParameter.Rows.Clear();
1615  this.gridViewContext.Rows.Clear();
1616  this.gridViewState.Rows.Clear();
1617  this.gridViewSettings.Rows.Clear();
1618  }
1619 
1620  private void ClearCustomImplemetationCaches()
1621  {
1622  if (this.contextVariableImplementation != null)
1623  this.contextVariableImplementation.ResetAllValues();
1624 
1625  if (this.contextVariableOutputImpl != null)
1626  this.contextVariableOutputImpl.ResetAllValues();
1627 
1628  if (this.stateVariableImplementation != null)
1629  this.stateVariableImplementation.ResetAllValues();
1630 
1631  //TODO: if (this.stateVariableOutputImpl != null)
1632  }
1633 
1634  // TODO: refactor
1635  private IProject LoadPtx(IRuntimeManager runtimeManager)
1636  {
1637  IProject project = null;
1638 
1639  if (runtimeManager.IsProtected(cbFilePath.Text))
1640  {
1641  project = runtimeManager.LoadPtx(cbFilePath.Text, txtPassword.Text.Trim());
1642  }
1643  else
1644  {
1645  project = runtimeManager.LoadPtx(cbFilePath.Text);
1646  }
1647 
1648  if (project.MainProcedure != null)
1649  {
1650  buttonExecuteMain.Enabled = true;
1651  }
1652 
1653  return project;
1654  }
1655 
1656  // TODO: refactor
1657  private IPlayerProject LoadPpx(IRuntimeManager runtimeManager)
1658  {
1659  IPlayerProject playerProject = null;
1660 
1661  if (runtimeManager.IsProtected(cbFilePath.Text))
1662  {
1663  playerProject = runtimeManager.LoadPpx(cbFilePath.Text, txtPassword.Text.Trim());
1664  }
1665  else
1666  {
1667  playerProject = runtimeManager.LoadPpx(cbFilePath.Text);
1668  }
1669 
1670  return playerProject;
1671  }
1672 
1673  // TODO: refactor
1674  private void LoadPackage(IProject project)
1675  {
1676  PrintText("Browse project...");
1677 
1678  IPackage[] packages = project.Packages;
1679  if (packages == null)
1680  {
1681  IDocument document = project.StartupDocument;
1682  this.treeViewOtxProject.Nodes.Add(CreateDocumentNode(document));
1683  }
1684  else
1685  {
1686  string treeNodeText = project.Name;
1687  if (!String.IsNullOrWhiteSpace(project.Version))
1688  treeNodeText = String.Format("{0} ({1})", treeNodeText, project.Version);
1689 
1690  TreeNode root = new TreeNode(treeNodeText);
1691  root.ImageKey = root.SelectedImageKey = "ODFProject.bmp"; // TODO: move
1692  foreach (IPackage pack in packages)
1693  {
1694  root.Nodes.Add(this.CreatePackageNode(pack));
1695  }
1696 
1697  this.treeViewOtxProject.Nodes.Add(root);
1698  }
1699 
1700  this.treeViewOtxProject.Enabled = this.gridViewParameter.Enabled = true;
1701  this.treeViewOtxProject.ExpandAll();
1702  this.treeViewOtxProject.SelectedNode = startupNode;
1703  this.treeViewOtxProject.Focus();
1704 
1705  PrintText("... project browsing finished.");
1706  }
1707 
1708  // TODO: refactor
1709  private TreeNode CreateDocumentNode(IDocument doc)
1710  {
1711  TreeNode documentNode = new TreeNode(doc.Name);
1712  documentNode.Tag = doc;
1713  documentNode.ImageKey = documentNode.SelectedImageKey = "DocumentOTX16.bmp";
1714  if (doc.IsStartup)
1715  {
1716  documentNode.Text += " (StartUp)";
1717  }
1718 
1719  foreach (IProcedure p in doc.Procedures)
1720  {
1721  TreeNode procedureNode = CreateProcedureNode(p);
1722  documentNode.Nodes.Add(procedureNode);
1723  if (doc.IsStartup && p.Name == "main")
1724  {
1725  startupNode = procedureNode;
1726  buttonExecuteMain.Enabled = true;
1727  }
1728  else if (startupNode == null)
1729  {
1730  startupNode = procedureNode;
1731  }
1732  }
1733 
1734  return documentNode;
1735  }
1736 
1737  // TODO: refactor
1738  private TreeNode CreateProcedureNode(IProcedure procedure)
1739  {
1740  if (checkBoxCyclicReload.Checked &&
1741  procedureToExecute != null &&
1742  procedureToExecute.FullName == procedure.FullName)
1743  {
1744  procedureToExecute = procedure;
1745  }
1746 
1747  TreeNode procedureNode = new TreeNode(procedure.Name);
1748  procedureNode.ImageKey = procedureNode.SelectedImageKey = "Procedure.bmp"; // TODO: move
1749  procedureNode.Tag = procedure;
1750  return procedureNode;
1751  }
1752 
1753  // TODO: refactor
1754  private TreeNode CreatePackageNode(IPackage pack)
1755  {
1756  TreeNode packageNode = new TreeNode(pack.Name);
1757  packageNode.Tag = pack;
1758  packageNode.ImageKey = packageNode.SelectedImageKey = "Package.bmp"; // TODO: move
1759 
1760  List<IDocument> documents = pack.Documents.ToList();
1761  foreach (IDocument doc in documents)
1762  {
1763  packageNode.Nodes.Add(CreateDocumentNode(doc));
1764  }
1765 
1766  foreach (IPackage package in pack.Packages)
1767  {
1768  packageNode.Nodes.Add(this.CreatePackageNode(package));
1769  }
1770 
1771  return packageNode;
1772  }
1773 
1774  private void gridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
1775  {
1776  try
1777  {
1778  if (e != null)
1779  {
1780  DataGridView gridView = sender as DataGridView;
1781  DataGridViewComboBoxCell comboBoxCell = gridView.Rows[e.RowIndex].Cells[e.ColumnIndex] as DataGridViewComboBoxCell;
1782  if (comboBoxCell != null)
1783  {
1784  gridView.BeginEdit(true);
1785  (gridView.EditingControl as DataGridViewComboBoxEditingControl).DroppedDown = true;
1786  return;
1787  }
1788  }
1789  }
1790  catch { }
1791 
1792  gridView_CellValueChanged(sender, e);
1793  }
1794 
1795  // Commit new value for gridView's data source (only apply for DataGridViewCheckBoxCell and DataGridViewComboBoxCell)
1796  private void gridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
1797  {
1798  DataGridView gridView = sender as DataGridView;
1799  string columnName;
1800 
1801  if (gridView == this.gridViewParameter)
1802  {
1803  columnName = dataGridViewTextBoxColumnValue.Name;
1804  }
1805  else if (gridView == this.gridViewContext)
1806  {
1807  columnName = dataGridViewTextBoxColumnContextValue.Name;
1808  }
1809  else
1810  {
1811  return;
1812  }
1813 
1814  if (!this.isLoadding && e.RowIndex >= 0 && gridView.Columns[e.ColumnIndex].Name == columnName &&
1815  (
1816  gridView.Rows[e.RowIndex].Cells[columnName] is DataGridViewCheckBoxCell ||
1817  gridView.Rows[e.RowIndex].Cells[columnName] is DataGridViewComboBoxCell
1818  )
1819  )
1820  {
1821  // Force a validation to set the value after CheckBox click
1822  this.Validate(false);
1823  }
1824  }
1825 
1826  // Convert parameter's value into valid string then display it on gridview
1827  void gridViewParameter_CellValidated(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
1828  {
1829  DataGridView gridView = sender as DataGridView;
1830  if (!this.isLoadding && gridView != null && e.RowIndex >= 0 && gridView.Columns[e.ColumnIndex].Name == dataGridViewTextBoxColumnValue.Name)
1831  {
1832  IProcedureParameter parameter = gridView.Rows[e.RowIndex].Tag as IProcedureParameter;
1833  if (parameter.Value != null)
1834  {
1835  gridView.Rows[e.RowIndex].Cells[dataGridViewTextBoxColumnValue.Name].Value = ConvertValue2String(parameter.Value);
1836  gridView.Rows[e.RowIndex].Cells[dataGridViewTextBoxColumnDetails.Name].Value = GetDetails(parameter.Value);
1837  }
1838  }
1839  }
1840 
1841  // Trigger if the "value" cell is changed, convert new string into new value,
1842  // then set into corresponding parameter (only apply for IProcedureInParameter and IProcedureInOutParameter)
1843  void gridViewParameter_CellValidating(object sender, System.Windows.Forms.DataGridViewCellValidatingEventArgs e)
1844  {
1845  DataGridView gridView = sender as DataGridView;
1846  if (!this.isLoadding && gridView != null && e.RowIndex >= 0 &&
1847  gridView.Columns[e.ColumnIndex].Name == dataGridViewTextBoxColumnValue.Name &&
1848  !gridView.Rows[e.RowIndex].ReadOnly &&
1849  e.FormattedValue != null)
1850  {
1851  IProcedureParameter parameter = gridView.Rows[e.RowIndex].Tag as IProcedureParameter;
1852  if (e.FormattedValue.ToString() == ValueConverter.Value2String(parameter.Value) || e.FormattedValue.ToString() == ConvertValue2String(parameter.Value))
1853  {
1854  return;
1855  }
1856 
1857  object value = null;
1858 
1859  try
1860  {
1861  value = ValueConverter.String2Value(e.FormattedValue.ToString(), parameter.DataType);
1862  }
1863  catch (System.Exception ex)
1864  {
1865  //string errorMsg = ex.GetType().FullName;
1866  //errorMsg += Environment.NewLine;
1867  //errorMsg += ex.Message;
1868  //gridView.Rows[e.RowIndex].Cells[1].ErrorText = errorMsg;
1869  PrintException(ex);
1870  // e.Cancel = true;
1871  return;
1872  }
1873 
1874  if (value == null)
1875  {
1876  System.Media.SystemSounds.Exclamation.Play();
1877  }
1878  else
1879  {
1880  gridView.Rows[e.RowIndex].Cells[dataGridViewTextBoxColumnDataType.Name].ErrorText = string.Empty;
1881  if (parameter is IProcedureInParameter)
1882  (parameter as IProcedureInParameter).Value = value;
1883  else if (parameter is IProcedureInOutParameter)
1884  (parameter as IProcedureInOutParameter).Value = value;
1885  }
1886  }
1887  }
1888 
1889  //private void GridViewParameter_CellEndEdit(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
1890  //{
1891  // DataGridView gridView = sender as DataGridView;
1892  // gridView.Rows[e.RowIndex].Cells[1].ErrorText = string.Empty;
1893  //}
1894 
1895  void gridViewContextVariable_CellValidated(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
1896  {
1897  DataGridView gridView = sender as DataGridView;
1898  if (!this.isLoadding && gridView != null &&
1899  e.RowIndex >= 0 &&
1900  gridView.Columns[e.ColumnIndex].Name == dataGridViewTextBoxColumnContextValue.Name)
1901  {
1902  IContextVariable context = gridView.Rows[e.RowIndex].Tag as IContextVariable;
1903  if (gridView.Rows[e.RowIndex].Cells[dataGridViewTextBoxColumnContextValue.Name].Tag != null)
1904  {
1905  gridView.Rows[e.RowIndex].Cells[dataGridViewTextBoxColumnContextValue.Name].Value = ConvertValue2String(gridView.Rows[e.RowIndex].Cells[dataGridViewTextBoxColumnContextValue.Name].Tag);
1906  gridView.Rows[e.RowIndex].Cells[dataGridViewTextBoxColumnContextDetails.Name].Value = GetDetails(gridView.Rows[e.RowIndex].Cells[dataGridViewTextBoxColumnContextValue.Name].Tag);
1907  }
1908  }
1909  }
1910 
1911  // TODO: refactor. consider update to use together with Parameter validating?
1912  void gridViewContextVariable_CellValidating(object sender, System.Windows.Forms.DataGridViewCellValidatingEventArgs e)
1913  {
1914  DataGridView gridView = sender as DataGridView;
1915  if (!this.isLoadding && gridView != null &&
1916  e.RowIndex >= 0 &&
1917  gridView.Columns[e.ColumnIndex].Name == dataGridViewTextBoxColumnContextValue.Name &&
1918  !gridView.Rows[e.RowIndex].ReadOnly &&
1919  e.FormattedValue != null)
1920  {
1921  IContextVariable context = gridView.Rows[e.RowIndex].Tag as IContextVariable;
1922  if (e.FormattedValue.ToString() == ValueConverter.Value2String(gridView.Rows[e.RowIndex].Cells[dataGridViewTextBoxColumnContextValue.Name].Tag) || e.FormattedValue.ToString() == ConvertValue2String(gridView.Rows[e.RowIndex].Cells[dataGridViewTextBoxColumnContextValue.Name].Tag))
1923  {
1924  return;
1925  }
1926 
1927  object value = null;
1928 
1929  try
1930  {
1931  value = ValueConverter.String2Value(e.FormattedValue.ToString(), context.DataType);
1932  }
1933  catch (System.Exception ex)
1934  {
1935  //string errorMsg = ex.GetType().FullName;
1936  //errorMsg += Environment.NewLine;
1937  //errorMsg += ex.Message;
1938  //gridView.Rows[e.RowIndex].Cells[1].ErrorText = errorMsg;
1939  PrintException(ex);
1940  // e.Cancel = true;
1941  return;
1942  }
1943 
1944  if (value == null)
1945  {
1946  System.Media.SystemSounds.Exclamation.Play();
1947  }
1948  else
1949  {
1950  //we will save value with fullName of context, if not the bug of #10798 occurs.
1951  //Because the name of context variable in imported document may have the same name.
1952  string fullNameContextVariable = context.Document.FullName + '.' + context.Name;
1953  this.contextVariableImplementation.SetValue(fullNameContextVariable, value);
1954  this.contextVariableOutputImpl.SetValue(fullNameContextVariable, value);
1955  gridView.Rows[e.RowIndex].Cells[dataGridViewTextBoxColumnContextValue.Name].Tag = value;//used at gridViewContextVariable_CellValidated
1956  gridView.Rows[e.RowIndex].Cells[dataGridViewTextBoxColumnContextDataType.Name].ErrorText = string.Empty; //refresh error text
1957  }
1958  }
1959  }
1960 
1961  //private void gridViewContextVariable_CellEndEdit(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
1962  //{
1963  // DataGridView gridView = sender as DataGridView;
1964  // gridView.Rows[e.RowIndex].Cells[1].ErrorText = string.Empty;
1965  //}
1966 
1967  private void treeViewOtxProject_AfterSelect(object sender, TreeViewEventArgs e)
1968  {
1969  TreeNode node = e.Node;
1970  if (node != null)
1971  {
1972  // TODO: move to different method
1973  this.gridViewParameter.Rows.Clear();
1974  this.gridViewContext.Rows.Clear();
1975  this.gridViewState.Rows.Clear();
1976 
1977  if (node.Tag is IProcedure)
1978  {
1979  IProcedure procedure = node.Tag as IProcedure;
1980  ClearCustomImplemetationCaches();
1981  UpdateGridview(procedure);
1982 
1983  buttonExecuteSelectedProcedure.Enabled = true;
1984  this.project = GetProject(procedure); // TODO: why update this???
1985  return;
1986  }
1987  }
1988 
1989  buttonExecuteSelectedProcedure.Enabled = false;
1990  }
1991 
1992  // TODO: refactor
1993  private IProject GetProject(IProcedure procedure)
1994  {
1995  if (procedure != null)
1996  {
1997  IPackage package = procedure.Document.Package;
1998  if (package != null)
1999  {
2000  while (package.Parent != null)
2001  {
2002  package = package.Parent;
2003  }
2004 
2005  return package.Project;
2006  }
2007  }
2008  return null;
2009  }
2010 
2011  private void treeViewOtxProject_DrawNode(object sender, DrawTreeNodeEventArgs e)
2012  {
2013  e.DrawDefault = true;
2014  }
2015 
2016  // TODO: refactor
2017  private void buttonReadSettings_Click(object sender, EventArgs e)
2018  {
2019  Dictionary<string, string> settings = null;
2020  if (this.playerProject != null)
2021  {
2022  settings = this.playerProject.Settings;
2023  }
2024  else if (this.project != null)
2025  {
2026  settings = this.project.Settings;
2027  }
2028  ReadSettings(settings);
2029  }
2030 
2031  private void ReadSettings(Dictionary<string, string> settings)
2032  {
2033  if (settings != null)
2034  {
2035  PrintText("Read settings");
2036 
2037  gridViewSettings.Rows.Clear();
2038 
2039  foreach (KeyValuePair<string, string> setting in settings)
2040  {
2041  object[] values = new object[] { setting.Key, setting.Value };
2042  this.gridViewSettings.Rows.Add(values);
2043  }
2044  }
2045  }
2046 
2047  // TODO: refactor
2048  private void buttonWriteSettings_Click(object sender, EventArgs e)
2049  {
2050  string nameSetting;
2051  string valueSetting;
2052  int selectedSettingPosition = 0;
2053  Dictionary<string, string> newSettings = new Dictionary<string, string>();
2054 
2055  if (this.gridViewSettings.SelectedRows.Count != 0)
2056  {
2057  selectedSettingPosition = this.gridViewSettings.SelectedRows[0].Index;
2058  }
2059 
2060  try
2061  {
2062  if (project != null || playerProject != null)
2063  {
2064  foreach (DataGridViewRow row in this.gridViewSettings.Rows)
2065  {
2066  nameSetting = row.Cells[this.dataGridViewTextBoxColumnSettingName.Name].Value.ToString();
2067  valueSetting = row.Cells[this.dataGridViewTextBoxColumnSettingValue.Name].Value?.ToString();
2068 
2069  newSettings.Add(nameSetting, valueSetting);
2070  }
2071 
2072  if (playerProject != null)
2073  {
2074  playerProject.Settings = newSettings;
2075  ReadSettings(playerProject.Settings); //Refresh gridViewSettings
2076  }
2077  else
2078  {
2079  project.Settings = newSettings;
2080  ReadSettings(project.Settings); //Refresh gridViewSettings
2081  }
2082  //focus on previous selected row
2083  this.gridViewSettings.Rows[selectedSettingPosition].Selected = true;
2084  this.gridViewSettings.Rows[selectedSettingPosition].Cells[dataGridViewTextBoxColumnSettingValue.Name].Selected = true;
2085 
2086  PrintText("Write settings finished");
2087  }
2088  }
2089  catch (System.Exception ex)
2090  {
2091  PrintException(ex);
2092  }
2093  }
2094 
2095  private void checkBoxCyclicExecution_CheckedChanged(object sender, EventArgs e)
2096  {
2097  this.checkBoxCyclicReload.Enabled = this.checkBoxNewRuntimeManager.Enabled = this.checkBoxCyclicExecution.Checked;
2098 
2099  if (!this.checkBoxCyclicExecution.Checked)
2100  {
2101  this.checkBoxCyclicReload.Checked = this.checkBoxNewRuntimeManager.Checked = false;
2102  }
2103 
2104  SyncWithParent(this.creatorForm?.checkBoxCyclicExecution, this.checkBoxCyclicExecution);
2105  }
2106 
2107  private void checkBoxAsyncExecution_CheckedChanged(object sender, EventArgs e)
2108  {
2109  SyncWithParent(this.creatorForm?.checkBoxAsyncExecution, this.checkBoxAsyncExecution);
2110  }
2111 
2112  private void checkBoxNewRuntimeManager_CheckedChanged(object sender, EventArgs e)
2113  {
2114  if (this.checkBoxNewRuntimeManager.Checked)
2115  {
2116  this.checkBoxCyclicReload.Checked = true;
2117  }
2118 
2119  SyncWithParent(this.creatorForm?.checkBoxNewRuntimeManager, this.checkBoxNewRuntimeManager);
2120  }
2121 
2122  private void checkBoxCyclicReload_CheckedChanged(object sender, EventArgs e)
2123  {
2124  SyncWithParent(this.creatorForm?.checkBoxCyclicReload, this.checkBoxCyclicReload);
2125  }
2126 
2127  // TODO: refactor
2128  private void buttonExecuteSelectedProcedure_Click(object sender, EventArgs e)
2129  {
2130  try
2131  {
2132  if (treeViewOtxProject.SelectedNode != null && treeViewOtxProject.SelectedNode.Tag is IProcedure)
2133  {
2134  IDocument document = treeViewOtxProject.SelectedNode.Parent.Tag as IDocument;
2135  List<string> listItemsReviewed = new List<string>();
2136 
2137  this.procedureToExecute = treeViewOtxProject.SelectedNode.Tag as IProcedure;
2138  this.ExecuteProcedure();
2139  }
2140  else
2141  {
2142  PrintText("Please select Procedure to execute");
2143  }
2144 
2145  }
2146  catch (System.Exception ex)
2147  {
2148  PrintException(ex);
2149  }
2150 
2151  SyncWithParent(this.creatorForm?.buttonExecuteSelectedProcedure, null, true);
2152  }
2153 
2154 
2155  // TODO: refactor
2156  private void buttonExecuteMain_Click(object sender, EventArgs e)
2157  {
2158  try
2159  {
2160  if (project != null)
2161  {
2162  treeViewOtxProject.SelectedNode = startupNode;
2163  this.procedureToExecute = project.MainProcedure;
2164  this.ExecuteProcedure();
2165  }
2166  else
2167  {
2168  PrintText("There is no project has been loaded. Please reload the project");
2169  }
2170  }
2171  catch (System.Exception ex)
2172  {
2173  PrintException(ex);
2174  }
2175 
2176  SyncWithParent(this.creatorForm?.buttonExecuteMain, null, true);
2177  }
2178 
2179  // TODO: refactor
2180  private async void ExecuteProcedure()
2181  {
2182  PrintText("Start procedure execution...");
2183 
2184  if (checkBoxAsyncExecution.Checked == false)
2185  {
2186  this.buttonExecuteMain.Enabled = false;
2187  this.buttonExecuteSelectedProcedure.Enabled = false;
2188  }
2189 
2190  try
2191  {
2192  if (this.globalRuntimeManager != null && procedureToExecute != null)
2193  {
2194  this.buttonStop.Enabled = true;
2195 
2196  CheckBatteryIgnitionState(this.globalRuntimeManager);
2197 
2198  if (checkBoxCyclicExecution.Checked)
2199  {
2200  ThreadPool.QueueUserWorkItem(state => DoCyclic());
2201  }
2202  else
2203  {
2204  //OtxDiagManager.OtxDiagApi.License.LicenseManager.SetLicenseKey(SampleConstants.LICENSE_KEY);
2205  if (checkBoxAsyncExecution.Checked)
2206  {
2207  this.globalRuntimeManager.ExecuteAsync(textBoxRuntimeContextName.Text.Trim(), procedureToExecute, this.expectedConnectionState, (ulong)Convert.ToInt32(this.textBoxTimeout.Text));
2208  }
2209  else
2210  {
2211  await Task.Run(() => this.globalRuntimeManager.Execute(textBoxRuntimeContextName.Text.Trim(), procedureToExecute, this.expectedConnectionState, (ulong)Convert.ToInt32(this.textBoxTimeout.Text)));
2212  }
2213  }
2214  }
2215  else
2216  {
2217  PrintText("RuntimeManager is null or no Procedure is selected to run.");
2218  }
2219  }
2220  catch (System.Exception ex)
2221  {
2222  PrintException(ex);
2223 
2224  UpdateButtonStateAfterThrowException();
2225  }
2226  }
2227 
2228  private void UpdateButtonStateAfterThrowException()
2229  {
2230  this.buttonExecuteMain.Enabled = !checkBoxCyclicExecution.Checked;
2231  this.buttonExecuteSelectedProcedure.Enabled = !checkBoxCyclicExecution.Checked;
2232 
2233  UpdateExecutionStateButtons(false);
2234  UpdatePauseButton(true, false);
2235 
2236  this.labelBatteryState.Image = OpenTestSystem.Otx.Runtime2.Api.DotNet.Sample.Properties.Resources.IconBattery16;
2237  this.labelIgnitionState.Image = OpenTestSystem.Otx.Runtime2.Api.DotNet.Sample.Properties.Resources.IconIgnition16;
2238  }
2239 
2240  // TODO: refactor
2241  private void DoCyclic()
2242  {
2243  DateTime startTime = DateTime.Now;
2244 
2245  int cyclic = 0;
2246  try
2247  {
2248  while (checkBoxCyclicExecution.Checked)
2249  {
2250  IRuntimeManager runtimeManager = null;
2251  IRuntimeContext runtimeContext = null;
2252 
2253  cyclic++;
2254  this.cyclicExecutionCount++;
2255 
2256  if (this.checkBoxNewRuntimeManager.Checked)
2257  {
2258  runtimeManager = CreateRuntimeManager();
2259  }
2260  else
2261  {
2262  runtimeManager = this.globalRuntimeManager;
2263  }
2264 
2265  CheckCyclicReloadOrNewRuntimeManager(runtimeManager);
2266 
2267  cyclicExecuteAsyncIsProcessing = true;
2268  CheckBatteryIgnitionState(this.globalRuntimeManager);
2269 
2270  if (this.checkBoxAsyncExecution.Checked)
2271  {
2272  runtimeContext = runtimeManager.ExecuteAsync(textBoxRuntimeContextName.Text.Trim(), procedureToExecute, this.expectedConnectionState, (ulong)Convert.ToInt32(this.textBoxTimeout.Text));
2273  if (runtimeContext != null)
2274  {
2275  //Let it break for a little bit to avoid freezing the GUI
2276  Application.DoEvents();
2277  Thread.Sleep(50);
2278  }
2279  }
2280  else
2281  {
2282  runtimeContext = runtimeManager.Execute(textBoxRuntimeContextName.Text.Trim(), procedureToExecute, this.expectedConnectionState, (ulong)Convert.ToInt32(this.textBoxTimeout.Text));
2283  }
2284 
2285  if (runtimeContext.ExecutionState == ExecutionState.Stopped)
2286  {
2287  this.cyclicExecutionCount = 0;
2288  break;
2289  }
2290  }
2291  }
2292  catch (System.Exception ex)
2293  {
2294  PrintException(ex);
2295 
2296  UpdateButtonStateAfterThrowException();
2297  }
2298 
2299  //TODO: Do we need to load this again?
2300  Invoke(new MethodInvoker(delegate ()
2301  {
2302  if (this.checkBoxNewRuntimeManager.Checked)
2303  {
2304  LoadContextFile(this.globalRuntimeManager);
2305  }
2306  }));
2307 
2308  PrintText("Cyclic execution finished");
2309 
2310  var duration = DateTime.Now - startTime;
2311  string text = String.Format("Execution Statistics: Duration {0} (hh:mm:ss) with {1} cycles and {2:0} ms per cycle", duration.ToString(@"hh\:mm\:ss"), cyclic, duration.TotalMilliseconds / cyclic);
2312 
2313  this.cyclicExecutionCount -= cyclic;
2314  if (this.cyclicExecutionCount < 0)
2315  {
2316  this.cyclicExecutionCount = 0;
2317  }
2318 
2319  if (this.checkBoxAdd2Output.Checked)
2320  {
2321  PrintText(text);
2322  }
2323  else
2324  {
2325  MessageBox.Show(text);
2326  }
2327  }
2328  private void WaitCyclicExecuteAsyncIsProcessing()
2329  {
2330  if (this.checkBoxCyclicReload.Checked || this.checkBoxNewRuntimeManager.Checked)
2331  {
2332  // OR while (!(runtimeContext.IsFinished || runtimeContext.IsStopped))
2333  while (cyclicExecuteAsyncIsProcessing)
2334  {
2335  //detail in #10461
2336  Application.DoEvents();
2337  Thread.Sleep(50);
2338  }
2339  }
2340  }
2341 
2342  private void CheckCyclicReloadOrNewRuntimeManager(IRuntimeManager runtimeMgr, bool waitCyclic = true)
2343  {
2344  if (waitCyclic)
2345  {
2346  WaitCyclicExecuteAsyncIsProcessing();
2347  }
2348 
2349  if (InvokeRequired)
2350  {
2351  Invoke(new MethodInvoker(delegate ()
2352  {
2353  CheckCyclicReloadOrNewRuntimeManager(runtimeMgr, false);
2354  }));
2355  return;
2356  }
2357 
2358  if (this.checkBoxCyclicReload.Checked || this.checkBoxNewRuntimeManager.Checked)
2359  {
2360  LoadContextFile(runtimeMgr);
2361  }
2362 
2363  if (this.checkBoxCyclicExecution.Checked)
2364  {
2365  this.checkBoxCyclicExecution.Text = "Cyclic (" + this.cyclicExecutionCount + ")";
2366  }
2367  else
2368  {
2369  this.checkBoxCyclicExecution.Text = "Cyclic";
2370  }
2371  }
2372 
2373  // TODO: refactor
2374  private void buttonStop_Click(object sender, EventArgs e)
2375  {
2376  if (this.globalRuntimeManager != null)
2377  {
2378  PrintText(String.Format("Try to stop all running procedures..."));
2379  this.globalRuntimeManager.StopAll();
2380  this.procedureExecutionCount = 0;
2381  }
2382 
2383  // To make sure that cyclic execution will stop in some cases
2384  this.checkBoxCyclicExecution.Checked = false;
2385 
2386  SyncWithParent(this.creatorForm?.buttonStop);
2387  }
2388 
2389  private void PrintException(System.Exception ex, string additionalText = "")
2390  {
2391  PrintText(String.Format("!!! {2}{0}: {1}", ex.GetType().Name, ex.Message, additionalText));
2392  System.Media.SystemSounds.Hand.Play();
2393  }
2394 
2395  private void PrintText(string text)
2396  {
2397  if (InvokeRequired)
2398  {
2399  this.Invoke(new Action(() => PrintText(text)));
2400  return;
2401  }
2402 
2403  if (!this.checkBoxAdd2Output.Checked)
2404  {
2405  return;
2406  }
2407 
2408  // TODO: handle multithreading?
2409  lock (printTextLock)
2410  {
2411  if (this.listBoxOuput.Items.Count >= 10000)
2412  {
2413  buttonClearOutput_Click(null, null);
2414  }
2415 
2416  string prefixString = GetTimeAndDurationStringSinceLastTimeAndUpdateLastTime();
2417  string itemToAdd = prefixString + text + "\t\t";
2418 
2419  this.listBoxOuput.Items.Add(itemToAdd);
2420  this.listBoxOuput.SelectedItems.Clear();
2421  this.listBoxOuput.TopIndex = listBoxOuput.Items.Count - 1;
2422 
2423  if (this.listBoxOuput.Items.Count >= 7500)
2424  {
2425  this.labelOutputOverflow.Visible = true;
2426  }
2427 
2428  // If many procedures are running simultaneously,
2429  // Stop button or other controls cannot receive events. This code allows controls to receive events.
2430  Application.DoEvents();
2431  }
2432  }
2433 
2434  private string GetTimeAndDurationStringSinceLastTimeAndUpdateLastTime()
2435  {
2436  DateTime now = DateTime.Now;
2437  var duration = now - this.lastTime;
2438 
2439  this.lastTime = now;
2440 
2441  string timeDurationString = String.Format("{0:HH:mm:ss.fff} {1,6:#,##0} ms ", now, duration.TotalMilliseconds);
2442  return timeDurationString;
2443  }
2444 
2445  private void buttonCopyRow_Click(object sender, EventArgs e)
2446  {
2447  if (this.listBoxOuput.SelectedItems != null)
2448  {
2449  string text = string.Empty;
2450  foreach (string row in this.listBoxOuput.SelectedItems)
2451  {
2452  text += row.Trim('\t') + "\n";
2453  }
2454 
2455  Clipboard.SetText(text);
2456  }
2457  }
2458 
2459  private void buttonClearOutput_Click(object sender, EventArgs e)
2460  {
2461  this.listBoxOuput.Items.Clear();
2462  this.labelOutputOverflow.Visible = false;
2463  this.buttonCopyRow.Enabled = false;
2464  }
2465 
2466  // TODO: refactor
2467  private void UpdateGridview(IProcedure procedure)
2468  {
2469  if (procedure == null ||
2470  procedure.Parameters == null)
2471  {
2472  return;
2473  }
2474 
2475  UpdateGridviewParameter(procedure);
2476 
2477  IDocument document = procedure.Document;
2478  if (document != null)
2479  {
2480  List<string> listItemsReviewed = new List<string>();
2481  UpdateGridViewContextVariable(document, false, listItemsReviewed);
2482 
2483  listItemsReviewed.Clear();
2484  UpdateGridViewStateVariable(document, false, listItemsReviewed);
2485  }
2486  }
2487 
2488  private void UpdateGridviewParameter(IProcedure procedure)
2489  {
2490  foreach (IProcedureParameter param in procedure.Parameters)
2491  {
2492  UpdateGridviewParameter(param);
2493  }
2494  }
2495 
2496  // TODO: refactor
2497  private void UpdateGridviewParameter(IProcedureParameter parameter)
2498  {
2499  if (parameter == null)
2500  {
2501  return;
2502  }
2503 
2504  bool bValueWasSet = false;
2505 
2506  // Update existing value
2507  string collumnName = dataGridViewTextBoxColumnName.Name;
2508  string collumnValue = dataGridViewTextBoxColumnValue.Name;
2509  string collumnDetails = dataGridViewTextBoxColumnDetails.Name;
2510  foreach (DataGridViewRow row in this.gridViewParameter.Rows)
2511  {
2512  if (row.Cells[collumnName].Value.Equals(parameter.Name))
2513  {
2514  try
2515  {
2516  if (parameter.Value != null)
2517  {
2518  row.Cells[collumnValue].Value = ConvertValue2String(parameter.Value);
2519  }
2520  }
2521  catch (System.Exception ex)
2522  {
2523  row.Cells[collumnValue].ErrorText = ex.Message;
2524  }
2525 
2526  bValueWasSet = true;
2527  break;
2528  }
2529  }
2530 
2531  // Add value if not exist
2532  if (!bValueWasSet)
2533  {
2534  string direction = parameter is IProcedureInParameter ? "In" : (parameter is IProcedureInOutParameter ? "InOut" : "Out");
2535  object[] values = new object[] { parameter.Name, direction, parameter.DataType };
2536  int index = this.gridViewParameter.Rows.Add(values);
2537 
2538  this.gridViewParameter.Rows[index].Tag = parameter;
2539 
2540  this.gridViewParameter.Rows[index].ReadOnly = parameter is IProcedureOutParameter || parameter.InitValue == null;
2541 
2542  if (parameter.DataType.Equals("Boolean"))
2543  {
2544  gridViewParameter.Rows[index].Cells[collumnValue] = new DataGridViewCheckBoxCell();
2545  DataGridViewCheckBoxCell cell = gridViewParameter.Rows[index].Cells[collumnValue] as DataGridViewCheckBoxCell;
2546  cell.Value = ConvertValue2String(parameter.Value);
2547  cell.Tag = parameter.Value;
2548  }
2549  else if (parameter.Value is EnumerationElement) // Enumeration type
2550  {
2551  gridViewParameter.Rows[index].Cells[collumnValue] = new DataGridViewComboBoxCell();
2552  DataGridViewComboBoxCell cell = gridViewParameter.Rows[index].Cells[collumnValue] as DataGridViewComboBoxCell;
2553  cell.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox;
2554  cell.FlatStyle = FlatStyle.Flat;
2555  cell.DisplayMember = "Name";
2556  cell.ValueMember = "Name";
2557  cell.DataSource = (parameter.Value as EnumerationElement).Enumeration.Elements;
2558  cell.Value = ConvertValue2String(parameter.Value);
2559  cell.Tag = parameter.Value;
2560  }
2561  else
2562  {
2563  try
2564  {
2565  if (parameter.Value != null)
2566  {
2567  gridViewParameter.Rows[index].Cells[collumnValue].Value = ConvertValue2String(parameter.Value);
2568  }
2569  }
2570  catch (System.Exception ex)
2571  {
2572  gridViewParameter.Rows[index].Cells[collumnValue].ErrorText = ex.Message;
2573  }
2574  }
2575 
2576  gridViewParameter.Rows[index].Cells[collumnDetails].Value = GetDetails(parameter.Value);
2577  }
2578  }
2579 
2580  private string ConvertValue2String(object value)
2581  {
2582  if (value == null)
2583  {
2584  return string.Empty;
2585  }
2586 
2587  return ValueConverter.Value2String(value);
2588  }
2589 
2590  private string GetDetails(object value)
2591  {
2592  if (value == null)
2593  {
2594  return string.Empty;
2595  }
2596 
2597  if (value is TranslationKey translationKey)
2598  {
2599  if (!string.IsNullOrEmpty(translationKey.TextIdMappingName))
2600  {
2601  return string.Format("MappingName: {0}", translationKey.TextIdMappingName);
2602  }
2603  }
2604  else if (value is EnumerationElement enumerationElement)
2605  {
2606  if (enumerationElement.TranslationKey != null)
2607  {
2608  if (!string.IsNullOrEmpty(enumerationElement.TranslationKey.TextId) && !string.IsNullOrEmpty(enumerationElement.TranslationKey.TextIdMappingName))
2609  {
2610  return string.Format("TextId: {0}, MappingName: {1}", enumerationElement.TranslationKey.TextId, enumerationElement.TranslationKey.TextIdMappingName);
2611  }
2612  else if (!string.IsNullOrEmpty(enumerationElement.TranslationKey.TextId))
2613  {
2614  return string.Format("TextId: {0}", enumerationElement.TranslationKey.TextId);
2615  }
2616 
2617  return string.Format("MappingName: {0}", enumerationElement.TranslationKey.TextIdMappingName);
2618  }
2619  }
2620 
2621  return string.Empty;
2622  }
2623 
2624  // TODO: refactor
2625  private void UpdateGridViewContextVariable(IDocument document, bool withPrefix, List<string> listItemsReviewed)
2626  {
2627  string @namespace = string.Concat(document.Package.Name, ".", document.Name);
2628  if (listItemsReviewed.Contains(@namespace))
2629  {
2630  return;
2631  }
2632 
2633  listItemsReviewed.Add(@namespace);
2634  foreach (IContextVariable contextVariable in document.ContextVariables)
2635  {
2636  if (contextVariable == null)
2637  {
2638  return;
2639  }
2640 
2641  bool bValueWasSet = false;
2642 
2643  // Update existing value
2644  foreach (DataGridViewRow row in this.gridViewContext.Rows)
2645  {
2646  if (row.Cells[this.dataGridViewTextBoxColumnContextName.Name].Value != null && row.Cells[this.dataGridViewTextBoxColumnContextName.Name].Value.Equals(withPrefix ? (document.Package.FullName + "." + document.Name + "." + contextVariable.Name) : contextVariable.Name))
2647  {
2648  if (radioButtonDefaultImplementation.Checked)
2649  {
2650  object contextVariableValue = this.contextVariableImplementation.GetValue(null, contextVariable, null);
2651  row.Cells[this.dataGridViewTextBoxColumnContextValue.Name].Value = contextVariableValue != null ? ConvertValue2String(contextVariableValue) : null;
2652  }
2653  else
2654  {
2655  row.Cells[this.dataGridViewTextBoxColumnContextValue.Name].Value = contextVariable.InitValue != null ? ConvertValue2String(contextVariable.InitValue) : null;
2656  }
2657 
2658  bValueWasSet = true;
2659  break;
2660  }
2661  }
2662 
2663  // Add value if not exist
2664  if (!bValueWasSet)
2665  {
2666  object[] values = null;
2667  values = new object[] { withPrefix ? (document.Package.FullName + "." + document.Name + "." + contextVariable.Name) : contextVariable.Name, contextVariable.DataType };
2668  int index = this.gridViewContext.Rows.Add(values);
2669 
2670  object contextVariableValue = contextVariable.InitValue;
2671 
2672  this.gridViewContext.Rows[index].Tag = contextVariable;
2673  this.gridViewContext.Rows[index].ReadOnly = contextVariable.InitValue == null;
2674  this.gridViewContext.Rows[index].Cells[dataGridViewTextBoxColumnContextValue.Name].Tag = contextVariable.InitValue;
2675  if (contextVariable.DataType.Equals("Boolean"))
2676  {
2677  gridViewContext.Rows[index].Cells[dataGridViewTextBoxColumnContextValue.Name] = new DataGridViewCheckBoxCell();
2678  DataGridViewCheckBoxCell cell = gridViewContext.Rows[index].Cells[dataGridViewTextBoxColumnContextValue.Name] as DataGridViewCheckBoxCell;
2679  cell.Value = ConvertValue2String(contextVariableValue);
2680  cell.Tag = contextVariableValue;
2681  }
2682  else if(contextVariableValue is EnumerationElement) // Enumeration type
2683  {
2684  gridViewContext.Rows[index].Cells[dataGridViewTextBoxColumnContextValue.Name] = new DataGridViewComboBoxCell();
2685  DataGridViewComboBoxCell cell = gridViewContext.Rows[index].Cells[dataGridViewTextBoxColumnContextValue.Name] as DataGridViewComboBoxCell;
2686  cell.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox;
2687  cell.FlatStyle = FlatStyle.Flat;
2688  cell.DisplayMember = "Name";
2689  cell.ValueMember = "Name";
2690  cell.DataSource = (contextVariableValue as EnumerationElement).Enumeration.Elements;
2691  cell.Value = ConvertValue2String(contextVariableValue);
2692  cell.Tag = contextVariableValue;
2693  }
2694  else
2695  {
2696  try
2697  {
2698  if (contextVariableValue != null)
2699  {
2700  gridViewContext.Rows[index].Cells[dataGridViewTextBoxColumnContextValue.Name].Value = ConvertValue2String(contextVariableValue);
2701  }
2702  else
2703  {
2704  gridViewContext.Rows[index].Cells[dataGridViewTextBoxColumnContextValue.Name].Value = null;
2705  }
2706  }
2707  catch (System.Exception ex)
2708  {
2709  gridViewContext.Rows[index].Cells[dataGridViewTextBoxColumnContextValue.Name].ErrorText = ex.Message;
2710  }
2711  }
2712 
2713  gridViewContext.Rows[index].Cells[dataGridViewTextBoxColumnContextDetails.Name].Value = GetDetails(contextVariableValue);
2714  }
2715  }
2716 
2717  try
2718  {
2719  foreach (IDocument importDoc in document.Imports)
2720  {
2721  UpdateGridViewContextVariable(importDoc, true, listItemsReviewed);
2722  }
2723  }
2724  catch (System.Exception ex)
2725  {
2726  PrintException(ex);
2727  }
2728  }
2729 
2730  // TODO: refactor this
2731  private void StateVariableValueChanged(IStateVariable stateVariable, object value)
2732  {
2733  if (this.InvokeRequired)
2734  {
2735  this.Invoke(new Action(() => StateVariableValueChanged(stateVariable, value)));
2736  return;
2737  }
2738 
2739  string mappingStr = String.Format("[MappedTo: {0}{1}]",
2740  stateVariable.MappingName,
2741  (stateVariable.MappingIndex > -1 ? "[" + stateVariable.MappingIndex + "]" : String.Empty));
2742  string strValue = "null";
2743  try
2744  {
2745  strValue = ValueConverter.Value2String(value);
2746  }
2747  catch
2748  {
2749  }
2750 
2751  string outputLog = String.Format("StateVariableChanged({0} {1}.{2}{3} = {4})",
2752  stateVariable.DataType,
2753  stateVariable.Document.FullName,
2754  stateVariable.Name,
2755  mappingStr,
2756  strValue);
2757  PrintText(outputLog);
2758  UpdateGridViewStateVariable(stateVariable);
2759  }
2760 
2761  private void ContextVariableRead(IContextVariable contextVariable, object value)
2762  {
2763  if (this.InvokeRequired)
2764  {
2765  this.Invoke(new Action(() => ContextVariableRead(contextVariable, value)));
2766  return;
2767  }
2768 
2769  string mappingStr = String.Format("[MappedTo: {0}{1}]",
2770  contextVariable.MappingName,
2771  (contextVariable.MappingIndex > -1 ? "[" + contextVariable.MappingIndex + "]" : String.Empty));
2772  string strValue = "null";
2773  try
2774  {
2775  strValue = ValueConverter.Value2String(value);
2776  }
2777  catch
2778  {
2779  }
2780  string outputLog = String.Format("ContextVariableRead({0} {1}.{2}{3} = {4})",
2781  contextVariable.DataType,
2782  contextVariable.Document.FullName,
2783  contextVariable.Name,
2784  mappingStr,
2785  strValue);
2786 
2787  PrintText(outputLog);
2788  }
2789 
2790  private void UpdateGridViewStateVariable(IDocument document, bool withPrefix, List<string> listItemsReviewed)
2791  {
2792  string @namespace = string.Concat(document.Package.Name, ".", document.Name);
2793  if (listItemsReviewed.Contains(@namespace))
2794  {
2795  return;
2796  }
2797 
2798  listItemsReviewed.Add(@namespace);
2799  foreach (IStateVariable stateVariable in document.StateVariables)
2800  {
2801  object[] values = null;
2802  values = new object[] { withPrefix ? (document.Package.FullName + "." + document.Name + "." + stateVariable.Name) : stateVariable.Name, stateVariable.DataType };
2803  int index = this.gridViewState.Rows.Add(values);
2804 
2805  this.gridViewState.Rows[index].Tag = stateVariable;
2806 
2807  if (stateVariable.DataType.Equals("Boolean"))
2808  {
2809  gridViewState.Rows[index].Cells[dataGridViewTextBoxColumnStateValue.Name] = new DataGridViewCheckBoxCell();
2810  DataGridViewCheckBoxCell cell = gridViewState.Rows[index].Cells[dataGridViewTextBoxColumnStateValue.Name] as DataGridViewCheckBoxCell;
2811  cell.Value = ConvertValue2String(stateVariable.InitValue);
2812  cell.Tag = stateVariable.InitValue;
2813  }
2814  else if (stateVariable.InitValue is EnumerationElement) // Enumeration type
2815  {
2816  gridViewState.Rows[index].Cells[dataGridViewTextBoxColumnStateValue.Name] = new DataGridViewComboBoxCell();
2817  DataGridViewComboBoxCell cell = gridViewState.Rows[index].Cells[dataGridViewTextBoxColumnStateValue.Name] as DataGridViewComboBoxCell;
2818  cell.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox;
2819  cell.FlatStyle = FlatStyle.Flat;
2820  cell.DisplayMember = "Name";
2821  cell.ValueMember = "Name";
2822  cell.DataSource = (stateVariable.InitValue as EnumerationElement).Enumeration.Elements;
2823  cell.Value = ConvertValue2String(stateVariable.InitValue);
2824  cell.Tag = stateVariable.InitValue;
2825  }
2826  else
2827  {
2828  try
2829  {
2830  if (stateVariable.InitValue != null)
2831  {
2832  gridViewState.Rows[index].Cells[dataGridViewTextBoxColumnStateValue.Name].Value = ConvertValue2String(stateVariable.InitValue);
2833  }
2834  }
2835  catch (System.Exception ex)
2836  {
2837  gridViewState.Rows[index].Cells[dataGridViewTextBoxColumnStateValue.Name].ErrorText = ex.Message;
2838  }
2839  }
2840 
2841  if (stateVariable.InitValue != null)
2842  {
2843  gridViewState.Rows[index].Cells[dataGridViewTextBoxColumnStateDetails.Name].Value = GetDetails(stateVariable.InitValue);
2844  }
2845  }
2846 
2847  try
2848  {
2849  foreach (IDocument importDoc in document.Imports)
2850  {
2851  UpdateGridViewStateVariable(importDoc, true, listItemsReviewed);
2852  }
2853  }
2854  catch (System.Exception ex)
2855  {
2856  PrintException(ex);
2857  }
2858  }
2859 
2860 
2861  // TODO: refactor
2862  private void UpdateGridViewStateVariable(IStateVariable stateVar)
2863  {
2864  if (stateVar == null)
2865  {
2866  return;
2867  }
2868 
2869  // Update existing value
2870  foreach (DataGridViewRow row in this.gridViewState.Rows)
2871  {
2872  if (row.Cells[this.dataGridViewTextBoxColumnStateName.Name].Value != null && row.Tag == stateVar)
2873  {
2874  object stateVariableValue = null;
2875 
2876  if (radioButtonDefaultImplementation.Checked)
2877  stateVariableValue = this.stateVariableImplementation.GetValue(stateVar);
2878  else
2879  stateVariableValue = stateVar.InitValue;
2880 
2881  if (stateVariableValue != null)
2882  {
2883  try
2884  {
2885  row.Cells[this.dataGridViewTextBoxColumnStateValue.Name].Value = ConvertValue2String(stateVariableValue);
2886  }
2887  catch (System.Exception ex)
2888  {
2889  row.Cells[this.dataGridViewTextBoxColumnStateValue.Name].ErrorText = ex.Message;
2890  }
2891 
2892  row.Cells[this.dataGridViewTextBoxColumnStateDetails.Name].Value = GetDetails(stateVariableValue);
2893  }
2894  else
2895  {
2896  row.Cells[this.dataGridViewTextBoxColumnStateValue.Name].Value = null;
2897  row.Cells[this.dataGridViewTextBoxColumnStateDetails.Name].Value = string.Empty;
2898  }
2899  break;
2900  }
2901  }
2902 
2903  Application.DoEvents();
2904  }
2905 
2906  private void buttonNewInstance_Click(object sender, EventArgs e)
2907  {
2908  Form form = new SampleForm(SampleConstants.CHILD_INSTANCE_NAME, this);
2909  form.Show();
2910  }
2911 
2912  private void buttonNewInstanceNewThread_Click(object sender, EventArgs e)
2913  {
2914  Thread thread = new Thread(new ThreadStart(NewInstanceInThread));
2915  thread.SetApartmentState(ApartmentState.STA);
2916  thread.Start();
2917  }
2918 
2919  private void NewInstanceInThread()
2920  {
2921  Application.Run(new SampleForm(SampleConstants.CHILD_INSTANCE_NAME, this));
2922  }
2923 
2924  private void buttonHmi_Click(object sender, EventArgs e)
2925  {
2926  if (this.buttonHmi.Checked)
2927  {
2928  hmiWindow = new HmiWindow();
2929  hmiWindow.FormClosing += HmiWindow_FormClosing;
2930  hmiWindow.SizeChanged += HmiWindow_SizeChanged;
2931  hmiWindow.Activated += HmiWindow_Activated;
2932  hmiWindow.Show();
2933 
2934  hmiWindow.Size = new Size(userSettings.HmiWindowWidth, userSettings.HmiWindowHeight);
2935  hmiWindow.Location = new Point(this.Location.X + this.Size.Width, this.Location.Y);
2936  if (this.creatorForm != null && this.creatorForm.hmiWindow != null)
2937  {
2938  this.hmiWindow.Size = this.creatorForm.hmiWindow.Size;
2939  }
2940 
2941  hmiWindow.BringToFront();
2942 
2943  customScreenImplementation.HmiScreenContainer = this.hmiWindow.HmiControl;
2944  }
2945  else if (this.hmiWindow != null)
2946  {
2947  this.hmiWindow.Close();
2948  }
2949 
2950  SyncWithParent(this.creatorForm?.buttonHmi, this.buttonHmi);
2951  }
2952 
2953  private void HmiWindow_FormClosing(object sender, FormClosingEventArgs e)
2954  {
2955  customScreenImplementation.HmiScreenContainer = null;
2956  this.hmiWindow = null;
2957 
2958  this.buttonHmi.Checked = false;
2959  }
2960 
2961  private void HmiWindow_Activated(object sender, EventArgs e)
2962  {
2963  if (customScreenImplementation != null)
2964  {
2965  customScreenImplementation.ActivateHmiScreen();
2966  }
2967  }
2968 
2969  private void HmiWindow_SizeChanged(object sender, EventArgs e)
2970  {
2971  userSettings.HmiWindowWidth = this.hmiWindow.Width;
2972  userSettings.HmiWindowHeight = this.hmiWindow.Height;
2973 
2974  if (customScreenImplementation != null)
2975  {
2976  customScreenImplementation.RefreshHmiScreen();
2977  }
2978  }
2979 
2980  private void CustomScreenImplementation_KeyDown(Runtime.Api.Custom.KeyEventArgs e)
2981  {
2982  string key = "Unknown";
2983  if (e is WpfKeyEventArgs)
2984  {
2985  WpfKeyEventArgs wpfArgs = e as WpfKeyEventArgs;
2986  key = wpfArgs.Key.ToString();
2987  if (wpfArgs.ModifierKey == System.Windows.Input.ModifierKeys.Alt &&
2988  wpfArgs.Key == System.Windows.Input.Key.F4)
2989  {
2990  hmiWindow?.Close();
2991  }
2992  else if (wpfArgs.Key == System.Windows.Input.Key.F10 || wpfArgs.Key == System.Windows.Input.Key.LeftAlt || wpfArgs.Key == System.Windows.Input.Key.RightAlt)
2993  {
2994  e.Handled = true;
2995  }
2996  }
2997  else if (e is FormKeyEventArgs)
2998  {
2999  FormKeyEventArgs formArgs = e as FormKeyEventArgs;
3000  if (formArgs.ModifierKey == Keys.Alt &&
3001  formArgs.Key == Keys.F4)
3002  {
3003  hmiWindow?.Close();
3004  }
3005  else if (formArgs.Key == Keys.F10 || formArgs.Key == Keys.Alt)
3006  {
3007  e.Handled = true;
3008  }
3009  key = formArgs.Key.ToString();
3010  }
3011 
3012  this.PrintText("CustomScreenImplementation_KeyDown(" + key + ")");
3013  }
3014 
3015  // TODO: refactor
3016  private void SampleForm_FormClosing(object sender, FormClosingEventArgs e)
3017  {
3018  listForms.Remove(this);
3019 
3020  // Remove Event
3021  if (this.globalRuntimeManager != null)
3022  {
3023  this.globalRuntimeManager.StopAll();
3024  this.globalRuntimeManager.ProcedurePending -= ProcedurePending;
3025  this.globalRuntimeManager.ProcedureStarted -= ProcedureStarted;
3026  this.globalRuntimeManager.ProcedurePaused -= ProcedurePaused;
3027  this.globalRuntimeManager.ProcedureContinued -= ProcedureContinued;
3028  this.globalRuntimeManager.ProcedureFinished -= ProcedureFinished;
3029  this.globalRuntimeManager.ProcedureStopped -= ProcedureStopped;
3030  this.globalRuntimeManager.ProcedureAborted -= ProcedureAborted;
3031  this.globalRuntimeManager.InOutParameterValueChanged -= InOutParameterValueChanged;
3032  }
3033 
3034  this.stateVariableImplementation.StateVariableValueChanged -= StateVariableValueChanged;
3035 
3036  basicScreenOutputImpl.LogEvent -= PrintText;
3037  customScreenOutputImpl.LogEvent -= PrintText;
3038  loggingOutputImpl.LogEvent -= PrintText;
3039  contextVariableOutputImpl.LogEvent -= PrintText;
3040 
3041  stateVariableOutputImpl.LogEvent -= PrintText;
3042  measureOutputImpl.LogEvent -= PrintText;
3043  i18NOutputImpl.LogEvent -= PrintText;
3044  serviceProviderOutputImpl.LogEvent -= PrintText;
3045 
3046  sqlOutputImpl.LogEvent -= PrintText;
3047  commonDialogsOutputImpl.LogEvent -= PrintText;
3048 
3049  SaveSettings();
3050  }
3051 
3052  private void SaveSettings()
3053  {
3054  if (this.creatorForm == null)
3055  {
3056  // Only main form
3057  userSettings.Ptx_Ppx_Directory = cbFilePath.Text.Trim();
3058  userSettings.Ptx_Ppx_DirectoryList = string.Join(";", this.cbFilePath.Items.Cast<Object>().Select(item => item.ToString()).ToArray());
3059 
3060  userSettings.TraceFileMaxCount = Convert.ToInt32(this.textBoxTraceFileMaxCount.Text.Trim());
3061  userSettings.TraceFileMaxSize = Convert.ToInt32(this.textBoxTraceFileMaxSize.Text.Trim());
3062  userSettings.TraceLevels = RuntimeConfig.Instance.TraceLevel;
3063  userSettings.TracingDirectory = textBoxTraceFolder.Text.Trim();
3064 
3065  userSettings.WindowWidth = this.Size.Width;
3066  userSettings.WindowHeight = this.Size.Height;
3067  userSettings.WindowLocationX = this.Location.X;
3068  userSettings.WindowLocationY = this.Location.Y;
3069  userSettings.CustomImplTypes = GetCurrentCustomImpl();
3070  SaveSocketPortOrPipeName();
3071 
3072  userSettings.Asynchron = checkBoxAsyncExecution.Checked;
3073  userSettings.Cyclic = checkBoxCyclicExecution.Checked;
3074  userSettings.CyclicReload = checkBoxCyclicReload.Checked;
3075  userSettings.NewRunTimeManager = checkBoxNewRuntimeManager.Checked;
3076  userSettings.AddMessageToOutput = checkBoxAdd2Output.Checked;
3077  userSettings.StartAllParents = checkBoxStartAllParents.Checked;
3078 
3079  userSettings.TimeOut = (ulong)Convert.ToInt32(this.textBoxTimeout.Text.Trim());
3080  userSettings.ConnectionState = checkBoxUseConnectionState.Checked;
3081  userSettings.Ignition = checkBoxIgnition.CheckState == CheckState.Checked ? 1 : (checkBoxIgnition.CheckState == CheckState.Unchecked ? 0 : -1);
3082  userSettings.PollingTime = Convert.ToInt32(this.textBoxPollingTime.Text.Trim());
3083  userSettings.VoltageThreshold = Convert.ToInt32(this.textBoxVoltageThreshold.Text.Trim());
3084 
3085  userSettings.RuntimeContextName = this.textBoxRuntimeContextName.Text.Trim();
3086 
3087  SaveSettingUtil.Save();
3088  }
3089  }
3090 
3091  private void SaveSocketPortOrPipeName()
3092  {
3093  var ipcType = (IpcTypes)comboBoxIpcType.SelectedItem;
3094 
3095  switch (ipcType)
3096  {
3097  case IpcTypes.SOCKET:
3098  {
3099  try
3100  {
3101  userSettings.DiagManagerPort = Convert.ToUInt16(textBoxDiagPortPipe.Text.Trim());
3102  userSettings.RuntimePort = Convert.ToUInt16(textBoxRtPortPipe.Text.Trim());
3103  userSettings.IpcType = IpcTypes.SOCKET.ToString();
3104  }
3105  catch (System.Exception)
3106  {
3107  userSettings.DiagManagerPort = SampleConstants.DEFAULT_DM_PORT;
3108  userSettings.RuntimePort = SampleConstants.DEFAULT_RT_PORT;
3109  }
3110  break;
3111  }
3112 
3113  case IpcTypes.PIPE:
3114  {
3115  string diagManagerPipeName = textBoxDiagPortPipe.Text.Trim();
3116  string runtimePipeName = textBoxRtPortPipe.Text.Trim();
3117 
3118  userSettings.DiagManagerPipeName = (diagManagerPipeName != string.Empty) ? diagManagerPipeName : SampleConstants.DEFAULT_DM_PIPE_NAME;
3119  userSettings.RuntimePipeName = (runtimePipeName != string.Empty) ? runtimePipeName : SampleConstants.DEFAULT_RT_PIPE_NAME;
3120  userSettings.IpcType = IpcTypes.PIPE.ToString();
3121  break;
3122  }
3123 
3124  default:
3125  break;
3126  }
3127  }
3128 
3129  private void textBoxRuntimeContextName_TextChanged(object sender, EventArgs e)
3130  {
3131  this.runtimeContextName = this.textBoxRuntimeContextName.Text = this.textBoxRuntimeContextName.Text.Trim();
3132  SetTitle(this.title);
3133  }
3134 
3135  private void listBoxOuput_SelectedIndexChanged(object sender, EventArgs e)
3136  {
3137  this.buttonCopyRow.Enabled = this.listBoxOuput.SelectedItems.Count > 0;
3138  }
3139 
3140  private void buttonPause_Click(object sender, EventArgs e)
3141  {
3142  lock (this.runtimeContexts)
3143  {
3144  if (this.buttonPause.Checked)
3145  {
3146  foreach (IRuntimeContext runtimeContext in this.runtimeContexts)
3147  {
3148  if (runtimeContext.ExecutionState == ExecutionState.Running)
3149  {
3150  runtimeContext.Pause();
3151  }
3152  }
3153  }
3154  else
3155  {
3156  foreach (IRuntimeContext runtimeContext in this.runtimeContexts)
3157  {
3158  if (runtimeContext.ExecutionState == ExecutionState.Paused)
3159  {
3160  runtimeContext.Continue();
3161  }
3162  }
3163  }
3164  }
3165 
3166  SyncWithParent(this.creatorForm?.buttonPause, this.buttonPause);
3167  }
3168 
3169  private void AddRuntimeContext(IRuntimeContext context)
3170  {
3171  lock (this.runtimeContexts)
3172  {
3173  if (!this.runtimeContexts.Contains(context))
3174  {
3175  this.runtimeContexts.Add(context);
3176  }
3177 
3178  if (!runtimeContextsExecutionStartTime.ContainsKey(context))
3179  {
3180  runtimeContextsExecutionStartTime.Add(context, DateTime.Now);
3181  }
3182  }
3183  }
3184 
3185  private void RemoveRuntimeContext(IRuntimeContext context)
3186  {
3187  lock (this.runtimeContexts)
3188  {
3189  if (this.runtimeContexts.Contains(context))
3190  {
3191  this.runtimeContexts.Remove(context);
3192  }
3193 
3194  if (runtimeContextsExecutionStartTime.ContainsKey(context))
3195  {
3196  runtimeContextsExecutionStartTime.Remove(context);
3197  }
3198  }
3199  }
3200 
3201  private bool IsPauseEnabled()
3202  {
3203  lock (this.runtimeContexts)
3204  {
3205  foreach (IRuntimeContext runtimeContext in this.runtimeContexts)
3206  {
3207  if (runtimeContext.ExecutionState == ExecutionState.Running)
3208  {
3209  return true;
3210  }
3211  }
3212 
3213  return false;
3214  }
3215  }
3216 
3217  private bool IsContinueEnable()
3218  {
3219  lock (this.runtimeContexts)
3220  {
3221  foreach (IRuntimeContext runtimeContext in this.runtimeContexts)
3222  {
3223  if (runtimeContext.ExecutionState == ExecutionState.Paused)
3224  {
3225  return true;
3226  }
3227  }
3228 
3229  return false;
3230  }
3231  }
3232 
3233  private void SetExpectedState()
3234  {
3235  if (this.useConnectionState)
3236  {
3237  if (this.KL15State == null)
3238  {
3239  this.expectedConnectionState = ExpectedState.BatteryOn;
3240  }
3241  else if (this.KL15State.Value)
3242  {
3243  this.expectedConnectionState = ExpectedState.IgnitionOn;
3244  }
3245  else
3246  {
3247  this.expectedConnectionState = ExpectedState.IgnitionOff;
3248  }
3249  }
3250  else
3251  {
3252  this.expectedConnectionState = ExpectedState.None;
3253  }
3254  }
3255 
3256  private void checkBoxUseConnectionState_CheckedChanged(object sender, EventArgs e)
3257  {
3258  this.useConnectionState = this.checkBoxUseConnectionState.Checked;
3259  this.EnableConnectionState();
3260  this.SetExpectedState();
3261 
3262  this.labelBatteryState.Image = OpenTestSystem.Otx.Runtime2.Api.DotNet.Sample.Properties.Resources.IconBattery16;
3263  this.labelIgnitionState.Image = OpenTestSystem.Otx.Runtime2.Api.DotNet.Sample.Properties.Resources.IconIgnition16;
3264 
3265  SyncWithParent(this.creatorForm?.checkBoxUseConnectionState, this.checkBoxUseConnectionState);
3266  }
3267 
3268  private void CheckBoxKL15_CheckStateChanged(object sender, System.EventArgs e)
3269  {
3270  switch (this.checkBoxIgnition.CheckState)
3271  {
3272  case CheckState.Checked:
3273  this.KL15State = true;
3274  break;
3275  case CheckState.Unchecked:
3276  this.KL15State = false;
3277  break;
3278  default:
3279  this.KL15State = null;
3280  break;
3281  }
3282 
3283  this.SetExpectedState();
3284 
3285  SyncWithParent(this.creatorForm?.checkBoxIgnition, this.checkBoxIgnition);
3286  }
3287 
3288  private void EnableConnectionState()
3289  {
3290  this.labelExpectedState.Enabled = this.useConnectionState;
3291  this.labelBatteryState.Enabled = this.useConnectionState;
3292  this.checkBoxIgnition.Enabled = this.useConnectionState;
3293  this.labelPollingTime.Enabled = this.useConnectionState;
3294  this.textBoxPollingTime.Enabled = this.useConnectionState;
3295  this.labelVoltageThreshold.Enabled = this.useConnectionState;
3296  this.textBoxVoltageThreshold.Enabled = this.useConnectionState;
3297  this.buttonCheckBatteryIgnition.Enabled = this.useConnectionState;
3298  }
3299 
3300  private void buttonCheckBatteryIgnition_Click(object sender, EventArgs e)
3301  {
3302  CheckBatteryIgnitionState(this.globalRuntimeManager);
3303 
3304  SyncWithParent(this.creatorForm?.buttonCheckBatteryIgnition, this.buttonCheckBatteryIgnition);
3305  }
3306 
3307  private void textBoxPollingTime_TextChanged(object sender, EventArgs e)
3308  {
3309  try
3310  {
3311  if (Convert.ToInt32(this.textBoxPollingTime.Text) <= 0)
3312  {
3313  this.textBoxPollingTime.Text = defaultPollingTime.ToString();
3314  }
3315  }
3316  catch
3317  {
3318  this.textBoxPollingTime.Text = defaultPollingTime.ToString();
3319  }
3320 
3321  SyncWithParent(this.creatorForm?.textBoxPollingTime, this.textBoxPollingTime);
3322  }
3323 
3324  private void textBoxBatteryVoltageThreshold_TextChanged(object sender, EventArgs e)
3325  {
3326  try
3327  {
3328  if (Convert.ToInt32(this.textBoxVoltageThreshold.Text) <= 0)
3329  {
3330  this.textBoxVoltageThreshold.Text = defaultBatteryVoltageThreshold.ToString();
3331  }
3332  }
3333  catch
3334  {
3335  this.textBoxVoltageThreshold.Text = defaultBatteryVoltageThreshold.ToString();
3336  }
3337 
3338  SyncWithParent(this.creatorForm?.textBoxVoltageThreshold, this.textBoxVoltageThreshold);
3339  }
3340 
3341  private void CheckBatteryIgnitionState(IRuntimeManager runtimeManager)
3342  {
3343  if (this.useConnectionState)
3344  {
3345  runtimeManager.DiagConnectionState.PollingTime = Convert.ToInt32(this.textBoxPollingTime.Text);
3346  runtimeManager.DiagConnectionState.BatteryVoltageThreshold = Convert.ToInt32(this.textBoxVoltageThreshold.Text);
3347  ClampState batteryState = runtimeManager.DiagConnectionState.BatteryState;
3348  ClampState ignitionState = runtimeManager.DiagConnectionState.IgnitionState;
3349 
3350  SetBatteryIgnitionState(batteryState, ignitionState);
3351 
3352  PrintText("Check DiagConnection State - BatteryState = " + batteryState.ToString() + ", IgnitionState = " + ignitionState.ToString());
3353  }
3354  }
3355 
3356  private void SetBatteryIgnitionState(ClampState batteryState, ClampState ignitionState)
3357  {
3358  switch (batteryState)
3359  {
3360  case ClampState.NotAvailable:
3361  this.labelBatteryState.Image = OpenTestSystem.Otx.Runtime2.Api.DotNet.Sample.Properties.Resources.IconBatteryNotAvailable16;
3362  break;
3363  case ClampState.Off:
3364  this.labelBatteryState.Image = OpenTestSystem.Otx.Runtime2.Api.DotNet.Sample.Properties.Resources.IconBatteryOff16;
3365  break;
3366  case ClampState.On:
3367  this.labelBatteryState.Image = OpenTestSystem.Otx.Runtime2.Api.DotNet.Sample.Properties.Resources.IconBatteryOn16;
3368  break;
3369  }
3370 
3371  switch (ignitionState)
3372  {
3373  case ClampState.NotAvailable:
3374  this.labelIgnitionState.Image = OpenTestSystem.Otx.Runtime2.Api.DotNet.Sample.Properties.Resources.IconIgnitionNotAvailable16;
3375  break;
3376  case ClampState.Off:
3377  this.labelIgnitionState.Image = OpenTestSystem.Otx.Runtime2.Api.DotNet.Sample.Properties.Resources.IconIgnitionOff16;
3378  break;
3379  case ClampState.On:
3380  this.labelIgnitionState.Image = OpenTestSystem.Otx.Runtime2.Api.DotNet.Sample.Properties.Resources.IconIgnitionOn16;
3381  break;
3382  }
3383  }
3384 
3385  private void ShowConnectionStateMessage(IRuntimeContext runtimeContext)
3386  {
3387  if (runtimeContext != null)
3388  {
3389  System.Media.SystemSounds.Beep.Play();
3390 
3391  switch (this.expectedConnectionState)
3392  {
3393  case ExpectedState.BatteryOn:
3394  this.PrintText("----------------------------------------------------------------------------------------------------------------------------------------------");
3395  this.PrintText("-- Not expected connection state: Either it is not possible to communicate with the VCI or the battery is not connected. Please connect it! --");
3396  this.PrintText("----------------------------------------------------------------------------------------------------------------------------------------------");
3397  break;
3398  case ExpectedState.IgnitionOff:
3399  this.PrintText("------------------------------------------------------------------------------------");
3400  this.PrintText("-- Not expected connection state: The ignition must be OFF. Please switch it OFF! --");
3401  this.PrintText("------------------------------------------------------------------------------------");
3402  break;
3403  case ExpectedState.IgnitionOn:
3404  this.PrintText("----------------------------------------------------------------------------------");
3405  this.PrintText("-- Not expected connection state: The ignition must be ON. Please switch it ON! --");
3406  this.PrintText("----------------------------------------------------------------------------------");
3407  break;
3408  default:
3409  break;
3410  }
3411  }
3412  }
3413 
3414  private void checkBoxAdd2Output_CheckedChanged(object sender, EventArgs e)
3415  {
3416  SyncWithParent(this.creatorForm?.checkBoxAdd2Output, this.checkBoxAdd2Output);
3417  }
3418 
3419  private void ParentNumericUpDownSetValue(NumericUpDown numericUpDown, int value)
3420  {
3421  var action = new Action(() => numericUpDown.Value = value);
3422  if (numericUpDown.InvokeRequired)
3423  this.creatorForm.Invoke(action);
3424  else
3425  action.Invoke();
3426  }
3427 
3428  private void comboBoxTraceLevel_SelectedIndexChanged(object sender, EventArgs e)
3429  {
3430  SyncWithParent(this.creatorForm?.comboBoxTraceLevel, this.comboBoxTraceLevel);
3431  }
3432  private void txtPassword_TextChanged(object sender, EventArgs e)
3433  {
3434  SyncWithParent(this.creatorForm?.txtPassword, this.txtPassword);
3435  }
3436 
3437  private void textBoxTimeout_TextChanged(object sender, EventArgs e)
3438  {
3439  SyncWithParent(this.creatorForm?.textBoxTimeout, this.textBoxTimeout);
3440  }
3441 
3442  private void textBoxTraceFileMaxSize_TextChanged(object sender, EventArgs e)
3443  {
3444  if (globalRuntimeManager == null)
3445  return;
3446 
3447  PrintText("Set TraceFileMaxSize to " + this.textBoxTraceFileMaxSize.Text);
3448 
3449  try
3450  {
3451  RuntimeConfig.Instance.TraceFileMaxSize = Convert.ToInt32(this.textBoxTraceFileMaxSize.Text);
3452  SyncWithParent(this.creatorForm?.textBoxTraceFileMaxSize, this.textBoxTraceFileMaxSize);
3453  }
3454 
3455  catch (System.Exception ex)
3456  {
3457  PrintException(ex);
3458  this.textBoxTraceFileMaxSize.Text = RuntimeConfig.Instance.TraceFileMaxSize.ToString();
3459  }
3460  }
3461 
3462  private void textBoxTraceFileMaxCount_TextChanged(object sender, EventArgs e)
3463  {
3464  if (globalRuntimeManager == null)
3465  return;
3466 
3467  PrintText("Set TraceFileMaxCount to " + this.textBoxTraceFileMaxCount.Text);
3468 
3469  try
3470  {
3471  RuntimeConfig.Instance.TraceFileMaxCount = Convert.ToInt32(this.textBoxTraceFileMaxCount.Text);
3472  SyncWithParent(this.creatorForm?.textBoxTraceFileMaxCount, this.textBoxTraceFileMaxCount);
3473  }
3474  catch (System.Exception ex)
3475  {
3476  PrintException(ex);
3477  this.textBoxTraceFileMaxCount.Text = RuntimeConfig.Instance.TraceFileMaxCount.ToString();
3478  }
3479  }
3480 
3481  private void WebServerTimer_Tick(object sender, EventArgs e)
3482  {
3483  this.UpdateWebServerButton();
3484  }
3485 
3486  int isStartWebSever = -1;
3487  private void UpdateWebServerButton()
3488  {
3489  if (this.InvokeRequired)
3490  {
3491  this.Invoke(new Action(() => UpdateWebServerButton()));
3492  return;
3493  }
3494 
3495  if (customScreenImplementation == null)
3496  {
3497  this.buttonStartStopWebServer.Enabled = false;
3498  return;
3499  }
3500 
3501  this.buttonStartStopWebServer.Enabled = true;
3502  if (DefaultCustomScreenImplementation.IsStartedHtmlWebserver()) // Has Start WebServer
3503  {
3504  if (isStartWebSever == 1)
3505  {
3506  return;
3507  }
3508 
3509  this.buttonStartStopWebServer.Image = global::OpenTestSystem.Otx.Runtime2.Api.DotNet.Sample.Properties.Resources.StopWebServer;
3510  isStartWebSever = 1;
3511  }
3512  else
3513  {
3514  if (isStartWebSever == 0)
3515  {
3516  return;
3517  }
3518 
3519  this.buttonStartStopWebServer.Image = global::OpenTestSystem.Otx.Runtime2.Api.DotNet.Sample.Properties.Resources.StartWebServer;
3520  isStartWebSever = 0;
3521  }
3522  }
3523 
3524  private void buttonStartStopWebServer_Click(object sender, EventArgs e)
3525  {
3526  if (this.InvokeRequired)
3527  {
3528  this.Invoke(new Action(() => buttonStartStopWebServer_Click(sender, e)));
3529  return;
3530  }
3531 
3532  if (customScreenImplementation == null)
3533  {
3534  return;
3535  }
3536 
3537  if (!DefaultCustomScreenImplementation.IsStartedHtmlWebserver()) // No Start WebServer
3538  {
3539  customScreenImplementation.StartHtmlWebServer();
3540  }
3541  else
3542  {
3543  customScreenImplementation.StopHtmlWebServer();
3544  }
3545  }
3546 
3547  private void buttonLicense_Click(object sender, EventArgs e)
3548  {
3549  LicenseForm licenseForm = new LicenseForm(this);
3550  licenseForm.ShowDialog(this);
3551  }
3552 
3553  public void ReloadRuntimeManager()
3554  {
3555  try
3556  {
3557  String licenseKeyFromOTForOTPLicense = OpenTestSystem.Common.LicensingV5.Util.CreateLicenseKeyFromOTForOTPLicense();
3558  if (!String.IsNullOrEmpty(licenseKeyFromOTForOTPLicense) && LicensingUtil.CheckLicenseKeyFormat(licenseKeyFromOTForOTPLicense))
3559  {
3560  LicenseManager.SetLicenseKey(licenseKeyFromOTForOTPLicense);
3561  }
3562 
3563  this.globalRuntimeManager = CreateRuntimeManager();
3564 
3565  // TODO: update GUI if license is not valid
3566  }
3567  catch (System.Exception ex)
3568  {
3569  PrintException(ex);
3570  PrintText("No RuntimeManager created");
3571  }
3572  }
3573  }
3574 }
Namespace containing all interfaces for custom implementations
Definition: IBasicScreenImplementation.cs:7
Namespace which contains all supported data types
Definition: BlackBox.cs:5
Namespace containing exceptions
Definition: ConnectionStateException.cs:7
Namespace containing all objects related to licensing
Definition: IpcLicenseCheckerBase.cs:10
Namespace for browsing at OTX data structure.
Definition: IContextVariable.cs:5
Namespace containing main entries: IProject and IPlayerProject.
Definition: IPlayerProject.cs:5
Namespace containing the programming interface for browsing and execution of OTX procedures in own ap...
Definition: ApiConstants.cs:5
ExecutionStateChangeReason
Reason, why a procedure execution state was changed, e.g. Paused or Running
Definition: ExecutionStateChangeReason.cs:13
ClampState
Contains the state of a clamp
Definition: ClampState.cs:13
ExecutionState
Contains the state of the runtime context.
Definition: ExecutionState.cs:7
ExpectedState
Contains the expected state of the diagnostic connection
Definition: ExpectedState.cs:13
Namespace containing all objects for browsing and execution of OTX procedures
Definition: ApiConstants.cs:5
Namespace containing all objects which are standardized according to ISO 13209 (OTX)
Namespace containing all objects related to testing inside automotive industry