Example #1
0
        /// <summary>
        /// Executes the action defined by the VT100/ANSI escape sequence
        /// </summary>
        private void EscapeSeqAction(byte[] payload)
        {
            //Debug.WriteLine(string.Format("Payload: {0} Data: {1}",
            //    SharedFunc.ByteArrayToHexString(payload),
            //    Encoding.ASCII.GetString(payload)));

            int lenght = payload.Length;

            string data = string.Empty;

            int num = 0, index = 2;

            // determine whether to lookup two digits or 1
            if (lenght == 4)
            {
                index = 1;
            }

            //Debug.Assert(payload[0] == esc && payload[1] == bracket);

            if (lenght >= 3)
            {
                // Extract the data portion of the payload
                data = encode.GetString(payload, 2, (lenght - 3));

                // split the data into strings
                string[] vars = data.Split(';');

                // function code byte
                byte func = payload[lenght - 1];
                #region Text Attributes <Esc> m
                if (func == 0x6D) // func: m
                {
                    if (lenght == 3)
                    {
                        // Esc[m  Turn off character attributes
                        ResetAttributes();
                    }
                    else
                    {
                        foreach (string var in vars)
                        {
                            if (int.TryParse(var, out num))
                            {
                                if (num < 10)
                                {
                                    switch (num)
                                    {
                                    case 0:         // Esc[0m	Turn off character attributes
                                        ResetAttributes();
                                        break;

                                    case 1:         // Esc[1m   Turn bold mode on
                                        NativeMethods.AddIntensity();
                                        intensity = true;
                                        break;

                                    case 2:         // Esc[2m   Turn low intensity mode on
                                        NativeMethods.RemoveIntensity();
                                        intensity = false;
                                        break;

                                    case 3:         // Esc[3m   Standout.
                                        NativeMethods.AddIntensity();
                                        intensity = true;
                                        break;

                                    case 4:         // Esc[4m   Turn underline mode on
                                        Debug.WriteLine(string.Format("Turn underline mode on"));
                                        break;

                                    case 5:         // Esc[5m   Turn blinking mode on
                                        Debug.WriteLine(string.Format("Turn blinking mode on"));
                                        break;

                                    case 7:         // Esc[7m   Turn reverse video on
                                        RollColors();
                                        reversed = true;
                                        break;

                                    case 8:         // Esc[8m   Turn invisible text mode on
                                        InvisibleText();
                                        break;

                                    default:
                                        Debug.WriteLine(string.Format("Unknown m number: {0}", num));
                                        break;
                                    }
                                }
                                else if (num >= 30 && num <= 49)
                                {
                                    switch (num)
                                    {
                                    case 30:     // black foreground
                                        SetForegroundColor(ConsoleColor.Black);
                                        break;

                                    case 31:     // red foreground
                                        SetForegroundColor(ConsoleColor.Red);
                                        break;

                                    case 32:     // green foreground
                                        SetForegroundColor(ConsoleColor.Green);
                                        break;

                                    case 33:     // yellow foreground
                                        SetForegroundColor(ConsoleColor.Yellow);
                                        break;

                                    case 34:     // blue foreground
                                        SetForegroundColor(ConsoleColor.Blue);
                                        break;

                                    case 35:     // magenta foreground
                                        SetForegroundColor(ConsoleColor.Magenta);
                                        break;

                                    case 36:     // cyan foreground
                                        SetForegroundColor(ConsoleColor.Cyan);
                                        break;

                                    case 37:     // white foreground
                                        SetForegroundColor(ConsoleColor.White);
                                        break;

                                    case 39:     // default foreground
                                        SetForegroundColor(ConsoleColor.Gray);
                                        break;

                                    case 40:     // black background
                                        SetBackgroundColor(ConsoleColor.Black);
                                        break;

                                    case 41:     // red background
                                        SetBackgroundColor(ConsoleColor.Red);
                                        break;

                                    case 42:     // green background
                                        SetBackgroundColor(ConsoleColor.Green);
                                        break;

                                    case 43:     // yellow background
                                        SetBackgroundColor(ConsoleColor.Yellow);
                                        break;

                                    case 44:     // blue background
                                        SetBackgroundColor(ConsoleColor.Blue);
                                        break;

                                    case 45:     // magenta background
                                        SetBackgroundColor(ConsoleColor.Magenta);
                                        break;

                                    case 46:     // cyan background
                                        SetBackgroundColor(ConsoleColor.Cyan);
                                        break;

                                    case 47:     // white background
                                        SetBackgroundColor(ConsoleColor.Gray);
                                        break;

                                    case 49:     // default background
                                        SetBackgroundColor(ConsoleColor.Black);
                                        break;

                                    default:
                                        Debug.WriteLine(string.Format("Unknown Color change: {0} Data: {1}",
                                                                      SharedFunc.ByteArrayToHexString(payload), Encoding.ASCII.GetString(payload)));
                                        break;
                                    }
                                }
                                else
                                {
                                    Debug.WriteLine(string.Format("Unable to Parse Number in: {0} Data: {1}",
                                                                  SharedFunc.ByteArrayToHexString(payload), Encoding.ASCII.GetString(payload)));
                                }
                            }
                            else
                            {
                                Debug.WriteLine(string.Format("Unable to parse: {0} Data: {1}",
                                                              SharedFunc.ByteArrayToHexString(payload), Encoding.ASCII.GetString(payload)));
                            }
                        }
                    }
                }
                #endregion
                #region Cursor Position  <Esc> A - G Or <Esc> f
                else if ((func >= 0x41 && func <= 0x44) ||
                         (func == 0x48) || (func == 0x66))
                {
                    int left = PositionLeft;
                    int top  = PositionTop;

                    if (lenght == 3)
                    {
                        switch (func)
                        {
                        case 0x66:
                        case 0x48:
                            SetCursorPosition(0, 0);
                            break;

                        default:
                            Debug.WriteLine(string.Format("Error converting to cursor move: {0} Data {1}: ",
                                                          SharedFunc.ByteArrayToHexString(payload), Encoding.ASCII.GetString(payload)));
                            break;
                        }
                    }
                    else if (lenght > 3 && (func == 0x48 || func == 0x66))
                    {
                        if (vars.Count() > 1)
                        {
                            if (int.TryParse(vars[0], out top) && int.TryParse(vars[1], out left))
                            {
                                // Esc[ (line;column) H or Esc[ (line;column) f
                                // remove 1 for zero based offset
                                SetCursorPosition((top - 1), (left - 1));
                            }
                            else
                            {
                                Debug.WriteLine(string.Format("Error converting to cursor move: {0} Data {1}: ",
                                                              SharedFunc.ByteArrayToHexString(payload), Encoding.ASCII.GetString(payload)));
                            }
                        }
                        else if (int.TryParse(vars[0], out top))
                        {
                            // Esc[;H or Esc[;f
                            SetCursorPosition(0, 0);
                        }
                        else
                        {
                            Debug.WriteLine(string.Format("Error converting to cursor move: {0} Data {1}: ",
                                                          SharedFunc.ByteArrayToHexString(payload), Encoding.ASCII.GetString(payload)));
                        }
                    }
                    else if (lenght > 3 && (func >= 0x41 && func <= 0x44)) // Move cursor up n lines
                    {
                        Direction dir = Direction.Invalid;

                        List <int> dirs = new List <int>();

                        foreach (string var in vars)
                        {
                            if (int.TryParse(var, out num))
                            {
                                dirs.Add(num);
                            }
                        }

                        switch (func)
                        {
                        case 0x41:
                            dir = Direction.Up;
                            break;

                        case 0x42:
                            dir = Direction.Down;
                            break;

                        case 0x43:
                            dir = Direction.Right;
                            break;

                        case 0x44:
                            dir = Direction.Left;
                            break;

                        default:
                            Debug.WriteLine(string.Format("Unknown Direction: {0} Data {1}: ",
                                                          SharedFunc.ByteArrayToHexString(payload), Encoding.ASCII.GetString(payload)));
                            break;
                        }

                        if (dirs.Count > 0 && dir != Direction.Invalid)
                        {
                            DirectionalMove(dirs.ToArray(), dir);
                        }
                    }
                    else
                    {
                        Debug.WriteLine(string.Format("Move/Tab unknown: {0}  Bytes: {1} Data {2}: ", lenght,
                                                      SharedFunc.ByteArrayToHexString(payload), Encoding.ASCII.GetString(payload)));
                    }
                }
                #endregion
                #region Set Console Mode <Esc>=h
                else if (func == 0x68)
                {
                    // set mode
                    if (payload[2] == 0x3D)
                    {
                        foreach (string var in vars)
                        {
                            if (int.TryParse(var.Substring(1), out num))
                            {
                                switch (num)
                                {
                                case 0:         // 0	40 x 25 monochrome (text)
                                    SetConsoleSize(40, 25);
                                    SetConsoleBufferSize(40, 25);
                                    break;

                                case 1:         // 1	40 x 25 color (text)
                                    SetConsoleSize(40, 25);
                                    SetConsoleBufferSize(40, 25);
                                    break;

                                case 2:         // 2	80 x 25 monochrome (text)
                                    SetConsoleSize(80, 25);
                                    SetConsoleBufferSize(80, 25);
                                    break;

                                case 3:         // 3	80 x 25 color (text)
                                    SetConsoleSize(80, 25);
                                    SetConsoleBufferSize(80, 25);
                                    break;

                                case 7:         // 7	Enables line wrapping
                                    NativeMethods.EnableWordWrap();
                                    wordwrap = true;
                                    break;

                                case 4:         // 4	320 x 200 4-color (graphics)
                                case 5:         // 5	320 x 200 monochrome (graphics)
                                case 6:         // 6	640 x 200 monochrome (graphics)
                                case 13:        // 13	320 x 200 color (graphics)
                                case 14:        // 14	640 x 200 color (16-color graphics)
                                case 15:        // 15	640 x 350 monochrome (2-color graphics)
                                case 16:        // 16	640 x 350 color (16-color graphics)
                                case 17:        // 17	640 x 480 monochrome (2-color graphics)
                                case 18:        // 18	640 x 480 color (16-color graphics)
                                case 19:        // 19	320 x 200 color (256-color graphics)
                                    Debug.WriteLine(string.Format("Set Mode Graphics Mode Un-supported: {0}  Number: {1} Data {2}: ", num,
                                                                  SharedFunc.ByteArrayToHexString(payload), Encoding.ASCII.GetString(payload)));
                                    break;

                                default:
                                    Debug.WriteLine(string.Format("Set Mode <ESC>[= num.  Unknown num: {0}  Number: {1} Data {2}: ", num,
                                                                  SharedFunc.ByteArrayToHexString(payload), Encoding.ASCII.GetString(payload)));
                                    break;
                                }
                            }
                            else
                            {
                                Debug.WriteLine(string.Format("Set Mode <ESC>[= num.  Unable to convert num: {0}  Lenght: {1} Data {2}: ", lenght,
                                                              SharedFunc.ByteArrayToHexString(payload), Encoding.ASCII.GetString(payload)));
                            }
                        }
                    }
                    else
                    {
                        Debug.WriteLine(string.Format("Set Mode <ESC>[= num.  3rd char not = symbol: {0}  Lenght: {1} Data {2}: ", lenght,
                                                      SharedFunc.ByteArrayToHexString(payload), Encoding.ASCII.GetString(payload)));
                    }
                }
                #endregion
                #region Clear Line <Esc> J & <Esc> K
                else if (func == 0x4A) // J
                {
                    //Erase Down		<ESC>[J
                    //Erases the screen from the current position down to the bottom of the screen.
                    if (lenght == 3)
                    {
                        ClearToDirection(Direction.Down);
                    }
                    else
                    {
                        foreach (string var in vars)
                        {
                            if (int.TryParse(var, out num))
                            {
                                switch (num)
                                {
                                case 0:
                                    //Erase Up		<ESC>[0J
                                    //Erases the screen from the current position up to the end the screen.
                                    ClearToDirection(Direction.Down);
                                    break;

                                case 1:
                                    //Erase Up		<ESC>[1J
                                    //Erases the screen from the current line up to the top of the screen.
                                    ClearToDirection(Direction.Up);
                                    break;

                                case 2:
                                    //Erase Screen		<ESC>[2J
                                    //Erases the screen with the background color and moves the cursor to home.
                                    Clear();

                                    // reset the cursor position
                                    SetCursorPosition(0, 0);

                                    break;

                                default:
                                    Debug.WriteLine(string.Format("Erase Screen <ESC>[num J unknown num: {0}  Num: {1} Data: {2} ", num,
                                                                  SharedFunc.ByteArrayToHexString(payload), Encoding.ASCII.GetString(payload)));
                                    break;
                                }
                            }
                            else
                            {
                                Debug.WriteLine(string.Format("Erase Line <ESC> J unable to convert number: {0}  Bytes: {1} Data: {2}, {3} ", lenght,
                                                              SharedFunc.ByteArrayToHexString(payload), Encoding.ASCII.GetString(payload), index));
                            }
                        }
                    }
                }
                else if (func == 0x4B) // K
                {
                    //Erases from the current cursor position to the end of the current line.
                    if (lenght == 3)
                    {
                        //Erase End of Line	<ESC>[K
                        ClearToDirection(Direction.Right);
                    }
                    else
                    {
                        foreach (string var in vars)
                        {
                            if (int.TryParse(var, out num))
                            {
                                switch (num)
                                {
                                case 0:
                                    ClearToDirection(Direction.Right);
                                    break;

                                case 1:
                                    ClearToDirection(Direction.Left);
                                    break;

                                case 2:
                                    ClearLine();
                                    break;

                                default:
                                    Debug.WriteLine(string.Format("Erase Screen <ESC>[num K unknown num: {0}  Num: {1} Data {2}: ", num,
                                                                  SharedFunc.ByteArrayToHexString(payload), Encoding.ASCII.GetString(payload)));
                                    break;
                                }
                            }
                            else
                            {
                                Debug.WriteLine(string.Format("Erase Line <ESC>[ K unable to convert number: {0}  Bytes: {1} Data {2}: ", lenght,
                                                              SharedFunc.ByteArrayToHexString(payload), Encoding.ASCII.GetString(payload)));
                            }
                        }
                    }
                }
                else
                {
                    Debug.WriteLine(string.Format("Erase Line <ESC>[ ?? unable to convert function: {0}  Bytes: {1} Data {2}: ", lenght,
                                                  SharedFunc.ByteArrayToHexString(payload), Encoding.ASCII.GetString(payload)));
                }
                #endregion
            }
            else
            {
                Debug.WriteLine(string.Format("Malformed function length: {0}  Bytes: {1} Data {2}: ", lenght,
                                              SharedFunc.ByteArrayToHexString(payload), Encoding.ASCII.GetString(payload)));
            }
        }
        internal override void commandImplementation()
        {
            uint              sledId      = 1;
            BladeResponse     myResponse  = new BladeResponse();
            AllBladesResponse myResponses = new AllBladesResponse();

            try
            {
                if (this.argVal.ContainsKey('a'))
                {
                    myResponses = WcsCli2CmConnectionManager.channel.SetAllBladesPowerLimitOff();
                }
                else if (this.argVal.ContainsKey('i'))
                {
                    dynamic mySledId = null;
                    this.argVal.TryGetValue('i', out mySledId);
                    sledId     = (uint)mySledId;
                    myResponse = WcsCli2CmConnectionManager.channel.SetBladePowerLimitOff((int)mySledId);
                }
            }
            catch (Exception ex)
            {
                SharedFunc.ExceptionOutput(ex);
                return;
            }

            if ((this.argVal.ContainsKey('a') && myResponses == null) || myResponse == null)
            {
                Console.WriteLine(WcsCliConstants.serviceResponseEmpty);
                return;
            }

            if (this.argVal.ContainsKey('a'))
            {
                for (int index = 0; index < myResponses.bladeResponseCollection.Count(); index++)
                {
                    if (myResponses.bladeResponseCollection[index].completionCode == Contracts.CompletionCode.Success)
                    {
                        Console.WriteLine("Blade " + myResponses.bladeResponseCollection[index].bladeNumber + ": Power Limit Deactive");
                    }
                    else if (myResponse.completionCode == Contracts.CompletionCode.Unknown)
                    {
                        Console.WriteLine("Blade " + myResponses.bladeResponseCollection[index].bladeNumber + ": " + WcsCliConstants.bladeStateUnknown);
                    }
                    else
                    {
                        // Display error if other than success/unknown
                        Console.WriteLine("Blade " + myResponses.bladeResponseCollection[index].bladeNumber + " failed with completion code: " + myResponses.bladeResponseCollection[index].completionCode.ToString());
                    }
                }
            }
            else
            {
                if (myResponse.completionCode == Contracts.CompletionCode.Success)
                {
                    Console.WriteLine("Blade " + myResponse.bladeNumber + ": Power Limit Deactive");
                }
                else if (myResponse.completionCode == Contracts.CompletionCode.Unknown)
                {
                    Console.WriteLine("Blade " + myResponse.bladeNumber + ": " + WcsCliConstants.bladeStateUnknown);
                }
                else
                {
                    // Display error if other than success/unknown
                    Console.WriteLine("Blade " + myResponse.bladeNumber + ": " + myResponse.completionCode.ToString());
                }
            }
        }
