Ejemplo n.º 1
0
        private bool Receive2Processed(string scriptName, ref utils.testFrames tf)
        {
            if (scriptName.IndexOf("js") < 0)
            {
                tf.processed = tf.raw;
            }
            else
            {
                utils.ScriptAnswer sa = new utils.ScriptAnswer();


                try
                {
                    sa = utils.callScript(scriptName, "", tf.raw, 0);

                    if (!String.IsNullOrWhiteSpace(sa.error))
                    {
                        MessageBox.Show($"O script retornou o seguinte erro: {sa.error}");
                        scriptRunning = false;
                        this.screenText.Dispatcher.Invoke((Action)(() => updateScreenButtons(state.connected)));
                        return(true);
                    }
                    else
                    {
                        tf.processed = sa.processed_frame;
                    }
                }
                catch (Exception exce)
                {
                    MessageBox.Show(exce.Message, "Error");
                    return(true);
                }
            }
            return(false);
        }
Ejemplo n.º 2
0
        private void RunScript(List <utils.command> command_list, int timeOut)
        {
            List <utils.test_results> _test = new List <utils.test_results>();

            utils.test_results commandOnTest = new utils.test_results();
            for (int i = 0; i < command_list.Count; i++)
            {
                if (!scriptRunning)
                {
                    return;
                }

                receive = "";

                utils.command c = command_list[i];
                htmlTextOutput.Add($"<p><b>[{i}] - {c.name}</b></p>");

                for (int j = 0; j < replacer.Count; j++)
                {
                    c.send    = c.send.ToUpper().Replace($"#{tags[j]}#", utils.getFrameFormat(replacer[j], false));
                    c.receive = c.receive.ToUpper().Replace($"#{tags[j]}#", utils.getFrameFormat(replacer[j], false));
                }
                commandOnTest.sent.raw       = c.send;
                commandOnTest.expectedAnswer = c.receive;
                if (c.send_script == null)
                {
                    c.send_script = "";
                }

                if (c.send_script.ToLower().IndexOf("js") < 0)
                {
                    if (commandOnTest.sent.raw.IndexOf("#CS#") > 0)
                    {
                        commandOnTest.sent.raw = commandOnTest.sent.raw
                                                 .Replace("#CS#",
                                                          c.checksum.calcCRC(commandOnTest.sent.raw.Substring
                                                                                 (c.checksum.from, commandOnTest.sent.raw.IndexOf("#CS#") - c.checksum.from)));
                    }
                    commandOnTest.sent.processed = commandOnTest.sent.raw;
                    byte[] b = utils.str2Hex(commandOnTest.sent.processed);
                    receive = "";
                    s0.Write(b, 0, b.Length);
                    this.screenText.Dispatcher.Invoke((Action)(() => write2Screen(commandOnTest.sent.processed)));
                    Thread.Sleep(timeOut);
                    commandOnTest.realAnswer.raw = receive;
                    if (c.receive_script == null)
                    {
                        c.receive_script = "";
                    }
                    if (Receive2Processed(c.receive_script, ref commandOnTest.realAnswer))
                    {
                        return;
                    }
                    if (!String.IsNullOrWhiteSpace(commandOnTest.realAnswer.processed))
                    {
                        this.Dispatcher.Invoke((Action)(() => read2Screen(commandOnTest.realAnswer.processed)));
                    }
                }
                else
                {
                    utils.ScriptAnswer sa = new utils.ScriptAnswer();
                    sa.newRound = true;
                    int round = 0;
                    commandOnTest.realAnswer.raw = "";
                    while (sa.newRound)
                    {
                        if (!scriptRunning)
                        {
                            return;
                        }

                        sa = utils.callScript(c.send_script, commandOnTest.sent.raw, commandOnTest.realAnswer.raw, round);

                        if (!String.IsNullOrWhiteSpace(sa.error))
                        {
                            MessageBox.Show($"{sa.error}", "Error ao executar o script", MessageBoxButton.OK, MessageBoxImage.Error);
                            scriptRunning = false;
                            this.screenText.Dispatcher.Invoke((Action)(() => updateScreenButtons(state.connected)));
                            return;
                        }
                        else
                        {
                            commandOnTest.sent.processed = sa.processed_frame;
                            if (!String.IsNullOrWhiteSpace(commandOnTest.sent.processed))
                            {
                                if (commandOnTest.sent.processed.IndexOf("#CS#") > 0)
                                {
                                    commandOnTest.sent.processed = commandOnTest.sent.processed
                                                                   .Replace("#CS#",
                                                                            c.checksum.calcCRC(commandOnTest.sent.processed.Substring
                                                                                                   (c.checksum.from, commandOnTest.sent.processed.IndexOf("#CS#") - c.checksum.from)));
                                }
                                byte[] b = utils.str2Hex(commandOnTest.sent.processed);
                                receive = "";
                                s0.Write(b, 0, b.Length);
                                this.screenText.Dispatcher.Invoke((Action)(() => write2Screen(commandOnTest.sent.processed)));
                                Thread.Sleep(timeOut);
                                commandOnTest.realAnswer.raw = receive;
                                if (c.receive_script == null)
                                {
                                    c.receive_script = "";
                                }
                                if (Receive2Processed(c.receive_script, ref commandOnTest.realAnswer))
                                {
                                    return;
                                }
                                if (!String.IsNullOrWhiteSpace(commandOnTest.realAnswer.processed))
                                {
                                    this.Dispatcher.Invoke((Action)(() => read2Screen(commandOnTest.realAnswer.processed)));
                                }
                                round++;
                            }
                        }
                    }
                }
                try
                {
                    commandOnTest.expectedAnswer = commandOnTest.expectedAnswer.Replace(" ", "");
                    string ans = "";
                    if (commandOnTest.realAnswer.processed != null)
                    {
                        ans = commandOnTest.realAnswer.processed.Replace(" ", "");
                    }
                    for (int ch = 0; ch < commandOnTest.expectedAnswer.Length; ch++)
                    {
                        if (commandOnTest.expectedAnswer[ch] == 'X')
                        {
                            char[] array = commandOnTest.expectedAnswer.ToCharArray();
                            array[ch] = ans[ch];
                            commandOnTest.expectedAnswer = new string(array);
                        }
                    }
                    if (commandOnTest.expectedAnswer.IndexOf("#CS#") > 0)
                    {
                        commandOnTest.expectedAnswer = commandOnTest.expectedAnswer
                                                       .Replace("#CS#",
                                                                c.checksum.calcCRC(
                                                                    commandOnTest.expectedAnswer.Substring(c.checksum.from,
                                                                                                           commandOnTest.expectedAnswer.IndexOf("#CS#") - c.checksum.from))).ToUpper();
                        Console.WriteLine(commandOnTest.expectedAnswer.Length);
                    }
                    commandOnTest.evaluation = commandOnTest.expectedAnswer.Replace(" ", "").Equals(ans);
                }
                catch
                {
                    commandOnTest.evaluation = false;
                }

                if (!String.IsNullOrWhiteSpace(commandOnTest.expectedAnswer))
                {
                    total_evaluation &= commandOnTest.evaluation;
                }

                _test.Add(commandOnTest);

                this.Dispatcher.Invoke((Action)(() =>
                {
                    if (total_evaluation)
                    {
                        this.partial_result.Background = Brushes.Green;
                    }
                    else
                    {
                        this.partial_result.Background = Brushes.Red;
                    }
                }));
            }

            scriptRunning = false;
            htmlTextOutput.Add("<br><br><h3>Análise automática dos resultados:</h3>");
            htmlTextOutput.Add("<table  class=\"table table-striped\">");
            htmlTextOutput.Add("<tr>");
            htmlTextOutput.Add($"<th>ID</th>");
            htmlTextOutput.Add($"<th>Resultado</th>");
            htmlTextOutput.Add("</tr>");
            int id = 0;

            foreach (utils.test_results r in _test)
            {
                string eval = "";
                if (r.expectedAnswer == "")
                {
                    eval = "Análise manual";
                }
                else
                {
                    eval = r.evaluation ? "Aprovado" : "Reprovado";
                }

                htmlTextOutput.Add("<tr>");
                htmlTextOutput.Add($"<td>{id}</td>");
                htmlTextOutput.Add($"<td>{eval}</td>");
                htmlTextOutput.Add("</tr>");
                id++;
            }
            htmlTextOutput.Add("</table>");
            this.screenText.Dispatcher.Invoke((Action)(() => updateScreenButtons(state.connected)));
        }