teco-kit/PointAndControl

View on GitHub
IGS/Devices/NECLCDmonitorMultiSyncV421.cs

Summary

Maintainability
B
5 hrs
Test Coverage
using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Text;

namespace PointAndControl.Devices
{
    /// <summary>
    ///     This class specializes the device class to the class LCDTV
    ///     It contains all information as well as functions available for a LCDTV.
    ///     Follwing functions are available:
    ///     "on"
    ///     "off"
    ///     Volume (raise ("volup"), lower("voldown"), mute ("mute"))
    ///     Sources ("source")(VGA ("1"), RGB ("2"), DVI "3", HDMI "4", Video1 "5", Video2 "6", S-Video "7", DVD HD1 "12", DVD HD2 "14", HDMI (VESA STD) "17")
    ///     Audio Input ("audio")(Audio 1 PC ("1"), Audio 2 "2", Audio 3 "3", HDMI "4")
    ///     @author Christopher Baumgärtner
    /// </summary>
    public class NecLcdMonitorMultiSyncV421 : NativeTransmittingDevice
    {
        /// <summary>
        ///     Constructor of a LCDTV object.
        ///     <param name="id">ID of the object for identifying it</param>
        ///     <param name="name">Userdefined name of the device</param>
        ///     <param name="form">Shape of the device in the room</param>
        ///      <param name = "path" > The Path to communicate with the device</param>  
        /// </summary>

        public NecLcdMonitorMultiSyncV421(String name, String id, String path, List<Ball> form)
            : base(name, id, path, form)
        {
            String[] ipAndPort = splitPathToIPAndPort();
            connection = new Tcp(Convert.ToInt32(ipAndPort[1]), ipAndPort[0]);
        }

        /// <summary>
        ///     The Transmit method is responsible for the correct invocation of a function of the LCDTV
        ///     which is implicated by the "commandID"
        ///     <param name="cmdId">
        ///         With the commandID the Transmit-method recieves which command
        ///         should be send to the device (LCDTV)
        ///     </param>
        ///     <param name="value">
        ///         The value belonging to the command
        ///     </param>
        ///     <returns>
        ///     If execution was successful
        ///     </returns>
        /// </summary>
        public override String Transmit(String cmdId, String value)
        {
            switch (cmdId)
            {
                case "on":
                    return Power(0x31);
                case "off":
                    return Power(0x34);
                case "volup":
                    return Vol(1);
                case "voldown":
                    return Vol(-1);
                case "mute":
                    return Mute();
                case "source":
                    return Input((byte)(0x30 + Convert.ToInt32(value)));
                case "audio":
                    return Audio((byte)(0x30 + Convert.ToInt32(value)));
            }
            return Properties.Resources.InvalidCommand;
        }

        private String Power(byte b)
        {
            byte[] msg = new byte[21];
            byte[] message = new byte[] { 0x01, 0x30, 0x41, 0x30, 0x41, 0x30, 0x43, 0x02, 0x43, 0x32, 0x30, 0x33, 0x44, 0x36, 0x30, 0x30, 0x30, b, 0x03 }; //Message
            message.CopyTo(msg, 0);
            msg[19] = CalcBcc(msg);
            msg[20] = 0x0D;
            return connection.Send(Encoding.ASCII.GetString(msg));
        }

        private String Vol(int i)
        {
            byte[] msg = new byte[15];
            byte[] message = new byte[] { 0x01, 0x30, 0x41, 0x30, 0x43, 0x30, 0x36, 0x02, 0x30, 0x30, 0x36, 0x32, 0x03 };

            //Message
            message.CopyTo(msg, 0);
            msg[13] = CalcBcc(msg);
            msg[14] = 0x0D;

            byte[] response;

            try
            {
                response = Encoding.ASCII.GetBytes
                    (connection.Send(Encoding.ASCII.GetString(msg)));
            }
            catch (SocketException e)
            {
                throw e;
            }

            byte newValue = (byte)(response[23] + i);

            msg = new byte[15];
            message = new byte[] { 0x01, 0x30, 0x41, 0x30, 0x45, 0x30, 0x3A, 0x02, 0x30, 0x30, 0x36, newValue, 0x03 };

            //Message
            message.CopyTo(msg, 0);
            msg[13] = CalcBcc(msg);
            msg[14] = 0x0D;

            return connection.Send(Encoding.ASCII.GetString(msg));

        }

        private String Mute()
        {
            byte[] msg = new byte[15];
            byte[] message = new byte[] { 0x01, 0x30, 0x41, 0x30, 0x43, 0x30, 0x36, 0x02, 0x30, 0x30, 0x38, 0x3D, 0x03 };

            //Message
            message.CopyTo(msg, 0);
            msg[13] = CalcBcc(msg);
            msg[14] = 0x0D;

            byte[] response;

            try
            {
                response = Encoding.ASCII.GetBytes
                    (connection.Send(Encoding.ASCII.GetString(msg)));
            }
            catch (SocketException e)
            {
                throw e;
            }

            byte newValue = response[16];
            if (newValue == 0x31)
            {
                newValue = 0x30;
            }
            else
            {
                newValue = 0x31;
            }

            msg = new byte[19];
            message = new byte[]
                {
                    0x01, 0x30, 0x41, 0x30, 0x45, 0x30, 0x3A, 0x02, 0x30, 0x30, 0x38, 0x3D, 0x30, 0x30, 0x30, newValue,
                    0x03
                }; //Message
            message.CopyTo(msg, 0);
            msg[17] = CalcBcc(msg);
            msg[18] = 0x0D;

            return connection.Send(Encoding.ASCII.GetString(msg));
        }

        private String Input(byte b)
        {
            byte[] msg = new byte[19];
            byte[] message = new byte[] { 0x01, 0x30, 0x41, 0x30, 0x45, 0x30, 0x3A, 0x02, 0x30, 0x30, 0x36, 0x30, 0x30, 0x30, 0x30, b, 0x03 };

            //Message
            message.CopyTo(msg, 0);
            msg[17] = CalcBcc(msg);
            msg[18] = 0x0D;

            return connection.Send(Encoding.ASCII.GetString(msg));
        }

        private String Audio(byte b)
        {
            byte[] msg = new byte[19];
            byte[] message = new byte[] { 0x01, 0x30, 0x41, 0x30, 0x45, 0x30, 0x3A, 0x02, 0x30, 0x32, 0x32, 0x3E, 0x30, 0x30, 0x30, b, 0x03 };

            //Message
            message.CopyTo(msg, 0);
            msg[17] = CalcBcc(msg);
            msg[18] = 0x0D;

            return connection.Send(Encoding.ASCII.GetString(msg));
        }

        private static byte CalcBcc(byte[] command)
        {
            byte temp = command[1];
            for (int i = 2; i < command.Length; i++)
            {
                temp ^= command[i];
            }
            return temp;
        }
    }
}