Example #3
0
        /// <summary>
        /// Encode VT100 escape sequences into function key
        /// enterEncodeCRLF: VT100 encoding for Enter key. True: CR+LF for Enter. False: CR for Enter.
        /// </summary>
        private byte[] Vt100Encode(ConsoleKeyInfo keyInfo, bool enterEncodeCRLF)
        {
            byte[] enc = new byte[3];
            enc[0] = 0x1B; // Esc

            if (keyInfo.Key >= ConsoleKey.F1 && // if key between F1 & F4
                keyInfo.Key <= ConsoleKey.F4)
            {
                enc[1] = 0x4F; // O
                switch (keyInfo.Key)
                {
                case ConsoleKey.F1:
                    enc[2] = 0x50;     // P
                    break;

                case ConsoleKey.F2:
                    enc[2] = 0x51;     // Q
                    break;

                case ConsoleKey.F3:
                    enc[2] = 0x52;     // R
                    break;

                case ConsoleKey.F4:
                    enc[2] = 0x53;     // S
                    break;

                default:
                    break;
                }
            }
            else if (keyInfo.Key >= ConsoleKey.F5 && // if key between F5 & F12
                     keyInfo.Key <= ConsoleKey.F12)
            {
                enc    = new byte[2];
                enc[0] = 0x1B; // Esc

                switch (keyInfo.Key)
                {
                case ConsoleKey.F5:
                    enc[1] = 0x35;
                    break;

                case ConsoleKey.F6:
                    enc[1] = 0x36;
                    break;

                case ConsoleKey.F7:
                    enc[1] = 0x37;
                    break;

                case ConsoleKey.F8:
                    enc[1] = 0x38;
                    break;

                case ConsoleKey.F9:
                    enc[1] = 0x39;
                    break;

                case ConsoleKey.F10:
                    enc[1] = 0x30;
                    break;

                case ConsoleKey.F11:
                    enc[1] = 0x2A;
                    break;

                case ConsoleKey.F12:
                    enc[1] = 0x28;
                    break;

                default:
                    break;
                }
            }
            else if (keyInfo.Key >= ConsoleKey.LeftArrow && // if key is Arrow
                     keyInfo.Key <= ConsoleKey.DownArrow)
            {
                enc[1] = 0x5B; // bracket

                switch (keyInfo.Key)
                {
                case ConsoleKey.UpArrow:
                    enc[2] = 0x41;     // A
                    break;

                case ConsoleKey.DownArrow:
                    enc[2] = 0x42;     // B
                    break;

                case ConsoleKey.RightArrow:
                    enc[2] = 0x43;     // C
                    break;

                case ConsoleKey.LeftArrow:
                    enc[2] = 0x44;     // D
                    break;

                default:
                    break;
                }
            }
            else if (keyInfo.Key == ConsoleKey.Enter) // if key is Enter
            {
                byte enc_length;

                if (enterEncodeCRLF)
                {
                    enc_length = 2;
                    enc        = new byte[2] {
                        0x0D, 0x0A
                    };
                }
                else
                {
                    enc_length = 1;
                    enc        = new byte[1] {
                        0x0D
                    };
                }

                if (scrData != string.Empty && scrData.Length > 0)
                {
                    // get screen data bytes
                    byte[] scrPayload = Encoding.UTF8.GetBytes(scrData);

                    // flush screen data
                    scrData = string.Empty;

                    // create new serialized packet with screen bytes and return payload
                    enc = new byte[(scrPayload.Length + enc_length)];

                    Buffer.BlockCopy(scrPayload, 0, enc, 0, scrPayload.Length);

                    // Add return key
                    enc[scrPayload.Length] = 0x0D;
                    if (enterEncodeCRLF)
                    {
                        enc[(scrPayload.Length + 1)] = 0x0A;
                    }
                }
            }
            else if (keyInfo.Key == ConsoleKey.Delete)
            {
                // ^[3~"
                enc    = new byte[4];
                enc[0] = 0x1B;
                enc[1] = 0x5B; // bracket
                enc[2] = 0x33;
                enc[3] = 0x7E;
            }
            else if (keyInfo.Key == ConsoleKey.Escape) // Escape
            {
                enc    = new byte[1];
                enc[0] = 0x1B;
            }
            else if (keyInfo.Key == ConsoleKey.X &&
                     keyInfo.Modifiers == ConsoleModifiers.Control) // Ctrl + X
            {
                // Issue a graceful terminate.
                SharedFunc.SetSerialSession(false);

                // Clear console window
                Console.Clear();
            }
            else if (keyInfo.Key == ConsoleKey.C &&
                     keyInfo.Modifiers == ConsoleModifiers.Control) // Ctrl + C
            {
                enc = new byte[1] {
                    0x03
                };
            }
            else if (keyInfo.Key == ConsoleKey.Tab) // TAB
            {
                enc    = new byte[1];
                enc[0] = 0x09;
            }

            return(enc);
        }