Skip to content

ProFarjan/ZKTeco-Windows-Service

Repository files navigation

ZKTeco Windows Service - Complete Setup Guide

πŸ“¦ Project Structure

windows_services/
β”œβ”€β”€ nssm.exe                    βœ“ Local NSSM (no system install needed)
β”œβ”€β”€ php/                        βœ“ Standalone PHP (no WAMP dependency)
β”‚   β”œβ”€β”€ php.exe
β”‚   β”œβ”€β”€ php.ini
β”‚   └── ext/                   (PHP extensions)
β”œβ”€β”€ config.json                 βš™οΈ Service configuration
β”œβ”€β”€ service_worker.php          πŸ”„ Main service worker
β”œβ”€β”€ install_service.bat         πŸ“₯ Install service
β”œβ”€β”€ uninstall_service.bat       πŸ“€ Uninstall service
β”œβ”€β”€ test_connection.php         πŸ§ͺ Test device connectivity
β”œβ”€β”€ test_api.php               πŸ§ͺ Test API connectivity
β”œβ”€β”€ check_php.bat              πŸ” PHP diagnostics
β”œβ”€β”€ fix_php_extensions.bat     πŸ”§ Fix PHP issues
└── logs/                      πŸ“ Service logs
    β”œβ”€β”€ service.log
    β”œβ”€β”€ service_stdout.log
    └── service_stderr.log

πŸš€ Quick Start (5 Steps)

Step 1: Download PHP (if not already done)

download_php.bat

This downloads PHP 8.2 to php/ folder (standalone, no WAMP needed)


Step 2: Configure Your API & Devices

Edit config.json:

{
    "sync_interval": 300,
    "devices": [
        {
            "ip": "192.168.1.100",
            "port": 4370,
            "name": "Main Gate",
            "enabled": true
        }
    ],
    "api": {
        "enabled": true,
        "endpoint": "https://your-api.com/attendance/sync",
        "headers": {
            "Authorization": "Bearer YOUR_TOKEN"
        },
        "custom_params": {
            "company_id": "YOUR_COMPANY_ID"
        }
    }
}

Step 3: Test Connections

Test Device:

php\php.exe test_connection.php

Test API:

php\php.exe test_api.php

Step 4: Install Windows Service

Right-click β†’ Run as Administrator:

install_service.bat

Output:

[1/6] Checking prerequisites...
[OK] NSSM found at: C:\wamp64\www\windows_services\nssm.exe
[OK] PHP found at: C:\wamp64\www\windows_services\php\php.exe

[2/6] Verifying PHP installation...
[OK] PHP version: PHP 8.2.30

[3/6] Checking PHP extensions...
[OK] Required extensions loaded

[4/6] Checking existing service...
[OK] No existing service found

[5/6] Installing Windows service...
[OK] Service installed successfully

[6/6] Configuring service...
[OK] Service configured

Installation Complete! βœ“

Step 5: Start Service

net start ZkTecoSync

Or the installer will ask to start automatically.


βœ… What's Included (No External Dependencies)

βœ“ Local NSSM (nssm.exe)

  • Already in project folder
  • No system PATH modification needed
  • No separate download required
  • Version: 2.24 64-bit

βœ“ Standalone PHP (php/)

  • Self-contained PHP installation
  • No WAMP/XAMPP required
  • Includes all required extensions
  • Portable and isolated

βœ“ All Scripts Included

  • Installation, uninstallation, testing
  • Diagnostics and troubleshooting tools
  • Everything runs from project folder

🎯 Features

Service Configuration

  • βœ… 5-minute sync (configurable)
  • βœ… Multiple devices support
  • βœ… API integration with custom parameters
  • βœ… Field mapping (rename fields)
  • βœ… Batch processing (configurable size)
  • βœ… Retry logic on failures
  • βœ… Failed request storage
  • βœ… Auto-restart on crashes
  • βœ… Database optional (API-only mode)

Data Sync

  • Syncs attendance records every 5 minutes
  • Supports check-in, check-out, overtime
  • Multiple authentication types (fingerprint, password, card)
  • Automatic device reconnection
  • Error logging and monitoring

πŸ“‹ Service Management

Start Service

net start ZkTecoSync

Stop Service

net stop ZkTecoSync

Restart Service

nssm.exe restart ZkTecoSync

Check Status

sc query ZkTecoSync

View Logs

type logs\service.log

Monitor in Real-Time

powershell Get-Content logs\service.log -Wait -Tail 20

