private void CarMoveCommand(CarControlCommand carControlCommand) { var now = DateTime.Now; if (now.Subtract(_moveCarLastAction) >= TimeSpan.FromMilliseconds(MOVE_CAR_PERIOD_ACTION_MILLISECONDS) || (!carControlCommand.DirectionControlUp && !carControlCommand.DirectionControlLeft && !carControlCommand.DirectionControlRight && !carControlCommand.DirectionControlDown && !carControlCommand.SpeedControlForward && !carControlCommand.SpeedControlBackward)) { _motorController.MoveCar(carControlCommand, null); _servoController.MoveServo(carControlCommand); _moveCarLastAction = now; Task.Run(async() => { await CarControlCommandTimeout(now); }); } }
private async Task WriteResponse(string relativeUrl, string request, bool requestError, IOutputStream outputStream) { var relativeUrlLower = relativeUrl.ToLowerInvariant(); //Request read not successfully if (requestError) { HttpServerResponse.WriteResponseError("Request wurde nicht vollständig übermittelt oder zu langsam übermittelt und ist daher nicht vollständig.", outputStream); } //Get javascript else if (relativeUrlLower.StartsWith("/javascript")) { HttpServerResponse.WriteResponseFile(ToFolderPath(relativeUrl), HttpContentType.JavaScript, outputStream); } //Get style else if (relativeUrlLower.StartsWith("/styles")) { HttpServerResponse.WriteResponseFile(ToFolderPath(relativeUrl), HttpContentType.Css, outputStream); } //Get image else if (relativeUrlLower.StartsWith("/images")) { HttpServerResponse.WriteResponseFile(ToFolderPath(relativeUrl), HttpContentType.Png, outputStream); } //Toggle automatic drive on/off else if (relativeUrlLower.StartsWith("/automaticdrive")) { var requestBodyResult = GetRequestBody(request); if (!requestBodyResult.Key) { HttpServerResponse.WriteResponseError("Request wurde nicht vollständig übermittelt.", outputStream); } var automaticDriveOn = requestBodyResult.Value["automaticDrive"].GetBoolean(); if (automaticDriveOn) { _automaticDrive.Start(); } else { await _automaticDrive.Stop(); } HttpServerResponse.WriteResponseOk(outputStream); } //Move car with command else if (relativeUrlLower.StartsWith("/carcontrolcommand")) { var requestBodyResult = GetRequestBody(request); if (!requestBodyResult.Key) { HttpServerResponse.WriteResponseError("Request wurde nicht vollständig übermittelt.", outputStream); } var carControlCommandRequest = requestBodyResult.Value; var carControlCommand = new CarControlCommand { DirectionControlUp = carControlCommandRequest["directionControlUp"].GetBoolean(), DirectionControlLeft = carControlCommandRequest["directionControlLeft"].GetBoolean(), DirectionControlRight = carControlCommandRequest["directionControlRight"].GetBoolean(), DirectionControlDown = carControlCommandRequest["directionControlDown"].GetBoolean(), SpeedControlForward = carControlCommandRequest["speedControlForward"].GetBoolean(), SpeedControlBackward = carControlCommandRequest["speedControlBackward"].GetBoolean(), SpeedControlLeftRight = carControlCommandRequest["speedControlLeftRight"].GetNumber(), DirectionControlUpDownStepSpeed = 7 }; if (carControlCommand.DirectionControlLeft && carControlCommand.DirectionControlRight) { carControlCommand.DirectionControlLeft = false; carControlCommand.DirectionControlRight = false; } CarMoveCommand(carControlCommand); HttpServerResponse.WriteResponseOk(outputStream); } //Get car speed else if (relativeUrlLower.StartsWith("/speed")) { var speedResponse = new JsonObject { { "RoundsPerMinute", JsonValue.CreateStringValue(SpeedSensor.RoundsPerMinute.ToString()) }, { "KilometerPerHour", JsonValue.CreateStringValue(string.Format("{0:0.00}", SpeedSensor.KilometerPerHour).Replace(".", ",")) } }; HttpServerResponse.WriteResponseJson(speedResponse.Stringify(), outputStream); } //Get speaker states (on/off) and automatic drive state (on/off) else if (relativeUrlLower.StartsWith("/getstate")) { var stateResponse = new JsonObject { { "CarSpeakerOn", JsonValue.CreateBooleanValue(AudioPlayerController.CarSpeakerOn) }, { "HeadsetSpeakerOn", JsonValue.CreateBooleanValue(AudioPlayerController.HeadsetSpeakerOn) }, { "SoundModeOn", JsonValue.CreateBooleanValue(AudioPlayerController.SoundModeOn) }, { "AutomaticDriveOn", JsonValue.CreateBooleanValue(_automaticDrive.IsRunning) } }; HttpServerResponse.WriteResponseJson(stateResponse.Stringify(), outputStream); } //Set all speaker on else if (relativeUrlLower.StartsWith("/speakeronoff?on=true")) { await AudioPlayerController.SetAllSpeakerOnOff(true); HttpServerResponse.WriteResponseOk(outputStream); } //Set all speaker off else if (relativeUrlLower.StartsWith("/speakeronoff?on=false")) { await AudioPlayerController.SetAllSpeakerOnOff(false); HttpServerResponse.WriteResponseOk(outputStream); } //Set car speaker on else if (relativeUrlLower.StartsWith("/carspeakeronoff?on=true")) { await AudioPlayerController.SetCarSpeakerOnOff(true); HttpServerResponse.WriteResponseOk(outputStream); } //Set car speaker off else if (relativeUrlLower.StartsWith("/carspeakeronoff?on=false")) { await AudioPlayerController.SetCarSpeakerOnOff(false); HttpServerResponse.WriteResponseOk(outputStream); } //Set headset speaker on else if (relativeUrlLower.StartsWith("/headsetspeakeronoff?on=true")) { await AudioPlayerController.SetHeadsetSpeakerOnOff(true); HttpServerResponse.WriteResponseOk(outputStream); } //Set headset speaker off else if (relativeUrlLower.StartsWith("/headsetspeakeronoff?on=false")) { await AudioPlayerController.SetHeadsetSpeakerOnOff(false); HttpServerResponse.WriteResponseOk(outputStream); } //Set sound mode on else if (relativeUrlLower.StartsWith("/soundmodeonoff?on=true")) { await AudioPlayerController.SetSoundModeOnOff(true); HttpServerResponse.WriteResponseOk(outputStream); } //Set sound mode off else if (relativeUrlLower.StartsWith("/soundmodeonoff?on=false")) { await AudioPlayerController.SetSoundModeOnOff(false); HttpServerResponse.WriteResponseOk(outputStream); } //Shutdown Windows else if (relativeUrlLower.StartsWith("/ausschalten")) { await SystemController.Shutdown(); HttpServerResponse.WriteResponseOk(outputStream); } //Restart Windows else if (relativeUrlLower.StartsWith("/neustarten")) { await SystemController.Restart(); HttpServerResponse.WriteResponseOk(outputStream); } //Get debug file else if (relativeUrlLower.StartsWith("/debug")) { var debugFile = await ApplicationData.Current.LocalFolder.TryGetItemAsync(Logger.FILE_NAME); if (debugFile != null) { HttpServerResponse.WriteResponseFileFromLocalFolder(Logger.FILE_NAME, HttpContentType.Text, outputStream); } else { HttpServerResponse.WriteResponseText("Debug file is empty", outputStream); } } //Get camera frame else if (relativeUrlLower.StartsWith("/videoframe")) { if (_camera.Frame != null) { HttpServerResponse.WriteResponseFile(_camera.Frame, HttpContentType.Jpeg, outputStream); } else { HttpServerResponse.WriteResponseError("Not camera fram available. Maybe there is an error or camera is not started.", outputStream); } } //Get desktop html view else if (relativeUrlLower.StartsWith("/desktop")) { HttpServerResponse.WriteResponseFile(@"\Views\Desktop.html", HttpContentType.Html, outputStream); } //Get mobile html view else if (relativeUrlLower.StartsWith("/mobile")) { HttpServerResponse.WriteResponseFile(@"\Views\Mobile.html", HttpContentType.Html, outputStream); } //Get view that redirect to desktop oder mobile view else { HttpServerResponse.WriteResponseFile(@"\Views\Index.html", HttpContentType.Html, outputStream); } }
private async Task StartGamepadReading(Gamepad gamepad) { _isGamepadReadingStopped = false; var buttonDownTimeMiddle = TimeSpan.FromSeconds(2); var buttonDownTimeLong = TimeSpan.FromSeconds(5); var viewButton = new GamepadButtonDown(TimeSpan.Zero, GamepadButtons.View); var menuButton = new GamepadButtonDown(TimeSpan.Zero, GamepadButtons.Menu); var dPadUpButton = new GamepadButtonDown(buttonDownTimeMiddle, GamepadButtons.DPadUp); var dPadDownButton = new GamepadButtonDown(buttonDownTimeMiddle, GamepadButtons.DPadDown); var dPadLeftButton = new GamepadButtonDown(buttonDownTimeMiddle, GamepadButtons.DPadLeft); var dPadRightButton = new GamepadButtonDown(buttonDownTimeMiddle, GamepadButtons.DPadRight); var yButton = new GamepadButtonDown(buttonDownTimeMiddle, GamepadButtons.Y); var bButton = new GamepadButtonDown(buttonDownTimeMiddle, GamepadButtons.B); var xRightShoulderButton = new GamepadButtonDown(buttonDownTimeLong, GamepadButtons.X, GamepadButtons.RightShoulder); var aRightShoulderButton = new GamepadButtonDown(buttonDownTimeLong, GamepadButtons.A, GamepadButtons.RightShoulder); var xRightShoulderButtonNotClickableButtons = new List <GamepadButtons> { GamepadButtons.Y }; var aRightShoulderButtonNotClickableButtons = new List <GamepadButtons> { GamepadButtons.X }; var dPadUpNotClickableButtons = new List <GamepadButtons> { GamepadButtons.DPadDown, GamepadButtons.DPadLeft, GamepadButtons.DPadRight }; var dPadDownNotClickableButtons = new List <GamepadButtons> { GamepadButtons.DPadUp, GamepadButtons.DPadLeft, GamepadButtons.DPadRight }; var dPadLeftNotClickableButtons = new List <GamepadButtons> { GamepadButtons.DPadUp, GamepadButtons.DPadDown, GamepadButtons.DPadRight }; var dPadRightNotClickableButtons = new List <GamepadButtons> { GamepadButtons.DPadUp, GamepadButtons.DPadDown, GamepadButtons.DPadLeft }; while (_gamepad == gamepad) { //gamepad variable could be null var gamepadReadingTry = gamepad?.GetCurrentReading(); if (!gamepadReadingTry.HasValue) { break; } var gamepadReading = gamepadReadingTry.Value; var carMoveCommand = new CarMoveCommand(gamepadReading); var carControlCommand = new CarControlCommand(gamepadReading); //Move car and servo if is new command available if (!(_carStopped && carMoveCommand.Speed == 0.0 && !carControlCommand.DirectionControlUp && !carControlCommand.DirectionControlDown)) { _carStopped = false; _motorController.MoveCar(null, carMoveCommand); _servoController.MoveServo(carControlCommand); } //Car and servo not moving if (carMoveCommand.Speed == 0.0 && !carControlCommand.DirectionControlUp && !carControlCommand.DirectionControlDown) { _carStopped = true; } // Enable/ disable gamepad vibration var viewButtonResult = viewButton.UpdateGamepadButtonState(gamepadReading); if (viewButtonResult.ButtonClicked) { _gamepadShouldVibrate = !_gamepadShouldVibrate; if (_gamepadShouldVibrate) { await AudioPlayerController.Play(AudioName.GamepadVibrationOn); } else { await AudioPlayerController.Play(AudioName.GamepadVibrationOff); } } //Automatic drive toggle var menuButtonResult = menuButton.UpdateGamepadButtonState(gamepadReading); if (menuButtonResult.ButtonClicked) { await _automaticDrive.StartStopToggle(); } var dPadUpButtonResult = dPadUpButton.UpdateGamepadButtonState(gamepadReading, dPadUpNotClickableButtons); var dPadDownButtonResult = dPadDownButton.UpdateGamepadButtonState(gamepadReading, dPadDownNotClickableButtons); var dPadLeftButtonResult = dPadLeftButton.UpdateGamepadButtonState(gamepadReading, dPadLeftNotClickableButtons); var dPadRightButtonResult = dPadRightButton.UpdateGamepadButtonState(gamepadReading, dPadRightNotClickableButtons); //All speaker on if (dPadUpButtonResult.ButtonClicked) { await AudioPlayerController.SetAllSpeakerOnOff(true); } //All speaker off if (dPadDownButtonResult.ButtonClicked) { await AudioPlayerController.SetAllSpeakerOnOff(false); } //Car speaker on/off toggle if (dPadLeftButtonResult.ButtonClicked) { await AudioPlayerController.SetCarSpeakerOnOffToggle(); } //Headset speaker on/off toggle if (dPadRightButtonResult.ButtonClicked) { await AudioPlayerController.SetHeadsetSpeakerOnOffToggle(); } //Sound mode on/off toggle var yButtonResult = yButton.UpdateGamepadButtonState(gamepadReading); if (yButtonResult.ButtonClicked) { await AudioPlayerController.SetSoundModeOnOffToggle(); } //Speak current speaker and sound mode state var bButtonResult = bButton.UpdateGamepadButtonState(gamepadReading); if (bButtonResult.ButtonClicked) { await AudioPlayerController.PlaySpeakerOnOffSoundMode(); } //Shutdown var xRightShoulderButtonResult = xRightShoulderButton.UpdateGamepadButtonState(gamepadReading, xRightShoulderButtonNotClickableButtons); if (xRightShoulderButtonResult.ButtonClicked) { _isGamepadReadingStopped = true; await SystemController.Shutdown(); } //Restart var aRightShoulderButtonResult = aRightShoulderButton.UpdateGamepadButtonState(gamepadReading, aRightShoulderButtonNotClickableButtons); if (aRightShoulderButtonResult.ButtonClicked) { _isGamepadReadingStopped = true; await SystemController.Restart(); } await Task.Delay(25); } _isGamepadReadingStopped = true; }