public static TerminalCommandResponse SendGTCommandRequest(TerminalCommandRequest commandRequest,
                                                                   GTCommandType commandType)
        {
            var config = WebApiServerConfigurationManager.Instance.Configurations;
            TerminalCommandResponse response = new TerminalCommandResponse();

            try
            {
                var connected = WebServer.server.ChannelKeys.Contains(commandRequest.TerminalId);
                if (!connected)
                {
                    return(new TerminalCommandResponse
                    {
                        RequestId = commandRequest.RequestId,
                        Data = null,//commandRequest.Data,
                        RequestType = commandRequest.RequestType,
                        ResponseStatus = ResponseStatus.Failed,
                        Message = "Site not connected."
                    });
                }
                var cmd = TerminalCommandHandler.Instance.Find(commandRequest.TerminalId);
                if (cmd != null)
                {
                    return(new TerminalCommandResponse
                    {
                        RequestId = commandRequest.RequestId,
                        Data = commandRequest.Data,
                        RequestType = commandRequest.RequestType,
                        ResponseStatus = ResponseStatus.Failed,
                        Message = "Another configuration already in process. Please try again later"
                    });
                }
                TerminalCommandHandler.Instance.Add(new TerminalCommand
                {
                    RequestData       = commandRequest.Data,
                    TerminalId        = commandRequest.TerminalId,
                    RequestReceivedOn = DateTimeHelper.CurrentUniversalTime,
                    Status            = CommandStatus.RequestReceived
                });
                WebServer.server.Notify(commandRequest);

                int retries = config.TerminalCommandRetries;
                while (retries > 0)
                {
                    retries--;
                    try
                    {
                        var command = TerminalCommandHandler.Instance.Find(commandRequest.TerminalId);
                        if (command.Status == CommandStatus.ResponseReceived)
                        {
                            TerminalCommandHandler.Instance.Remove(commandRequest.TerminalId);
                            var   data     = command.ResponseData;
                            ICGRC gtConfig = null;
                            if (!string.IsNullOrEmpty(data))
                            {
                                var packet = ParsingManager.FirstLevelParser(data);
                                gtConfig = GTCommandFactory.GetConfiguration(packet, commandType);
                            }
                            return(new TerminalCommandResponse
                            {
                                RequestId = commandRequest.RequestId,
                                Data = gtConfig,
                                RequestType = commandRequest.RequestType,
                                ResponseStatus = ResponseStatus.Success,
                                Message = "Response Received",
                                TerminalId = command.TerminalId
                            });
                        }
                    }
                    catch (Exception ex)
                    {
                        return(new TerminalCommandResponse
                        {
                            RequestId = commandRequest.RequestId,
                            Data = null,
                            RequestType = commandRequest.RequestType,
                            ResponseStatus = ResponseStatus.Failed,
                            Message = ex.Message
                        });
                    }
                    Thread.Sleep(config.TerminalCommandRetryIntervalInSeconds * 1000);
                }

                TerminalCommandHandler.Instance.Remove(commandRequest.TerminalId);
                return(new TerminalCommandResponse
                {
                    RequestId = commandRequest.RequestId,
                    Data = null,
                    RequestType = commandRequest.RequestType,
                    ResponseStatus = ResponseStatus.Failed,
                    Message = "Request Timed Out",
                    TerminalId = commandRequest.TerminalId
                });
            }
            catch (Exception ex)
            {
                return(new TerminalCommandResponse
                {
                    RequestId = commandRequest.RequestId,
                    Data = null,
                    RequestType = commandRequest.RequestType,
                    ResponseStatus = ResponseStatus.Failed,
                    Message = ex.Message
                });
            }
        }
        public static ICGRC GetConfiguration(ReceivedPacket packet, GTCommandType commandType)
        {
            ICGRC cgrc = null;

            string[] strArray = null;
            switch (commandType)
            {
            case GTCommandType.GeneralSettings:
                strArray = SplitPacket(packet.Data);
                cgrc     = new GTGeneralSettings(packet.TerminalId);
                cgrc.Parse(strArray);
                break;

            case GTCommandType.SimAndServerSettings:
                strArray = SplitPacket(packet.Data);
                cgrc     = new GTSimAndServerSettings(packet.TerminalId);
                cgrc.Parse(strArray);
                break;

            case GTCommandType.PollingAndGprsSettings:
                strArray = SplitPacket(packet.Data);
                cgrc     = new GTPollingAndGprsSettings(packet.TerminalId);
                cgrc.Parse(strArray);
                break;

            case GTCommandType.WatchdogSettings:
                strArray = SplitPacket(packet.Data);
                cgrc     = new GTWatchdogSettings(packet.TerminalId);
                cgrc.Parse(strArray);
                break;

            case GTCommandType.Reset:
                cgrc = new GTReset(packet.TerminalId);
                break;

            case GTCommandType.ResetRom:
                cgrc = new GTResetRom(packet.TerminalId);
                break;

            case GTCommandType.ExtendedConfigurationSettings:
                strArray = SplitPacket(packet.Data);
                cgrc     = new GTExtendedConfigurationSettings(packet.TerminalId);
                cgrc.Parse(strArray);
                break;

            case GTCommandType.NetworkReadOnlyInformation:
                strArray = SplitPacket(packet.Data);
                cgrc     = new GTNetworkReadOnlyInformation(packet.TerminalId);
                cgrc.Parse(strArray);
                break;

            case GTCommandType.GetModbusDevice:
                if (!packet.Data.Equals(GTCommandFactory.ModbusInvalidString))
                {
                    if (packet.Data.StartsWith("GET"))
                    {
                        strArray = SplitPacket(packet.Data.Replace("GET[", "").TrimEnd(']'));
                        cgrc     = new GTGetModbusDevice(packet.TerminalId);
                        cgrc.Parse(strArray);
                    }
                }
                break;

            case GTCommandType.GetMultipleModbusDevices:
                strArray = SplitPacket(packet.Data, ';');
                cgrc     = new GTGetModbusDeviceCollection(packet.TerminalId);
                cgrc.Parse(strArray);
                break;

            case GTCommandType.ClearAllModbusDevices:
                cgrc = new GTClearModbusDevices(packet.TerminalId);
                cgrc.Parse(new string[] { packet.Data });
                break;

            case GTCommandType.AddModbusDevice:
                if (packet.Data.StartsWith("ADD"))
                {
                    strArray = SplitPacket(packet.Data.Replace("ADD[", "").TrimEnd(']'));
                    cgrc     = new GTAddModbusDevice(packet.TerminalId);
                    cgrc.Parse(strArray);
                }
                break;

            case GTCommandType.AddMultipleModbusDevices:
                strArray = SplitPacket(packet.Data, ';');
                cgrc     = new GTAddModbusDeviceCollection(packet.TerminalId);
                cgrc.Parse(strArray);
                break;

            default:
                break;
            }
            return(cgrc);
        }