4.9 파일 서비스 클래스
개요
FileManager 는 호스트와 로봇 컨트롤러 간의 프로그램 /trajectory/temporary 파일을 업로드, 다운로드, 삭제 및 검색하는 데 사용됩니다.
핵심 기능
- 컨트롤러에 로컬 파일 업로드
- 컨트롤러 파일을 로컬 경로로 다운로드
- 컨트롤러에서 파일 삭제
- 파일 이름 패턴으로 파일 검색
- 여러 파일 형식 관리(
UserProgram,BlockProgram,TrajectoryProgram,TmpFile) - 업로드 중 덮어쓰기 동작 제어/download
사용 사례
- 컨트롤러에 프로그램 배포
- 컨트롤러 측 프로그램 및 궤적 백업
- 생산 라인 디버깅 데이터 동기화
- 컨트롤러의 배치 파일 관리
4.9.1 로봇에 로컬 파일 업로드
| 메서드 이름 | FileManager.Upload(string filePath , FileType ft , bool overWriting = false) |
|---|---|
| 설명 | 로봇 컨트롤러에 로컬 파일을 업로드합니다. |
| 요청 매개변수 | filePath : string 업로드할 로컬 파일의 절대 경로 ft : FileType 업로드할 파일 유형 overWriting : bool 로봇 컨트롤러의 기존 파일을 덮어쓸지 여부, 기본값은 false(덮어쓰기 없음) |
| 메모 | USER_PROGRAM 및 BLOCK_PROGRAM의 경우 .xml / .block 파일의 전체 경로를 제공하십시오. 시스템은 또한 동일한 이름의 .json (및 BlockProgram의 경우 .xml )를 업로드합니다. |
| 반환 값 | StatusCode: 업로드 작업 실행 결과 |
| 호환 로봇 소프트웨어 버전 | 협업(Copper): v7.5.0.0+ 산업용(Bronze): v7.5.0.0+ |
4.9.2 로컬 머신에 로봇 파일 다운로드
| 메서드 이름 | FileManager.Download(string fileName , FileType ft , string savePath ) |
|---|---|
| 설명 | 로봇 컨트롤러에서 로컬로 파일을 다운로드합니다. |
| 요청 매개변수 | fileName : string 다운로드할 파일 이름 ft : FileType 다운로드할 파일 유형 savePath : string 다운로드한 파일의 로컬 저장 경로 |
| 메모 | UserProgram / BlockProgram / TrajectoryProgram ,의 경우 프로그램 이름(확장자가 없는 파일 이름)만 지정하십시오. TmpFile 의 경우 확장명을 포함한 전체 파일 이름을 제공하십시오. |
| 반환 값 | StatusCode: 작업 실행 결과 다운로드 |
| 호환 로봇 소프트웨어 버전 | 협업(Copper): v7.5.0.0+ 산업용(Bronze): v7.5.0.0+ |
4.9.3 로봇에서 파일 삭제
| 메서드 이름 | FileManager.Delete(string fileName , FileType ft ) |
|---|---|
| 설명 | 로봇 컨트롤러에서 파일을 삭제합니다. |
| 요청 매개변수 | fileName : 문자열 삭제할 파일 이름 ft : FileType 삭제할 파일 유형 |
| 메모 | UserProgram / BlockProgram / TrajectoryProgram ,의 경우 프로그램 이름(확장자가 없는 파일 이름)만 지정하십시오. TmpFile 의 경우 확장명을 포함한 전체 파일 이름을 제공하십시오. |
| 반환 값 | StatusCode: 작업 실행 결과 삭제 |
| 호환 로봇 소프트웨어 버전 | 협업(Copper): v7.5.0.0+ 산업용(Bronze): v7.5.0.0+ |
예제 코드
cs
using System.Collections.Generic;
using System.IO;
using Agilebot.IR;
using Agilebot.IR.FileManager;
using Agilebot.IR.Types;
public class UserProgramOperations
{
/// <summary>
/// 测试用户程序文件的完整操作流程:上传、下载、搜索和删除
/// </summary>
public static StatusCode Run(string controllerIP, bool useLocalProxy = true)
{
// [ZH] 初始化捷勃特机器人
// [EN] Initialize the Agilebot robot
Arm controller = new Arm(controllerIP, useLocalProxy);
// [ZH] 连接捷勃特机器人
// [EN] Connect to the Agilebot robot
StatusCode code = controller.ConnectSync();
Console.WriteLine(code != StatusCode.OK ? code.GetDescription() : "连接成功/Successfully connected.");
if (code != StatusCode.OK)
{
return code;
}
try
{
Console.WriteLine("开始用户程序文件操作测试/Starting User Program File Operations Test");
// [ZH] 获取测试文件路径
// [EN] Get test file path
string file_user_program = GetTestFilePath("test_prog.xml");
string fileName = "test_prog";
string save_path = GetTestFilePath("download");
// [ZH] 上传用户程序文件
// [EN] Upload user program file
code = controller.FileManager.Upload(file_user_program, FileType.UserProgram, true);
if (code == StatusCode.OK)
{
Console.WriteLine($"用户程序文件上传成功/User Program File Upload Success: {fileName}");
}
else
{
Console.WriteLine($"用户程序文件上传失败/User Program File Upload Failed: {code.GetDescription()}");
return code;
}
// [ZH] 等待下载
// [EN] Wait before download
Thread.Sleep(1000);
// [ZH] 下载用户程序文件
// [EN] Download user program file
code = controller.FileManager.Download(fileName, FileType.UserProgram, save_path);
if (code == StatusCode.OK)
{
Console.WriteLine($"用户程序文件下载成功/User Program File Download Success: {fileName}");
}
else
{
Console.WriteLine($"用户程序文件下载失败/User Program File Download Failed: {code.GetDescription()}");
return code;
}
// [ZH] 搜索用户程序文件
// [EN] Search user program file
List<string> results = new List<string>();
(results, code) = controller.FileManager.Search(fileName);
if (code == StatusCode.OK)
{
Console.WriteLine($"用户程序文件搜索成功/User Program File Search Success");
Console.WriteLine($"搜索结果数量/Search Results Count: {results.Count}");
foreach (var result in results)
{
Console.WriteLine($" 找到文件/Found File: {result}");
}
}
else
{
Console.WriteLine($"用户程序文件搜索失败/User Program File Search Failed: {code.GetDescription()}");
return code;
}
// [ZH] 等待删除
// [EN] Wait before delete
Thread.Sleep(1000);
// [ZH] 删除用户程序文件
// [EN] Delete user program file
code = controller.FileManager.Delete(fileName, FileType.UserProgram);
if (code == StatusCode.OK)
{
Console.WriteLine($"用户程序文件删除成功/User Program File Delete Success: {fileName}");
}
else
{
Console.WriteLine($"用户程序文件删除失败/User Program File Delete Failed: {code.GetDescription()}");
return code;
}
Console.WriteLine("用户程序文件操作测试完成/User Program File Operations Test Completed");
}
catch (Exception ex)
{
Console.WriteLine($"执行过程中发生异常/Exception occurred during execution: {ex.Message}");
code = StatusCode.OtherReason;
}
finally
{
// [ZH] 关闭连接
// [EN] Close the connection
StatusCode disconnectCode = controller.Disconnect();
if (disconnectCode != StatusCode.OK)
{
Console.WriteLine(disconnectCode.GetDescription());
if (code == StatusCode.OK)
code = disconnectCode;
}
}
return code;
}
/// <summary>
/// 获取test_files文件夹中文件的路径示例方法
/// 展示如何获取当前程序目录下的test_files文件夹中的文件路径
/// </summary>
private static string GetTestFilePath(string fileName)
{
// [ZH] 获取当前程序集的目录
// [EN] Get current assembly directory
string? codeFilePath = new System.Diagnostics.StackTrace(true).GetFrame(0)?.GetFileName();
if (string.IsNullOrEmpty(codeFilePath))
{
throw new InvalidOperationException("无法获取当前文件路径/Cannot get current file path");
}
string? codeDirectory = Path.GetDirectoryName(codeFilePath);
if (string.IsNullOrEmpty(codeDirectory))
{
throw new InvalidOperationException("无法获取当前目录路径/Cannot get current directory path");
}
// [ZH] 构建test_files文件夹路径
// [EN] Build test_files folder path
string testFilesDirectory = Path.Combine(codeDirectory, "test_files");
// [ZH] 构建文件完整路径
// [EN] Build complete file path
string filePath = Path.Combine(testFilesDirectory, fileName);
return filePath;
}
}4.9.4 파일 이름 패턴으로 파일 검색
| 메서드 이름 | FileManager.Search(string pattern , ref List<string> fl ) |
|---|---|
| 설명 | 파일 이름 패턴과 일치하는 로봇 컨트롤러의 파일을 검색합니다. |
| 요청 매개변수 | pattern : 문자열 파일 이름 일치 패턴 fl : ref List<string> 반환된 파일 목록 |
| 반환 값 | StatusCode: 검색 연산 실행 결과 |
| 호환 로봇 소프트웨어 버전 | 협업(Copper): v7.5.0.0+ 산업용(Bronze): v7.5.0.0+ |