Uninstall Service

uninstall_service.bat

πŸ”§ Troubleshooting

Problem: PHP Extension Warnings

Solution:

fix_php_extensions.bat

This fixes:

  • Wrong extension paths
  • Missing Visual C++ libraries
  • Incorrect php.ini settings

Problem: NSSM Not Found

Check:

dir nssm.exe

If missing, download from: https://nssm.cc/download Extract to project folder.


Problem: Service Won't Start

Check:

  1. Test manually:

    php\php.exe service_worker.php
  2. Check logs:

    type logs\service_stderr.log
  3. Verify config:

    php\php.exe -r "print_r(json_decode(file_get_contents('config.json')));"

Problem: Can't Connect to Device

Test connection:

php\php.exe test_connection.php

Check:

  • Device IP address correct
  • Network connectivity (can you ping the device?)
  • Firewall not blocking port 4370
  • Device is powered on

Problem: API Requests Failing

Test API:

php\php.exe test_api.php

Check:

  • API endpoint URL correct
  • Authorization headers valid
  • Network allows outbound HTTPS
  • API is accessible

πŸ“Š Monitoring

Service Status

sc query ZkTecoSync

View Latest Logs

type logs\service.log

Live Log Monitoring

powershell Get-Content logs\service.log -Wait -Tail 20

Check Failed API Requests

type logs\failed_api_requests.json

Windows Services Manager

services.msc

Look for: ZKTeco Attendance Sync Service


πŸ—‚οΈ Configuration Files

config.json - Main Configuration

  • Device IPs and settings
  • API endpoint and authentication
  • Sync interval (5 minutes = 300 seconds)
  • Custom parameters and field mapping
  • Database settings (optional)

php\php.ini - PHP Configuration

  • Extension settings
  • Memory limits
  • Timeouts

πŸ“ Log Files

logs/service.log

Main application log with:

  • Service start/stop events
  • Device sync activities
  • API request/response logs
  • Errors and warnings

logs/service_stdout.log

Standard output from PHP

logs/service_stderr.log

Error output from PHP (helpful for crashes)

logs/failed_api_requests.json

Failed API requests for manual retry


πŸ”„ Updating Configuration

  1. Edit config.json
  2. Restart service:
    nssm.exe restart ZkTecoSync

No need to reinstall service for config changes!


πŸ†˜ Support & Documentation

Available Documentation

  • README_SERVICE.md - General service documentation
  • README_API.md - API integration guide
  • README_CUSTOM_API.md - Custom parameters guide
  • README_PHP_FIX.md - PHP troubleshooting
  • config_examples.json - Configuration examples

Diagnostic Tools

check_php.bat              # PHP diagnostics
test_connection.php        # Test device connectivity
test_api.php              # Test API connectivity
fix_php_extensions.bat    # Fix PHP issues

🎯 Deployment Checklist

  • NSSM exists in project folder (nssm.exe)
  • PHP downloaded and configured (php/)
  • No PHP warnings (php\php.exe -v)
  • Config file updated (config.json)
  • Device connection tested (test_connection.php)
  • API connection tested (test_api.php)
  • Service installed (install_service.bat)
  • Service running (sc query ZkTecoSync)
  • Logs showing activity (logs/service.log)

πŸ’‘ Tips

  1. Portable Setup: Copy entire folder to another machine - it just works!
  2. No Admin for Updates: Only service install/uninstall needs admin
  3. Test Before Service: Always test manually first
  4. Monitor Logs: Use real-time log viewing during setup
  5. Backup Config: Keep a copy of config.json

πŸ“ Notes

  • Service runs as Local System account
  • Automatically starts on Windows boot
  • Auto-restarts on crashes (5-second delay)
  • No external dependencies (fully portable)
  • All paths are relative (easy to move/copy)

✨ Version

  • PHP: 8.2.30
  • NSSM: 2.24 64-bit
  • Service: ZkTecoSync v1.0

πŸ“ž Quick Commands Reference

# Installation
install_service.bat

# Service Control
net start ZkTecoSync
net stop ZkTecoSync
nssm.exe restart ZkTecoSync

# Testing
php\php.exe test_connection.php
php\php.exe test_api.php
check_php.bat

# Monitoring
type logs\service.log
powershell Get-Content logs\service.log -Wait

# Troubleshooting
fix_php_extensions.bat
sc query ZkTecoSync

# Uninstall
uninstall_service.bat

Everything runs from this folder - no system modifications needed! πŸŽ‰

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors