public static async Task <RoboRioConnection> StartConnectionTaskAsync(string teamNumberS) { int teamNumber; int.TryParse(teamNumberS, out teamNumber); if (teamNumber < 0) { teamNumber = 0; } var m_roboRioConnection = new RoboRioConnection(teamNumber, TimeSpan.FromSeconds(2)); bool connected = await m_roboRioConnection.GetConnectionAsync().ConfigureAwait(false); if (connected) { StringBuilder builder = new StringBuilder(); ConnectionStatus status = m_roboRioConnection.ConnectionStatus; builder.AppendLine("Connected to RoboRIO..."); builder.AppendLine("Interface: " + status.ConnectionType); builder.Append("IP Address: " + status.ConnectionIp.ToString()); await OutputWriter.Instance.WriteLineAsync(builder.ToString()).ConfigureAwait(false); } else { await OutputWriter.Instance.WriteLineAsync("Failed to Connect to RoboRIO...").ConfigureAwait(false); } return(m_roboRioConnection); }
public static async Task <bool> CheckAndDeployNativeLibrariesAsync(string remoteDirectory, string propertiesName, string dirToUpload, IList <string> ignoreFiles, RoboRioConnection rioConn) { MemoryStream stream = new MemoryStream(); bool readFile = await rioConn.ReceiveFileAsync($"{remoteDirectory}/{propertiesName}.properties", stream, ConnectionUser.LvUser).ConfigureAwait(false); string nativeLoc = dirToUpload; var fileMd5List = GetMD5ForFiles(nativeLoc, ignoreFiles); if (fileMd5List == null) { await OutputWriter.Instance.WriteLineAsync( "Native libraries cannot be found. Please see our troubleshooting site (https://robotdotnet.github.io/articles/troubleshooting.html) for instructions for fixing this.") .ConfigureAwait(false); return(false); } if (!readFile) { // Libraries definitely do not exist, deploy return(await DeployNativeLibrariesAsync(fileMd5List, remoteDirectory, propertiesName, rioConn).ConfigureAwait(false)); } stream.Position = 0; bool foundError = false; int readCount = 0; StreamReader reader = new StreamReader(stream); string line = null; while (!string.IsNullOrWhiteSpace(line = reader.ReadLine())) { // Split line at = string[] split = line.Split('='); if (split.Length < 2) { continue; } readCount++; foreach (Tuple <string, string> tuple in fileMd5List) { if (split[0] == tuple.Item1) { // Found a match file name if (split[1] != tuple.Item2) { foundError = true; } break; } } if (foundError) { break; } } reader.Dispose(); if (foundError || readCount != fileMd5List.Count) { return(await DeployNativeLibrariesAsync(fileMd5List, remoteDirectory, propertiesName, rioConn).ConfigureAwait(false)); } await OutputWriter.Instance.WriteLineAsync("Native libraries exist. Skipping deploy").ConfigureAwait(false); return(true); }
public static async Task <bool> DeployNativeLibrariesAsync(List <Tuple <string, string> > files, string remoteDirectory, string propertiesName, RoboRioConnection rioConn) { List <string> fileList = new List <string>(files.Count); bool nativeDeploy = false; string tempFile = Path.Combine(Path.GetTempPath(), $"{propertiesName}.properties"); using (FileStream memStream = new FileStream(tempFile, FileMode.Create)) using (StreamWriter writer = new StreamWriter(memStream)) { foreach (Tuple <string, string> tuple in files) { fileList.Add(tuple.Item1); await writer.WriteLineAsync($"{tuple.Item1}={tuple.Item2}").ConfigureAwait(false); } writer.Flush(); fileList.Add(tempFile); } await OutputWriter.Instance.WriteLineAsync("Deploying native files").ConfigureAwait(false); nativeDeploy = await rioConn.DeployFiles(fileList, remoteDirectory, ConnectionUser.Admin).ConfigureAwait(false); // TODO: Figure out why trying to deploy the raw MemoryStream was Deadlocking //md5Deploy = await rioConn.DeployFileAsync(memStream, $"{remoteDirectory}/{propertiesName}32.properties", ConnectionUser.Admin).ConfigureAwait(false); await rioConn.RunCommandAsync("ldconfig", ConnectionUser.Admin).ConfigureAwait(false); try { File.Delete(tempFile); } catch (Exception) { } return(nativeDeploy); }
//Uploads code to the robot and then runs it. public async Task <bool> DeployCodeAsync(string teamNumber, bool debug, Project robotProject) { var writer = OutputWriter.Instance; if (robotProject == null) { await writer.WriteLineAsync("Robot Project not valid. Contact RobotDotNet for support.").ConfigureAwait(false); return(false); } //Connect to Robot Async await OutputWriter.Instance.WriteLineAsync("Attempting to Connect to RoboRIO").ConfigureAwait(false); Task <RoboRioConnection> rioConnectionTask = RoboRioConnection.StartConnectionTaskAsync(teamNumber); CodeReturnStruct codeReturn = await BuildAndPrepareCodeAsync(debug, robotProject).ConfigureAwait(false); if (codeReturn == null) { return(false); } await writer.WriteLineAsync("Waiting for Connection to Finish").ConfigureAwait(false); // Kill our connection if we have already ran once with this same object. m_roboRioConnection?.Dispose(); m_roboRioConnection = await rioConnectionTask.ConfigureAwait(false); if (m_roboRioConnection.Connected) { //Connected successfully await OutputWriter.Instance.WriteLineAsync("Successfully Connected to RoboRIO.").ConfigureAwait(false); if (!await CheckMonoInstallAsync().ConfigureAwait(false)) { //TODO: Make this error message better await OutputWriter.Instance.WriteLineAsync("Mono not properly installed. Please try reinstalling to Mono Runtime.").ConfigureAwait(false); return(false); } await OutputWriter.Instance.WriteLineAsync("Mono correctly installed").ConfigureAwait(false); await OutputWriter.Instance.WriteLineAsync("Checking RoboRIO Image").ConfigureAwait(false); if (!await CheckRoboRioImageAsync().ConfigureAwait(false)) { // Ignore image requirement on selected option if (!SettingsProvider.ExtensionSettingsPage.IgnoreImageRequirements) { await OutputWriter.Instance.WriteLineAsync( "RoboRIO Image does not match plugin, allowed image versions: " + string.Join(", ", DeployProperties.RoboRioAllowedImages.ToArray())).ConfigureAwait(false); await OutputWriter.Instance.WriteLineAsync( "Please follow FIRST's instructions on imaging your RoboRIO, and try again.").ConfigureAwait(false); return(false); } } await OutputWriter.Instance.WriteLineAsync("RoboRIO Image Correct").ConfigureAwait(false); //Force making mono directory await CreateMonoDirectoryAsync().ConfigureAwait(false); bool nativeDeploy = await CachedFileHelper.CheckAndDeployNativeLibrariesAsync(DeployProperties.UserLibraryDir, "WPI_Native_Libraries", await GetProjectPathAsync(robotProject).ConfigureAwait(false) + "wpinative" + Path.DirectorySeparatorChar, new List <string>(), m_roboRioConnection).ConfigureAwait(false); if (!nativeDeploy) { await OutputWriter.Instance.WriteLineAsync("Failed to deploy native files.").ConfigureAwait(false); return(false); } //DeployAllFiles bool retVal = await DeployRobotFilesAsync(codeReturn.RobotFiles).ConfigureAwait(false); if (!retVal) { await OutputWriter.Instance.WriteLineAsync("File deploy failed.").ConfigureAwait(false); return(false); } await OutputWriter.Instance.WriteLineAsync("Successfully Deployed Files. Starting Code.").ConfigureAwait(false); await StartRobotCodeAsync(codeReturn.RobotExe, debug, robotProject).ConfigureAwait(false); await OutputWriter.Instance.WriteLineAsync("Successfully started robot code.").ConfigureAwait(false); return(true); } else { //Failed to connect await writer.WriteLineAsync("Failed to Connect to RoboRIO. Exiting.").ConfigureAwait(false); return(false); } }