public Status SendFile(RioFile file) { /* This will only happen on second tab, so don't bother logging */ Status error = Status.Ok; SftpClient client = new SftpClient(_host.GetHostName(), "admin", ""); /* Check if file exists on PC */ if (error == Status.Ok) { if (file.CheckExistanceOnPC() == false) { /* could not open file? */ Log(" Could not open file:" + file.TargetPath); error = Status.CouldNotOpenFile; } } return(SendFileContents(file.GetContents(), file.TargetPath)); }
public Status SendFile(RioFile file) { /* This will only happen on second tab, so don't bother logging */ Status error = Status.Ok; /* Check if file exists on PC */ if (error == Status.Ok) { if (file.CheckExistanceOnPC() == false) { /* could not open file? */ Log(" Could not open file:" + file.TargetPath); error = Status.CouldNotOpenFile; } } if (error == Status.Ok) { error = SendFileContents(file.GetContents(), file.TargetPath); } return(error); }
public void UpdateRobotController(bool bInstallAnimIntoRioWebServer) { SftpClient ftpClient = null; int error = 0; /* start stop watch */ Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); /* copy a mutable list of File objects */ ArrayList files = new ArrayList(); foreach (RioFile sourceFile in RioFiles.kFilesToCreate) { files.Add(sourceFile); } /* copy a mutable list of File objects to delete */ ArrayList filesToDel = new ArrayList(); foreach (string sourceFile in RioFiles.kFilesToDeleteBeforeInstall) { filesToDel.Add(sourceFile); } /* throw out whatever needs to be removed */ if (bInstallAnimIntoRioWebServer == false) { /* loop thru all tentaive target filesand throw out js files */ for (int i = 0; i < files.Count;) { RioFile file = (RioFile)files[i]; if (file.Sourcename.Contains(".js")) { /* remove file, now i will point to next elem */ files.RemoveAt(i); } else { /* go to next elem */ ++i; } } } /* start connection */ if (error == 0) { Log("Connecting to roboRIO " + DateTime.Now.ToString("MM/dd/yyyy h:mm tt")); ftpClient = new SftpClient(_host.GetHostName(), "admin", ""); ftpClient.ConnectionInfo.Timeout = new TimeSpan(0, 0, 0, 0, 6000); try { ftpClient.Connect(); } catch (Exception) { error = -1; } if (error == 0) { Log("Connected sucessfully."); } else { Log("Could not connect."); } } /* stop diag server if running already */ if (error == 0) { SendCommand("Attempting to close server", "/etc/init.d/Phoenix-diagnostics-server stop"); /* even if this fails, keep going */ } /* check file presence */ if (error == 0) { foreach (RioFile file in files) { if (file.CheckExistanceOnPC() == false) { /* could not open file? */ Log(" Could not open file:" + file.TargetPath); error = -10; } } } /* backup what needs to be backed up */ if (error == 0) { foreach (string filepath in RioFiles.kFilesToBackup) { string backUpPath = filepath + ".bak"; if (ftpClient.Exists(backUpPath)) { /* already there, don't change anything */ } else if (ftpClient.Exists(filepath) == false) { /* not there, don't bother */ } else { /* copy command */ String command = "cp " + filepath + " " + backUpPath; SendCommand(command); } } } /* delete them the diag related files */ if (error == 0) { Log("Removing old files..."); foreach (string filePathToDel in filesToDel) { if (ftpClient.Exists(filePathToDel) == false) { /* do nothing */ } else { /* delete the target */ try { ftpClient.DeleteFile(filePathToDel); } catch (Exception) { } } } } /* write new files */ if (error == 0) { Log("Writing files..."); foreach (RioFile file in files) { byte[] content = file.GetContents(); if (content == null) { Log("Failed to read " + file.SourcePath); error = -30; } else { /* delete file first, regardless of success, continue */ try { /* if file exists, delete it */ if (ftpClient.Exists(file.TargetPath)) { ftpClient.DeleteFile(file.TargetPath); } } catch (Exception) { } /* write it */ try { ftpClient.WriteAllBytes(file.TargetPath, content); Log("Written file: " + file.TargetPath + " (" + file.Sourcename + ")"); } catch (Exception) { Log("Failed to write " + file.TargetPath); error = -70; } } } } /* always close the sftp */ if (ftpClient != null) { try { ftpClient.Disconnect(); } catch (Exception) { } } /* permissions */ if (error == 0) { Log("Updating File Write Permissions"); SendCommand("chmod 777 /etc/init.d/Phoenix-diagnostics-server"); SendCommand("chmod 777 /usr/local/frc/bin/Phoenix-diagnostics-server"); Thread.Sleep(1000); //Wait a bit to make sure any file changes actually took } /* sync */ if (error == 0) { Log("Syncing RoboRIO to ensure files are on the flash"); SendCommand("sync"); Thread.Sleep(1000); //Wait a bit to make sure any file changes actually took } /* Create Symlink for Phoenix-diagnostics-server in etc/rc5.d/ */ if (error == 0) { Log("Creating/Updating Symlink for startup"); if (false == SendCommand("ln -sf /etc/init.d/Phoenix-diagnostics-server /etc/rc5.d/S25Phoenix-diagnostics-server")) { error = -39; } } /* restart server */ if (error == 0) { if (false == SendCommand("Starting Server", "/etc/init.d/Phoenix-diagnostics-server start")) { error = -40; Log("Server could not be started, you many need to restart the RoboRIO. "); } else { Log("Server has successfully started."); } } /* stop and report time */ OnFinished(error); stopWatch.Stop(); TimeSpan ts = stopWatch.Elapsed; string elapsedTime = string.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10); Log("\nDuration: " + elapsedTime); }