Dahua.Api

🇹🇭 ภาษาไทย

Dahua.Api คือ C# NuGet library ที่ wrap Dahua NetSDK สำหรับควบคุมกล้อง CCTV และ NVR ของ Dahua ผ่าน .NET — ใช้งานได้ตั้งแต่ login, ดึงรายการวิดีโอ, download footage, ไปจนถึงดึงข้อมูล config ของอุปกรณ์

ดูใน context ของการใช้งาน: CCTV Integration

ข้อมูลพื้นฐาน

รายการค่า
PackageDahua.Api
Version1.0.1
NuGetdotnet add package Dahua.Api --version 1.0.1
SDK ที่ wrapDahua NetSDK v3.057.0000000.0.R.230309
Platformwin-x64 (Windows เท่านั้น)
ภาษาC# / .NET
GitHubgithub.com/vov4uk/Dahua.Api
Authorvov4uk

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 streamSubscribe to face detection events ผ่าน SDK callback
Face databaseเพิ่ม/ลบใบหน้าใน NVR face library ผ่าน HTTP API หรือ SDK
Match resultEvent คืน 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 + กล้องความละเอียดสูง
EventLPR event คืน plate number + timestamp + direction (เข้า/ออก)
Databaseบันทึก plate + time → ระบบจอดรถ / access control
WhitelistConfig ทะเบียนที่อนุญาต → trigger gate open

ข้อจำกัด

ข้อจำกัดรายละเอียด
Windows onlySDK เป็น win-x64 ไม่รองรับ Linux/macOS
SDK version lockผูกกับ v3.057 — อุปกรณ์รุ่นใหม่กว่าอาจต้องอัพเดท
Single vendorใช้ได้กับ Dahua เท่านั้น (ไม่ compatible กับ Hikvision, Axis)
Face/LPR APIฟีเจอร์ขั้นสูงอาจต้องใช้ HTTP API ของ Dahua แยก
  • 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

ItemValue
PackageDahua.Api
Version1.0.1
Installdotnet add package Dahua.Api --version 1.0.1
Underlying SDKDahua NetSDK v3.057.0000000.0.R.230309
Platformwin-x64 only
LanguageC# / .NET
GitHubgithub.com/vov4uk/Dahua.Api

Planned Use Cases

Use CasePrimary Components
Automated video backupVideoService.FindFiles + StartDownloadFile
Face recognition for attendanceFace detection event stream
Vehicle entry/exit logging (LPR)License Plate Recognition events
Device monitoringConfigService (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

LimitationDetail
Windows onlywin-x64 DLL — no Linux/macOS support
SDK version lockTied to v3.057; newer firmware may require update
Vendor-specificDahua only — not compatible with Hikvision, Axis, or ONVIF directly
Advanced featuresFace recognition and LPR may require supplementary HTTP API calls
  • CCTV Integration — system design: backup architecture, face recognition pipeline, LPR workflow
  • BLEAF — enterprise workflow system that could consume attendance data from CCTV