Ejemplo n.º 1
0
        //Basic DMX command TODO: Implement Substring to cut out the 'DMX ' for easyer usage
        static void command_DMX(string input)
        {
            string[]    dmxCommand;                      //Stores the elements of the command
            List <int>  dmxChannelL = new List <int>();  //List of the DMX channels
            List <byte> dmxValueL   = new List <byte>(); //List of the DMX values
            int         channel;                         //The channelof a subcommand
            byte        value;                           //The value of a subcommand

            dmxCommand = input.Split();                  //Split the command into its elements
            //Console.WriteLine("DMX Command length: {0}, Invalid-Length: {1}", dmxCommand.Length, (dmxCommand.Length%2 == 0));

            //DMX Command basic error checking
            if ((dmxCommand.Length % 2) == 0)
            {
                if (verbose)
                {
                    Console.WriteLine("[ERROR] - Modulo exception");
                }
                throw new Exception();
            }

            //Extract Channel-Value pairs from DMX command, check them and add them to a list
            for (int i = 1; i < ((dmxCommand.Length - 1) / 2) + 1; i++)
            {
                channel = Int16.Parse(dmxCommand[i * 2 - 1]); //Channel
                value   = byte.Parse(dmxCommand[i * 2]);      //Value

                //Check if channel is blocked
                if (blockedChannels.Contains(channel))
                {
                    if (verbose)
                    {
                        Console.WriteLine("[DEBUG] - Channel {0} is blocked atm", channel);
                    }
                    continue;
                }

                //Console.WriteLine("{0} -> Val1: {1}, Val2: {2}", i, val1, val2);
                if (channel < 0 || channel > 513 || value < 0 || value > 255)
                {
                    throw new Exception();
                }
                dmxChannelL.Add(channel);
                dmxValueL.Add(value);
            }

            //Itterate over list and set the channels->values
            //We do this in a seperate loop to ensure that the whole DMXCommand was valid (1 fails ->all fail)
            for (int i = 0; i < dmxChannelL.Count; i++)
            {
                if (verbose)
                {
                    Console.WriteLine("[DEBUG] - Setting: {0} -> {1}", dmxChannelL[i], dmxValueL[i]);
                }
                OpenDMX.setDmxValue(dmxChannelL[i], dmxValueL[i]);
            }
        }
Ejemplo n.º 2
0
        public static Dictionary <string, List <int> > activeEffects = new Dictionary <string, List <int> >(); //Dict of all active effects and the channels they block

        //Main method the programm launches into
        public static void Main(string[] args)
        {
            //Commandline arguments
            int    nPos = -1;
            string argsT;

            try {
                for (int i = 0; i < args.Length; i++)
                {
                    argsT = args[i];

                    switch (argsT)
                    {
                    case "-n":
                        nPos = i + 1;
                        break;

                    case "-v":
                        verbose = true;
                        break;

                    case "-s":
                        stayUp = true;
                        break;

                    case "-h":
                        show_help = true;
                        break;
                    }
                }

                //Basic error checking in pipeName (-n -s) -> -s would be the pipeName
                if (nPos != -1)
                {
                    pipeName = args[nPos];
                    if (pipeName.Contains("-"))
                    {
                        throw new Exception();
                    }
                }
                else if (!show_help)
                {
                    throw new Exception();
                }
            } catch {
                Console.WriteLine("Invalid commandline arguments");
                Console.WriteLine("Try `DMXServer -h' for more information.");
                return;
            }
            if (show_help)
            {
                ShowHelp();
                return;
            }

            if (verbose)
            {
                Console.WriteLine("Name: {0}, V: {1}, S: {2}, H: {3}", pipeName, verbose, stayUp, show_help);
            }

            //DMXServer
            if (verbose)
            {
                Console.WriteLine("[DEBUG] - DMXServer started");
            }
            OpenDMX.start(); //Starting the OpenDMX interface
            if (verbose)
            {
                Console.WriteLine("[DEBUG] - DMXLink opened");
            }
            do
            {
                //Define namedPipe
                using (NamedPipeClientStream pipeClient =
                           new NamedPipeClientStream(".", pipeName, PipeDirection.In)) {
                    // Connect to the pipe or wait until the pipe is available.
                    if (verbose)
                    {
                        Console.Write("[DEBUG] - Waiting for connection to DMXClient...");
                    }
                    pipeClient.Connect();

                    if (verbose)
                    {
                        Console.WriteLine("Connected");
                    }
                    //if (verbose) Console.WriteLine("{0} DMXClients connected at the moment", pipeClient.NumberOfServerInstances); TODO: Work in progress
                    using (StreamReader sr = new StreamReader(pipeClient)) {
                        //variables used for command handling
                        string input;     //Holds the string recieved over the named pipe

                        //Command handling
                        while ((input = sr.ReadLine()) != null)
                        {
                            try {                             //Errors indicate malformed command
                                if (input.StartsWith("DMX ")) // 'DMX ...'
                                {
                                    command_DMX(input);
                                }
                                else if (input.StartsWith("EFFECT "))         // 'EFFECT ...'
                                {
                                    command_EFFECT(input);
                                }
                                else
                                {
                                    if (verbose)
                                    {
                                        Console.WriteLine("[ERROR] - Command not supported: {0}", input);
                                    }
                                }
                            } catch {
                                if (verbose)
                                {
                                    Console.WriteLine("[ERROR] - Malformed DMXCommand: {0}", input);
                                }
                            }
                        }
                    }
                    if (verbose)
                    {
                        Console.WriteLine("[DEBUG] - Connection to DMXClient lost...");
                    }
                }
            } while (stayUp);
            if (verbose)
            {
                Console.WriteLine("[DEBUG] - Closing...");
            }
            Thread.Sleep(500); //Sleep so that the last received command can still be executed from the worker thread
            OpenDMX.stop();
            Environment.Exit(Environment.ExitCode);
        }