Пример #1
0
        private void resetCounts(Socket clientLabVIEW)
        {
            List <ConsoleCommand> Commands = BlackfinConsoleCommands.GetAllConsoleCommands();

            // sort commands by command name (don't take get/set prefix into account)
            Commands.Sort((o1, o2) =>
            {
                return(o1.getName().Substring(3).CompareTo(o2.getName().Substring(3)));
            });

            Console.WriteLine("########################");
            Console.WriteLine("Inicio de envio de comandos al aparato del infierno.");
            Console.WriteLine("########################");
            Console.WriteLine();

            // Console.WriteLine("Please enter a command to execute (or 'help'):");
            // String userString = Console.ReadLine().ToLower();
            String userString = "resetcurrentcount";

            //String userString = "getlastncounts";
            //String userString = "getcounts";

            Console.WriteLine("Comando: ");
            Console.WriteLine(userString + "\n");

            if (userString.Equals("help"))
            {
                Console.WriteLine("Help - Prints this list of available commands");
                Console.WriteLine("Disconnect - Disconnects from the current device");
                foreach (ConsoleCommand c in Commands)
                {
                    Console.WriteLine(c.getName() + " - " + c.getHelp());
                }
            }
            else
            {
                // try and execute a command
                bool executed    = false;
                bool bDisconnect = false;
                foreach (ConsoleCommand c in Commands)
                {
                    try
                    {
                        if (c.getName().ToLower().Equals(userString))
                        {
                            executed = true;

                            // assemble any required arguments
                            String[] args = new String[c.getArguments().Length];

                            int i = 0;
                            foreach (String s in c.getArguments())
                            {
                                Console.WriteLine("Please enter a value for " + s + ":");
                                //args[i++] = Console.ReadLine();
                                args[i++] = "y";
                            }

                            // execute command
                            string result = c.execute(blackfin, args);
                            {
                                if (result == null)
                                {
                                    Console.WriteLine("There was a problem executing the command");
                                }
                                else
                                {
                                    Console.Write("Respuesta :\n" + result + "\n");
                                    Console.Write("OK! ");

                                    try
                                    {
                                        string size = result.Length.ToString().PadLeft(10, '0');
                                        Console.WriteLine("hecho!");
                                        Console.WriteLine();
                                    }
                                    catch (Exception e)
                                    {
                                        Console.WriteLine("Error!");
                                        Console.WriteLine(e.Message);
                                        Console.WriteLine("Program now exits ...");
                                        Environment.Exit(0);
                                    }
                                }

                                break;
                            }
                        }
                    }
                    catch (CommandArgumentException e)
                    {
                        Console.WriteLine("Problem with command argument: " + e.Message);
                    }
                    catch (ConnectionResetException e)
                    {
                        Console.WriteLine(e.Message);
                        Console.WriteLine("");
                        Console.WriteLine("This command has caused the connection to be reset");
                        bDisconnect = true;
                    }
                }
                if (!executed)
                {
                    Console.WriteLine("No command was found matching: " + userString);
                }

                // wait for a specific time
                Console.WriteLine("#################################################");
                Console.WriteLine("Fin de la ejecucion ...");
                Console.WriteLine("#################################################");
                Console.WriteLine();

                Thread.Sleep(4000);
            }
        }
