Hello Caffeine
Caffeine Framework의 첫 번째 프로그램을 만들어봅니다.
🎯 학습 목표
이 튜토리얼을 완료하면 다음을 할 수 있습니다:
- Caffeine CLI 도구 사용
- 기본 프로젝트 생성
- 태그 읽기/쓰기
- 로컬 실행 및 테스트
예상 소요 시간: 15분
📋 사전 요구사항
- .NET 10.0 SDK 설치
- Caffeine CLI 설치
- Docker (선택사항)
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("서버 연결 중...");
bool connected = await client.ConnectAsync();
if (!connected)
{
Console.WriteLine("❌ 서버 연결 실패");
return;
}
Console.WriteLine("✅ 서버 연결 성공");
// 3. 태그 읽기
Console.WriteLine("\n📖 태그 읽기:");
var temperature = await client.ReadTagAsync("Equipment1.Temperature");
if (temperature != null)
{
Console.WriteLine($" 온도: {temperature.Value}°C");
Console.WriteLine($" 시각: {temperature.Timestamp:HH:mm:ss}");
Console.WriteLine($" 품질: {temperature.Quality}");
}
// 4. 태그 쓰기
Console.WriteLine("\n✏️ 태그 쓰기:");
bool written = await client.WriteTagAsync("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.ReadTagsAsync(tagNames);
foreach (var (name, value) in values)
{
Console.WriteLine($" {name}: {value.Value}");
}
// 6. 연결 해제
await client.DisconnectAsync();
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!
서버 연결 중...
✅ 서버 연결 성공
📖 태그 읽기:
온도: 72.5°C
시각: 10:30:45
품질: Good
✏️ 태그 쓰기:
설정값 변경 완료: 75.0
📚 여러 태그 읽기:
Equipment1.Temperature: 72.5
Equipment1.Pressure: 101.3
Equipment1.Flow: 45.2
👋 연결 해제 완료
🎓 배운 내용
1. Caffeine Client 사용
var client = new CaffeineClient(options);
await client.ConnectAsync();
2. 태그 읽기
var tagValue = await client.ReadTagAsync("TagName");
3. 태그 쓰기
await client.WriteTagAsync("TagName", value);
4. 배치 읽기
var values = await client.ReadTagsAsync(tagNames);
🔍 문제 해결
서버 연결 실패
증상: ❌ 서버 연결 실패
해결:
- Caffeine 서버가 실행 중인지 확인
- URL이 올바른지 확인 (
https://localhost:5001) - 방화벽 설정 확인
태그를 찾을 수 없음
증상: tagValue == null
해결:
- 태그 이름 확인
- 서버에 태그가 등록되어 있는지 확인
- 드라이버가 실행 중인지 확인
🚀 다음 단계
축하합니다! 첫 번째 Caffeine 프로그램을 완성했습니다.
다음 튜토리얼:
- 첫 번째 드라이버 만들기 - 커스텀 드라이버 개발
- 데이터 파이프라인 - 실시간 데이터 처리
추가 학습:
- Caffeine.Client API - 전체 API 레퍼런스
- 데이터 수집 가이드 - 고급 사용법
완료 시간: 약 15분
난이도: ⭐ 초급
다음: 첫 번째 드라이버 만들기 →