Skip to content

Latest commit

 

History

History
239 lines (181 loc) · 7.26 KB

File metadata and controls

239 lines (181 loc) · 7.26 KB

Dahua Focus Manager

Automatically manages day/night mode transitions and triggers autofocus on Dahua cameras at optimal times.

Problem

Dahua cameras with motorized zoom lenses can go out of focus when switching between day and night modes (when the IR cut filter engages/disengages). This is especially problematic for LPR (License Plate Recognition) cameras where:

  1. The optimized exposure settings result in very dark night images
  2. The camera's auto day/night switch happens when it's too dark for autofocus to work properly

Solution

Instead of relying on the camera's automatic day/night switching, this script:

  1. Takes full control of day/night mode transitions via DayNightColor setting
  2. Uses sunrise/sunset times (via astral library) to schedule transitions
  3. Triggers transitions early - before sunset and after sunrise when there's still good light
  4. Runs autofocus during each transition when lighting conditions are optimal
  5. Preserves your settings - your configured gain, exposure, aperture, etc. are not modified

How It Works

  1. Cameras are set to forced Day or Night mode (DayNightColor=1 or 2)
  2. Script calculates optimal transition times based on your location's sunrise/sunset
  3. Before sunset (default: 30 min before, while still light): forces Night mode + autofocus
  4. After sunrise (default: 30 min after, with good light): forces Day mode + autofocus
  5. Your VideoInOptions settings (gain, exposure, etc.) are preserved - only the mode switching is controlled

DayNightColor Values

  • DayNightColor=0 → Auto (camera decides - NOT used by this script)
  • DayNightColor=1 → Day (Color mode, IR cut filter engaged)
  • DayNightColor=2 → Night (B&W mode, IR cut filter disengaged)

Requirements

  • Python 3.9+ (for zoneinfo)
  • requests library
  • astral library (for sunrise/sunset calculation)
pip install requests astral

Configuration

1. Create config directory

mkdir -p ~/.config/dahua-focus-manager

2. Create credentials file

cp credentials.example ~/.config/dahua-focus-manager/credentials
# Edit with your actual credentials

The credentials file format:

DAHUA_USER=admin
DAHUA_PASS=your_password_here

Credentials are searched in this order:

  1. ~/.config/dahua-focus-manager/credentials
  2. ~/.dahua_creds
  3. /etc/dahua-focus-manager/credentials

3. Create config file

cp config.example.json ~/.config/dahua-focus-manager/config.json
# Edit with your camera IPs and location

Example config:

{
    "cameras": [
        {"name": "front_door", "ip": "192.168.1.100"},
        {"name": "back_yard", "ip": "192.168.1.101"}
    ],
    "location": {
        "latitude": 45.0,
        "longitude": -93.0,
        "timezone": "America/Chicago"
    },
    "night_transition_offset": -30,
    "day_transition_offset": 30,
    "poll_interval": 60,
    "autofocus_wait": 15,
    "session_refresh_interval": 120,
    "health_check_file": "/tmp/dahua_focus_manager_health",
    "brightness_log_file": "/tmp/dahua_brightness_log.csv"
}

Configuration Options

Option Default Description
cameras - List of cameras with name and ip
location.latitude 45.0 Your latitude for sunrise/sunset calculation
location.longitude -93.0 Your longitude (negative for West)
location.timezone America/Chicago Your timezone (IANA format)
night_transition_offset -30 Minutes relative to sunset (negative = before)
day_transition_offset 30 Minutes relative to sunrise (positive = after)
poll_interval 60 Seconds between time checks
autofocus_wait 15 Seconds to wait for autofocus to complete
session_refresh_interval 120 Seconds between session keepalives
health_check_file /tmp/dahua_focus_manager_health Path for health check JSON
brightness_log_file /tmp/dahua_brightness_log.csv Path for brightness sensor CSV log

Environment Variables

  • DAHUA_CONFIG_DIR - Override config directory (default: ~/.config/dahua-focus-manager)

Installation

Manual Run

python3 dahua_focus_manager.py

Systemd Service (Linux)

  1. Copy the script:
sudo cp dahua_focus_manager.py /usr/local/bin/
  1. Copy and edit the service file:
sudo cp dahua-focus-manager.service /etc/systemd/system/
# Edit the service file to set the correct User
sudo nano /etc/systemd/system/dahua-focus-manager.service
  1. Reload systemd and enable the service:
sudo systemctl daemon-reload
sudo systemctl enable dahua-focus-manager
sudo systemctl start dahua-focus-manager
  1. Check status:
sudo systemctl status dahua-focus-manager
journalctl -u dahua-focus-manager -f

Health Check

The script writes a JSON health check file (default: /tmp/dahua_focus_manager_health) every 60 seconds:

{
  "timestamp": "2024-01-15T18:30:00.000000",
  "sunrise": "2024-01-15T07:42:00.000000",
  "sunset": "2024-01-15T17:10:00.000000",
  "day_transition": "2024-01-15T08:12:00.000000",
  "night_transition": "2024-01-15T16:40:00.000000",
  "current_target_mode": "night",
  "cameras": [
    {
      "name": "front_door",
      "ip": "192.168.1.100",
      "current_mode": "night",
      "session_active": true,
      "consecutive_failures": 0,
      "last_autofocus": "2024-01-15T16:40:30.000000"
    }
  ]
}

Brightness Logging

The script logs brightness sensor data every 60 seconds to a CSV file (default: /tmp/dahua_brightness_log.csv). This data can be used to:

  • Analyze light patterns throughout the day
  • Optimize transition timing based on actual brightness levels
  • Detect anomalies like storms that cause early darkness

CSV columns:

  • timestamp - ISO format timestamp with timezone
  • camera - Camera name
  • brightness_0, brightness_1 - Brightness sensor values (0-100+)
  • gain_0, gain_1 - Current gain values (higher = darker conditions)
  • exposure_0, exposure_1 - Current exposure values
  • dnc_mode - Current IR filter state (0=Night, 1=Day)
  • need_fill_light - Camera's assessment if more light is needed (0/1)

Example analysis:

# View recent brightness data
tail -20 /tmp/dahua_brightness_log.csv

# Check readings around transition times
grep -E 'T(07|08|16|17):' /tmp/dahua_brightness_log.csv

Tested Cameras

  • Dahua IPC-HFW5442H-ZHE (and similar models with motorized zoom)

Troubleshooting

Camera not responding

  • Verify the camera IP is correct and reachable
  • Check credentials in your credentials file
  • Ensure the camera's web interface is accessible

Autofocus not working well

  • Adjust night_transition_offset to trigger earlier (e.g., -45 for 45 min before sunset)
  • Adjust day_transition_offset to trigger later (e.g., 45 for 45 min after sunrise)
  • Increase autofocus_wait if autofocus needs more time

Wrong sunrise/sunset times

  • Verify your latitude and longitude are correct
  • Check timezone setting matches your location
  • Longitude should be negative for Western Hemisphere

Camera shows "Custom" mode in UI

  • This is expected! The script uses DayNightColor to force day/night mode
  • Your configured settings (gain, exposure, etc.) are still applied
  • The camera UI may show "Custom" but your Day/Night profile settings are preserved

License

MIT