コード例 #1
0
        private void Start()
        {
            Abort = false;

            ClearPassFailResultColumn();

            dataGridView1[(int)ColText.PASSFAIL, 0].Value = "Starting...";
            dataGridView1.Refresh();

            if (Tm.TestClass is IPowerSupply)
            {
                (Tm.TestClass as IPowerSupply).SetSupplyState(false);
                Thread.Sleep(750);
            }

            new Thread(() =>
            {
                try
                {
                    Thread.CurrentThread.Name = "Test Runner";
                    Log.WriteLine(LogType.General, "Test thread started.");

                    if (Form1.AppSettings.UseDb)
                    {
                        Db.OpenExisting(Form1.AppSettings.DbConnectString);
                        Log.WriteLine(LogType.General, "Database opened");
                    }

                    HtmlWriter html = new HtmlWriter(ReportDirectory);

                    bool allPassed   = true;
                    string opMessage = "";

                    // Tests that run together (eg left and right channels) are part of the same test group
                    Guid testGroup = Guid.NewGuid();

                    for (int i = 0; i < Form1.AppSettings.TestList.Count; i++)
                    {
                        dataGridView1[(int)ColText.PASSFAIL, i].Style.BackColor = Color.White;
                    }

                    Tm.LocalStash = new Dictionary <string, string>();

                    for (int i = 0; i < Form1.AppSettings.TestList.Count; i++)
                    {
                        if (Abort)
                        {
                            dataGridView1.Invoke((MethodInvoker) delegate { dataGridView1[(int)ColText.PASSFAIL, i].Value = "Aborted..."; });
                            allPassed = false;
                            break;
                        }
                        else
                        {
                            dataGridView1.Invoke((MethodInvoker) delegate { dataGridView1[(int)ColText.PASSFAIL, i].Value = "Running..."; });
                        }

                        dataGridView1.Invoke((MethodInvoker) delegate
                        {
                            dataGridView1[(int)ColText.TARGET, i].Value   = Form1.AppSettings.TestList[i].GetTestLimits();
                            dataGridView1.FirstDisplayedScrollingRowIndex = i > 2 ? i - 2 : 0;
                        });

                        while (Pause)
                        {
                            Thread.Sleep(500);
                        }

                        TestResult tr = null;

                        if (Form1.AppSettings.TestList[i] is AudioTestBase)
                        {
                            for (int j = 0; j < ((AudioTestBase)Form1.AppSettings.TestList[i]).RetryCount; j++)
                            {
                                if (j > 0)
                                {
                                    dataGridView1.Invoke((MethodInvoker) delegate
                                    {
                                        dataGridView1[(int)ColText.PASSFAIL, i].Value = "Retry: " + j.ToString();
                                    });
                                }

                                Form1.AppSettings.TestList[i].DoTest(Form1.AppSettings.TestList[i].Name, out tr);

                                if (tr.Pass)
                                {
                                    break;
                                }
                            }
                        }
                        else
                        {
                            Form1.AppSettings.TestList[i].DoTest(Form1.AppSettings.TestList[i].Name, out tr);
                        }

                        if (Form1.AppSettings.AbortOnFailure && (tr.Pass == false))
                        {
                            Abort = true;
                        }

                        if (tr.Pass == false)
                        {
                            allPassed = false;
                        }

                        // any test can update the operator message. But only the last test that updates
                        // the operator message will have its result shown.
                        if (tr.OperatorMessage != "")
                        {
                            opMessage = tr.OperatorMessage;
                        }

                        dataGridView1.Invoke((MethodInvoker) delegate
                        {
                            dataGridView1[(int)ColText.L, i].Value                  = tr.StringValue[0];
                            dataGridView1[(int)ColText.R, i].Value                  = tr.StringValue[1];
                            dataGridView1[(int)ColText.PASSFAIL, i].Value           = tr.Pass ? "PASS" : "FAIL";
                            dataGridView1[(int)ColText.PASSFAIL, i].Style.BackColor = tr.Pass ? Color.Green : Color.Red;
                            dataGridView1.Refresh();
                        });

                        // Delineate new tests
                        if (i == 0)
                        {
                            html.AddParagraph("================================================");
                            html.AddHeading2(string.Format("{0} {1}", DateTime.Now.ToShortDateString(), DateTime.Now.ToShortTimeString()));
                        }

                        // If new test AND first test is serial number, then print it larger
                        if (i == 0 && (Form1.AppSettings.TestList[0] is IdInputA00))
                        {
                            html.AddHeading2(string.Format("Device SN: {0}", tr.StringValue[0]));
                        }

                        html.AddParagraph(string.Format("<B>Test Name:</B> {0}  <B>Result L:</B> {1}   <B>Result R:</B> {2} <B>P/F</B>: {3} <B>Image:</B> {4}",
                                                        Form1.AppSettings.TestList[i].Name,
                                                        tr.StringValue[0],
                                                        tr.StringValue[1],
                                                        tr.Pass ? "PASS" : "<mark>FAIL</mark>",
                                                        Form1.AppSettings.TestList[i].TestResultBitmap == null ? "[No Image]" : html.ImageLink("Screen", Form1.AppSettings.TestList[i].TestResultBitmap)
                                                        ));

                        if (Form1.AppSettings.UseCsvLog)
                        {
                            string fileName = Path.Combine(Constants.CsvLogsPath, Form1.AppSettings.CsvFileName);

                            if (File.Exists(fileName) == false)
                            {
                                try
                                {
                                    Log.WriteLine(LogType.General, "CSV Log file created: " + fileName);
                                    File.AppendAllText(fileName, GetCsvHeader());
                                }
                                catch (Exception ex)
                                {
                                    Log.WriteLine(LogType.Error, "CSV Log File could not be created. " + ex.Message);
                                    throw new InvalidOperationException("Unabled to write to CSV log file. Disable CSV logging to bypass.");
                                }
                            }

                            if (Form1.AppSettings.TestList[i] is AudioTestBase)
                            {
                                if (((AudioTestBase)Form1.AppSettings.TestList[i]).LeftChannel)
                                {
                                    SubmitToCsv(fileName, testGroup, 0, Form1.AppSettings.TestList[i], tr);
                                }
                                if (((AudioTestBase)Form1.AppSettings.TestList[i]).RightChannel)
                                {
                                    SubmitToCsv(fileName, testGroup, 1, Form1.AppSettings.TestList[i], tr);
                                }
                            }
                            else
                            {
                                SubmitToCsv(fileName, testGroup, 0, Form1.AppSettings.TestList[i], tr);
                            }
                        }

                        if (Form1.AppSettings.UseAuditDb)
                        {
                            if (Form1.AppSettings.TestList[i] is AudioTestBase)
                            {
                                if (((AudioTestBase)Form1.AppSettings.TestList[i]).LeftChannel)
                                {
                                    SubmitToAuditDb(testGroup, 0, Form1.AppSettings.TestList[i], tr);
                                }
                                if (((AudioTestBase)Form1.AppSettings.TestList[i]).RightChannel)
                                {
                                    SubmitToAuditDb(testGroup, 1, Form1.AppSettings.TestList[i], tr);
                                }
                            }
                            else
                            {
                                SubmitToAuditDb(testGroup, 0, Form1.AppSettings.TestList[i], tr);
                            }
                        }

                        // Add to database if needed
                        if (Form1.AppSettings.UseDb)
                        {
                            if (Form1.AppSettings.TestList[i] is AudioTestBase)
                            {
                                // Left channel
                                if (((AudioTestBase)Form1.AppSettings.TestList[i]).LeftChannel)
                                {
                                    SubmitToDb(testGroup, 0, Form1.AppSettings.TestList[i], tr);
                                }
                                if (((AudioTestBase)Form1.AppSettings.TestList[i]).RightChannel)
                                {
                                    SubmitToDb(testGroup, 1, Form1.AppSettings.TestList[i], tr);
                                }
                            }
                            else
                            {
                                SubmitToDb(testGroup, 0, Form1.AppSettings.TestList[i], tr);
                            }
                        }

                        if (IsConnected() == false)
                        {
                            Log.WriteLine(LogType.Error, "Equipment is no longer connected. Testing will now stop.");
                            dataGridView1[(int)ColText.PASSFAIL, i].Value = "ERROR";
                            throw new InvalidOperationException("Equipment is no longer connected. Testing will now stop.");
                        }
                    }
                    TimeSpan ts = DateTime.Now.Subtract(TestStartTime);
                    html.AddParagraph(string.Format("Elapsed Time: {0:N1} sec", ts.TotalSeconds));

                    html.Render();
                    this.Invoke(((MethodInvoker) delegate { TestPassFinished(allPassed, opMessage); }));
                }
                catch (Exception ex)
                {
                    Log.WriteLine(LogType.Error, ex.Message);
                    this.Invoke(((MethodInvoker) delegate { TestPassFinished(false, "See Log"); }));
                }

                if (Tm.TestClass is IPowerSupply)
                {
                    (Tm.TestClass as IPowerSupply).SetSupplyState(false);
                }
            }).Start();
        }