Dahua.Api
🇹🇭 ภาษาไทย
Dahua.Api คือ C# NuGet library ที่ wrap Dahua NetSDK สำหรับควบคุมกล้อง CCTV และ NVR ของ Dahua ผ่าน .NET — ใช้งานได้ตั้งแต่ login, ดึงรายการวิดีโอ, download footage, ไปจนถึงดึงข้อมูล config ของอุปกรณ์
ดูใน context ของการใช้งาน: CCTV Integration
ข้อมูลพื้นฐาน
| รายการ | ค่า |
|---|---|
| Package | Dahua.Api |
| Version | 1.0.1 |
| NuGet | dotnet add package Dahua.Api --version 1.0.1 |
| SDK ที่ wrap | Dahua NetSDK v3.057.0000000.0.R.230309 |
| Platform | win-x64 (Windows เท่านั้น) |
| ภาษา | C# / .NET |
| GitHub | github.com/vov4uk/Dahua.Api |
| Author | vov4uk |
Use Cases ที่วางแผนไว้
| กรณีใช้งาน | Component ที่เกี่ยวข้อง |
|---|---|
| สำรองวิดีโอ อัตโนมัติตามเวลา | VideoService.FindFiles + StartDownloadFile |
| การสแกนใบหน้า (Face Recognition) เพื่อลงเวลา | Face detection event stream จากกล้อง |
| เก็บข้อมูลเข้า-ออกรถยนต์ (LPR) | License Plate Recognition event / config |
| Monitor สถานะกล้อง | ConfigService (device info, time sync) |
Architecture
.NET Application
│
▼
DahuaApi (static)
│
┌───┴──────────┐
▼ ▼
Login Cleanup / Init
│
▼
Session (DahuaApi instance)
├── ConfigService → device info, time, serial number
├── VideoService → find files, download, progress
└── AllChannels → NVR channel list
การใช้งานหลัก
1. เริ่มต้นและ Login
// Initialize SDK
DahuaApi.Init();
// Login คืน session object
var session = DahuaApi.Login("192.168.1.63", 37777, "admin", "pass");
// Logout และ cleanup
session.Logout();
DahuaApi.Cleanup();2. ConfigService — ข้อมูลอุปกรณ์
// ชื่อเครื่อง
session.ConfigService.GetMachineName();
// Serial number (สำหรับ audit / ระบุอุปกรณ์)
session.ConfigService.GetDeviceSerialNumber();
// ประเภทอุปกรณ์ (NVR, DVR, IP Camera)
session.ConfigService.GetDeviceType();
// ดึงและตั้งเวลาอุปกรณ์
var time = session.ConfigService.GetTime();
session.ConfigService.SetTime(DateTime.Now);3. VideoService — ค้นหาและ Download วิดีโอ
// ค้นหาวิดีโอในช่วงเวลา (default channel)
var videos = session.VideoService.FindFiles(DateTime.Today, DateTime.Now);
// ระบุ channel (สำหรับ NVR หลายกล้อง)
var videos = session.VideoService.FindFiles(DateTime.Today, DateTime.Now, channel: 2);
// Download พร้อม progress tracking
foreach (var video in videos)
{
var name = $"{video.Date:yyyyMMdd_HHmmss}_{video.Duration}.mp4";
var dest = Path.Combine(@"D:\Backup\CCTV", name);
var downloadId = session.VideoService.StartDownloadFile(video, dest);
if (downloadId > 0)
{
do {
await Task.Delay(5000);
var progress = session.VideoService.GetDownloadPosition(downloadId);
if (progress.downloadSize == progress.totalSize)
{
session.VideoService.StopDownloadFile(downloadId);
break;
}
} while (true);
}
}4. Channel Management (NVR)
// ดูรายการ channel ทั้งหมดใน NVR
var channels = session.AllChannels;
foreach (var ch in channels)
Console.WriteLine($"Channel {ch.Id}: {ch.Name}");Use Case: สำรองวิดีโออัตโนมัติ
Pattern ที่แนะนำสำหรับ backup job:
// Scheduled job (เช่น Hangfire, Windows Task Scheduler, cron)
public async Task BackupYesterdayFootage()
{
DahuaApi.Init();
var session = DahuaApi.Login(ip, port, user, pass);
try
{
var yesterday = DateTime.Today.AddDays(-1);
foreach (var ch in session.AllChannels)
{
var videos = session.VideoService.FindFiles(
yesterday, yesterday.AddDays(1), ch.Id);
foreach (var v in videos)
await DownloadWithRetry(session, v, backupPath);
}
}
finally
{
session.Logout();
DahuaApi.Cleanup();
}
}Use Case: Face Recognition / ลงเวลา
Dahua NVR รองรับ Face Recognition ที่ระดับ firmware แยกออกมาจาก Dahua.Api หลัก:
| ขั้นตอน | วิธี |
|---|---|
| Event stream | Subscribe to face detection events ผ่าน SDK callback |
| Face database | เพิ่ม/ลบใบหน้าใน NVR face library ผ่าน HTTP API หรือ SDK |
| Match result | Event คืน person ID + confidence + timestamp + channel |
| บันทึกลงเวลา | Map person ID → พนักงาน → insert attendance record |
⚠️ Face Recognition API อาจต้องใช้ Dahua HTTP API (port 80) เพิ่มเติม ไม่ใช่แค่ NetSDK
Use Case: LPR (License Plate Recognition) — เข้า-ออกรถยนต์
| ขั้นตอน | วิธี |
|---|---|
| กล้อง LPR | กล้อง Dahua รุ่น LPR หรือ NVR + กล้องความละเอียดสูง |
| Event | LPR event คืน plate number + timestamp + direction (เข้า/ออก) |
| Database | บันทึก plate + time → ระบบจอดรถ / access control |
| Whitelist | Config ทะเบียนที่อนุญาต → trigger gate open |
ข้อจำกัด
| ข้อจำกัด | รายละเอียด |
|---|---|
| Windows only | SDK เป็น win-x64 ไม่รองรับ Linux/macOS |
| SDK version lock | ผูกกับ v3.057 — อุปกรณ์รุ่นใหม่กว่าอาจต้องอัพเดท |
| Single vendor | ใช้ได้กับ Dahua เท่านั้น (ไม่ compatible กับ Hikvision, Axis) |
| Face/LPR API | ฟีเจอร์ขั้นสูงอาจต้องใช้ HTTP API ของ Dahua แยก |
Related
- CCTV Integration — concept: architecture รวม, face recognition workflow, LPR system design
- BLEAF — ระบบ workflow ที่อาจ integrate ข้อมูลลงเวลาจาก CCTV
🇬🇧 English
Dahua.Api is a C# NuGet library wrapping the Dahua NetSDK, enabling .NET applications to control Dahua CCTV cameras and NVRs programmatically — covering login, video retrieval, footage download, and device configuration.
See usage context: CCTV Integration
Package Details
| Item | Value |
|---|---|
| Package | Dahua.Api |
| Version | 1.0.1 |
| Install | dotnet add package Dahua.Api --version 1.0.1 |
| Underlying SDK | Dahua NetSDK v3.057.0000000.0.R.230309 |
| Platform | win-x64 only |
| Language | C# / .NET |
| GitHub | github.com/vov4uk/Dahua.Api |
Planned Use Cases
| Use Case | Primary Components |
|---|---|
| Automated video backup | VideoService.FindFiles + StartDownloadFile |
| Face recognition for attendance | Face detection event stream |
| Vehicle entry/exit logging (LPR) | License Plate Recognition events |
| Device monitoring | ConfigService (health, time sync) |
Session Model
All operations flow through a session object returned by DahuaApi.Login(). The session exposes three main services:
- ConfigService — device identity (name, serial, type) and time management
- VideoService — search recordings by date/channel, download with progress tracking
- AllChannels — enumerate NVR channels for multi-camera installations
Core API
DahuaApi.Init();
var session = DahuaApi.Login(ip, port, user, pass);
// ConfigService
session.ConfigService.GetMachineName();
session.ConfigService.GetDeviceSerialNumber();
session.ConfigService.GetDeviceType();
session.ConfigService.GetTime();
session.ConfigService.SetTime(DateTime.Now);
// VideoService
var videos = session.VideoService.FindFiles(from, to, channelId);
var dlId = session.VideoService.StartDownloadFile(video, destPath);
var progress = session.VideoService.GetDownloadPosition(dlId);
session.VideoService.StopDownloadFile(dlId);
session.Logout();
DahuaApi.Cleanup();Advanced Use Cases
Automated Backup Pattern
Schedule a nightly job that iterates all NVR channels, finds recordings from the previous day, and downloads them to a backup path. Use retry logic and StopDownloadFile as a cleanup step in finally blocks.
Face Recognition → Attendance Dahua NVRs with face detection firmware emit events containing person ID, confidence score, timestamp, and channel. Map person IDs to employee records and insert into an attendance table on each match event. Note: face library management (add/remove faces) may require the Dahua HTTP API (port 80) rather than NetSDK.
LPR → Vehicle Access Log LPR-capable cameras emit plate number + direction (entry/exit) + timestamp events. Store to a vehicle log database, enforce whitelist rules (trigger gate relay), and correlate entry/exit pairs for dwell time reporting.
Limitations
| Limitation | Detail |
|---|---|
| Windows only | win-x64 DLL — no Linux/macOS support |
| SDK version lock | Tied to v3.057; newer firmware may require update |
| Vendor-specific | Dahua only — not compatible with Hikvision, Axis, or ONVIF directly |
| Advanced features | Face recognition and LPR may require supplementary HTTP API calls |
Related
- CCTV Integration — system design: backup architecture, face recognition pipeline, LPR workflow
- BLEAF — enterprise workflow system that could consume attendance data from CCTV