public TerminalCommandResponse AddModbusDevice(GTSetConfigurationRequest request) { var config = WebApiServerConfigurationManager.Instance.Configurations; TerminalCommandRequest commandRequest = new TerminalCommandRequest(); commandRequest.TerminalId = request.TerminalId; try { if (!(request.RequestType == GatewayRequestType.TerminalCommand && request.CommandType == GTCommandType.AddModbusDevice)) { return(new TerminalCommandResponse { RequestType = request.RequestType, RequestId = request.RequestId, Message = "Invalid Request Type", ResponseStatus = ResponseStatus.Failed, TerminalId = request.TerminalId }); } commandRequest.Data = GTCommandFactory.CreateSetCommand(request.TerminalId, request.Data, request.CommandType); return(TerminalRequestHandler.SendGTCommandRequest(commandRequest, request.CommandType)); } catch (Exception ex) { return(new TerminalCommandResponse { RequestId = commandRequest.RequestId, Data = null, RequestType = commandRequest.RequestType, ResponseStatus = ResponseStatus.Failed, Message = ex.Message }); } }
public async Task PerformTerminalCommandRequest(TerminalCommandRequest request) { RestClientFactory factory = new RestClientFactory("RMS"); var response = await factory.PostCallAsync <TerminalCommandResponse, TerminalCommandRequest> (factory.apiConfiguration.Apis["TerminalCommand"], request); OnResponseReceived(response); }
public TerminalCommandResponse SetConfiguration(GTSetConfigurationRequest request) { var config = WebApiServerConfigurationManager.Instance.Configurations; TerminalCommandRequest commandRequest = new TerminalCommandRequest(); commandRequest.TerminalId = request.TerminalId; try { if (request.RequestType != GatewayRequestType.TerminalCommand) { return(new TerminalCommandResponse { RequestType = request.RequestType, RequestId = request.RequestId, Message = "Invalid Request Type", ResponseStatus = ResponseStatus.Failed, TerminalId = request.TerminalId }); } if (request.CommandType == GTCommandType.GeneralSettings || request.CommandType == GTCommandType.ExtendedConfigurationSettings || request.CommandType == GTCommandType.PollingAndGprsSettings || request.CommandType == GTCommandType.Reset || request.CommandType == GTCommandType.ResetRom || request.CommandType == GTCommandType.SimAndServerSettings || request.CommandType == GTCommandType.WatchdogSettings) { commandRequest.Data = GTCommandFactory.CreateSetCommand(request.TerminalId, request.Data, request.CommandType); var response = TerminalRequestHandler.SendGTCommandRequest(commandRequest, request.CommandType); response.RequestId = request.RequestId; return(response); } else { return(new TerminalCommandResponse { RequestType = request.RequestType, RequestId = request.RequestId, Message = "Invalid Request Type", ResponseStatus = ResponseStatus.Failed, TerminalId = request.TerminalId }); } } catch (Exception ex) { return(new TerminalCommandResponse { RequestId = commandRequest.RequestId, Data = null, RequestType = commandRequest.RequestType, ResponseStatus = ResponseStatus.Failed, Message = ex.Message }); } }
public void Notify(object request) { var jsonObject = JObject.FromObject(request); TerminalCommandRequest command = jsonObject.ToObject <TerminalCommandRequest>(); // parse and store command to a dictionary Console.WriteLine(command.ToFormattedJson()); Task.Run(() => server.Send(command.TerminalId, command.Data)); }
private void btnSendCommand_Click(object sender, EventArgs e) { UpdateTextBox(txtResponse, "Please Wait... (PerformTerminalCommandRequest)"); TerminalCommandRequest request = new TerminalCommandRequest(); request.RequestType = GatewayRequestType.TerminalCommand; request.TerminalId = txtTerminalId.Text; request.Data = txtCommand.Text; Task.Run(() => PerformTerminalCommandRequest(request)); }
public TerminalCommandResponse GetModbusDevice(GTGetModbusDeviceRequest request) { var config = WebApiServerConfigurationManager.Instance.Configurations; TerminalCommandRequest commandRequest = new TerminalCommandRequest(); commandRequest.TerminalId = request.TerminalId; try { if (request.CommandType == GTCommandType.GetModbusDevice) { commandRequest.Data = GTCommandFactory.CreateGetModbusDeviceCommand(request.TerminalId, request.StartIndex, 1); var response = TerminalRequestHandler.SendGTCommandRequest(commandRequest, request.CommandType); return(response); } else if (request.CommandType == GTCommandType.GetMultipleModbusDevices) { commandRequest.Data = GTCommandFactory.CreateGetModbusDeviceCommand(request.TerminalId, request.StartIndex, request.NumberOfDevices); var response = TerminalRequestHandler.SendGTCommandRequest(commandRequest, request.CommandType); return(response); } else { return(new TerminalCommandResponse { RequestId = commandRequest.RequestId, Data = null, RequestType = commandRequest.RequestType, ResponseStatus = ResponseStatus.Failed, Message = "Invalid Command Request" }); } } catch (Exception ex) { return(new TerminalCommandResponse { RequestId = commandRequest.RequestId, Data = null, RequestType = commandRequest.RequestType, ResponseStatus = ResponseStatus.Failed, Message = ex.Message }); } }
public TerminalCommandResponse Command(TerminalCommandRequest request) { var config = WebApiServerConfigurationManager.Instance.Configurations; try { if (request.RequestType != GatewayRequestType.TerminalCommand) { return(new TerminalCommandResponse { RequestType = request.RequestType, RequestId = request.RequestId, Message = "Invalid Request Type", ResponseStatus = ResponseStatus.Failed, TerminalId = request.TerminalId }); } var cmd = TerminalCommandHandler.Instance.Find(request.TerminalId); if (cmd != null) { return(new TerminalCommandResponse { RequestId = request.RequestId, Data = null, RequestType = request.RequestType, ResponseStatus = ResponseStatus.Failed, Message = "Another configuration already in process. Please try again later" }); } TerminalCommandHandler.Instance.Add(new TerminalCommand { RequestData = request.Data, TerminalId = request.TerminalId, RequestReceivedOn = DateTimeHelper.CurrentUniversalTime, Status = CommandStatus.RequestReceived }); WebServer.server.Notify(request); int retries = config.TerminalCommandRetries; while (retries > 0) { retries--; try { var command = TerminalCommandHandler.Instance.Find(request.TerminalId); if (command.Status == CommandStatus.ResponseReceived) { TerminalCommandHandler.Instance.Remove(request.TerminalId); return(new TerminalCommandResponse { RequestId = request.RequestId, Data = command.ResponseData, RequestType = request.RequestType, ResponseStatus = ResponseStatus.Success, Message = "Response Received from Device", TerminalId = command.TerminalId }); } } catch (Exception ex) { return(new TerminalCommandResponse { RequestId = request.RequestId, Data = null, RequestType = request.RequestType, ResponseStatus = ResponseStatus.Failed, Message = ex.Message }); } Thread.Sleep(config.TerminalCommandRetryIntervalInSeconds * 1000); } TerminalCommandHandler.Instance.Remove(request.TerminalId); return(new TerminalCommandResponse { RequestId = request.RequestId, Data = null, RequestType = request.RequestType, ResponseStatus = ResponseStatus.Failed, Message = "Request Timed Out", TerminalId = request.TerminalId }); } catch (Exception ex) { return(new TerminalCommandResponse { RequestId = request.RequestId, Data = null, RequestType = request.RequestType, ResponseStatus = ResponseStatus.Failed, Message = ex.Message }); } }
public TerminalCommandResponse Command(TerminalCommandRequest request) { var config = WebApiServerConfigurationManager.Instance.Configurations; try { var cmd = TerminalCommandHandler.Instance.Find(request.TerminalId); if (cmd != null) { return(new TerminalCommandResponse { RequestId = request.RequestId, Data = null, RequestType = request.RequestType, ResponseStatus = ResponseStatus.Failed, Message = "Another configuration already in process. Please try again later" }); } TerminalCommandHandler.Instance.Add(new TerminalCommand { RequestData = request.Data, TerminalId = request.TerminalId, RequestReceivedOn = DateTimeHelper.CurrentUniversalTime, Status = CommandStatus.RequestReceived }); WebServer.server.Notify(request); int retries = config.TerminalCommandRetries; while (retries > 0) { retries--; try { var command = TerminalCommandHandler.Instance.Find(request.TerminalId); if (command.Status == CommandStatus.ResponseReceived) { TerminalCommandHandler.Instance.Remove(request.TerminalId); return(new TerminalCommandResponse { RequestId = request.RequestId, Data = command.ResponseData, RequestType = request.RequestType, ResponseStatus = ResponseStatus.Success, Message = "Configuration Successful", TerminalId = command.TerminalId }); } } catch (Exception ex) { return(new TerminalCommandResponse { RequestId = request.RequestId, Data = null, RequestType = request.RequestType, ResponseStatus = ResponseStatus.Failed, Message = ex.Message }); } Thread.Sleep(config.TerminalCommandRetryIntervalInSeconds * 1000); } TerminalCommandHandler.Instance.Remove(request.TerminalId); return(new TerminalCommandResponse { RequestId = request.RequestId, Data = null, RequestType = request.RequestType, ResponseStatus = ResponseStatus.Failed, Message = "Request Timed Out", TerminalId = request.TerminalId }); //var configurations = WebApiServerConfigurationManager.Instance.Configurations; //IOwinContext owinContext = Request.GetOwinContext(); //string clientIp = owinContext.Environment["server.RemoteIpAddress"] as string; //return RequestHandler.HandleRequest(jsonObject, new CommunicationContext { IP = clientIp }); } catch (Exception ex) { return(new TerminalCommandResponse { RequestId = request.RequestId, Data = null, RequestType = request.RequestType, ResponseStatus = ResponseStatus.Failed, Message = ex.Message }); } }
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 }); } }