본문으로 건너뛰기

Hello Caffeine

Caffeine Framework의 첫 번째 프로그램을 만들어봅니다.

🎯 학습 목표​

이 튜토리얼을 완료하면 다음을 할 수 있습니다:

  • Caffeine CLI 도구 사용
  • 기본 프로젝트 생성
  • 태그 읽기/쓰기
  • 로컬 실행 및 테스트

예상 소요 시간: 15분


📋 사전 요구사항​

  • .NET 10.0 SDK 설치
  • Caffeine CLI 설치
  • Docker (선택사항)

Caffeine CLI 설치

cafe 명령어를 사용하려면 CLI를 먼저 설치하세요:

dotnet tool install -g NEXCODE.Caffeine.Cli
cafe --version

이미 설치된 경우 업데이트: dotnet tool update -g NEXCODE.Caffeine.Cli

Step 1: 프로젝트 생성​

Caffeine CLI로 새 프로젝트를 생성합니다.

# 프로젝트 생성
cafe init --name HelloCaffeine --template app

# 디렉토리 이동
cd HelloCaffeine

생성된 파일:

HelloCaffeine/
├── HelloCaffeine.csproj
├── Program.cs
└── README.md

Step 2: 코드 작성​

Program.cs를 다음과 같이 수정합니다:

using Caffeine.Client;

Console.WriteLine("🚀 Hello Caffeine!");

// 1. 클라이언트 설정
var options = new CaffeineClientOptions
{
ServerUrl = "https://localhost:5001"
};

var client = new CaffeineClient(options);

try
{
// 2. 서버 연결
Console.WriteLine("서버 연결 중...");
await client.ConnectAsync();
Console.WriteLine("✅ 서버 연결 성공");

// 3. 태그 읽기
Console.WriteLine("\n📖 태그 읽기:");
var temperature = await client.Tags.ReadAsync("Equipment1.Temperature");

if (temperature != null)
{
Console.WriteLine($" 이름: {temperature.Name}");
Console.WriteLine($" 값: {temperature.CurrentValue}");
Console.WriteLine($" 품질: {temperature.Quality}");
}

// 4. 태그 쓰기
Console.WriteLine("\n✏️ 태그 쓰기:");
bool written = await client.Tags.WriteAsync(
"sim-driver", "Equipment1.SetPoint", 75.0);

if (written)
{
Console.WriteLine(" 설정값 변경 완료: 75.0");
}

// 5. 여러 태그 읽기 (배치)
Console.WriteLine("\n📚 여러 태그 읽기:");
var tagNames = new[]
{
"Equipment1.Temperature",
"Equipment1.Pressure",
"Equipment1.Flow"
};

var values = await client.Tags.ReadBatchAsync(tagNames);

foreach (var tag in values)
{
Console.WriteLine($" {tag.Name}: {tag.CurrentValue}");
}

// 6. 연결 해제
await client.DisposeAsync();
Console.WriteLine("\n👋 연결 해제 완료");
}
catch (Exception ex)
{
Console.WriteLine($"❌ 오류: {ex.Message}");
}

Step 3: 의존성 추가​

HelloCaffeine.csproj에 Caffeine.Client 패키지를 추가합니다:

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NEXCODE.Caffeine.Client" Version="2.0.*" />
</ItemGroup>
</Project>

Step 4: 빌드 및 실행​

# 의존성 복원
dotnet restore

# 빌드
dotnet build

# 실행
dotnet run

예상 출력:

🚀 Hello Caffeine!
서버 연결 중...
✅ 서버 연결 성공

📖 태그 읽기:
이름: Equipment1.Temperature
값: { ... }
품질: Good

✏️ 태그 쓰기:
설정값 변경 완료: 75.0

📚 여러 태그 읽기:
Equipment1.Temperature: { ... }
Equipment1.Pressure: { ... }
Equipment1.Flow: { ... }

👋 연결 해제 완료

🎓 배운 내용​

1. Caffeine Client 사용​

var client = new CaffeineClient(options);
await client.ConnectAsync();

2. 태그 읽기​

var tagValue = await client.Tags.ReadAsync("TagName");

3. 태그 쓰기​

await client.Tags.WriteAsync("driverId", "address", value);

4. 배치 읽기​

var values = await client.Tags.ReadBatchAsync(tagNames);

🔍 문제 해결​

서버 연결 실패​

증상: ❌ 서버 연결 실패

해결:

  1. Caffeine 서버가 실행 중인지 확인
  2. URL이 올바른지 확인 (https://localhost:5001)
  3. 방화벽 설정 확인

태그를 찾을 수 없음​

증상: tagValue == null

해결:

  1. 태그 이름 확인
  2. 서버에 태그가 등록되어 있는지 확인
  3. 드라이버가 실행 중인지 확인

🚀 다음 단계​

축하합니다! 첫 번째 Caffeine 프로그램을 완성했습니다.

다음 튜토리얼:

추가 학습:


완료 시간: 약 15분
난이도: ⭐ 초급
다음: 첫 번째 드라이버 만들기 →