/// <summary> /// Initializes the simulation and its datamembers. /// </summary> /// <param name="roomSize"></param> /// <param name="startPosition"></param> /// <param name="commands"></param> /// <param name="entityBody"></param> /// <returns></returns> public override int Initialize(string roomSize, string startPosition, string commands, Body entityBody) { if (_isLogging) Console.WriteLine("Initializing simulation."); try { string[] roomData = Helper.Parse(roomSize); string[] carData = Helper.Parse(startPosition); string[] carCommands = Helper.Parse(commands, true); int roomWidth = Int32.Parse(roomData[0]); int roomHeight = Int32.Parse(roomData[1]); int carStartX = Int32.Parse(carData[0]); int carStartY = Int32.Parse(carData[1]); _car = new Car(carStartX, carStartY, (CarBody)entityBody, carData[2][0], _isLogging); CommandsQueue = new CommandsQueue(carCommands.Length); _room = new Room(roomWidth, roomHeight, _car, _isLogging); foreach (string command in carCommands) { CommandsQueue.Enqueue(new CarCommand(command[0])); } } catch (IsNotEnumException e) { Console.WriteLine("Simulation error in Initialize.\n" + e.Message); return -1; } catch (Exception) { Console.WriteLine("Simulation error in Initialize."); return -1; } return 0; }
/// <summary> /// Request set SRB1 for board /// </summary> public bool RequestSetSrb1(int board, bool enable) { if (BoardSettings == null || (!BoardSettings.IsValid && BoardSettings.Boards.Count() < board)) { return(false); } // this is the board configuration index, not the board ID switch (board) { case 0: StartSrb1CytonSet = enable; break; case 1: StartSrb1DaisySet = enable; break; } var channel = BoardSettings.Boards[board].Channels[0]; var setSrb1String = FormatSetSrb1String(channel, enable); CommandsQueue.Enqueue(setSrb1String); return(true); }
public async Task Subscribe(string jwt) { await Task.WhenAny(Task.Delay(TIME_OUT), Task.Run(async() => { AppLog.Info("Connecting to server..."); var options = new IO.Options(); options.Query = new Dictionary <string, string>(); options.Query["token"] = jwt; options.Reconnection = true; _socket = IO.Socket(_config.ServerUrl, options); _socket.On(Socket.EVENT_CONNECT, () => { AppLog.Info("Connected to server"); }); _socket.On(Socket.EVENT_RECONNECTING, () => { AppLog.Info("Reconnecting to server..."); }); _socket.On(EVENT_OPEN, (payload) => { int idNo = Convert.ToInt32(payload); AppLog.Info("Received command open lock: {0}", idNo); lock (CommandsQueue) { CommandsQueue.Enqueue(idNo); } }); // wait for connect to server while (_socket.Io().ReadyState != Manager.ReadyStateEnum.OPEN) { await Task.Yield(); } })); if (_socket.Io().ReadyState != Manager.ReadyStateEnum.OPEN) { throw new Exception("Open socket timeout."); } }
public async Task <R> Send <R>(CommandMessage cmd) where R : CommandResponse, new() => await CommandsQueue.Enqueue(() => SendCommand <R>(cmd));