Пример #2
0
        /// <summary>
        /// Prompts the user to input a command
        /// and then executes it with appropriate arguments
        /// </summary>
        private void DoConsoleCommandLoop(Socket clientLabVIEW)
        {
            List <ConsoleCommand> Commands = BlackfinConsoleCommands.GetAllConsoleCommands();

            // sort commands by command name (don't take get/set prefix into account)
            Commands.Sort((o1, o2) =>
            {
                return(o1.getName().Substring(3).CompareTo(o2.getName().Substring(3)));
            });

            Console.WriteLine("########################");
            Console.WriteLine("Inicio de envio de comandos al aparato del infierno.");
            Console.WriteLine("########################");
            Console.WriteLine();

            int i = 0;

            for (i = 0; i < 3; i++)
            {
                // Console.WriteLine("Please enter a command to execute (or 'help'):");
                // String userString = Console.ReadLine().ToLower();
                //String userString = "getcurrentcount";
                String userString = "getlastncounts";
                //String userString = "getcounts";

                Console.WriteLine("Comando: ");
                Console.WriteLine(userString + "\n");

                if (userString.Equals("help"))
                {
                    Console.WriteLine("Help - Prints this list of available commands");
                    Console.WriteLine("Disconnect - Disconnects from the current device");
                    foreach (ConsoleCommand c in Commands)
                    {
                        Console.WriteLine(c.getName() + " - " + c.getHelp());
                    }
                }
                else
                {
                    // try and execute a command
                    bool executed    = false;
                    bool bDisconnect = false;
                    foreach (ConsoleCommand c in Commands)
                    {
                        try
                        {
                            if (c.getName().ToLower().Equals(userString))
                            {
                                executed = true;

                                // assemble any required arguments
                                String[] args = new String[c.getArguments().Length];
                                args[0] = "30";
                                //args[0] = DateTime.Now.AddMinutes(-20).ToString();
                                //args[1] = DateTime.Now.ToString();

                                //int i = 0;
                                //foreach (String s in c.getArguments())
                                //{
                                //    //Console.WriteLine("Please enter a value for " + s + ":");
                                //    //args[i++] = Console.ReadLine();

                                //}

                                // execute command
                                string result = c.execute(blackfin, args);
                                {
                                    if (result == null)
                                    {
                                        Console.WriteLine("There was a problem executing the command");
                                    }
                                    else
                                    {
                                        int entradas        = 0;
                                        int salidas         = 0;
                                        int entradas_viejas = 0;
                                        int salidas_viejas  = 0;
                                        int entradas_nuevas = 0;
                                        int salidas_nuevas  = 0;
                                        Console.Write("Respuesta :\n" + result + "\n");
                                        string[] resultados = result.Split('\n'); // Corto por cada renglon

                                        //obtengo los resultados del minuto 20:
                                        string[] datos = resultados[0].Split(' '); // Corto por cada espacio
                                        entradas_viejas = int.Parse(datos[2]);
                                        salidas_viejas  = int.Parse(datos[3]);
                                        //obtengo el resultado mas nuevo
                                        datos           = resultados[29].Split(' '); // Corto por cada espacio
                                        entradas_nuevas = int.Parse(datos[2]);
                                        salidas_nuevas  = int.Parse(datos[3]);

                                        // si los resultados mas viejos son mayores a los nuevos no los tengo en cuenta proque se reseteo hace poco
                                        if (entradas_viejas > entradas_nuevas)
                                        {
                                            entradas = entradas_nuevas;
                                            salidas  = salidas_nuevas;
                                        }
                                        else
                                        {
                                            entradas = entradas_nuevas - entradas_viejas;
                                            salidas  = salidas_nuevas - salidas_viejas;
                                        }

                                        //foreach (string s in resultados)
                                        //{
                                        //    try
                                        //    {
                                        //        string[] datos = s.Split(' '); // Corto por cada espacio
                                        //        entradas = int.Parse(datos[2]) - entradas;
                                        //        salidas = int.Parse(datos[3]) - salidas;
                                        //    }
                                        //    catch
                                        //    {
                                        //        // do nothing
                                        //    }
                                        //}

                                        Bathroom b = new Bathroom();
                                        b.entradas = entradas.ToString();
                                        b.salidas  = salidas.ToString();

                                        Console.WriteLine("Entradas: " + entradas);
                                        Console.WriteLine("Salidas: " + salidas);
                                        Console.WriteLine("Gente dentro: " + (entradas - salidas));

                                        using (StreamWriter file = File.CreateText(@"./web/counter.json"))
                                        {
                                            JsonSerializer serializer = new JsonSerializer();
                                            //serialize object directly into file stream
                                            serializer.Serialize(file, b);
                                        }

                                        Console.Write("OK! ");

                                        try
                                        {
                                            string size = result.Length.ToString().PadLeft(10, '0');

                                            Console.WriteLine("hecho!");
                                            Console.WriteLine();
                                        }
                                        catch (Exception e)
                                        {
                                            Console.WriteLine("Error!");
                                            Console.WriteLine(e.Message);
                                            Console.WriteLine("Program now exits ...");
                                            Environment.Exit(0);
                                        }
                                    }

                                    break;
                                }
                            }
                        }
                        catch (CommandArgumentException e)
                        {
                            Console.WriteLine("Problem with command argument: " + e.Message);
                        }
                        catch (ConnectionResetException e)
                        {
                            Console.WriteLine(e.Message);
                            Console.WriteLine("");
                            Console.WriteLine("This command has caused the connection to be reset");
                            bDisconnect = true;
                        }
                    }
                    if (!executed)
                    {
                        Console.WriteLine("No command was found matching: " + userString);
                    }

                    //if (bDisconnect)
                    //    break;
                }

                // wait for a specific time
                Console.WriteLine("#################################################");
                Console.WriteLine("Esperando 4 segundos para la proxima ejecucion ...");
                Console.WriteLine("#################################################");
                Console.WriteLine();

                Thread.Sleep(4000); // espero 30 segundos para la proxima ejecucion.
            }
        }
Пример #3
0
        /// <summary>
        /// Prompts the user to input a command
        /// and then executes it with appropriate arguments
        /// </summary>
        private void DoConsoleCommandLoop(Socket clientLabVIEW)
        {
            List <ConsoleCommand> Commands = BlackfinConsoleCommands.GetAllConsoleCommands();

            // sort commands by command name (don't take get/set prefix into account)
            Commands.Sort((o1, o2) =>
            {
                return(o1.getName().Substring(3).CompareTo(o2.getName().Substring(3)));
            });

            Console.WriteLine("########################");
            Console.WriteLine("Start sending commands.");
            Console.WriteLine("########################");
            Console.WriteLine();

            while (!isEnding)
            {
                // Console.WriteLine("Please enter a command to execute (or 'help'):");
                // String userString = Console.ReadLine().ToLower();
                String userString = "getcurrentcount";
                Console.WriteLine("command: ");
                Console.WriteLine(userString + "\n");

                if (userString.Equals("help"))
                {
                    Console.WriteLine("Help - Prints this list of available commands");
                    Console.WriteLine("Disconnect - Disconnects from the current device");
                    foreach (ConsoleCommand c in Commands)
                    {
                        Console.WriteLine(c.getName() + " - " + c.getHelp());
                    }
                }
                else if (userString.Equals("disconnect"))
                {
                    break;
                }
                else
                {
                    // try and execute a command
                    bool executed    = false;
                    bool bDisconnect = false;
                    foreach (ConsoleCommand c in Commands)
                    {
                        try
                        {
                            if (c.getName().ToLower().Equals(userString))
                            {
                                executed = true;

                                // assemble any required arguments
                                String[] args = new String[c.getArguments().Length];
                                int      i    = 0;

                                foreach (String s in c.getArguments())
                                {
                                    Console.WriteLine("Please enter a value for " + s + ":");
                                    args[i++] = Console.ReadLine();
                                }

                                // execute command
                                string result = c.execute(blackfin, args);

                                if (result == null)
                                {
                                    Console.WriteLine("There was a problem executing the command");
                                }
                                else
                                {
                                    Console.Write("response:\n" + result + "\n");
                                    Console.Write("sending to LabVIEW: ");

                                    try
                                    {
                                        string size = result.Length.ToString().PadLeft(10, '0');

                                        // first send the size of result
                                        clientLabVIEW.Send(Encoding.ASCII.GetBytes(size));

                                        // then send the result itself
                                        clientLabVIEW.Send(Encoding.ASCII.GetBytes(result));

                                        Console.WriteLine("done!");
                                        Console.WriteLine();
                                    }
                                    catch (Exception e)
                                    {
                                        Console.WriteLine("Error!");
                                        Console.WriteLine(e.Message);
                                        Console.WriteLine("Program now exits ...");
                                        Environment.Exit(0);
                                    }
                                }

                                break;
                            }
                        }
                        catch (CommandArgumentException e)
                        {
                            Console.WriteLine("Problem with command argument: " + e.Message);
                        }
                        catch (ConnectionResetException e)
                        {
                            Console.WriteLine(e.Message);
                            Console.WriteLine("");
                            Console.WriteLine("This command has caused the connection to be reset");
                            bDisconnect = true;
                        }
                    }
                    if (!executed)
                    {
                        Console.WriteLine("No command was found matching: " + userString);
                    }

                    if (bDisconnect)
                    {
                        break;
                    }
                }

                // wait for a specific time
                Console.WriteLine("#################################################");
                Console.WriteLine("Waiting for 4 seconds to get the next sample ...");
                Console.WriteLine("#################################################");
                Console.WriteLine();

                Thread.Sleep(4000);
            }
        }